This article is adapted from this pdf.
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
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.
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 available, 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). 这个问题有两个版本,如果每个item的数量不限, 最优的方案是取1个1和2个4,共48元; 如果每个item的数量只有一个,最优的方案是1和3各取一个,共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
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