Common Dynamic Programming Problems
Common Dynamic Programming Problems
1
Outline
1. Concepts of dynamic programming
2. Longest common subsequence
3. Chain matrix multiplication
4. 0/1 Knapsack problem
MergeSort(1..4) MergeSort(5..8)
MergeSort(1..2) MergeSort(3..4)
MergeSort(5..6) MergeSort(7..8)
int fib(n)
{ if (n == 0 || n == 1) return n;
else return fib(n - 1) + fib(n - 2);
}
Dynamic Programming SC2001/CX2101 6
Tree of function 0
calls: each
2 1
node 0
represents a 1
function call 1
3 2
4
1 0
6
3 2 1
0
5
4 2 1
1 0
Many subproblems are 3
2 1
overlapping: a lot of
recomputation
Dynamic Programming SC2001/CX2101 7
• Example of repetition is given in the shaded nodes
• Notice that this is a full binary tree up to depth 3 (i.e.
n/2)
• The deepest level is 5 (i.e. n-1)
• The number of recursive calls R is such that
24 - 1 < R < 26 - 1
In general,
n +1
−1 R 2 −1
n
2 2
int fibDP(n)
{ int f1, f2;
if (n == 0 || n == 1) { Store,
member,
store(Soln, n, n);
retrieve are all
return n; } methods of the
else { Dictionary
if (not member(Soln, n - 1))
f1 = fibDP(n - 1);
else f1 = retrieve(Soln, n - 1);
f1 += f2;
store(Soln, n, f1);
return f1; }
}
• Before calling fibDP, the dictionary Soln has to be initialized.
E.g.
0 1 2 3 4 5 6
Soln -1 -1 -1 -1 -1 -1 -1
6 5 4 3 2 1 0
0 1 2 3 4 5 6
Soln 0 1 1 2 3 5 8
LCS(i,j) LCS(i-1,j-1)
LCS(5,3)
A C T G
0 0 0 0 0
A 0
C 0
G 0
G 0
A 0
A C T G
for i = 1 to n
0 0 0 0 0
for j = 1 to m
A 0 1 1 1 1
if x[i] == y[j]
C 0 1 2 2 2
c[i][j] = c[i-1][j-1] + 1;
G 0 1 2 2 3
else if c[i-1][j] >= c[i][j-1]
G 0 1 2 2 3
c[i][j] = c[i-1][j];
A 0 1 2 2
3 else c[i][j] = c[i][j-1];
LCS(5,4) = 3
o For the hint array cell h[i][j] where i≠0 and j≠0,
▪ If we do LCS(i,j) = LCS(i-1, j-1)+1, h[i][j] = ‘\’
▪ If we do LCS(i,j) = LCS(i-1, j), h[i][j] = ‘|’
▪ If we do LCS(i,j) = LCS(i, j-1), h[i][j] = ‘—’
o First column of the hint array will be filled with ‘|’.
o First row of the hint array will be filled ‘—’.
if (h[i][j] == ‘\’)
{ [Link](x[i]); i--; j--; } Bounded by a
constant time
else if (h[i][j] == ‘|’)
i--;
else j--;
pop and output from s; }
Dynamic Programming SC2001/CX2101 37
1 2 3 4 5
Example 1
x A C G G A
A C T G
0 0 0 0 0 y A C T G
A 0 1 1 1 1
C 0 1 2 2 2
G 0 1 2 2 3 h(5,4) = ‘|’
G 0 1 2 2 3 h(4,4) = ‘\’ G
A 0 1 2 2 3 h(3,3) = ‘|’
h(2,3) = ‘—’
A C T G
h(2,2) = ‘\’ C
— — — — —
h(1,1) = ‘\’
A | \ — — —
end A
C | | \ — —
G | | | | \
G | | | | \
The sub sequence: A C G
A | \ | | |
Dynamic Programming SC2001/CX2101 38
Example 2: x C G G T A T
y A G T T G C
A G T T G C A G T T G C
0 0 0 0 0 0 0 — — — — — — —
C 0 0 0 0 0 0 1 C | | | | | | \
G 0 0 1 1 1 1 1 G | | \ — — \ |
G 0 0 1 1 1 2 2 G | | \ | | \ —
T 0 0 1 2 2 2 2 T | | | \ \ | |
A 0 1 1 2 2 2 2 A | \ | | | | |
T 0 1 1 2 3 3 T | | | \ \ — —
3
LCS(6,6) = 3
A G T T G C h(6,6) = ‘—’
— — — — — — — h(6,5) = ‘—’
C | | | | | | \ h(6,4) = ‘\’ T
G | | \ — — \ | h(5,3) = ‘|’
G | | \ | | \ — h(4,3) = ‘\’ T
T | | | \ \ | | h(3,2) = ‘\’
A | \ | | | | | h(2,1) = ‘|’ G
T | | | \ \ — — h(1,1) = ‘|’
h(0,1) = ‘—’
end
LCS(6,6) = 3
The subsequence: G T T
Dynamic Programming SC2001/CX2101 40
Chain Matrix Multiplication
• The Order problem . . . . . . .
. . . . . . .
Consider A1 x A2 x A3 x A4
. . . .
30x1 1x40 40x10 10x25
2X3X4
Many possibilities. For examples,
((A1A2)A3)A4 30x1x40 + 30x40x10 + 30x10x25 =
20,700 multiplications
A1(A2(A3A4)) 40x10x25 + 1x40x25+ 30x1x25 =
11,750 multiplications
(A1A2)(A3A4) 30x1x40 + 40x10x25 + 30x40x25 =
41,200 multiplications
A1((A2A3)A4) 1x40x10 + 1x10x25 + 30x1x25 =
1,400 multiplications
Dynamic Programming SC2001/CX2101 41
Problem definition: given matrices A1, A2, ….An where
dimensions of Ai are di-1 x di (for 1 i n), what order
should the matrix multiplications be computed in order to
incur minimum cost? Cost is the number of
multiplications.
d0 d1 d2 d3 … dn-1 dn
• There are (n-1)! ways for n matrices
• Matrix multiplication is associative: (AB)C = A(BC). So
different ways give the same result
• This is an optimization problem
B1 B2
Suppose the last matrix multiplication were at A3; then
1) We need to multiply A1 x A2 x A3 to create B1, a
d0 x d3 matrix
2) We need to multiply A4 x A5 x A6 to create B2, a
d3 x d6 matrix
Cost would be the cost of (1)+(2)+ cost of(B1 x B2)
d0 x d1 d1 x d2 d2 x d3 d3 x d4 d4 x d5 d5 x d6 dimensions
d0 x d1 d1 x d2 d2 x d3 d3 x d4 d4 x d5 d5 x d6 dimensions
The dynamic programming selection rule: when given a
number of possibilities, compute all and take the best.
The optimal cost of multiplying the 6 matrices:
OptCost(A1A2A3A4A5A6) = Min(
OptCost(A1A2A3A4A5) + OptCost(A6) + d0 x d5 x d6,
OptCost(A1A2A3A4) + OptCost(A5A6) + d0 x d4 x d6,
OptCost(A1A2A3) + OptCost(A4A5A6) + d0 x d3 x d6,
OptCost(A1A2) + OptCost(A3A4A5A6) + d0 x d2 x d6,
OptCost(A1) + OptCost(A2A3A4A5A6) + d0 x d1 x d6 )
OptCost(A) = 0
Dynamic Programming SC2001/CX2101 45
Step 2: Turn this formulation into a recursive function to
solve the chain matrix multiplication problem.
Suppose we use array d to store the dimensions of the
matrices.
d0 d1 d2 …
(A1... A5)(A6)
(A2)(A3...A6) (A2 A3)(A4...A6) (A2... A4)(A5A6) (0,5)(5,6)
(1,2)(2,6) (1,3)(3,6) (1,4)(4,6)
(A2... A5)(A6)
(1,5)(5,6)
(A3)(A4...A6) (A3 A4)(A5A6) (A3... A5)(A6)
(2,3)(3,6) (2,4)(4,6) (2,5)(5,6)
0 1 2 3 4 5 6
0
1
cost 2
3
4
5
6
Not computed
Not computed
Array d
30 1 40 10 25
0 1 2 3 4
Call to matrixOrder(d, 4)
cost
0 1 2 3 4
0 0
1 0 for i = 0 to n-1
2 0 cost[i][i+1] = 0;
3 0
4
(1,2) A A3 (2,3)
2
So the best sequence is
(A1 x ((A2 x A3 ) x A4 ))
……
1 2 3 4
wi 4 6 8 6
pi 7 6 9 5
Dynamic Programming SC2001/CX2101 68
1 2 3 4
wi 4 6 8 6
pi 7 6 9 5
(0,1)
P(C, j) = max(P(C, j-1), pj +
P(C-wj, j-1))
(2,0)
(4,0)
(6,0) (6,1) (6,2)
(8,0) (8,1)
(10,0) (i,j) represents
P(i, j)
(12,0) (12,1) (12,2)
(14,0) (14,1) (14,2) (14,3)
(16,0)
(20,0) (20,1) (20,2) (20,3) (20,4)
0 1 2 … n
0
1
2
3
4
5
6
7
…
C
profit[r][c] = profit[r][c-1];
if (w[c] <= r)
if (profit[r][c] < profit[r-w[c]][c-1] + p[c])
profit[r][c] = profit[r-w[c]][c-1] + p[c];
}
Dynamic Programming SC2001/CX2101 71
1 2 3 4
Example 1: wi 4 6 8 6
C = 20 pi 7 6 9 5
0 1 2 3 4
0 0 0 0 0 0 for c = 0 to n
2 0 profit[0][c] = 0;
4 0 for r = 1 to C
6 0 profit[r][0] = 0;
8 0
10 0
12 0 Not all rows
14 0 are shown
16 0
20 0
for c = 0 to n
0 1 2 3 profit[0][c] = 0;
0 0 0 0 0 for r = 1 to C
1 0 profit[r][0] = 0;
2 0
3 0
for r = 1 to C
0 1 2 3 for c = 1 to n
0 0 0 0 0 profit[r][c] = profit[r][c-1];
1 0 1 1 1 if (w[c] <= r)
2 0 1 4 4 if (profit[r][c] <
3 0 1 5 profit[r-w[c]][c-1] + p[c])
6
profit[r][c] =
profit[r-w[c]][c-1]
+ p[c]