The 0/1 knapsack DP table animated cell by cell: skip-or-take decisions with the exact cells each value reads from, ending at the optimal bottom-right answer.
Tip: use samples, upload, copy, download, and send-to actions inside the workspace where available.
0/1 Knapsack Visualizer fills the dynamic-programming table one cell at a time, and for each cell it shows the two candidates it is choosing between: skip this item and inherit the value above, or take it and add its value to the best solution for the remaining capacity. The arrows to the exact cells being read are the part that makes DP click — the recurrence stops being a formula and becomes two specific cells you can point at.
Fractional knapsack is solvable greedily — see why 0/1 is not.
Open tool| 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | |
|---|---|---|---|---|---|---|---|---|---|
| ∅ | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
| #1 w1 $3 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
| #2 w2 $1 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
| #3 w4 $11 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
| #4 w2 $2 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
| #5 w4 $1 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
10/1 Knapsack, capacity 8: dp[i][c] = best value using the first i items within weight c. Items: #1(w=1, $3), #2(w=2, $1), #3(w=4, $11), #4(w=2, $2), #5(w=4, $1).
dp[0][*] = 0for each item i:for capacity c = 0..W:if w[i] > c: dp[i][c] = dp[i-1][c]else: dp[i][c] = max(skip, take)answer = dp[n][W]
Take-or-leave each item within a weight budget. The take/skip max is the template for dozens of DP problems.