DP 1
DP 1
CSE 304
Dynamic Programming
Dynamic Programming
• An algorithm design technique (like divide and
conquer)
• Divide and conquer
– Partition the problem into independent subproblems
– Solve the subproblems recursively
2
DP - Two key ingredients
• Two key ingredients for an optimization problem
to be suitable for a dynamic-programming
solution:
4
Fibonacci numbers
F =0
0
F =1
1
F = F − + F − for i>1 .
i i 1 i 2
5
How to compute F10?
F8
F9
F7 ……
F10
F7
F8
F6
6
Dynamic Programming
• Applicable when subproblems are not independent
– Subproblems share subsubproblems
7
Tabular computation
• The tabular computation can avoid
recompuation.
F0 F1 F2 F3 F4 F5 F6 F7 F8 F9 F10
0 1 1 2 3 5 8 13 21 34 55
Result
8
Dynamic Programming Algorithm
1. Characterize the structure of an optimal
solution
2. Recursively define the value of an optimal
solution
3. Compute the value of an optimal solution in a
bottom-up fashion
4. Construct an optimal solution from computed
information
9
Longest increasing subsequence(LIS)
10
A naive approach for LIS
• Let L[i] be the length of a longest increasing
subsequence ending at position i.
L[i] = 1 + max j = 0..i-1{L[j] | aj < ai}
(use a dummy a0 = minimum, and L[0]=0)
Index 0 1 2 3 4 5 6 7 8 9 10
Input 0 9 2 5 3 7 11 8 10 13 6
Length 0 1 1 2 2 3 4 4 5 6 3
Prev -1 0 0 2 2 4 5 5 7 8 4
Path 1 1 1 1 1 2 2 2 2 2 2
5 3 3 3 3 3 3 BestEnd[2]
7 7 7 7 7 BestEnd[3]
11 8 8 8 BestEnd[4]
10 10 BestEnd[5]
13 BestEnd[6]
12
An O(n log n) method for LIS
5 3 3 3 3 3 3 3 BestEnd[2]
7 7 7 7 7 6 BestEnd[3]
11 8 8 8 8 BestEnd[4]
For each position, we perform
a binary search to update 10 10 10 BestEnd[5]
BestEnd. Therefore, the
13 13 BestEnd[6]
running time is O(n log n).
13
Sum of Subset Problem
• Problem:
– Suppose you are given N positive integer numbers
A[1…N] and it is required to produce another number
K using a subset of A[1..N] numbers. How can it be
done using Dynamic programming approach?
• Example:
N = 6, A[1..N] = {2, 5, 8, 12, 6, 14}, K = 19
Result: 2 + 5 + 12 = 19
14
Coin Change Problem
• Suppose you are given n types of coin - C1, C2,
… , Cn coin, and another number K.
• Is it possible to make K using above types of
coin?
– Number of each coin is infinite
– Number of each coin is finite
• Find minimum number of coin that is required to
make K?
– Number of each coin is infinite
– Number of each coin is finite
Maximum-sum interval
• Given a sequence of real numbers a1a2…an ,
find a consecutive subsequence with the
maximum sum.
9 –3 1 7 –15 2 3 –4 2 –7 6 –2 8 4 -9
Try Yourself
16
The Knapsack Problem
• The 0-1 knapsack problem
– A thief robbing a store finds n items: the i-th item is
worth vi dollars and weights wi pounds (vi, wi integers)
– The thief can only carry W pounds in his knapsack
– Items must be taken entirely or left behind
– Which items should the thief take to maximize the
value of his load?
• The fractional knapsack problem
– Similar to above
– The thief can take fractions of items
17
The 0-1 Knapsack Problem
• Thief has a knapsack of capacity W
• There are n items: for i-th item value vi and
weight wi
• Goal:
– find xi such that for all xi = {0, 1}, i = 1, 2, .., n
∑ wixi ≤ W and
∑ xivi is maximum
18
0-1 Knapsack - Greedy Strategy
• E.g.:
3
Item 3 $120
0
5 5 5 +
Item 2 2
0 0 $100 0
3 0
Item 1 2 0 2
+ $100
1 0 1 0
$60
0 0
$60 $100 $120 $160 $220
19
0-1 Knapsack - Dynamic Programming
• P(i, w) – the maximum profit that can be
obtained from items 1 to i, if the knapsack
has size w
• Case 1: thief takes item i
P(i, w) =
P(i - 1, w)
20
0-1 Knapsack - Dynamic Programming
0 0 0 0 0 0 0 0 0 0 0 0
0 first
0 second
i-1 0
i 0
0
n 0
21
W=5 Item Weight Value
Example:
1 2 12
P(i, w) = max {vi + P(i - 1, w-wi), P(i - 1, w) }
2 1 10
3 3 20
0 1 2 3 4 5
4 2 15
0 0 0 0 0 0 0 P(1, 1) = P(0, 1) = 0
1 0 0 12 12 12 12 P(1, 2) = max{12+0, 0} = 12
2 0 10 12 22 22 22 P(1, 3) = max{12+0, 0} = 12
3 0 10 12 22 30 32 P(1, 4) = max{12+0, 0} = 12
4 0 10 15 25 30 37 P(1, 5) = max{12+0, 0} = 12
• Start at P(n, W)
• When you go left-up ⇒ item i has been taken
• When you go straight up ⇒ item i has not been
taken
23
Overlapping Subproblems
P(i, w) = max {vi + P(i - 1, w-wi), P(i - 1, w) }
0: 1 w W
0 0 0 0 0 0 0 0 0 0 0 0
0
0
i-1 0
i 0
0
n 0
E.g.: all the subproblems shown in grey may
depend on P(i-1, w)
24
Longest Common Subsequence (LCS)
25
Longest Common Subsequence
• Given two sequences
X = 〈x1, x2, …, xm〉
Y = 〈y1, y2, …, yn〉
find a maximum length common subsequence
(LCS) of X and Y
• E.g.:
X = 〈A, B, C, B, D, A, B〉
• Subsequences of X:
– A subset of elements in the sequence taken in order
〈A, B, D〉, 〈B, C, D, B〉, etc.
26
Example
X = 〈A, B, C, B, D, A, B〉 X = 〈A, B, C, B, D, A,
B〉
Y = 〈B, D, C, A, B, A〉 Y = 〈B, D, C, A, B, A〉
28
LCS Algorithm
29
LCS recursive solution
30
LCS recursive solution
31
LCS recursive solution
3. if b[i, j] = “ ”
4. then PRINT-LCS(b, X, i - 1, j - 1)
5. print xi
6. elseif b[i, j] = “↑”
7. then PRINT-LCS(b, X, i - 1, j)
8. else PRINT-LCS(b, X, i, j - 1)
39
LCS Algorithm Running Time
O(m*n)
since each c[i,j] is calculated in constant
time, and there are m*n elements in the
array
40
Rock Climbing Problem
• Let C(i,j) be the rating of the hold (i,j). There are three
cases for A(i,j):
• Middle: C(i,j)+min{A(i-1,j-1),A(i-1,j),A(i-1,j+1)}
A(i,j) = C(i,j)+min{A(i-1,j-1),A(i-1,j),A(i-1,j+1)}
Rock climbing: example
C(i,j): A(i,j):
3 2 5 4 8 i\j 0 1 2 3 4 5 6
5 7 5 6 1 0 ∞ 0 0 0 0 0 ∞
4 4 6 2 3 1 ∞ ∞
2 8 9 5 8 2 ∞ ∞
3 ∞ ∞
4 ∞ ∞
Initialization: A(i,0)=A(i,m+1)=∞,
A(0,j)=0
Rock climbing: example
C(i,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∞ ∞
3 2 5 4 8 i\j 0 1 2 3 4 5 6
5 7 5 6 1 0 ∞ 0 0 0 0 0 ∞
4 4 6 2 3 1 ∞ 3 2 5 4 8 ∞
2 8 9 5 8 2 ∞ 7 9 7 10 5 ∞
3 ∞ 11 11 13 7 8 ∞
4 ∞ 13 19 16 12 15 ∞
3 2 5 4 8 i\j 0 1 2 3 4 5 6
5 7 5 6 1 0 ∞ 0 0 0 0 0 ∞
4 4 6 2 3 1 ∞ 3 2 5 4 8 ∞
2 8 9 5 8 2 ∞ 7 9 7 10 5 ∞
3 ∞ 11 11 13 7 8 ∞
4 ∞ 13 19 16 12 15 ∞
3 2 5 4 8 i\j 0 1 2 3 4 5 6
5 7 5 6 1 0 ∞ 0 0 0 0 0 ∞
4 4 6 2 3 1 ∞ 3 2 5 4 8 ∞
2 8 9 5 8 2 ∞ 7 9 7 10 5 ∞
3 ∞ 11 11 13 7 8 ∞
4 ∞ 13 19 16 12 15 ∞
We are done!
Printing out the solution recursively
PrintBest(A,i,j) // Printing the best path ending at (i,j)
if (i==0) OR (j=0) OR (j=m+1)
return;
if (A[i-1,j-1]<=A[i-1,j]) AND (A[i-1,j-1]<=A[i-1,j+1])
PrintBest(A,i-1,j-1);
elseif (A[i-1,j]<=A[i-1,j-1]) AND (A[i-1,j]<=A[i-1,j+1])
PrintBest(A,i-1,j);
elseif (A[i-1,j+1]<=A[i-1,j-1]) AND (A[i-1,j+1]<=A[i-1,j])
PrintBest(A,i-1,j+1);
printf(i,j)