Example: barber

CSCE 310J Data Structures & Algorithms

11 Dynamic programming0-1 knapsack problemDr. Steve 310 JData Structures & Algorithms2 Giving credit where credit is due: Most of slides for this lecture are based on slides created by Dr. David Luebke, University of Virginia. Some slides are based on lecture notes created by Dr. Chuck Cusack, UNL. I have modified them and added new 310 JData Structures & Algorithms3 Another strategy for designing Algorithms is dynamic programming A metatechnique, not an algorithm (like divide & conquer) The word programming is historical and predates computer programming Use when problem breaks down into recurring small subproblemsDynamic Programming4 dynamic programming It is used when the solution can be recursively described in terms of solutions to subproblems (optimal substructure). Algorithm finds solutions to subproblems and stores them in memory for later use. More efficient than brute-force methods , which solve the same subproblems over and over the Concept of dynamic programming Basic idea: Optimal substructure: optimal solution to problem consists of optimal solutions to subproblems Overlapping subproblems: few subproblems in total, many recurring instances of each Solve bottom-up, building a table of solved subproblems that are used to solve larger ones Variations: Table could be 3-dimensional, triangular, a tree, etc.

1 1 Dynamic programming 0-1 Knapsack problem Dr. Steve Goddard goddard@cse.unl.edu http://www.cse.unl.edu/~goddard/Courses/CSCE310J CSCE 310J Data Structures & Algorithms

Tags:

  Programming, Dynamics, Problem, Knapsack, Dynamic programming, Knapsack problem

Information

Domain:

Source:

Link to this page:

Please notify us if you found a problem with this document:

Other abuse

Transcription of CSCE 310J Data Structures & Algorithms

1 11 Dynamic programming0-1 knapsack problemDr. Steve 310 JData Structures & Algorithms2 Giving credit where credit is due: Most of slides for this lecture are based on slides created by Dr. David Luebke, University of Virginia. Some slides are based on lecture notes created by Dr. Chuck Cusack, UNL. I have modified them and added new 310 JData Structures & Algorithms3 Another strategy for designing Algorithms is dynamic programming A metatechnique, not an algorithm (like divide & conquer) The word programming is historical and predates computer programming Use when problem breaks down into recurring small subproblemsDynamic Programming4 dynamic programming It is used when the solution can be recursively described in terms of solutions to subproblems (optimal substructure). Algorithm finds solutions to subproblems and stores them in memory for later use. More efficient than brute-force methods , which solve the same subproblems over and over the Concept of dynamic programming Basic idea: Optimal substructure: optimal solution to problem consists of optimal solutions to subproblems Overlapping subproblems: few subproblems in total, many recurring instances of each Solve bottom-up, building a table of solved subproblems that are used to solve larger ones Variations: Table could be 3-dimensional, triangular, a tree, etc.

2 6 Given some items, pack the knapsack to get the maximum total value. Each item has some weight and some value. Total weight that we can carry is no more than some fixed number we must consider weights of items as well as their # Weight Value1 1 82 3 63 5 5 knapsack problem (Review)27 knapsack problemThere are two versions of the problem :1. 0-1 knapsack problem and2. Fractional knapsack problem are indivisible; you either take an item or not. Solved with dynamic are divisible: you can take any fraction of an item. Solved with a greedy algorithm. We have already seen this version8 Given a knapsack with maximum capacity W, and a set Sconsisting of nitems Each item ihas some weight wiand benefit value bi(all wi, biand Ware integer values) problem : How to pack the knapsack to achieve maximum total value of packed items?

3 0-1 knapsack problem9W = 20wibi10985544332 WeightBenefit valueThis is a knapsackMax weight: W = 20 Items0-1 knapsack problem : a picture10 problem , in other words, is to find TiiTiiWwb subject to max0-1 knapsack problem The problem is called a 0-1 problem , because each item must be entirely accepted or rejected. In the Fractional knapsack problem , we can take fractions of items. 11 Let s first solve this problem with a straightforward algorithm Since there are nitems, there are 2npossible combinations of items. We go through all combinations and find the one with maximum value and with total weight less or equal to W Running time will be O(2n)0-1 knapsack problem : brute-force approach12 Can we do better? Yes, with an algorithm based on dynamic programming We need to carefully identify the subproblemsLet s try this:If items are labeled , then a subproblem would be to find an optimal solution for Sk= {items labeled 1, 2.}

