Capacity alone cannot say which items have already been used. For the 0-1 knapsack problem, let use only the first items, each at most once. ThenThe two alternatives omit item or take it. In the latter case its remaining companions must solve the smaller problem using the first items; otherwise replacing them would improve the whole selection. Both branches reduce the item index, so taking item cannot take it again. This establishes the recurrence relation and optimal substructure.
Fill rows in increasing and return . There are entries with constant work each. Store the decisions for reconstruction; value-only computation uses space by keeping two rows, or by scanning capacities downwards in a single row. A forward scan in a single row would instead allow repetitions. Nonnegative integral weights suffice for this version, including zero-weight items, because the item index always decreases.
Articles by others on the same topic
There are currently no matching articles.