Presentation is loading. Please wait.

Presentation is loading. Please wait.

The Knapsack Problem.

Similar presentations


Presentation on theme: "The Knapsack Problem."— Presentation transcript:

1 The Knapsack Problem

2 The Knapsack Problem Knapsack of capacity M
N Types of Indivisible Items (unlimited number of each type) V1 V2 V3 VN Problem: What is the selection of items that fits in the knapsack maximizing the total value of its contents? CSCI Data Structures

3 The Knapsack Problem Type A Type B Type C Value 100 76 54 Weight 5 4 3
Note: For each node in this tree, we have a set of possible decisions. Each decision has a cost (its weight) and leads to an associated yield. The goal is to find a sequence of decisions that leads to an optimal solution. The number of possible solutions is exponential with M. We’d have to find them all and then choose the very best. 8 5 3 4 [100] 3 [76] 4 [54] 5 5 3 3 4 3 4 1 2 1 [154] [130] [152] [108] [130] [154] CSCI Data Structures

4 The Knapsack Problem The recursive nature of the problem jumps out at us when we observe the decision tree. The problem has optimal substructure and overlapping sub-problems, so it is solvable with dynamic programming. What we have to figure out is how to map the problem onto some kind of data structure to store solutions to each sub-problem as the tree is traversed. CSCI Data Structures

5 The Knapsack Problem N=3 M=27 27 22 23 24 17 12 7 2 100 76 54 5 4 3
Type A Type B Type C Value 100 76 54 Weight 5 4 3 [0] 27 5 3 4 [100] 22 [76] 23 [54] 24 5 [200] 17 5 Question: How many possibilities have to be tested if you use a brute-force approach? Question: Can you find overlapping sub-problems? Question: Can you see the problem’s optimal substructure? Question: Can you see what the optimal gain is for N=3, M=27? Question: What is the strategy that you follow to get to the optimal solution? [300] 12 5 [400] 7 5 [500] 2 Question: What kind of data structure is needed to apply DP to this problem? CSCI Data Structures

6 The Knapsack Problem (recursive solution)
knap(M) max = 0; for i = 1 to N // Loop through item types // Solve problem assuming we include // an item of type i do spaceLeft = M – size[i] if spaceLeft >= 0 // if type i fits then // Compute candidate sol’n t t = knap(spaceLeft)+val[i] if t > max then max = t return max; CSCI Data Structures

7 The Knapsack Problem (DP solution)
knap(M) if maxKnown[M]!= unknown then return maxKnown[M]; // Otherwise, result not yet known: max = 0 for i = 1 to N // Try each item type do spaceLeft = M – size[i] if spaceLeft >= 0 // If item type i fits then // Compute candidate solution t t = knap(spaceLeft) + val[i] if t > max then max = t; maxi = i; maxKnown[M] = max // memoize result return max CSCI Data Structures


Download ppt "The Knapsack Problem."

Similar presentations


Ads by Google