How unbounded knapsack reuses a capacity row

Short answer: Unbounded knapsack, and the classic coin-change combination count, let each item be used any number of times. The same capacity array as 0-1 knapsack is scanned upward. When the loop reaches t, dp[t−w] already includes the choices just made for this item, so the item may be taken again. Time is O(nW). The minimum number of coins is this recurrence with a minimum instead of a maximum. The number of combinations is the same loop shape with a sum.


What the problem actually asks

0-1 forbids reuse by reading a cell that has not yet seen the current item. If the item may be repeated, the cell just written is exactly the state the recurrence wants: one more copy of the item on top of a legal packing of the residual capacity. Scanning from low capacity to high capacity exposes those cells in the right order.

Coin change for the minimum number of coins is the same loop with dp[t] = min(dp[t], dp[t−w] + 1) and an infinity initialization, except dp[0] = 0. Counting combinations, where order does not matter, uses the item loop outside and the capacity loop inside. Counting permutations, where order matters, swaps those loops. The swap is the difference between the two counts.


What the algorithm guarantees

After the upward passes, dp[W] is the optimum value, the minimum coin count, or the combination count, according to the combine. Time is Θ(nW). Memory is Θ(W). Reconstruction of one multiset of items needs predecessors. The algorithm requires nonnegative integer weights. A zero-weight item with positive value makes the maximum unbounded. A zero-weight coin makes the coin loop skip or spin.

The combination count modulo a prime is the usual programming variant and does not change the loop order. The permutation count, which treats sequences of coins as distinct when their order differs, is a different DP and a frequent mix-up.


The working steps

  1. Initialize dp[0] to 0 for a count or a minimum, and to the appropriate identity for a maximum value. Set other minimum-coins cells to infinity.
  2. For each item or coin of weight w and value v, for t from w to W, update dp[t] from dp[t−w]. Use max for values, min for coin counts, and addition for combination counts.
  3. The upward direction is required. A downward direction would solve 0-1.
  4. To count permutations instead of combinations, loop over capacity on the outside and items on the inside, so each position in the sequence can be any coin.
  5. Reject a zero weight unless the problem statement has a separate rule for it. It does not reduce capacity.

Reuse is the upward scan. One-time use is the downward scan. Fractional cutting is the density greedy algorithm. The O(nW) bound is pseudo-polynomial in the same sense as 0-1 knapsack. A coin system that is canonical, such as standard denominations, sometimes admits a greedy choice. Arbitrary denominations do not, and the DP is the general solution.


Approaches that look simpler and fail

Approach Why it fails when the loops are swapped without changing the meaning
Scan downward and call the result unbounded The downward scan hides the current item from itself. Each item is used at most once, which is 0-1 knapsack.
Count combinations with the capacity loop outside That order counts ordered sequences, the permutation count. Combinations keep one item’s loop wrapped around the whole capacity scan.
Use a greedy largest-coin rule for arbitrary denominations Some denomination sets make the greedy choice use more coins than the optimum. The DP tries the recurrence, not the largest coin.
Leave dp[0] at infinity in the minimum-coin problem Zero coins make zero value. Every other cell is built from that base. An infinite base makes every cell infinite.

At most one copy of each item is 0-1 knapsack. Cuttable goods are fractional knapsack. Unbounded knapsack and coin change are the upward capacity DP, with the loop order choosing combinations or permutations.


A working example

The Bash script and the Python script count unbounded combinations. The item loop is outside and the capacity loop walks upward, so each coin may be reused and order does not matter. Coins 1, 2, 3 make 4 in 4 ways.

coins=(1 2 3)
amount=4
for ((x=0; x<=amount; x++)); do dp[x]=0; done
dp[0]=1
for coin in "${coins[@]}"; do
  for ((x=coin; x<=amount; x++)); do
    dp[x]=$((dp[x] + dp[x-coin]))
  done
done
echo "${dp[amount]}"
coins = [1, 2, 3]
amount = 4
dp = [0] * (amount + 1)
dp[0] = 1
for coin in coins:
    for x in range(coin, amount + 1):
        dp[x] += dp[x - coin]
assert dp[amount] == 4
print(dp[amount])

Where it is used

The unbounded knapsack recurrence is coin change: unlimited copies of each coin, and a capacity row that may reuse a coin in the same pass. A vending machine or a postage problem with unlimited stamps is that row. The 0-1 knapsack takes each item at most once.


When you can drop this algorithm

Drop the upward DP when each item may be selected only once. Scan downward. Keep the upward scan when repetition is legal and the capacity is an integer index that fits in memory.


Why this stays published

We publish this so a search for unbounded knapsack returns the upward scan and the loop order that separates coin combinations from coin permutations. Unbounded knapsack and coin-change combinations scan the capacity array upward, so an item may be reused, in O(nW) pseudo-polynomial time.

本文由 HoHo 與 AI 協作整理,最後更新於 2026 年 11 月 13 日。

Leave a Reply

Your email address will not be published. Required fields are marked *