Unbounded Knapsack
Fill capacities while allowing each item to be chosen repeatedly, exposing the loop-order difference from 0/1 Knapsack.
Core idea
Each item type may be chosen repeatedly. When a state takes the current item, its remainder stays on the same row instead of moving to the previous-item row.
Read the visualization
Every matrix cell compares skipping the item with taking one copy plus the best value at the smaller capacity. The highlighted dependency reveals why reuse is allowed.
| 0 | 1 | 2 | 3 | 4 | 5 | 6 | |
|---|---|---|---|---|---|---|---|
| ∅ | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
| A | 0 | ||||||
| B | 0 | ||||||
| C | 0 |
Initialize zero value for every capacity before considering any item type.
Complexity and tradeoffs
Time: O(nW). Space: O(nW). A one-dimensional table uses O(W) space when capacities iterate upward for reusable items.
Where it fits
Unbounded Knapsack models repeatable purchases, cutting, and resource composition. Reverse the one-dimensional capacity loop to recover the at-most-once 0/1 variant.