Presentation is loading. Please wait.

Presentation is loading. Please wait.

Dynamic Programming Dynamic Programming 1/15/ :41 PM

Similar presentations


Presentation on theme: "Dynamic Programming Dynamic Programming 1/15/ :41 PM"— Presentation transcript:

1 Dynamic Programming Dynamic Programming 1/15/2019 10:41 PM

2 Matrix Chain-Products
Dynamic Programming is a general algorithm design paradigm. Rather than give the general structure, let us first give a motivating example: Matrix Chain-Products Review: Matrix Multiplication. C = A*B A is d × e and B is e × f O(def ) time A C B d f e i j i,j Dynamic Programming

3 Matrix Chain-Products
Compute A=A0*A1*…*An-1 Ai is di × di+1 Problem: How to parenthesize? Example B is 3 × 100 C is 100 × 5 D is 5 × 5 (B*C)*D takes = 1575 ops B*(C*D) takes = 4000 ops Dynamic Programming

4 An Enumeration Approach
Matrix Chain-Product Alg.: Try all possible ways to parenthesize A=A0*A1*…*An-1 Calculate number of ops for each one Pick the one that is best Running time: The number of paranethesizations is equal to the number of binary trees with n nodes This is exponential! It is called the Catalan number, and it is almost 4n. This is a terrible algorithm! Dynamic Programming

5 A Greedy Approach Idea #1: repeatedly select the product that uses (up) the most operations. Counter-example: A is 10 × 5 B is 5 × 10 C is 10 × 5 D is 5 × 10 Greedy idea #1 gives (A*B)*(C*D), which takes = 2000 ops But A*((B*C)*D) takes = 1000 ops Dynamic Programming

6 Another Greedy Approach
Idea #2: repeatedly select the product that uses the fewest operations. Counter-example: A is 101 × 11 B is 11 × 9 C is 9 × 100 D is 100 × 99 Greedy idea #2 gives A*((B*C)*D)), which takes = ops (A*B)*(C*D) takes = ops The greedy approach is not giving us the optimal value. Dynamic Programming

7 Outline and Reading Fuzzy Explanation of Dynamic Programming
Introductory example: Matrix Chain-Product (§5.3.1) The General Technique (§5.3.2) A very good example: 0-1 Knapsack Problem (§5.3.3) Dynamic Programming

8 A “Recursive” Approach
Define subproblems: Find the best parenthesization of Ai*Ai+1*…*Aj. Let Ni,j denote the number of operations done by this subproblem. The optimal solution for the whole problem is N0,n-1. Subproblem optimality: The optimal solution can be defined in terms of optimal subproblems There has to be a final multiplication (root of the expression tree) for the optimal solution. Say, the final multiply is at index i: (A0*…*Ai)*(Ai+1*…*An-1). Then the optimal solution N0,n-1 is the sum of two optimal subproblems, N0,i and Ni+1,n-1 plus the time for the last multiply. If the global optimum did not have these optimal subproblems, we could define an even better “optimal” solution. Dynamic Programming

9 A Characterizing Equation
The global optimal has to be defined in terms of optimal subproblems, depending on where the final multiply is at. Let us consider all possible places for that final multiply: Recall that Ai is a di × di+1 dimensional matrix. So, a characterizing equation for Ni,j is the following: Note that subproblems are not independent--the subproblems overlap. Dynamic Programming

10 A Dynamic Programming Algorithm
Since subproblems overlap, we don’t use recursion. Instead, we construct optimal subproblems “bottom-up.” Ni,i’s are easy, so start with them Then do length 2,3,… subproblems, and so on. Running time: O(n3) Algorithm matrixChain(S): Input: sequence S of n matrices to be multiplied Output: number of operations in an optimal paranethization of S for i  1 to n-1 do Ni,i  0 for b  1 to n-1 do for i  0 to n-b-1 do j  i+b Ni,j  +infinity for k  i to j-1 do Ni,j  min{Ni,j , Ni,k +Nk+1,j +di dk+1 dj+1} Dynamic Programming

