Presentation is loading. Please wait.

Presentation is loading. Please wait.

Dynamic Programming.

Similar presentations


Presentation on theme: "Dynamic Programming."— Presentation transcript:

1 Dynamic Programming

2 Example 1: Fibonacci Numbers
Computing the nth Fibonacci number recursively: F(n) = F(n-1) + F(n-2) F(0) = 0 F(1) = 1 Top-down approach Time complexity? int Fib(int n) {     if (n <= 1)         return 1;     else         return Fib(n - 1) + Fib(n - 2); } F(n) F(n-1) F(n-2) F(n-2) F(n-3) F(n-3) F(n-4) n levels

3 Example 1: Fibonacci Numbers
Computing the nth Fibonacci number using a bottom-up approach: F(0) = 0 F(1) = 1 F(2) = 1+0 = 1 F(n-2) = F(n-1) = F(n) = F(n-1) + F(n-2) Complexity: Time – O(n) Space – O(n)

4 Example 1: Fibonacci Numbers
The bottom-up approach is only O(n). Why is the top-down so inefficient? Re-computes many sub-problems. How many times is F(n-5) computed? F(n) F(n-1) F(n-2) F(n-2) F(n-3) F(n-3) F(n-4) n levels

5 Example 1: Fibonacci Numbers
+ Fib(3) + Fib(4) + Fib(3) + Fib(2) + Fib(1) Fib(2) + Fib(2) + Fib(1) Fib(1) Fib(0) Fib(1) Fib(0) Fib(1) Fib(0)

6 Dynamic Programming Dynamic Programming is an algorithm design technique for optimization problems: often minimizing or maximizing. Like divide and conquer, DP solves problems by combining solutions to sub-problems. Unlike divide and conquer, sub-problems are not independent. Sub-problems overlap (share sub-sub-problems) Dynamic Programming = meta-technique (not a particular algorithm) Programming = “tableau method’’, not writing a code

7 Dynamic Programming In Dynamic Programming we usually reduce time by increasing the amount of space We solve the problem by solving sub-problems of increasing size and saving each optimal solution in a table. Time is saved since each sub-problem is solved only once. Main idea: set up a recurrence relating a solution to a larger instance to solutions of some smaller instances - solve smaller instances once record solutions in a table extract solution to the initial instance from that table A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 8 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.

8 Dynamic Programming Dynamic Programming is a general algorithm design technique for solving problems defined by recurrences with overlapping sub-problems “Programming” here means “tableau method” Main idea: set up a recurrence relating a solution to a larger instance to solutions of some smaller instances - solve smaller instances once record solutions in a table extract solution to the initial instance from that table A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 8 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.

9 Example 2: Coin-row problem
There is a row of n coins whose values are some positive integers c₁, c₂,...,cn, not necessarily distinct. The goal is to pick up the maximum amount of money subject to the constraint that no two coins adjacent in the initial row can be picked up. E.g.: 5, 1, 2, 10, 6, 2. What is the best selection? Answer: {c1, c4, c6} = {5, 10, 2} A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 8 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.

10 DP solution to the coin-row problem
Let F(n) be the maximum amount that can be picked up from the row of n coins. To derive a recurrence for F(n), we partition all the allowed coin selections into two groups: those without last coin – the max amount is ? those with the last coin -- the max amount is ? Thus we have the following recurrence F(n) = max{cn + F(n-2), F(n-1)} for n > 1, F(0) = 0, F(1)=c₁ A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 8 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.

11 DP solution to the coin-row problem (cont.)
F(n) = max{cn + F(n-2), F(n-1)} for n > 1, F(0) = 0, F(1)=c₁ index 1 2 3 4 5 6 coins -- 10 F( ) A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 8 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.

12 DP solution to the coin-row problem (cont.)
F(n) = max{cn + F(n-2), F(n-1)} for n > 1, F(0) = 0, F(1)=c₁ index 1 2 3 4 5 6 coins -- 10 F( ) F(2) = max{c2 + F(0), F(1)} = max{1 + 0, 5} = 5 A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 8 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.

13 DP solution to the coin-row problem (cont.)
F(n) = max{cn + F(n-2), F(n-1)} for n > 1, F(0) = 0, F(1)=c₁ index 1 2 3 4 5 6 coins -- 10 F( ) 7 F(3) = max{c3 + F(1), F(2)} = max{2 + 5, 5} = 7 A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 8 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.

14 DP solution to the coin-row problem (cont.)
F(n) = max{cn + F(n-2), F(n-1)} for n > 1, F(0) = 0, F(1)=c₁ index 1 2 3 4 5 6 coins -- 10 F( ) 7 15 F(4) = max{c4 + F(2), F(3)} = max{10 + 5, 7} = 15 A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 8 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.

