DSADPUnbounded

Rod Cutting Visualizer

What is Rod Cutting

A rod of length n may be cut into integer pieces. price[i] is the price of a piece of length i+1. The best profit for length L tries every first cut i and adds the already-solved remainder dp[L-i].

  • Unbounded knapsack with weight = length, value = price.
  • First cut of size i plus an optimal rest is enough — the rest is itself optimally cut.
  • n=8 classic: prices 1,5,8,9,10,17,17,20 answer 22 (2+6).

Brute force approach

Try every first-cut length i and recurse on n-i. The same remainder is recut from many parents — exponential.

A rod of length L tries every first-cut length, then solves the leftover length. The same leftover is priced many times. Memoization stores one best price per length.

Try a small input

At most 4 prices, for lengths 1, 2, 3, and 4. Prices 1 5 8 9 give 10.

Answer: 0 Brute calls: 0 Memo calls: 0 Calls saved: 0
Recursive brute force
def cut(len):
if len == 0: return 0
return max price[i-1] + cut(len-i)
Step 0

Memoized approach

dp[L] = max(price[i-1] + dp[L-i]). Each length is locked once. Time O(n^2).

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

Memoized recursion
def cut(len, memo):
if len in memo: return memo[len]
if len == 0: return 0
memo[len] = best first cut
return memo[len]
Step 0

How this visualizer works

Prices 1, 5, 8, 9 (n=4). Watch every first-cut for each length. The best for 4 is 10 (two 2s). 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 cutRod(price):
2n, dp = len(price), [0]*(len(price)+1)
3for L in range(1, n+1):
4best = 0
5for i in range(1, L+1):
6best = max(best, price[i-1] + dp[L-i])
7dp[L] = best
8return dp[n]

stdout — decision log

// each table write is recorded here
Speed Ready

Pseudocode & Complexity

dp_recurrence.txt

1def cutRod(price):
2n, dp = len(price), [0]*(len(price)+1)
3for L in range(1, n+1):
4best = 0
5for i in range(1, L+1):
6best = max(best, price[i-1] + dp[L-i])
7dp[L] = best
8return dp[n]

complexity.md

OperationTime
Each (L, cut)O(1)
All lengthsO(n²)
SpaceO(n)

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 cut_brute(price, n):
2if n == 0:
3return 0
4best = 0
5for i in range(1, n + 1):
6take = price[i - 1] + cut_brute(price, n - i)
7if take > best:
8best = take
9return best
1def cut_dp(price):
2n = len(price)
3dp = [0] * (n + 1)
4for L in range(1, n + 1):
5best = 0
6for i in range(1, L + 1):
7take = price[i - 1] + dp[L - i]
8if take > best:
9best = take
10dp[L] = best
11return dp[n]
1int cut_brute(int *price, int n) {
2int best = 0;
3int i;
4int take;
5if (n == 0) {
6return 0;
7}
8for (i = 1; i <= n; i++) {
9take = price[i - 1] + cut_brute(price, n - i);
10if (take > best) {
11best = take;
12}
13}
14return best;
15}
1int cut_dp(int *price, int n) {
2int dp[2001];
3int L;
4int i;
5int best;
6int take;
7dp[0] = 0;
8for (L = 1; L <= n; L++) {
9best = 0;
10for (i = 1; i <= L; i++) {
11take = price[i - 1] + dp[L - i];
12if (take > best) {
13best = take;
14}
15}
16dp[L] = best;
17}
18return dp[n];
19}