11 A Dynamic Programming Algorithm Visualization
The bottom-up construction fills in the N array by diagonals Ni,j gets values from pervious entries in i-th row and j-th column Filling in each entry in the N table takes O(n) time. Total run time: O(n3) Getting actual parenthesization can be done by remembering “k” for each N entry answer N n-1 1 2 j 1 i n-1 Dynamic Programming

12 Matrix Chain algorithm
Algorithm matrixChain(S): Input: sequence S of n matrices to be multiplied Output: # of multiplications in optimal parenthesization of S for i  0 to n-1 do Ni,i  0 for b  1 to n-1 do // b is # of ops in S for i  0 to n-b-1 do j  i+b Ni,j  +infinity for k  i to j-1 do sum = Ni,k +Nk+1,j +di dk+1 dj+1 if (sum < Ni,j) then Ni,j  sum Oi,j  k return N0,n-1 Example: ABCD A is 10 × 5 B is 5 × 10 C is 10 × 5 D is 5 × 10 N 500 500 1000 2 A AB A(BC) (A(BC))D 1 250 500 1 B BC (BC)D 2 500 C CD 3 D Dynamic Programming

13 Recovering operations
Example: ABCD A is 10 × 5 B is 5 × 10 C is 10 × 5 D is 5 × 10 // return expression for multiplying // matrix chain Ai through Aj exp(i,j) if (i=j) then // base case, 1 matrix return ‘Ai’ else k = O[i,j] // see red values on left S1 = exp(i,k) // 2 recursive calls S2 = exp(k+1,j) return ‘(‘ S1 S2 ‘)’ N 500 500 1000 2 A AB A(BC) (A(BC))D 1 250 500 1 B BC (BC)D 2 500 C CD 3 D Dynamic Programming

14 The General Dynamic Programming Technique
Applies to a problem that at first seems to require a lot of time (possibly exponential), provided we have: Simple subproblems: the subproblems can be defined in terms of a few variables, such as j, k, l, m, and so on. Subproblem optimality: the global optimum value can be defined in terms of optimal subproblems Subproblem overlap: the subproblems are not independent, but instead they overlap (hence, should be constructed bottom-up). Dynamic Programming

15 Knapsack problem Given some items, pack the knapsack to get
the maximum total value. Each item has some weight and some benefit. Total weight that we can carry is no more than some fixed number W. So we must consider weights of items as well as their value. Item # Weight benefit Dynamic Programming

16 Knapsack problem There are two versions of the problem:
Items are indivisible; you either take an item or not. Solved with dynamic programming. “Fractional knapsack problem” Items are divisible: you can take any fraction of an item. Solved with a greedy algorithm. Dynamic Programming

17 The 0/1 Knapsack Problem Given: A set S of n items, with each item i having bi - a positive benefit wi - a positive weight Goal: Choose items with maximum total benefit but with weight at most W. If we are not allowed to take fractional amounts, then this is the 0/1 knapsack problem. In this case, we let T denote the set of items we take Objective: maximize Constraint: Dynamic Programming

18 0-1 Knapsack problem: a picture
Modified from: David Luebke, University of Virginia Charlottesville 0-1 Knapsack problem: a picture Weight Benefit wi bi Items 3 2 This is a knapsack Max weight: W = 20 3 4 4 5 W = 20 5 8 9 10 Dynamic Programming

19 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) Dynamic Programming

20 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 subproblems Let’s try this: If items are labeled 1..n, then a subproblem would be to find an optimal solution for Sk = {items labeled 1, 2, .. k} Dynamic Programming

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

22 Defining a Subproblem wi bi Weight Benefit # Max weight: W = 20
Item # Max weight: W = 20 For S4: Total weight: 14; Total benefit: 20 1 2 3 2 3 4 S4 S5 3 4 5 4 5 8 w1 =2 b1 =3 w2 =4 b2 =5 w3 =5 b3 =8 w4 =9 b4 =10 5 9 10 For S5: Total weight: 20 Total benefit: 26 Solution for S4 is not part of the solution for S5!!! Dynamic Programming