15 DP solution to the coin-row problem (cont.)
F(n) = max{cn + F(n-2), F(n-1)} for n > 1, F(0) = 0, F(1)=c₁ index 1 2 3 4 5 6 coins -- 10 F( ) 7 15 F(5) = max{c5 + F(3), F(4)} = max{6 + 7, 15} = 15 A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 8 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.

16 DP solution to the coin-row problem (cont.)
F(n) = max{cn + F(n-2), F(n-1)} for n > 1, F(0) = 0, F(1)=c₁ index 1 2 3 4 5 6 coins -- 10 F( ) 7 15 17 F(6) = max{c6 + F(4), F(5)} = max{2 + 15, 15} = 17 A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 8 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.

17 DP solution to the coin-row problem (cont.)
F(n) = max{cn + F(n-2), F(n-1)} for n > 1, F(0) = 0, F(1)=c₁ index 1 2 3 4 5 6 coins -- 10 F( ) 7 15 17 Max amount: 17 Coins of optimal solution: {c1, c4, c6} Time efficiency: O(n) Space efficiency: O(n) Note: All smaller instances were solved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 8 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.

18 Example: Longest Common Subsequence
Longest common subsequence (LCS) problem: Given two sequences X = <x1 x2 … xm> and Y = <y1 y2 … yn>, find the longest subsequence which occurs in both Ex: X = {A B C B D A B }, Y = {B D C A B A} What’s the LCS? X = A B C B D A B Y = B D C A B A Brute-force algorithm: For every subsequence of X, check if it’s a subsequence of Y How many subsequences of X are there? What will be the running time of the brute-force algorithm?

19 LCS Algorithm Brute-force algorithm: 2m subsequences of X to check against n elements of Y: O(n 2m) We can do better: for now, let’s only worry about the problem of finding the length of LCS Notice LCS problem has overlapped subporblems Subproblems: LCS of pairs of prefixes of X and Y

20 LCS Algorithm First we’ll find the length of LCS. Later we’ll modify the algorithm to find LCS itself. Define Xi, Yj to be the prefixes of X and Y of length i and j respectively (i ≤ m; j ≤ n) Xi = <x1 x2 … xi>; Yj = <y1 y2 … yj> Define c[i,j] to be the length of LCS of Xi and Yj Then the length of LCS of X and Y will be c[m,n] X = A B C B D A B Y = B D C A B A

21 LCS recursive solution
We start with i = j = 0 (empty substrings of X and Y) Since X0 and Y0 are empty strings, their LCS is always empty (i.e., c[0,0] = 0) LCS of empty string and any other string is empty, so for every i and j: c[0, j] = c[i,0] = 0

22 LCS recursive solution
When we calculate c[i,j], we consider two cases: First case: x[i]=y[j]: one more symbol in strings X and Y matches, so the length of LCS Xi and Yj equals to the length of LCS of smaller strings Xi-1 and Yj-1 , plus 1

