DSADP0/1

0/1 Knapsack Visualizer

What is 0/1 Knapsack

Each item is taken once or not at all. A 1D table dp[cap] holds the best value for that capacity. Walking capacity downward after each item guarantees the item is not reused in the same pass.

  • Decision: skip the item, or take it if weight fits: dp[cap-w] + value.
  • Downward scan is the 0/1 trick. Upward would become unbounded.
  • 2^n subsets enumerate the same choices without sharing work — that will TLE.

Brute force approach

Each item is take-or-skip. 2^n subsets. Correct, but the hidden tests go to n = 200.

Each item is taken or skipped once. The state is (item index, capacity left). Brute force recomputes that pair. Memoization stores it.

Try a small input

At most 3 items, written as value weight pairs, and capacity up to 8. Default 3 2, 4 3, 5 4 with capacity 5 is worth 7. The practice case 60 10, 100 20, 120 30 with W = 50 is too wide for a full tree.

Answer: 0 Brute calls: 0 Memo calls: 0 Calls saved: 0
Recursive brute force
def knap(i, cap):
if i == n or cap == 0: return 0
return max(skip, take if weight fits)
Step 0

Memoized approach

dp[cap] = max(skip, take) walking capacity downward so an item is used at most once. Time O(nW).

Each state is solved once. A dashed node is a cache hit: the call returns immediately and that branch stops.

Memoized recursion
def knap(i, cap, memo):
if (i, cap) in memo: return memo
if i == n or cap == 0: return 0
memo = max(skip, take)
return memo
Step 0

How this visualizer works

Items (value, weight): (6,1), (10,2), (12,3), capacity 5. The table finishes at 22 (10+12). Step through the DP table. The live code on the right is the DP recurrence — brute force is shown in Implementations below so you can see why it TLE.

Interactive DP trace

dp_table.state

Input
DP table
Cell being filled
Source / window
Already computed

current_action

Load an input, then press Play or Step.

live_trace.py — DP fill

1def knapsack(items, W):
2dp = [0]*(W+1)
3for v, w in items:
4for cap in range(W, w-1, -1):
5dp[cap] = max(dp[cap], dp[cap-w] + v)
6return dp[W]

stdout — decision log

// each table write is recorded here
Speed Ready

Pseudocode & Complexity

dp_recurrence.txt

1def knapsack(items, W):
2dp = [0]*(W+1)
3for v, w in items:
4for cap in range(W, w-1, -1):
5dp[cap] = max(dp[cap], dp[cap-w] + v)
6return dp[W]

complexity.md

OperationTime
Each (item, cap)O(1)
Fill tableO(nW)
SpaceO(W)

Brute force explores the same subproblems many times and will TLE on the hidden tests. The DP codes below reuse each answer once.

Implementations

dp.py

1def knap_brute(items, W, i=0):
2if i == len(items):
3return 0
4value, weight = items[i]
5skip = knap_brute(items, W, i + 1)
6if weight > W:
7return skip
8take = value + knap_brute(items, W - weight, i + 1)
9return max(skip, take)
1def knap_dp(items, W):
2dp = [0] * (W + 1)
3for value, weight in items:
4for cap in range(W, weight - 1, -1):
5take = dp[cap - weight] + value
6if take > dp[cap]:
7dp[cap] = take
8return dp[W]
1int knap_brute(int *val, int *wt, int n, int W, int i) {
2int skip;
3int take;
4if (i == n) {
5return 0;
6}
7skip = knap_brute(val, wt, n, W, i + 1);
8if (wt[i] > W) {
9return skip;
10}
11take = val[i] + knap_brute(val, wt, n, W - wt[i], i + 1);
12return take > skip ? take : skip;
13}
1int knap_dp(int *val, int *wt, int n, int W) {
2int dp[10001];
3int i;
4int cap;
5int take;
6for (cap = 0; cap <= W; cap++) {
7dp[cap] = 0;
8}
9for (i = 0; i < n; i++) {
10for (cap = W; cap >= wt[i]; cap--) {
11take = dp[cap - wt[i]] + val[i];
12if (take > dp[cap]) {
13dp[cap] = take;
14}
15}
16}
17return dp[W];
18}