23 Defining a Subproblem (continued)
Modified from: David Luebke, University of Virginia Charlottesville Defining a Subproblem (continued) As we have seen, the solution for S4 is not part of the solution for S5 So our definition of a subproblem is flawed and we need another one! Let’s add another parameter: w, which will represent the exact weight for each subset of items The subproblem then will be to compute B[k,w] Dynamic Programming

24 Recursive Formula for subproblems
Modified from: David Luebke, University of Virginia Charlottesville Recursive Formula for subproblems Recursive formula for subproblems: It means, that the best subset of Sk that has total weight w is one of the two: the best subset of Sk-1 that has total weight w, or the best subset of Sk-1 that has total weight w-wk plus the item k Dynamic Programming

25 Modified from: David Luebke, University of Virginia Charlottesville
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 item k can be in the solution, and we choose the case with greater value Dynamic Programming

26 0-1 Knapsack Algorithm O(W) Repeat n times O(W)
Modified from: David Luebke, University of Virginia Charlottesville 0-1 Knapsack Algorithm i w B[i,w] B[i-1,w] B[i-1,w-wi] O(W) 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 Repeat n times O(W) <the rest of the code> What is the running time of this algorithm? O(n*W) Remember that the brute-force algorithm takes O(2n) Dynamic Programming

27 Example Let’s run our algorithm on the following data:
Modified from: David Luebke, University of Virginia Charlottesville Example 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) Dynamic Programming

28 Example (continue) for w = 0 to W B[0,w] = 0 i 1 2 3 4 W 1 2 3 4 5
Modified from: David Luebke, University of Virginia Charlottesville Example (continue) i 1 2 3 4 W 1 2 3 4 5 for w = 0 to W B[0,w] = 0 Dynamic Programming

29 Example (continue) for i = 0 to n B[i,0] = 0 i 1 2 3 4 W 1 2 3 4 5
Modified from: David Luebke, University of Virginia Charlottesville Example (continue) i 1 2 3 4 W 1 2 3 4 5 for i = 0 to n B[i,0] = 0 Dynamic Programming

30 Example (continue) Items: 1: (2,3) 2: (3,4) 3: (4,5) 4: (5,6) i=1 bi=3
Modified from: David Luebke, University of Virginia Charlottesville Example (continue) Items: 1: (2,3) 2: (3,4) 3: (4,5) 4: (5,6) i 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 Dynamic Programming

31 Example (continue) Items: 1: (2,3) 2: (3,4) 3: (4,5) 4: (5,6) i=1 bi=3
Modified from: David Luebke, University of Virginia Charlottesville Example (continue) Items: 1: (2,3) 2: (3,4) 3: (4,5) 4: (5,6) i 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 Dynamic Programming

32 Example (continue) Items: 1: (2,3) 2: (3,4) 3: (4,5) 4: (5,6) i=1 bi=3
Modified from: David Luebke, University of Virginia Charlottesville Example (continue) Items: 1: (2,3) 2: (3,4) 3: (4,5) 4: (5,6) i 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 Dynamic Programming

33 Example (continue) Items: 1: (2,3) 2: (3,4) 3: (4,5) 4: (5,6) i=1 bi=3
Modified from: David Luebke, University of Virginia Charlottesville Example (continue) Items: 1: (2,3) 2: (3,4) 3: (4,5) 4: (5,6) i 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 Dynamic Programming

34 Example (continue) Items: 1: (2,3) 2: (3,4) 3: (4,5) 4: (5,6) i=1 bi=3
Modified from: David Luebke, University of Virginia Charlottesville Example (continue) Items: 1: (2,3) 2: (3,4) 3: (4,5) 4: (5,6) i 1 2 3 4 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 Dynamic Programming

35 Example (continue) Items: 1: (2,3) 2: (3,4) 3: (4,5) 4: (5,6) i=2 bi=4
Modified from: David Luebke, University of Virginia Charlottesville Example (continue) Items: 1: (2,3) 2: (3,4) 3: (4,5) 4: (5,6) i 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 Dynamic Programming