23 LCS recursive solution
Second case: x[i] != y[j] As symbols don’t match, our solution is not improved, and the length of LCS(Xi , Yj) is the same as before (i.e., maximum of LCS(Xi, Yj-1) and LCS(Xi-1,Yj) For example: AB | C; BC | A Why not just take the length of LCS(Xi-1, Yj-1) ?

24 LCS Length Algorithm LCS-Length(X, Y)
1. m = length(X) // get the # of symbols in X 2. n = length(Y) // get the # of symbols in Y 3. for i = 1 to m c[i,0] = 0 // special case: Y0 4. for j = 1 to n c[0,j] = 0 // special case: X0 5. for i = 1 to m // for all Xi 6. for j = 1 to n // for all Yj 7. if ( Xi == Yj ) 8. c[i,j] = c[i-1,j-1] + 1 9. else c[i,j] = max( c[i-1,j], c[i,j-1] ) 10. return c

25 LCS Example What is the Longest Common Subsequence of X and Y?
We’ll see how LCS algorithm works on the following example: X = ABCB Y = BDCAB What is the Longest Common Subsequence of X and Y? LCS(X, Y) = BCB X = A B C B Y = B D C A B

26 LCS Example (0) ABCB BDCAB X = ABCB; m = |X| = 4
j ABCB BDCAB i Yj B D C A B Xi A 1 B 2 3 C 4 B X = ABCB; m = |X| = 4 Y = BDCAB; n = |Y| = 5 Allocate array c[4,5]

27 LCS Example (1) ABCB BDCAB for i = 1 to m c[i,0] = 0
j ABCB BDCAB i Yj B D C A B Xi A 1 B 2 3 C 4 B for i = 1 to m c[i,0] = 0 for j = 1 to n c[0,j] = 0

28 LCS Example (2) ABCB BDCAB j 0 1 2 3 4 5 i Yj B D C A B Xi A 1 B 2 3 C
A 1 B 2 3 C 4 B if ( Xi == Yj ) c[i,j] = c[i-1,j-1] + 1 else c[i,j] = max( c[i-1,j], c[i,j-1] )

29 LCS Example (3) ABCB BDCAB j 0 1 2 3 4 5 i Yj B D C A B Xi A 1 B 2 3 C
A 1 B 2 3 C 4 B if ( Xi == Yj ) c[i,j] = c[i-1,j-1] + 1 else c[i,j] = max( c[i-1,j], c[i,j-1] )

30 LCS Example (4) ABCB BDCAB j 0 1 2 3 4 5 i Yj B D C A B Xi A 1 1 B 2 3
A 1 1 B 2 3 C 4 B if ( Xi == Yj ) c[i,j] = c[i-1,j-1] + 1 else c[i,j] = max( c[i-1,j], c[i,j-1] )

31 LCS Example (5) ABCB BDCAB j 0 1 2 3 4 5 i Yj B D C A B Xi A 1 1 1 B 2
A 1 1 1 B 2 3 C 4 B if ( Xi == Yj ) c[i,j] = c[i-1,j-1] + 1 else c[i,j] = max( c[i-1,j], c[i,j-1] )

32 LCS Example (6) ABCB BDCAB j 0 1 2 3 4 5 i Yj B D C A B Xi A 1 1 1 B 2
A 1 1 1 B 2 1 3 C 4 B if ( Xi == Yj ) c[i,j] = c[i-1,j-1] + 1 else c[i,j] = max( c[i-1,j], c[i,j-1] )

33 LCS Example (7) ABCB BDCAB j 0 1 2 3 4 5 i Yj B D C A B Xi A 1 1 1 B 2
A 1 1 1 B 2 1 1 1 1 3 C 4 B if ( Xi == Yj ) c[i,j] = c[i-1,j-1] + 1 else c[i,j] = max( c[i-1,j], c[i,j-1] )

34 LCS Example (8) ABCB BDCAB j 0 1 2 3 4 5 i Yj B D C A B Xi A 1 1 1 B 2
A 1 1 1 B 2 1 1 1 1 2 3 C 4 B if ( Xi == Yj ) c[i,j] = c[i-1,j-1] + 1 else c[i,j] = max( c[i-1,j], c[i,j-1] )

35 LCS Example (10) ABCB BDCAB j 0 1 2 3 4 5 i Yj B D C A B Xi A 1 1 1 B
A 1 1 1 B 2 1 1 1 1 2 3 C 1 1 4 B if ( Xi == Yj ) c[i,j] = c[i-1,j-1] + 1 else c[i,j] = max( c[i-1,j], c[i,j-1] )

36 LCS Example (11) ABCB BDCAB j 0 1 2 3 4 5 i Yj B D C A B Xi A 1 1 1 B
A 1 1 1 B 2 1 1 1 1 2 3 C 1 1 2 4 B if ( Xi == Yj ) c[i,j] = c[i-1,j-1] + 1 else c[i,j] = max( c[i-1,j], c[i,j-1] )

37 LCS Example (12) ABCB BDCAB j 0 1 2 3 4 5 i Yj B D C A B Xi A 1 1 1 B
A 1 1 1 B 2 1 1 1 1 2 3 C 1 1 2 2 2 4 B if ( Xi == Yj ) c[i,j] = c[i-1,j-1] + 1 else c[i,j] = max( c[i-1,j], c[i,j-1] )

38 LCS Example (13) ABCB BDCAB j 0 1 2 3 4 5 i Yj B D C A B Xi A 1 1 1 B
A 1 1 1 B 2 1 1 1 1 2 3 C 1 1 2 2 2 4 B 1 if ( Xi == Yj ) c[i,j] = c[i-1,j-1] + 1 else c[i,j] = max( c[i-1,j], c[i,j-1] )

39 LCS Example (14) ABCB BDCAB j 0 1 2 3 4 5 i Yj B D C A B Xi A 1 1 1 B
A 1 1 1 B 2 1 1 1 1 2 3 C 1 1 2 2 2 4 B 1 1 2 2 if ( Xi == Yj ) c[i,j] = c[i-1,j-1] + 1 else c[i,j] = max( c[i-1,j], c[i,j-1] )

40 LCS Example (15) 3 ABCB BDCAB j 0 1 2 3 4 5 i Yj B D C A B Xi A 1 1 1
A 1 1 1 B 2 1 1 1 1 2 3 C 1 1 2 2 2 3 4 B 1 1 2 2 if ( Xi == Yj ) c[i,j] = c[i-1,j-1] + 1 else c[i,j] = max( c[i-1,j], c[i,j-1] )

41 LCS Algorithm Running Time
LCS algorithm calculates the values of each entry of the array c[m,n] So what is the running time? O(mn) since each c[i,j] is calculated in constant time, and there are mn elements in the array

42 How to find actual LCS For example, here
So far, we have just found the length of LCS, but not LCS itself. We want to modify this algorithm to make it output Longest Common Subsequence of X and Y Each c[i,j] depends on c[i-1,j] and c[i,j-1] or c[i-1, j-1] For each c[i,j] we can say how it was acquired: For example, here c[i,j] = c[i-1,j-1] +1 = 2+1=3 2 2 2 3

43 Finding LCS 3 ABCB BDCAB j 0 1 2 3 4 5 i Yj B D C A B Xi A 1 1 1 B 2 1
A 1 1 1 B 2 1 1 1 1 2 3 C 1 1 2 2 2 3 4 B 1 1 2 2

44 How to find actual LCS - continued
Remember that So we can start from c[m,n] and go backwards Whenever c[i,j] = c[i-1, j-1]+1, remember x[i] (because x[i] is a part of LCS) When i=0 or j=0 (i.e., we reached the beginning), output remembered letters in reverse order

45 Finding LCS (2) 3 B C B B C B LCS (reversed order):
j i Yj B D C A B Xi A 1 1 1 B 2 1 1 1 1 2 3 C 1 1 2 2 2 3 4 B 1 1 2 2 B C B LCS (reversed order): LCS (straight order): B C B

46 Properties of a problem that can be solved with dynamic programming
Simple Sub-problems We should be able to break the original problem to smaller sub-problems that have the same structure Optimal Substructure of the problems The solution to the problem must be a composition of sub-problem solutions Sub-problem Overlap Optimal sub-problems to unrelated problems can contain sub-problems in common

47 Example 3: Change-making Problem
Give change for amount n using the minimum number of coins of denominations d1 < d2 < … < dm. Example: n = 6, coin denominations 1, 3, and 4 A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 8 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.

48 DP solution to the change-making problem
Let F(n) be the minimum number of coins whose value add up to n add one coin of denomination dj to the amount n – dj Thus we have the following recurrence F(n) = min{F(n - dj)} + 1 for n > 0, F(0) = 0 A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 8 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.

49 DP solution to the change-making problem
F(n) = min{F(n - dj)} + 1 for n > 0, F(0) = 0 n 1 2 3 4 5 6 F( ) F(0) = 0 A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 8 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.

50 DP solution to the change-making problem
F(n) = min{F(n - dj)} + 1 for n > 0, F(0) = 0 n 1 2 3 4 5 6 F( ) F(1) = min{F[1 - 1]} + 1 = = 1 A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 8 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.

51 DP solution to the change-making problem
F(n) = min{F(n - dj)} + 1 for n > 0, F(0) = 0 n 1 2 3 4 5 6 F( ) F(2) = min{F[2 - 1]} + 1 = = 2 A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 8 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.

52 DP solution to the change-making problem
F(n) = min{F(n - dj)} + 1 for n > 0, F(0) = 0 n 1 2 3 4 5 6 F( ) F(3) = min{F[3 - 1], F[3 - 3]} + 1 = min{2, 0} + 1 = 1 A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 8 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.

53 DP solution to the change-making problem
F(n) = min{F(n - dj)} + 1 for n > 0, F(0) = 0 n 1 2 3 4 5 6 F( ) F(4) = min{F[4 - 1], F[4 - 3], F[4 - 4]} + 1 = min{1, 1, 0} + 1 = 1 A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 8 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.

54 DP solution to the change-making problem
F(n) = min{F(n - dj)} + 1 for n > 0, F(0) = 0 n 1 2 3 4 5 6 F( ) F(5) = min{F[5 - 1], F[5 - 3], F[5 - 4]} + 1 = min{1, 2, 1} + 1 = 2 A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 8 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.

55 DP solution to the change-making problem
F(n) = min{F(n - dj)} + 1 for n > 0, F(0) = 0 n 1 2 3 4 5 6 F( ) F(6) = min{F[6 - 1], F[6 - 3], F[6 - 4]} + 1 = min{2, 1, 2} + 1 = 2 A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 8 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.

56 DP solution to the change-making problem
F(n) = min{F(n - dj)} + 1 for n > 0, F(0) = 0 n 1 2 3 4 5 6 F( ) Min coin #: 2 Coins of optimal solution: two 3’s Time efficiency: O(nm) Space efficiency: O(n) Note: All smaller instances were solved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 8 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.

57 Example 4: Coin-collecting by robot
Several coins are placed in cells of an n×m board. A robot, located in the upper left cell of the board, needs to collect as many of the coins as possible and bring them to the bottom right cell. On each step, the robot can move either one cell to the right or one cell down from its current location. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 8 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.

58 Solution to the coin-collecting problem
Let F(i,j) be the largest number of coins the robot can collect and bring to cell (i,j) in the ith row and jth column. The largest number of coins that can be brought to cell (i,j): from the left neighbor ? from the neighbor above? The recurrence: F(i, j) = max{F(i-1, j), F(i, j-1)} + cij for 1 ≤ i ≤ n, 1 ≤ j ≤ m where cij = 1 if there is a coin in cell (i,j), and cij = 0 otherwise F(0, j) = 0 for 1 ≤ j ≤ m and F(i, 0) = 0 for 1 ≤ i ≤ n. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 8 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.

59 Solution to the coin-collecting problem (cont.)
F(i, j) = max{F(i-1, j), F(i, j-1)} + cij for 1 ≤ i ≤ n, 1 ≤ j ≤ m where cij = 1 if there is a coin in cell (i,j), and cij = 0 otherwise F(0, j) = 0 for 1 ≤ j ≤ m and F(i, 0) = 0 for 1 ≤ i ≤ n. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 8 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.

60 Solution to the coin-collecting problem (cont.)
F(i, j) = max{F(i-1, j), F(i, j-1)} + cij for 1 ≤ i ≤ n, 1 ≤ j ≤ m where cij = 1 if there is a coin in cell (i,j), and cij = 0 otherwise F(0, j) = 0 for 1 ≤ j ≤ m and F(i, 0) = 0 for 1 ≤ i ≤ n. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 8 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.

61 0-1 Knapsack problem Given a knapsack with maximum capacity W, and a set S consisting of n items Each item i has some weight wi and benefit value bi (all wi , bi and W are integer values) Problem: How to pack the knapsack to achieve maximum total value of packed items?

62 0-1 Knapsack problem wi bi Weight Benefit value Items
2 3 This is a knapsack Max weight: W = 20 4 5 5 8 W = 20 3 4 9 10

63 0-1 Knapsack problem Problem, in other words, is to find The problem is called a “0-1” problem, because each item must be entirely accepted or rejected. Just another version of this problem is the “Fractional Knapsack Problem”, where we can take fractions of items.

64 0-1 Knapsack problem: brute-force approach
Let’s first solve this problem with a straightforward algorithm Since there are n items, there are 2n possible combinations of items. We go through all combinations and find the one with the most total value and with total weight less or equal to W Running time will be O(2n)

65 0-1 Knapsack problem: brute-force approach
Can we do better? Yes, with an algorithm based on dynamic programming We need to carefully identify the sub-problems Let’s try this: If items are labeled 1..n, then a sub-problem would be to find an optimal solution for Sk = {items labeled 1, 2, .. k}, where k ≤ n

66 Defining a Subproblem If items are labeled 1..n, then a sub-problem would be to find an optimal solution for Sk = {items labeled 1, 2, .. k} This is a valid sub-problem definition. The question is: can we describe the final solution (Sn ) in terms of sub-problems (Sk)? Unfortunately, we can’t do that. Explanation follows….

67 ? Defining a Subproblem wi bi
Weight Benefit wi bi Item # 1 2 3 Max weight: W = 20 For S4: Total weight: 14; total benefit: 20 S4 2 4 5 Optimal Solution ? 3 5 8 4 3 4 5 9 10 w1 =2 b1 =3 w2=4 b2 =5 w3 =5 b3 =8 w5 =9 b5 =10 Solution for S4 is not part of the solution for the problem!!! Optimal Solution: Total weight: 20 total benefit: 26

68 Defining a Subproblem (continued)
As we have seen, the solution for S4 is not part of the solution for the problem So our definition of a sub-problem is flawed and we need another one! Let’s add another parameter: w, which will represent the remaining weight of the knapsack The sub-problem then will be to compute B[k,w]: maximum value in a knapsack with k item and remaining weight w

69 Recursive Formula for subproblems
It means, that the best subset of Sk that has total weight w is one of the two: 1) the best subset of Sk-1 that has total weight w, or 2) the best subset of Sk-1 that has total weight w-wk plus the value of item k

