Download presentation
Presentation is loading. Please wait.
1
The Knapsack Problem Input –Capacity K –n items with weights w i and values v i Goal –Output a set of items S such that the sum of weights of items in S is at most K the sum of values of items in S is maximized
2
Variations Fractional –You may take a fraction of the entire item All or nothing –You must take all of an item or none of it Special cases of All or nothing –All items have same weight (or same value) –All items have same density (value/weight) For which of the above variations does a greedy strategy work? For which of the above variations does a greedy strategy NOT work?
3
Dynamic Programming Consider the all or nothing version Assume that all weights/values are reasonably sized integers We can use dynamic programming to solve this problem (even though there is no explicit ordering among the items)
4
Definining subproblems Define P(i,w) to be the problem of choosing a set of objects from the first i objects that maximizes value subject to weight constraint of w. –Impose an arbitrary ordering on the items V(i,w) is the value of this set of items Original problem corresponds to V(n, K)
5
Recurrence Relation/Running Time V(i,w) = max (V(i-1,w-w i ) + v i, V(i-1, w)) –A maximal solution for P(i,w) either uses item i (first term in max) or does NOT use item i (second term in max) V(0,w) = 0 (no items to choose from) V(i,0) = 0 (no weight allowed) What is the running time of this solution? –Number of table entries: –Time to fill each entry:
6
Example w A = 2 v A = $40 w B = 3 v B = $50 w C = 1 v C = $100 w D = 5 v D = $95 w E = 3 v E = $30 Weight Items
7
Define Subproblems Based on Value Define P(i,w) to be the problem of choosing a set of objects from the first i objects that maximizes value subject to weight constraint of w. V(i,w) is the value of this set of items Original problem corresponds to V(n, W) Define P(i,v) to be …
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.