4 K}0-1 knapsack problem : brute-force approach313If items are labeled , then a subproblem would be to find an optimal solution for Sk= {items labeled 1, 2, .. k} This is a reasonable subproblem definition. The question is: can we describe the final solution (Sn) in terms of subproblems (Sk)? Unfortunately, we can tdo that. Defining a Subproblem14 Max weight: W = 20 For S4:Total weight: 14;Maximum benefit: 20w1 =2b1 =3w2 =4b2 =5w3 =5b3 =8w4 =3b4 =4wibi1085544332 WeightBenefit9 Item#43215S4S5w1 =2b1 =3w2 =4b2 =5w3 =5b3 =8w5 =9b5 =10 For S5:Total weight: 20 Maximum benefit: 26 Solution for S4is not part of the solution for S5!!!?Defining a Subproblem15 As we have seen, the solution for S4is 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 exactweight for each subset of items The subproblem then will be to compute B[k,w]Defining a Subproblem (continued)16It means, that the best subset of Skthat has total weight wis:1) the best subset of Sk-1that has total weight w, or2) the best subset of Sk-1that has total weight w-wkplus the item k + > =else }],1[],,1[max{ if ],1[],[kkkbwwkBwkBwwwkBwkBRecursive formula for subproblems:Recursive Formula for subproblems17 The best subset of Skthat has the total weight w,either contains item kor not.