70 Recursive Formula The best subset of Sk that has the total weight w, either contains item k or not. First case: wk>w. Item k can’t be part of the solution, since if it was, the total weight would be > w, which is unacceptable Second case: wk <=w. Then the item k can be in the solution, and we choose the case with greater value

71 0-1 Knapsack Algorithm for w = 0 to W B[0,w] = 0 for i = 0 to n
B[i,0] = 0 if wi <= w // item i can be part of the solution if bi + B[i-1,w-wi] > B[i-1,w] B[i,w] = bi + B[i-1,w- wi] else B[i,w] = B[i-1,w] else B[i,w] = B[i-1,w] // wi > w

72 Remember that the brute-force algorithm takes O(2n)
Running time What is the running time of this algorithm? O(n*W) Remember that the brute-force algorithm takes O(2n)

73 Example (1) Let’s run our algorithm on the following data: n = 4 (# of elements) W = 5 (max weight) Elements (weight, benefit): (2,3), (3,4), (4,5), (5,6)

74 Example (2) i 1 2 3 4 W 1 2 3 4 5 for w = 0 to W B[0,w] = 0

75 Example (3) i 1 2 3 4 W 1 2 3 4 5 for i = 0 to n B[i,0] = 0

76 Example (4) Items: 1: (2,3) 2: (3,4) 3: (4,5) 4: (5,6) i=1 bi=3 wi=2
1 2 3 4 W i=1 bi=3 wi=2 w=1 w-wi =-1 1 2 3 4 5 if wi <= w // item i can be part of the solution if bi + B[i-1,w-wi] > B[i-1,w] B[i,w] = bi + B[i-1,w- wi] else B[i,w] = B[i-1,w] else B[i,w] = B[i-1,w] // wi > w

77 Example (5) Items: 1: (2,3) 2: (3,4) 3: (4,5) 4: (5,6) i=1 bi=3 wi=2
1 2 3 4 W i=1 bi=3 wi=2 w=2 w-wi =0 1 2 3 3 4 5 if wi <= w // item i can be part of the solution if bi + B[i-1,w-wi] > B[i-1,w] B[i,w] = bi + B[i-1,w- wi] else B[i,w] = B[i-1,w] else B[i,w] = B[i-1,w] // wi > w

78 Example (6) Items: 1: (2,3) 2: (3,4) 3: (4,5) 4: (5,6) i=1 bi=3 wi=2
1 2 3 4 W i=1 bi=3 wi=2 w=3 w-wi=1 1 2 3 3 3 4 5 if wi <= w // item i can be part of the solution if bi + B[i-1,w-wi] > B[i-1,w] B[i,w] = bi + B[i-1,w- wi] else B[i,w] = B[i-1,w] else B[i,w] = B[i-1,w] // wi > w

79 Example (7) Items: 1: (2,3) 2: (3,4) 3: (4,5) 4: (5,6) i=1 bi=3 wi=2
1 2 3 4 W i=1 bi=3 wi=2 w=4 w-wi=2 1 2 3 3 3 4 3 5 if wi <= w // item i can be part of the solution if bi + B[i-1,w-wi] > B[i-1,w] B[i,w] = bi + B[i-1,w- wi] else B[i,w] = B[i-1,w] else B[i,w] = B[i-1,w] // wi > w

80 Example (8) Items: 1: (2,3) 2: (3,4) 3: (4,5) 4: (5,6) i=1 bi=3 wi=2
1 2 3 4 Items: 1: (2,3) 2: (3,4) 3: (4,5) 4: (5,6) W i=1 bi=3 wi=2 w=5 w-wi=2 1 2 3 3 3 4 3 5 3 if wi <= w // item i can be part of the solution if bi + B[i-1,w-wi] > B[i-1,w] B[i,w] = bi + B[i-1,w- wi] else B[i,w] = B[i-1,w] else B[i,w] = B[i-1,w] // wi > w

81 Example (9) Items: 1: (2,3) 2: (3,4) 3: (4,5) 4: (5,6) i=2 bi=4 wi=3
1 2 3 4 W i=2 bi=4 wi=3 w=1 w-wi=-2 1 2 3 3 3 4 3 5 3 if wi <= w // item i can be part of the solution if bi + B[i-1,w-wi] > B[i-1,w] B[i,w] = bi + B[i-1,w- wi] else B[i,w] = B[i-1,w] else B[i,w] = B[i-1,w] // wi > w

82 Example (10) Items: 1: (2,3) 2: (3,4) 3: (4,5) 4: (5,6) i=2 bi=4 wi=3
1 2 3 4 W i=2 bi=4 wi=3 w=2 w-wi=-1 1 2 3 3 3 3 4 3 5 3 if wi <= w // item i can be part of the solution if bi + B[i-1,w-wi] > B[i-1,w] B[i,w] = bi + B[i-1,w- wi] else B[i,w] = B[i-1,w] else B[i,w] = B[i-1,w] // wi > w

83 Example (11) Items: 1: (2,3) 2: (3,4) 3: (4,5) 4: (5,6) i=2 bi=4 wi=3
1 2 3 4 W i=2 bi=4 wi=3 w=3 w-wi=0 1 2 3 3 3 3 4 4 3 5 3 if wi <= w // item i can be part of the solution if bi + B[i-1,w-wi] > B[i-1,w] B[i,w] = bi + B[i-1,w- wi] else B[i,w] = B[i-1,w] else B[i,w] = B[i-1,w] // wi > w

84 Example (12) Items: 1: (2,3) 2: (3,4) 3: (4,5) 4: (5,6) i=2 bi=4 wi=3
1 2 3 4 W i=2 bi=4 wi=3 w=4 w-wi=1 1 2 3 3 3 3 4 4 3 4 5 3 if wi <= w // item i can be part of the solution if bi + B[i-1,w-wi] > B[i-1,w] B[i,w] = bi + B[i-1,w- wi] else B[i,w] = B[i-1,w] else B[i,w] = B[i-1,w] // wi > w

85 Example (13) Items: 1: (2,3) 2: (3,4) 3: (4,5) 4: (5,6) i=2 bi=4 wi=3
1 2 3 4 W i=2 bi=4 wi=3 w=5 w-wi=2 1 2 3 3 3 3 4 4 3 4 5 3 7 if wi <= w // item i can be part of the solution if bi + B[i-1,w-wi] > B[i-1,w] B[i,w] = bi + B[i-1,w- wi] else B[i,w] = B[i-1,w] else B[i,w] = B[i-1,w] // wi > w

86 Example (14) Items: 1: (2,3) 2: (3,4) 3: (4,5) 4: (5,6) i=3 bi=5 wi=4
1 2 3 4 W i=3 bi=5 wi=4 w=1..3 1 2 3 3 3 3 3 4 4 4 3 4 5 3 7 if wi <= w // item i can be part of the solution if bi + B[i-1,w-wi] > B[i-1,w] B[i,w] = bi + B[i-1,w- wi] else B[i,w] = B[i-1,w] else B[i,w] = B[i-1,w] // wi > w

87 Example (15) Items: 1: (2,3) 2: (3,4) 3: (4,5) 4: (5,6) i=3 bi=5 wi=4
1 2 3 4 W i=3 bi=5 wi=4 w=4 w- wi=0 1 2 3 3 3 3 3 4 4 4 3 4 5 5 3 7 if wi <= w // item i can be part of the solution if bi + B[i-1,w-wi] > B[i-1,w] B[i,w] = bi + B[i-1,w- wi] else B[i,w] = B[i-1,w] else B[i,w] = B[i-1,w] // wi > w

88 Example (15) Items: 1: (2,3) 2: (3,4) 3: (4,5) 4: (5,6) i=3 bi=5 wi=4
1 2 3 4 W i=3 bi=5 wi=4 w=5 w- wi=1 1 2 3 3 3 3 3 4 4 4 3 4 5 5 3 7 7 if wi <= w // item i can be part of the solution if bi + B[i-1,w-wi] > B[i-1,w] B[i,w] = bi + B[i-1,w- wi] else B[i,w] = B[i-1,w] else B[i,w] = B[i-1,w] // wi > w

89 Example (16) Items: 1: (2,3) 2: (3,4) 3: (4,5) 4: (5,6) i=3 bi=5 wi=4
1 2 3 4 W i=3 bi=5 wi=4 w=1..4 1 2 3 3 3 3 3 3 4 4 4 4 3 4 5 5 5 3 7 7 if wi <= w // item i can be part of the solution if bi + B[i-1,w-wi] > B[i-1,w] B[i,w] = bi + B[i-1,w- wi] else B[i,w] = B[i-1,w] else B[i,w] = B[i-1,w] // wi > w

90 Example (17) Items: 1: (2,3) 2: (3,4) 3: (4,5) 4: (5,6) i=3 bi=5 wi=4
1 2 3 4 W i=3 bi=5 wi=4 w=5 1 2 3 3 3 3 3 3 4 4 4 4 3 4 5 5 The greedy approach is not giving us the optimal value. 5 3 7 7 7 if wi <= w // item i can be part of the solution if bi + B[i-1,w-wi] > B[i-1,w] B[i,w] = bi + B[i-1,w- wi] else B[i,w] = B[i-1,w] else B[i,w] = B[i-1,w] // wi > w

91 Comments This algorithm only finds the max possible value that can be carried in the knapsack To know the items that make this maximum value, an addition to this algorithm is necessary Please see LCS algorithm from the previous lecture for the example how to extract this data from the table we built

92 Find Items Item (reversed order): i = 2, i = 1
1 2 3 4 W 1 2 3 3 3 3 3 3 4 4 4 4 3 4 5 5 5 3 7 7 7 Item (reversed order): i = 2, i = 1 Item (straight order): i=1, i = 2

93 Optimal Binary Search Trees
Example: What is an optimal BST for keys A, B, C, and D with search probabilities 0.1, 0.2, 0.4, and 0.3, respectively? Average number of comparison: a) 0.1 * * * * 4 = 2.9 b) 0.1 * * * * 3 = 2.1 c) 0.1 * * * * 2 = 1.7 A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 8 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.

