CSCE J Data Structures & Algorithms Dynamic programming - Knapsac problem Dr. Steve Goddard goddard@cse.unl.edu CSCE J Data Structures & Algorithms Giving credit where credit is due:» Most of slides for this lecture are based on slides created by Dr. David Luebe, University of Virginia.» Some slides are based on lecture notes created by Dr. Chuc Cusac, UNL.» I have modified them and added new slides. http://www.cse.unl.edu/~goddard/courses/cscej Dynamic Programming Another strategy for designing algorithms is dynamic programming» A metatechnique, not an algorithm (lie divide & conquer)» The word programming is historical and predates computer programming Use when problem breas down into recurring small subproblems 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 again. Summarizing 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 -dimensional, triangular, a tree, etc. Knapsac problem (Review) Given some items, pac the napsac 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 W. So we must consider weights of items as well as their value. Item # Weight Value 8 6 6
Knapsac problem There are two versions of the problem:. - napsac problem and. Fractional napsac problem. Items are indivisible; you either tae an item or not. Solved with dynamic programming. Items are divisible: you can tae any fraction of an item. Solved with a greedy algorithm. We have already seen this version - Knapsac problem Given a napsac with maximum capacity W, and a set S consisting of n items Each item i has some weight w i and benefit value b i (all w i, b i and W are integer values) Problem: How to pac the napsac to achieve maximum total value of paced items? 8 - Knapsac problem: a picture - Knapsac problem This is a napsac Max weight: W = W = Items Weight w i 9 Benefit value b i 8 Problem, in other words, is to find max i T b subject to i i T w W The problem is called a - problem, because each item must be entirely accepted or rejected. In the Fractional Knapsac Problem, we can tae fractions of items. i 9 - Knapsac problem: brute-force approach - Knapsac problem: brute-force approach Let s first solve this problem with a straightforward algorithm Since there are n items, there are n possible 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( n ) 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..n, then a subproblem would be to find an optimal solution for S = {items labeled,,.. }
Defining a Subproblem If items are labeled..n, then a subproblem would be to find an optimal solution for S = {items labeled,,.. } This is a reasonable subproblem definition. The question is: can we describe the final solution (S n ) in terms of subproblems (S )? Unfortunately, we can t do that. Defining a Subproblem w = b = w = b = w = b =8 w = Max weight: W = For S : Total weight: ; Maximum benefit: w = b = w = b = w = b =8 Weight Benefit Item # b = w i b i? w =9 b = For S : Total weight: Maximum benefit: 6 S S 9 8 Solution for S is not part of the solution for S!!! Defining a Subproblem (continued) Recursive Formula for subproblems As we have seen, the solution for S is not part of the solution for S 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,w] Recursive formula for subproblems:, w] =, w] if w > w max{, w],, w w ] + b } It means, that the best subset of S that has total weight w is: ) the best subset of S - that has total weight w, or ) the best subset of S - that has total weight w-w plus the item 6 Recursive Formula, w] =, w] if w > w max{, w],, w w ] + b } The best subset of S that has the total weight w, either contains item or not. First case: w >w. Item can t be part of the solution, since if it was, the total weight would be > w, which is unacceptable. Second case: w w. Then the item can be in the solution, and we choose the case with greater value. - Knapsac Algorithm for w = to W,w] = for i = to n i,] = for i = to n for w = to W if b i + i-,w-w i ] > i-,w] i,w] = b i + i-,w- w i ] i,w] = i-,w] i,w] = i-,w] // w i > w 8
Running time for w = to W,w] = for i = to n i,] = for i = to n for w = to W < the rest of the code > O(W) Repeat n times O(W) What is the running time of this algorithm? O(n*W) Remember that the brute-force algorithm taes O( n ) 9 Example Let s run our algorithm on the following data: n = (# of elements) W = (max weight) Elements (weight, benefit): (,), (,), (,), (,6) Example () Example () i\w i\w for w = to W,w] = for i = to n i,] = Example () i\w if b i + i-,w-w i ] > i-,w] i,w] = b i + i-,w- w i ] i,w] = i-,w] i,w] = i-,w] // w i > w : (,) : (,) : (,) i= : (,6) b i = w i = w= w-w i =- Example () i\w if b i + i-,w-w i ] > i-,w] i,w] = b i + i-,w- w i ] i,w] = i-,w] i,w] = i-,w] // w i > w : (,) : (,) : (,) i= : (,6) b i = w i = w= w-w i =
Example (6) i\w if b i + i-,w-w i ] > i-,w] i,w] = b i + i-,w- w i ] i,w] = i-,w] i,w] = i-,w] // w i > w : (,) : (,) : (,) i= : (,6) b i = w i = w= w-w i = Example () i\w if b i + i-,w-w i ] > i-,w] i,w] = b i + i-,w- w i ] i,w] = i-,w] i,w] = i-,w] // w i > w : (,) : (,) : (,) i= : (,6) b i = w i = w= w-w i = 6 Example (8) i\w if b i + i-,w-w i ] > i-,w] i,w] = b i + i-,w- w i ] i,w] = i-,w] i,w] = i-,w] // w i > w : (,) : (,) : (,) i= : (,6) b i = w i = w= w-w i = Example (9) i\w if b i + i-,w-w i ] > i-,w] i,w] = b i + i-,w- w i ] i,w] = i-,w] i,w] = i-,w] // w i > w : (,) : (,) : (,) i= : (,6) b i = w i = w= w-w i =- 8 Example () i\w if b i + i-,w-w i ] > i-,w] i,w] = b i + i-,w- w i ] i,w] = i-,w] i,w] = i-,w] // w i > w : (,) : (,) : (,) i= : (,6) b i = w i = w= w-w i =- Example () i\w if b i + i-,w-w i ] > i-,w] i,w] = b i + i-,w- w i ] i,w] = i-,w] i,w] = i-,w] // w i > w : (,) : (,) : (,) i= : (,6) b i = w i = w= w-w i = 9
Example () i\w if b i + i-,w-w i ] > i-,w] i,w] = b i + i-,w- w i ] i,w] = i-,w] i,w] = i-,w] // w i > w : (,) : (,) : (,) i= : (,6) b i = w i = w= w-w i = Example () i\w if b i + i-,w-w i ] > i-,w] i,w] = b i + i-,w- w i ] i,w] = i-,w] i,w] = i-,w] // w i > w : (,) : (,) : (,) i= : (,6) b i = w i = w= w-w i = Example () i\w : (,) : (,) : (,) i= : (,6) b i = w i = w=.. Example () i\w : (,) : (,) : (,) i= : (,6) b i = w i = w= w- w i = if b i + i-,w-w i ] > i-,w] i,w] = b i + i-,w- w i ] i,w] = i-,w] i,w] = i-,w] // w i > w if b i + i-,w-w i ] > i-,w] i,w] = b i + i-,w- w i ] i,w] = i-,w] i,w] = i-,w] // w i > w Example (6) i\w if b i + i-,w-w i ] > i-,w] i,w] = b i + i-,w- w i ] i,w] = i-,w] i,w] = i-,w] // w i > w : (,) : (,) : (,) i= : (,6) b i = w i = w= w- w i = Example () i\w if b i + i-,w-w i ] > i-,w] i,w] = b i + i-,w- w i ] i,w] = i-,w] i,w] = i-,w] // w i > w : (,) : (,) : (,) i= : (,6) b i =6 w i = w=.. 6 6
Example (8) i\w if b i + i-,w-w i ] > i-,w] i,w] = b i + i-,w- w i ] i,w] = i-,w] i,w] = i-,w] // w i > w : (,) : (,) : (,) i= : (,6) b i =6 w i = w= w- w i = Comments This algorithm only finds the max possible value that can be carried in the napsac» I.e., the value in n,w] To now the items that mae this maximum value, an addition to this algorithm is necessary. 8 How to find actual Knapsac Items All of the information we need is in the table. n,w] is the maximal value of items that can be placed in the Knapsac. Let i=n and =W if i,] i,] then mar the i th item as in the napsac i = i, = -w i i = i // Assume the i th item is not in the napsac // Could it be in the optimally paced napsac? 9 i\w i= : (,) : (,) : (,) : (,6) = b i =6 w i = i,] = i,] = Finding the Items while i, > if i,] i,] then mar the i th item as in the napsac i = i, = -w i i = i i\w i= : (,) : (,) : (,) : (,6) = b i =6 w i = i,] = i,] = Finding the Items () while i, > if i,] i,] then mar the i th item as in the napsac i = i, = -w i i = i i\w i= : (,) : (,) : (,) : (,6) = b i =6 w i = i,] = i,] = Finding the Items () while i, > if i,] i,] then mar the i th item as in the napsac i = i, = -w i i = i
Finding the Items () i\w i= while i, > if i,] i,] then mar the i th item as in the napsac i = i, = -w i i = i : (,) : (,) : (,) : (,6) = b i = w i = i,] = i,] = w i = Finding the Items () i\w i= while i, > if i,] i,] then mar the i th item as in the napsac i = i, = -w i i = i : (,) : (,) : (,) : (,6) = b i = w i = i,] = i,] = w i = Finding the Items (6) i\w while i, > if i,] i,] then mar the n th item as in the napsac i = i, = -w i i = i i= = : (,) : (,) : (,) : (,6) The optimal napsac should contain {, } Finding the Items () i\w while i, > if i,] i,] then mar the n th item as in the napsac i = i, = -w i i = i : (,) : (,) : (,) : (,6) The optimal napsac should contain {, } 6 Review: The Knapsac Problem And Optimal Substructure Solving The Knapsac Problem Both variations exhibit optimal substructure To show this for the - problem, consider the most valuable load weighing at most W pounds» If we remove item j from the load, what do we now about the remaining load?» A: remainder must be the most valuable load weighing at most W - w j that thief could tae, excluding item j The optimal solution to the fractional napsac problem can be found with a greedy algorithm» Do you recall how?» Greedy strategy: tae in order of dollars/pound The optimal solution to the - problem cannot be found with the same greedy strategy» Example: items weighing,, and pounds, napsac can hold pounds Suppose item is worth $. Assign values to the other items so that the greedy strategy will fail 8 8
The Knapsac Problem: Greedy Vs. Dynamic Memoization The fractional problem can be solved greedily The - problem can be solved with a dynamic programming approach Memoization is 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 looup With memoization, we implement the algorithm recursively:» If we encounter a subproblem we have seen, we loo up the answer» If not, compute the solution and add it to the list of subproblems we have seen. Must useful when the algorithm is easiest to implement recursively» Especially if we do not need solutions to all subproblems. 9 Conclusion Dynamic programming is a useful technique of solving certain ind 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 (memoization) Running time of dynamic programming algorithm vs. naïve algorithm:» - Knapsac problem: O(W*n) vs. O( n ) 9