36 Example (continue) Items: 1: (2,3) 2: (3,4) 3: (4,5) 4: (5,6) i=2 bi=4
Modified from: David Luebke, University of Virginia Charlottesville Example (continue) Items: 1: (2,3) 2: (3,4) 3: (4,5) 4: (5,6) i 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 Dynamic Programming

37 Example (continue) Items: 1: (2,3) 2: (3,4) 3: (4,5) 4: (5,6) i=2 bi=4
Modified from: David Luebke, University of Virginia Charlottesville Example (continue) Items: 1: (2,3) 2: (3,4) 3: (4,5) 4: (5,6) i 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 Dynamic Programming

38 Example (continue) Items: 1: (2,3) 2: (3,4) 3: (4,5) 4: (5,6) i=2 bi=4
Modified from: David Luebke, University of Virginia Charlottesville Example (continue) Items: 1: (2,3) 2: (3,4) 3: (4,5) 4: (5,6) i 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 Dynamic Programming

39 Example (continue) Items: 1: (2,3) 2: (3,4) 3: (4,5) 4: (5,6) i=2 bi=4
Modified from: David Luebke, University of Virginia Charlottesville Example (continue) Items: 1: (2,3) 2: (3,4) 3: (4,5) 4: (5,6) i 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 Dynamic Programming

40 Example (continue) Items: 1: (2,3) 2: (3,4) 3: (4,5) 4: (5,6) i=3 bi=5
Modified from: David Luebke, University of Virginia Charlottesville Example (continue) Items: 1: (2,3) 2: (3,4) 3: (4,5) 4: (5,6) i 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 Dynamic Programming

41 Example (continue) Items: 1: (2,3) 2: (3,4) 3: (4,5) 4: (5,6) i=3 bi=5
Modified from: David Luebke, University of Virginia Charlottesville Example (continue) Items: 1: (2,3) 2: (3,4) 3: (4,5) 4: (5,6) i 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 Dynamic Programming

42 Example (continue) Items: 1: (2,3) 2: (3,4) 3: (4,5) 4: (5,6) i=3 bi=5
Modified from: David Luebke, University of Virginia Charlottesville Example (continue) Items: 1: (2,3) 2: (3,4) 3: (4,5) 4: (5,6) i 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 Dynamic Programming

43 Example (continue) Items: 1: (2,3) 2: (3,4) 3: (4,5) 4: (5,6) i=3 bi=5
Modified from: David Luebke, University of Virginia Charlottesville Example (continue) Items: 1: (2,3) 2: (3,4) 3: (4,5) 4: (5,6) i 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 Dynamic Programming

44 Example (continue) Items: 1: (2,3) 2: (3,4) 3: (4,5) 4: (5,6) i=3 bi=5
Modified from: David Luebke, University of Virginia Charlottesville Example (continue) Items: 1: (2,3) 2: (3,4) 3: (4,5) 4: (5,6) i 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 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 Dynamic Programming

45 Modified from: David Luebke, University of Virginia Charlottesville
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 Dynamic Programming

46 How to find actual knapsack items
Modified from: David Luebke, University of Virginia Charlottesville How to find actual knapsack items All of the information we need is in the table. B[n,W] is the maximal value of items that can be placed in the Knapsack. Let i=n and k=W if B[i,k] ≠ B[i -1,k] then mark the ith item as in the knapsack i = i -1, k = k-wi else i = i -1 // Assume the ith item is not in the knapsack // Could it be in the optimally packed knapsack? Dynamic Programming

47 Finding Items (continue)
Modified from: David Luebke, University of Virginia Charlottesville Finding Items (continue) Items: 1: (2,3) 2: (3,4) 3: (4,5) 4: (5,6) i 1 2 3 4 W i=4 k=5 bi=5 wi=6 B[i,k]=7 B[i-1,k]=7 1 2 3 3 3 3 3 3 4 4 4 4 3 4 5 5 5 3 7 7 7 i = n , k = w while i , k > 0 if B[i,k] ≠ B[i-1,k] mark the ith item as the knapsack i = i-1, k = k-wi else i = i-1 Dynamic Programming