94 Optimal Binary Search Trees
Problem: Given n keys a1 < …< an and probabilities p1 ≤ … ≤ pn searching for them, find a BST with a minimum average number of comparisons in successful search. Since total number of BSTs with n nodes is given by the nth Catalan number C(2n,n)/(n+1), which grows exponentially, brute force is hopeless. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 8 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.

95 DP for Optimal BST Problem
Let C[i,j] be minimum average number of comparisons made in T[i,j], optimal BST for keys ai < …< aj , where 1 ≤ i ≤ j ≤ n. Consider optimal BST among all BSTs with some ak (i ≤ k ≤ j ) as their root; T[i,j] is the best among them. C[i,j] = min {pk · 1 + ∑ ps (level as in T[i,k-1] +1) + ∑ ps (level as in T[k+1,j] +1)} i ≤ k ≤ j k-1 s = i j s =k+1 A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 8 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.

96 DP for Optimal BST Problem (cont.)
After simplifications, we obtain the recurrence for C[i,j]: C[i,j] = min {C[i,k-1] + C[k+1,j]} + ∑ ps for 1 ≤ i ≤ j ≤ n C[i,i] = pi for 1 ≤ i ≤ j ≤ n s = i j i ≤ k ≤ j A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 8 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.

97 Example: key A B C D probability 0.1 0.2 0.4 0.3
The tables below are filled diagonal by diagonal: the left one is filled using the recurrence C[i,j] = min {C[i,k-1] + C[k+1,j]} + ∑ ps , C[i,i] = pi ; The initial tables look like this: j i ≤ k ≤ j s = i

