15 Dynamic Programming
15 Dynamic Programming
Conquer:
q conquer
q recursively
y to solve the subproblems
p
There is also an entry time (ei) for the chassis to enter assembly line i
and an exit time (xi) for the completed auto to exit assembly line i.
Ex1:Assembly line scheduling
Ex1:Assembly-line Ex1:Assembly-line
y schedulingg
•(Time between adjacent stations are nearly 0).
After going through station Si,j, can either
q stay on same line
next station is Si,j+1
no transfer cost , or
13
Example
Step 2: Recursive Solution
Define the value of an optimal solution recursively in
terms off the
h optimal
i l solution
l i to sub-problems
b bl
S b
Sub-problem
bl h
here
finding the fastest way through station j on both lines (i=1,2)
Let fi [j] be the fastest p
possible time to g
go from starting
gppoint
through Si,j
We also define
l*: the line whose station n is used in a fastest way
through the entire factory
37 38
Ex2: Matrix
Matrix-chain
chain multiplication Matrix-chain lti li ti – cont.
M t i h i multiplication t
We are given a sequence (chain) <A1
<A1, A2,
A2 ..., An> of n
We can multiply two matrices A and B only if they are
matrices to be multiplied, and we wish to compute the
compatible
product. A1.A2…An.
p
the number of columns of A must equal the number of rows of B
B.
Matrix multiplication
p is associative,, and so all If A is a p × q matrix and B is a q × r matrix,
matrix the resulting
parenthesizations yield the same product, matrix C is a p × r matrix.
39 40
chain multiplication – cont.
Matrix-chain
Matrix cont chain multiplication – cont.
Matrix-chain
Matrix cont
Ex: consider the problem of a chain <A1, A2, A3> of three
matrices with the dimensions of 10 × 100,
matrices, 100 100 × 5,
5 and 5 × 50,
50 Matrix multiplication
p is associative
respectively. (AB)C = A(BC)
Matrix Chain Multiplication (MCM) Problem Matrix Chain Multiplication (MCM) Problem
Input:
p Typically, the time invested in determining this
Typically
Matrices A1, A2, …, An, each Ai of size pi-1 x pi, optimal order is more than paid for by the time
saved later on when actually performing the
Output:
Fully parenthesised product A1 x A2 x … x An that
matrix multiplications
minimizes the number of scalar multiplications.
multiplications
So, exhaustively checking all possible
Note: parenthesizations does not yield an efficient
In MCM problem, we are not actually multiplying algorithm
matrices
O goall iis only
Our l tto d
determine
t i an order
d ffor multiplying
lti l i
matrices that has the lowest cost
43 44
Counting the Number of Parenthesizations Counting the Number of Parenthesizations
Denote the number of alternative parenthesizations of a
sequence off (n)
( ) matrices
ti by
b P(n)
P( ) then
th a fully
f ll
parenthesized matrix product is given by:
45 46
47 48
Step 1: Optimal Sub-structure
Sub structure Step 1: Optimal Sub-structure
Sub structure
49 50
51 52
Step
p 3: Computing
p g the Optimal
p Costs Elements of Dynamic
y Programming
g g
53 54
55 56
Step
p 3: Computing
p g the Optimal
p Costs Step 3: Computing the Optimal Costs
57 58
Step 3: Computing the Optimal Costs Step 3: Computing the Optimal Costs
59 60
Step 3: Computing the Optimal Costs Step 3: Computing the Optimal Costs
61 62
63 64
Step 4: Constructing an Optimal Solution Step 4: Constructing an Optimal Solution
65 66
69 70
A1 x A2 x A3 x A4 A1 x A2 x A3 x A4
30 x 1 1 x 40 40 x 10 10 x 25 30 x 1 1 x 40 40 x 10 10 x 25
( A1 ( ( A2 A3 ) A4 ) )
75 76
Longest Common Subsequence Longest Common Subsequence
Similarity can be defined in different ways:
In biological applications
applications, we often want to
Two DNA strands are similar if one is a substring
compare the DNA of two (or more) different of the other.
organisms.
organisms
Two strands are similar if the number of changes
A strand
t d off DNA consists
i t off a string
t i off needed
d d tto tturn one iinto
t th
the other
th iis small.
ll
molecules called bases, where the possible
bases are adenine
adenine, quanine
quanine, cytosine
cytosine, and Th
There is
i a thirds
thi d strand
t d S3 ini which
hi h th
the b
bases iin
thymine (A, C, G, T). S3 appear in each of S1 and S2; these bases
must appear in the same order
order, but not
necessarily consecutively. The longer the strand
S3 we can find, the more similar S1 and S2 are.
j 0 1 2 3 4 5 6 j 0 1 2 3 4 5 6
i yj B D C A B A i yj B D C A B A
0 xi 0 0 0 0 0 0 0 0 xi 0 0 0 0 0 0 0
A A ^ ^ ^
1 0 1 0 0 0 0 1 <1 1
B B ^
2 0 2 0 1 <1 <1 1 2 <2
C C ^ ^
3 0 3 0 1 1 2
4 B 0 First Optimal-LCS initializes 4 B 0
row 0 and column 0
5 D 0 5 D 0 Next each c[i, j] is computed,
computed row
6 A 0 6 A 0
by row, starting at c[1,1].
If xi == yj then c[i, j] = c[i
c[i-1,
1, jj-1]+1
1] 1
7 B 0 7 B 0 and b[i, j] =
j 0 1 2 3 4 5 6 j 0 1 2 3 4 5 6
i yj B D C A B A i yj B D C A B A
0 xi 0 0 0 0 0 0 0 0 xi 0 0 0 0 0 0 0
A ^ ^ ^ <1 A ^ ^ ^ <1
1 0 0 0 0 1 1 1 0 0 0 0 1 1
B <1 <1 ^ <2 B <1 <1 ^ <2
2 0 1 1 2 2 0 1 1 2
C ^ ^ <2 C ^ ^ <2 ^
3 0 1 1 2 3 0 1 1 2 2
4 B 0 4 B 0
5 D 0 5 D 0
If xi <> yj then c[i, j] = if c[i-1, j] == c[i, j-1]
6 A 0 max(c[i-1, j], c[i, j-1]) 6 A 0 then b[i,j] points up
B and b[i,
b[i j] points to the larger value B
7 0 7 0
j 0 1 2 3 4 5 6 j 0 1 2 3 4 5 6
i yj B D C A B A i yj B D C A B A
xi 0 xi 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
^ ^ ^ A ^ ^ ^
1 A 0 0 0 0 1 <1 1 1 0 0 0 0 1 <1 1
^ B <1 <1 ^ <2
2 B 0 1 <1 <1 1 2 <2 2 0 1 1 2
^ ^ ^ ^ C ^ ^ <2 ^ ^
3 C 0 1 1 2 <2 2 2 3 0 1 1 2 2 2
^ ^ ^ B ^ ^ ^ <3
4 B 0 1 1 2 2 3 <3 4 0 1 1 2 2 3
^ ^ ^ ^ ^ D ^ ^ ^ ^ ^
5 D 0 1 2 2 2 3 3 5 0 1 2 2 2 3 3
^ ^ ^ ^ A ^ ^ ^ ^
6 A 0 1 2 2 3 3 4 6 0 1 2 2 3 3 4
^ ^ ^ ^ B ^ ^ ^ ^
7 B 0 1 2 2 3 4 4 7 0 1 2 2 3 4 4
E.g.2:
find xi such that for all xi = {0, 1}, i = 1, 2, .., n Item: 1 2 3 4 5 6 7
Benefit: 5 8 3 2 7 9 4
∑ wixi ≤ W and Weight: 7 8 4 10 4 6 4
Knapsack
p holds a maximum of 22 p
pounds
∑ xivi is maximum Fill it to get the maximum benefit
95
0-1 Knapsack
p p
problem: brute-force 00-11 Knapsack - Dynamic Programming
approach
P(i w) – the maximum profit that can be
P(i,
Let’s first solve this problem with a straightforward
algorithm obtained from items 1 to i, if the
Since there are n items, there are 2n possible knapsack has size w
combinations
bi ti off items.
it
Case 1: thief takes item i
We go through all combinations and find the one
with the most total value and with total weight less P(i w)) = vi + P(i - 1,
P(i, 1 w-wi)
q to W
or equal
Case 2: thief does not take item i
Running time will be O(2n)
P(i w) = P(i - 1,
P(i, 1 w)
97
1 0 1 0 0 12 12 12 12
0 12 12 12 12 P(1, 2) = max{12
max{12+0,
0, 0} = 12 • Item 2
2 0 10 12 22 22 22 2 0 10 12 22 22 22
P(1, 3) = max{12+0, 0} = 12
3 0 10 12 22 30 32 • Item 1
3 0 10 12 22 30 32 P(1, 4) = max{12
max{12+0,
0, 0} = 12
4 0 10 15 25 30 37 4 0 10 15 25 30 37
P(1, 5) = max{12+0, 0} = 12
P(2, 1)= max{10+0, 0} = 10 P(3, 1)= P(2,1) = 10 P(4, 1)= P(3,1) = 10 Startt att P(n,
• St P( W)
P(2, 2)= max{10+0, 12} = 12 P(3, 2)= P(2,2) = 12 P(4, 2)= max{15+0, 12} = 15
• When you go left-up ⇒ item i has been taken
P(2, 3)= max{10+12, 12} = 22 P(3, 3)= max{20+0, 22}=22 P(4, 3)= max{15+10, 22}=25
• When you go straight up ⇒ item i has not been
P(2, 4)= max{10+12, 12} = 22 P(3, 4)= max{20+10,22}=30 P(4, 4)= max{15+12, 30}=30
taken
P(2, 5)= max{10+12, 12} = 22 P(4, 5)= max{20+12,22}=32 P(4, 5)= max{15+22, 32}=37
Conclusion
Dynamic
D i programming i isi a useful
f l technique
t h i off
solving certain kind of problems
107