DrawCode Algorithm Visualizer • Dynamic Programming • Medium

0/1 Knapsack

Tags: DP, 2D, Knapsack, Optimization

1. One-Liner

0/1 Knapsack builds a table dp[i][w]: the best value using the first i items with weight limit w, choosing each item once or not at all.


2. The Problem It Solves

Given weights w_i, values v_i, and capacity W, pick a subset with total weight ≤ W and maximum total value—each item can be taken 0 or 1 times (not fractional, not unlimited).


3. The Core Idea

For each item, either skip it (inherit best without it) or take it (add its value to the best at remaining capacity). The recurrence stitches smaller capacity and prefix decisions into the global optimum.


4. How It Works (Step-by-Step)

StepWhat Happens
1. Initdp[0][w]=0 for all w
2. ItemsFor i from 1 to n, for w from 0 to W
3. Skipdp[i][w] = dp[i-1][w]
4. TakeIf w_i ≤ w, also consider v_i + dp[i-1][w-w_i]
5. Resultdp[n][W]

5. Dry Run Example

Items (w,v): (2,3), (1,2), W=3. Build row by row; best is 5 (take both).


6. Key Properties

PropertyValue
StatesPrefix i, capacity w
TimeO(n·W) pseudo-polynomial
SpaceO(W) with 1D rolling

7. Where It Is Used

DomainUse
Resource allocationBudget-limited project selection
LogisticsLoading with discrete choices

8. Interview Tips

Discuss space optimization, subset reconstruction, and contrast with unbounded and fractional knapsack.


9. Comparison with Other Variants

VariantItemsTypical approach
0/1At most once2D or 1D DP
UnboundedUnlimitedInner loop forward on w
FractionalSplit itemsGreedy by value/weight

10. Complexity

MetricValue
TimeO(n·W)
SpaceO(n·W) or O(W) optimized

Implementation Example (PYTHON)

def knapsack_01(weights, values, W: int) -> int:
    n = len(weights)
    dp = [0] * (W + 1)
    for i in range(n):
        w, v = weights[i], values[i]
        for cap in range(W, w - 1, -1):
            dp[cap] = max(dp[cap], v + dp[cap - w])
    return dp[W]

Interactive Visualizer Workspace

Explore step-by-step interactive animations, memory state tracking, and live multi-language execution in DrawCode.

Launch Interactive Visualizer