Example: bachelor of science

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).

3 13 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 reasonable subproblem definition.

Tags:

  Solutions

Information

Domain:

Source:

Link to this page:

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

Other abuse

Advertisement

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).

2 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.

3 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.

4 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?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.

5 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.}

6 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!

7 !!?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.

8 Recursive Formula for subproblems17 The best subset of Skthat has the total weight w,either contains item kor not. 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?

9 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.

10 (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.


Related search queries