Algorithms_Part10 - S. Dasgupta, C.H. Papadimitriou, and...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: S. Dasgupta, C.H. Papadimitriou, and U.V. Vazirani 181 6.4 Knapsack During a robbery, a burglar finds much more loot than he had expected and has to decide what to take. His bag (or “knapsack”) will hold a total weight of at most W pounds. There are n items to pick from, of weight w 1 , . . . , w n and dollar value v 1 , . . . , v n . What’s the most valuable combination of items he can fit into his bag? 1 For instance, take W = 10 and Item Weight Value 1 6 $30 2 3 $14 3 4 $16 4 2 $9 There are two versions of this problem. If there are unlimited quantities of each item avail- able, the optimal choice is to pick item 1 and two of item 4 (total: $48). On the other hand, if there is one of each item (the burglar has broken into an art gallery, say), then the optimal knapsack contains items 1 and 3 (total: $46). As we shall see in Chapter 8, neither version of this problem is likely to have a polynomial- time algorithm. However, using dynamic programming they can both be solved in O ( nW ) time, which is reasonable when W is small, but is not polynomial since the input size is proportional to log W rather than W . Knapsack with repetition Let’s start with the version that allows repetition. As always, the main question in dynamic programming is, what are the subproblems? In this case we can shrink the original problem in two ways: we can either look at smaller knapsack capacities w ≤ W , or we can look at fewer items (for instance, items 1 , 2 , . . . , j , for j ≤ n ). It usually takes a little experimentation to figure out exactly what works. The first restriction calls for smaller capacities. Accordingly, define K ( w ) = maximum value achievable with a knapsack of capacity w. Can we express this in terms of smaller subproblems? Well, if the optimal solution to K ( w ) includes item i , then removing this item from the knapsack leaves an optimal solution to K ( w- w i ) . In other words, K ( w ) is simply K ( w- w i ) + v i , for some i . We don’t know which i , so we need to try all possibilities. K ( w ) = max i : w i ≤ w { K ( w- w i ) + v i } , where as usual our convention is that the maximum over an empty set is . We’re done! The algorithm now writes itself, and it is characteristically simple and elegant. 1 If this application seems frivolous, replace “weight” with “CPU time” and “only W pounds can be taken” with “only W units of CPU time are available.” Or use “bandwidth” in place of “CPU time,” etc. The knapsack problem generalizes a wide variety of resource-constrained selection tasks. 182 Algorithms K (0) = 0 for w = 1 to W : K ( w ) = max { K ( w- w i ) + v i : w i ≤ w } return K ( W ) This algorithm fills in a one-dimensional table of length W + 1 , in left-to-right order. Each entry can take up to O ( n ) time to compute, so the overall running time is O ( nW ) ....
View Full Document

This document was uploaded on 08/10/2011.

Page1 / 20

Algorithms_Part10 - S. Dasgupta, C.H. Papadimitriou, and...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online