48 Finding Items (continue)
Modified from: David Luebke, University of Virginia Charlottesville Finding Items (continue) Items: 1: (2,3) 2: (3,4) 3: (4,5) 4: (5,6) i 1 2 3 4 W i=4 k=5 bi=5 wi=6 B[i,k]=7 B[i-1,k]=7 1 2 3 3 3 3 3 3 4 4 4 4 3 4 5 5 5 3 7 7 7 i = n , k = w while i , k > 0 if B[i,k] ≠ B[i-1,k] mark the ith item as the knapsack i = i-1, k = k-wi else i = i-1 Dynamic Programming

49 Finding Items (continue)
Modified from: David Luebke, University of Virginia Charlottesville Finding Items (continue) Items: 1: (2,3) 2: (3,4) 3: (4,5) 4: (5,6) i 1 2 3 4 W i=3 k=5 bi=5 wi=4 B[i,k]=7 B[i-1,k]=7 1 2 3 3 3 3 3 3 4 4 4 4 3 4 5 5 5 3 7 7 7 i = n , k = w while i , k > 0 if B[i,k] ≠ B[i-1,k] mark the ith item as the knapsack i = i-1, k = k-wi else i = i-1 Dynamic Programming

50 Finding Items (continue)
Modified from: David Luebke, University of Virginia Charlottesville Finding Items (continue) Items: 1: (2,3) 2: (3,4) 3: (4,5) 4: (5,6) i 1 2 3 4 W i=2 k=5 bi=4 wi=3 B[i,k]=7 B[i-1,k]=3 k-wi=2 1 2 3 3 3 3 3 3 4 4 4 4 3 4 5 5 5 3 7 7 7 i = n , k = w while i , k > 0 if B[i,k] ≠ B[i-1,k] mark the ith item as the knapsack i = i-1, k = k-wi else i = i-1 Dynamic Programming

51 Finding Items (continue)
Modified from: David Luebke, University of Virginia Charlottesville Finding Items (continue) Items: 1: (2,3) 2: (3,4) 3: (4,5) 4: (5,6) i 1 2 3 4 W i=1 k=2 bi=3 wi=2 B[i,k]=3 B[i-1,k]=0 k-wi=0 1 2 3 3 3 3 3 3 4 4 4 4 3 4 5 5 5 3 7 7 7 i = n , k = w while i , k > 0 if B[i,k] ≠ B[i-1,k] mark the ith item as the knapsack i = i-1, k = k-wi else i = i-1 Dynamic Programming

52 Finding Items (continue)
Modified from: David Luebke, University of Virginia Charlottesville Finding Items (continue) Items: 1: (2,3) 2: (3,4) 3: (4,5) 4: (5,6) i 1 2 3 4 W i=0 k=0 1 2 3 3 3 3 3 3 4 4 4 4 3 4 5 5 5 3 7 7 7 The optimal knapsack should contain items: { 1 , 2 } i = n , k = w while i , k > 0 if B[i,k] ≠ B[i-1,k] mark the ith item as the knapsack i = i-1, k = k-wi else i = i-1 Dynamic Programming

53 Finding Items (continue)
Modified from: David Luebke, University of Virginia Charlottesville Finding Items (continue) Items: 1: (2,3) 2: (3,4) 3: (4,5) 4: (5,6) i 1 2 3 4 W i=0 k=0 1 2 3 3 3 3 3 3 4 4 4 4 3 4 5 5 5 3 7 7 7 The optimal knapsack should contain items: { 1 , 2 } i = n , k = w while i , k > 0 if B[i,k] ≠ B[i-1,k] mark the ith item as the knapsack i = i-1, k = k-wi else i = i-1 Dynamic Programming

54 Modified from: David Luebke, University of Virginia Charlottesville
Conclusions Dynamic programming is a useful technique for solving certain kind of problems When the solution can be recursively described in terms of partial solutions, we can store these partial solutions and re-use them as necessary Running time of dynamic programming algorithm vs. naïve algorithm: » 0-1 Knapsack problem: O(W*n) vs. O(2n) Dynamic Programming


Download ppt "Dynamic Programming Dynamic Programming 1/15/ :41 PM"

Similar presentations


Ads by Google