Look again at the recurrence. V(i, w) is the larger of V(i−1, w) and value(i) + V(i−1, w − weight(i)). Both terms are in row i−1. Row i−2 is never mentioned, and once row i is complete row i−1 is never read again. A table whose rows are used once and discarded does not need to be stored.
Definition · The one-row formKeep an array row[0..W], initialised to zero. For each item in turn, update row[w] = max(row[w], row[w − weight(i)] + value(i)) for every w from weight(i) to W. The order in which those w are visited is not specified by the recurrence and is the subject of this lesson.
Theorem · Backward gives 0/1; forward gives unboundedIf the inner loop runs w from W downward, the array after item i equals row i of the 0/1 grid. If it runs w upward, the array after item i holds the best value obtainable from the first i items with each usable any number of times.
Downward: when w is updated, the cell at w − weight(i) is strictly to the left and, because the loop is descending, has not yet been visited in this pass. It therefore still holds the value it had before item i was considered, which by induction is V(i−1, w − weight(i)). So the update computes exactly max(V(i−1, w), value(i) + V(i−1, w − weight(i))), which is the grid's recurrence, and the array after the pass is row i.
Upward: the cell at w − weight(i) has already been visited this pass, so it holds the best value using items 1..i with item i allowed. Adding one more copy of item i therefore extends a solution that may already contain it, and by induction on w the array holds the unbounded optimum over the first i items. Both statements are exact; neither direction is an approximation of the other.
□
Example · Three items, capacity eleven, and the two rows in fullThe items are 2/3, 3/5 and 5/9. Swept backward the array ends 0 0 3 5 5 9 9 12 14 14 17 17, so the answer is 17, which is all three items at a total weight of 10. Swept forward it ends 0 0 3 5 6 9 10 12 14 15 18 19, so the answer is 19, which is two copies of 3/5 and one of 5/9 at a total weight of exactly 11. The grid, filled in full, also says 17, and every one of the eight subsets says 17.
Watching the first item make the difference
The clearest place to see it is after the first pass. Backward, item 2/3 leaves the array 0 0 3 3 3 3 3 3 3 3 3 3: one copy of a weight-2 item is all that any capacity can hold, because the item appears once. Forward, the same item leaves 0 0 3 3 6 6 9 9 12 12 15 15: at capacity 4 it has been taken twice, at capacity 6 three times, and the pass built each of those on the value it had just written two columns earlier.
What the collapse costs
The reconstruction. The grid records, for each cell, which cell it came from, and the walk back through those pointers is what produced the item set on the previous page. One row has nowhere to keep them: by the time the answer is known, every intermediate value has been overwritten. So the one-row form answers what the optimum is and not what it consists of, and the panel's one-row figures sit beside the grid's rather than replacing them.
That is the trade in one sentence: Θ(W) space instead of Θ(nW), at the cost of the object. Recovering the object from one row means either keeping the rows after all, or running the whole thing again with a divide-and-conquer trick that this course does not develop. Knowing which of the two questions you are answering is the point.
The measured figures are 17 and 19 on three items at capacity 11, with the grid and the enumeration of all eight subsets both confirming 17. The proved statement is that time is Θ(nW) in both forms and space falls from Θ(nW) to Θ(W) — 48 cells against 12 numbers here, which at this size is a saving of thirty-six words and no wall-clock time at all. The space bound is what makes the collapse worth knowing; this instance is far too small to show it, and saying so is more useful than a ratio measured on twelve numbers.