5 First case: wk>w. Item kcan 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 kcanbe in the solution, and we choose the case with greater value. + > =else }],1[],,1[max{ if ],1[],[kkkbwwkBwkBwwwkBwkBRecursive Formula18for w = 0 to WB[0,w] = 0for i = 1 to nB[i,0] = 0for i = 1 to nfor w = 0 to Wif wi<= w // item i can be part of the solutionif bi+ B[i-1,w-wi] > B[i-1,w]B[i,w] = bi+ B[i-1,w- wi]elseB[i,w] = B[i-1,w]else B[i,w] = B[i-1,w] // wi> w 0-1 knapsack Algorithm419for w = 0 to WB[0,w] = 0for i = 1 to nB[i,0] = 0for i = 1 to nfor w = 0 to W< the rest of the code >What is the running time of this algorithm?O(W)O(W)Repeat ntimesO(n*W)Remember that the brute-force algorithm takes O(2n)Running time20 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)Example21for w = 0 to WB[0,w] = 000000001234501234i\WExample (2)22for i = 1 to nB[i,0] = 0000000000001234501234i\WExample (3)23if wi<= w // item i can be part of the solutionif bi+ B[i-1,w-wi] > B[i-1,w]B[i,w] = bi+ B[i-1,w- wi]elseB[i,w] = B[i-1,w]else B[i,w] = B[i-1,w]// wi> w 0 Items:1: (2,3)2: (3,4)3: (4,5) 4: (5,6)0i=1bi=3wi=2w=1w-wi=-10000000123450 1234i\W000 Example (4)24 Items:1: (2,3)2: (3,4)3: (4,5) 4: (5,6)30000000000001234501234i\Wi=1bi=3wi =2w=2w-wi=0if wi<= w // item i can be part of the solutionif bi+ B[i-1,w-wi] > B[i-1,w]B[i,w] = bi+ B[i-1,w- wi]elseB[i,w] = B[i-1,w]else B[i,w] = B[i-1,w] // wi> w Example (5)525 Items:1: (2,3)2: (3,4)3: (4,5) 4.

6 (5,6)30000000000001234501234i\Wi=1bi=3wi =2w=3w-wi=1if wi<= w // item i can be part of the solutionif bi+ B[i-1,w-wi] > B[i-1,w]B[i,w] = bi+ B[i-1,w- wi]elseB[i,w] = B[i-1,w]else B[i,w] = B[i-1,w] // wi> w 3 Example (6)26 Items:1: (2,3)2: (3,4)3: (4,5) 4: (5,6)30000000000001234501234i\Wi=1bi=3wi =2w=4w-wi=2if wi<= w // item i can be part of the solutionif bi+ B[i-1,w-wi] > B[i-1,w]B[i,w] = bi+ B[i-1,w- wi]elseB[i,w] = B[i-1,w]else B[i,w] = B[i-1,w] // wi> w 33 Example (7)27 Items:1: (2,3)2: (3,4)3: (4,5) 4: (5,6)30000000000001234501234i\Wi=1bi=3wi =2w=5w-wi=3if wi<= w // item i can be part of the solutionif bi+ B[i-1,w-wi] > B[i-1,w]B[i,w] = bi+ B[i-1,w- wi]elseB[i,w] = B[i-1,w]else B[i,w] = B[i-1,w] // wi> w 333 Example (8)28 Items:1: (2,3)2: (3,4)3: (4,5) 4: (5,6)0000000000001234501234i\Wi=2bi=4wi= 3w=1w-wi=-233330if wi<= w // item i can be part of the solutionif bi+ B[i-1,w-wi] > B[i-1,w]B[i,w] = bi+ B[i-1,w- wi]elseB[i,w] = B[i-1,w]else B[i,w] = B[i-1,w]// wi> w Example (9)29 Items:1: (2,3)2: (3,4)3: (4,5) 4: (5,6)0000000000001234501234i\Wi=2bi=4wi= 3w=2w-wi=-133333if wi<= w // item i can be part of the solutionif bi+ B[i-1,w-wi] > B[i-1,w]B[i,w] = bi+ B[i-1,w- wi]elseB[i,w] = B[i-1,w]else B[i,w] = B[i-1,w]// wi> w 0 Example (10)30 Items:1: (2,3)2: (3,4)3: (4,5) 4: (5,6)0000000000001234501234i\Wi=2bi=4wi= 3w=3w-wi=033330if wi<= w // item i can be part of the solutionif bi+ B[i-1,w-wi] > B[i-1,w]B[i,w] = bi+ B[i-1,w- wi]elseB[i,w] = B[i-1,w]else B[i,w] = B[i-1,w] // wi> w 43 Example (11)631 Items:1: (2,3)2: (3,4)3: (4,5) 4.

7 (5,6)0000000000001234501234i\Wi=2bi=4wi= 3w=4w-wi=133330if wi<= w // item i can be part of the solutionif bi+ B[i-1,w-wi] > B[i-1,w]B[i,w] = bi+ B[i-1,w- wi]elseB[i,w] = B[i-1,w]else B[i,w] = B[i-1,w] // wi> w 434 Example (12)32 Items:1: (2,3)2: (3,4)3: (4,5) 4: (5,6)0000000000001234501234i\Wi=2bi=4wi= 3w=5w-wi=233330if wi<= w // item i can be part of the solutionif bi+ B[i-1,w-wi] > B[i-1,w]B[i,w] = bi+ B[i-1,w- wi]elseB[i,w] = B[i-1,w]else B[i,w] = B[i-1,w] // wi> w 7344 Example (13)33 Items:1: (2,3)2: (3,4)3: (4,5) 4: (5,6)0000000000001234501234i\Wi=3bi=5wi= 4w= wi<= w // item i can be part of the solutionif bi+ B[i-1,w-wi] > B[i-1,w]B[i,w] = bi+ B[i-1,w- wi]elseB[i,w] = B[i-1,w]else B[i,w] = B[i-1,w]// wi> w 7340 Example (14)34 Items:1: (2,3)2: (3,4)3: (4,5) 4: (5,6)0000000000001234501234i\Wi=3bi=5wi= 4w= 4w- wi=03333034470345if wi<= w // item i can be part of the solutionif bi+ B[i-1,w-wi] > B[i-1,w]B[i,w] = bi+ B[i-1,w- wi]elseB[i,w] = B[i-1,w]else B[i,w] = B[i-1,w] // wi> w Example (15)35 Items:1: (2,3)2: (3,4)3: (4,5) 4: (5,6)0000000000001234501234i\Wi=3bi=5wi= 4w= 5w- wi=1333303447034if wi<= w // item i can be part of the solutionif bi+ B[i-1,w-wi] > B[i-1,w]B[i,w] = bi+ B[i-1,w- wi]elseB[i,w] = B[i-1,w]else B[i,w] = B[i-1,w] // wi> w 57 Example (16)36 Items:1: (2,3)2: (3,4)3: (4,5) 4: (5,6)0000000000001234501234i\Wi=4bi=6wi= 5w= wi<= w // item i can be part of the solutionif bi+ B[i-1,w-wi] > B[i-1,w]B[i,w] = bi+ B[i-1,w- wi]elseB[i,w] = B[i-1,w]else B[i,w] = B[i-1,w]// wi> w 7340703455 Example (17)737 Items:1: (2,3)2: (3,4)3: (4,5) 4.

8 (5,6)0000000000001234501234i\Wi=4bi=6wi= 5w= 5w- wi=0333303447034if wi<= w // item i can be part of the solutionif bi+ B[i-1,w-wi] > B[i-1,w]B[i,w] = bi+ B[i-1,w- wi]elseB[i,w] = B[i-1,w]else B[i,w] = B[i-1,w] // wi> w 5770345 Example (18)38 Comments This algorithm only finds the max possible value that can be carried in the knapsack , the value in B[n,W] To know the items that make this maximum value, an addition to this algorithm is 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=Wif B[i,k] B[i 1,k] thenmark theithitem as in the knapsacki = i 1, k= k-wielsei = i 1 // Assume the ithitem is notin the knapsack //Could it be in the optimally packed knapsack ?How to find actual knapsack Items40 Items:1: (2,3)2: (3,4)3: (4,5) 4: (5,6)0000000000001234501234i\Wi=4k= 5bi=6wi=5B[i,k] = 7B[i 1,k] =7333303447034i=n, k=Wwhile i,k > 0if B[i,k] B[i 1,k] then mark the ithitem as in the knapsacki = i 1, k= k-wielsei = i 15703457 Finding the Items41 Items:1: (2,3)2: (3,4)3: (4,5) 4: (5,6)0000000000001234501234i\Wi=4k= 5bi=6wi=5B[i,k] = 7B[i 1,k] =7333303447034i=n, k=Wwhile i,k > 0if B[i,k] B[i 1,k] then mark the ithitem as in the knapsacki = i 1, k= k-wielsei = i 15703457 Finding the Items (2)42 Items:1: (2,3)2: (3,4)3: (4,5) 4: (5,6)0000000000001234501234i\Wi=3k= 5bi=6wi=4B[i,k] = 7B[i 1,k] =7333303447034i=n, k=Wwhile i,k > 0if B[i,k] B[i 1,k] then mark the ithitem as in the knapsacki = i 1, k= k-wielsei = i 15703457 Finding the Items (3)843 Items:1: (2,3)2: (3,4)3: (4,5) 4.

9 (5,6)0000000000001234501234i\Wi=2k= 5bi=4wi=3B[i,k] = 7B[i 1,k] =3k wi=2333303447034i=n, k=Wwhile i,k > 0if B[i,k] B[i 1,k] then mark the ithitem as in the knapsacki = i 1, k= k-wielsei = i 157034577 Finding the Items (4)44 Items:1: (2,3)2: (3,4)3: (4,5) 4: (5,6)0000000000001234501234i\Wi=1k= 2bi=3wi=2B[i,k] = 3B[i 1,k] =0k wi=0333303447034i=n, k=Wwhile i,k > 0if B[i,k] B[i 1,k] then mark the ithitem as in the knapsacki = i 1, k= k-wielsei = i 157034573 Finding the Items (5)45 Items:1: (2,3)2: (3,4)3: (4,5) 4: (5,6)0000000000001234501234i\W3333034470 34i=n, k=Wwhile i,k > 0if B[i,k] B[i 1,k] then mark the nthitem as in the knapsacki = i 1, k= k-wielsei = i 15703457i=0k= 0 The optimal knapsack should contain {1, 2}Finding the Items (6)46 Items:1: (2,3)2: (3,4)3: (4,5) 4: (5,6)0000000000001234501234i\W3333034470 34i=n, k=Wwhile i,k > 0if B[i,k] B[i 1,k] then mark the nthitem as in the knapsacki = i 1, k= k-wielsei = i 15703457 The optimal knapsack should contain {1, 2}73 Finding the Items (7)47 Review: The knapsack problem And Optimal Substructure Both variations exhibit optimal substructure To show this for the 0-1 problem , consider the most valuable load weighing at most Wpounds If we remove item j from the load, what do we know about the remaining load?

10 A: remainder must be the most valuable load weighing at most W-wjthat thief could take, excluding item j48 Solving The knapsack problem The optimal solution to the fractional knapsack problem can be found with a greedy algorithm Do you recall how? Greedy strategy: take in order of dollars/pound The optimal solution to the 0-1 problem cannot be found with the same greedy strategy Example: 3 items weighing 10, 20, and 30 pounds, knapsack can hold 50 pounds Suppose item 2 is worth $100. Assign values to the other items so that the greedy strategy will fail 949 The knapsack problem : Greedy Vs. Dynamic The fractional problem can be solved greedily The 0-1 problem can be solved with a dynamic programming approach50 Memoization Memoizationis another way to deal with overlapping subproblems in dynamic programming After computing the solution to a subproblem, store it in a table Subsequent calls just do a table lookup With memoization, we implement the algorithm recursively: If we encounter a subproblem we have seen, we look up the answer If not, compute the solution and add it to the list of subproblems we have seen.


Related search queries