Dynamic Programming21 sections · 916 units
Open in Course

Unbounded Knapsack - Walkthrough

Unlimited items trace

Trace unbounded Knapsack with items [(w=2,v=3),(w=3,v=5)][(w=2, v=3), (w=3, v=5)] and capacity W=7W=7. Unlike 0/1 Knapsack, we can reuse items. The DP is dp[w]dp[w] = max value with capacity ww. Process each capacity, trying all items. dp[2]=3dp[2]=3 (one of item 1). dp[3]=5dp[3]=5 (one of item 2). dp[4]=6dp[4]=6 (two of item 1). dp[5]=8dp[5]=8 (item 1 + item 2). dp[6]=10dp[6]=10 (two of item 2). dp[7]=11dp[7]=11 (two of item 1 + one of item 2).

Answer: dp[7]=11dp[7]=11. The key difference from 0/1: each item can contribute multiple times, so we don't track which items were used, just the capacity.