98 Example: key A B C D probability 0.1 0.2 0.4 0.3
j C[i,j] = min {C[i,k-1] + C[k+1,j]} + ∑ ps , C[i,i] = pi ; i ≤ k ≤ j s = i i j 1 2 3 4 .1 ? .2 .4 .3 5 i j 1 2 3 4 ? 5 optimal BST

99 Example: key A B C D probability 0.1 0.2 0.4 0.3
j C[i,j] = min {C[i,k-1] + C[k+1,j]} + ∑ ps , C[i,i] = pi ; i ≤ k ≤ j s = i i j 1 2 3 4 .1 .4 .2 ? .3 5 i j 1 2 3 4 ? 5 optimal BST

100 Example: key A B C D probability 0.1 0.2 0.4 0.3
j i ≤ k ≤ j s = i i j 1 2 3 4 .1 .4 .2 .8 ? .3 5 i j 1 2 3 4 ? 5 optimal BST

101 Example: key A B C D probability 0.1 0.2 0.4 0.3
j i ≤ k ≤ j s = i i j 1 2 3 4 .1 .4 ? .2 .8 1.0 .3 5 i j 1 2 3 4 ? 5 optimal BST

102 Example: key A B C D probability 0.1 0.2 0.4 0.3
j i ≤ k ≤ j s = i i j 1 2 3 4 .1 .4 1.1 .2 .8 ? 1.0 .3 5 i j 1 2 3 4 ? 5 optimal BST

