DSADPUnbounded

Unbounded Knapsack Visualizer

What is Unbounded Knapsack

Same weights and values as 0/1, but each item may be taken any number of times. Walking capacity upward lets a just-written dp[cap-w] already include extra copies of the current item.

  • Upward scan is the reuse trick.
  • Rod cutting is this problem with weight = length and value = price.
  • Complete knapsack is another name for the same recurrence.

Brute force approach

For item i try 0, 1, 2, … copies that still fit, then move on. Branching is huge.

An item may be taken again, so the take branch stays on the same index with a smaller capacity. Capacity repeats across branches. Memoization stores (index, capacity).

Try a small input

At most 2 items and capacity up to 8. Two copies of value 10 weight 2 fill capacity 4 with value 20.

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 and stay on i)
Step 0

Memoized approach

Walk capacity upward: a write to dp[cap-w] may already include extra copies of the same item.

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 done: return 0
memo = max(skip, take again)
return memo
Step 0

How this visualizer works

Items (10,2) and (12,3), capacity 8. Four copies of the first item give 40. 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 unbounded(items, W):
2dp = [0]*(W+1)
3for cap in range(1, W+1):
4for v, w in items:
5if w <= cap: dp[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 unbounded(items, W):
2dp = [0]*(W+1)
3for cap in range(1, W+1):
4for v, w in items:
5if w <= cap: dp[cap] = max(dp[cap], dp[cap-w]+v)
6return dp[W]

complexity.md

OperationTime
Each (cap, item)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 unbounded_brute(items, W, i=0):
2if i == len(items) or W <= 0:
3return 0
4value, weight = items[i]
5best = unbounded_brute(items, W, i + 1)
6copies = 1
7while copies * weight <= W:
8take = copies * value + unbounded_brute(
9items, W - copies * weight, i + 1
10)
11if take > best:
12best = take
13copies += 1
14return best
1def unbounded_dp(items, W):
2dp = [0] * (W + 1)
3for cap in range(1, W + 1):
4for value, weight in items:
5if weight <= cap:
6take = dp[cap - weight] + value
7if take > dp[cap]:
8dp[cap] = take
9return dp[W]
1int unbounded_brute(int *val, int *wt, int n, int W, int i) {
2int best;
3int copies;
4int take;
5if (i == n || W <= 0) {
6return 0;
7}
8best = unbounded_brute(val, wt, n, W, i + 1);
9copies = 1;
10while (copies * wt[i] <= W) {
11take = copies * val[i]
12+ unbounded_brute(val, wt, n, W - copies * wt[i], i + 1);
13if (take > best) {
14best = take;
15}
16copies += 1;
17}
18return best;
19}
1int unbounded_dp(int *val, int *wt, int n, int W) {
2int dp[10001];
3int cap;
4int i;
5int take;
6for (cap = 0; cap <= W; cap++) {
7dp[cap] = 0;
8}
9for (cap = 1; cap <= W; cap++) {
10for (i = 0; i < n; i++) {
11if (wt[i] <= cap) {
12take = dp[cap - wt[i]] + val[i];
13if (take > dp[cap]) {
14dp[cap] = take;
15}
16}
17}
18}
19return dp[W];
20}