Definition · The 0/1 knapsack problemGiven n items with integer weights and values and an integer capacity W, choose a subset whose total weight is at most W and whose total value is as large as possible. Each item is taken whole or not at all — that is the 0/1 — and it is the whole difference from the fractional problem.
Greedy Algorithms and Matroids solves the fractional version by value density: sort by value over weight, take greedily, and cut the last item to fit. The exchange argument works because the last item can be cut. Remove that and the argument has nothing to exchange with, and the rule stops being optimal.
Example · Four items, capacity seven, and the rule that gets 8The items are 1/1, 3/4, 4/5 and 5/7, written weight over value, with densities 1, 1.33, 1.25 and 1.4. Densest first takes 5/7, leaving room for 2, which admits only 1/1: total weight 6, value 8. Heaviest first takes the same two. The table reports 9, taking 3/4 and 4/5 for a total weight of exactly 7, and the panel re-weighs that pair and confirms both numbers.
Theorem · The knapsack recurrenceLet V(i, w) be the greatest value obtainable from the first i items with capacity w, with V(0, w) = 0 for every w. Then for i ≥ 1, V(i, w) = V(i−1, w) when weight(i) > w, and otherwise V(i, w) = max( V(i−1, w), value(i) + V(i−1, w − weight(i)) ).
Consider an optimal subset S of the first i items within capacity w. Either item i is in S or it is not. If it is not, S is a subset of the first i−1 items within capacity w, and it must be an optimal one, so its value is V(i−1, w).
If item i is in S, then S without it is a subset of the first i−1 items within capacity w − weight(i), and again it must be optimal there — a better subset could be substituted in, keeping item i, and would beat S. So its value is value(i) + V(i−1, w − weight(i)). The optimum is the larger of the two cases, which is the recurrence.
□
What the painting shows, and why it cannot be wrong
Move the two range controls and the lab paints the chosen cell in one colour and the cells it read in another. For the cell at row 3, column 7 those are (2, 7) and (2, 3) — skip the third item, or take it and look back four columns. That list is not a description of the recurrence written out beside it. It is collected by the reads the recurrence itself performed while the cell was being computed, so it records what the code did rather than what its author believed.
Every cell here reads exactly two, which is what makes the grid Θ(nW) work rather than Θ(nW) cells times something. Compare with the chain table, where a cell reads two per split point and the total is Θ(n³); the number of reads per cell is a property of the recurrence and is worth reading off the painting rather than assumed.
Pseudo-polynomial, and why the bound is not what it looks like
Definition · Pseudo-polynomialAn algorithm is pseudo-polynomial when its running time is polynomial in the numeric value of the input rather than in the length of the input's encoding. The knapsack table is Θ(nW), which is polynomial in W; but W written down takes about log W digits, so the table is exponential in the input's size.
This matters and it is easy to miss, because Θ(nW) reads like a polynomial bound and behaves like one on the instances a lab can show you. Forty cells for four items at a capacity of seven; five hundred for the same four items at a capacity of ninety-nine; five hundred thousand at a capacity of ninety-nine thousand nine hundred and ninety-nine. Three more digits in the input, a thousand times the work. Intractability and Approximation returns to this as the definition of weak NP-hardness, and the table on this page is the reason that distinction exists.
The measured figures here are 40 cells and 51 reads on four items at capacity seven, with every one of the 16 subsets enumerated as a check. The proved statement is Θ(nW) cells with O(1) work each, against Θ(2ⁿ) for the enumeration. On this instance the enumeration walks 16 subsets where the table fills 40 cells, so the exponential route is again ahead — and it stays ahead until the item count and the capacity are both large enough for nW to fall below 2ⁿ, which on the lab's own presets never happens.