103 Example: key A B C D probability 0.1 0.2 0.4 0.3
j i ≤ k ≤ j s = i i j 1 2 3 4 .1 .4 1.1 ? .2 .8 1.4 1.0 .3 5 i j 1 2 3 4 ? 5 optimal BST

104 Example: key A B C D probability 0.1 0.2 0.4 0.3
j i ≤ k ≤ j s = i i j 1 2 3 4 .1 .4 1.1 1.7 .2 .8 1.4 1.0 .3 5 i j 1 2 3 4 5 optimal BST

105 Example: key A B C D probability 0.1 0.2 0.4 0.3
The tables below are filled diagonal by diagonal: the left one is filled using the recurrence C[i,j] = min {C[i,k-1] + C[k+1,j]} + ∑ ps , C[i,i] = pi ; main table root table j i ≤ k ≤ j s = i i j 1 2 3 4 .1 .4 1.1 1.7 .2 .8 1.4 1.0 .3 5 i j 1 2 3 4 5 B A C D optimal BST

106 DP for Optimal BST Problem (cont.)
j C[i,j] = min {C[i,k-1] + C[k+1,j]} + ∑ ps for 1 ≤ i ≤ j ≤ n C[i,i] = pi for 1 ≤ i ≤ j ≤ n i ≤ k ≤ j s = i A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 8 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.

107 Optimal Binary Search Trees
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 8 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.

108 Analysis DP for Optimal BST Problem
Time efficiency: Θ(n3) Space efficiency: Θ(n2) A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 8 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.


Download ppt "Dynamic Programming."

Similar presentations


Ads by Google