Fill a knapsack table cell by cell, watch fib’s memo erase repeat work, and learn why the 1-D loop must run backwards.
The big picture
The big picture
Stop re-solving the same subproblems: cache each answer once, and exponential call trees collapse into neat tables.
1
One idea, two styles
Memoization caches on the way down (top-down); tabulation fills a table on the way up (bottom-up). Same answers, opposite directions.
2
Overlapping subproblems
Naive fib(6) makes 25 calls and recomputes fib(3) twice; the memoized version answers every subproblem exactly once.
3
The knapsack question
Every cell asks one thing: is item i worth more skipped, or taken on top of the best load that fit before it?
4
The recurrence
dp[i][w] = max(dp[i-1][w], dp[i-1][w - wt[i]] + val[i]) — skip versus take, written in a single line.
5
The price tag
n items × W capacities = O(n × W) cells — pseudo-polynomial, and vastly cheaper than enumerating 2^n subsets.
6
The 1-D squeeze
One rolling row can replace the whole table — provided the capacity loop runs backwards.
The big picture
One idea, two styles1Memoization caches on the way down (top-down); tabulation fills a table on the way up (bottom-up). Same answers, opposite directions.
Overlapping subproblems2Naive fib(6) makes 25 calls and recomputes fib(3) twice; the memoized version answers every subproblem exactly once.
The knapsack question3Every cell asks one thing: is item i worth more skipped, or taken on top of the best load that fit before it?
The recurrence4dp[i][w] = max(dp[i-1][w], dp[i-1][w - wt[i]] + val[i]) — skip versus take, written in a single line.
The price tag5n items × W capacities = O(n × W) cells — pseudo-polynomial, and vastly cheaper than enumerating 2^n subsets.
The 1-D squeeze6One rolling row can replace the whole table — provided the capacity loop runs backwards.
6 building blocks make up The big picture
Algorithm in motion
Knapsack table live
Three items, one 5-unit knapsack — each cell asks: skip the item, or take it on top of the best smaller load?
cap 0cap 1cap 2cap 3cap 4cap 5
A (w2 v3)
B (w3 v4)
C (w4 v5)
1 / 13
A 0/1 knapsack table: three cargo items down the side, capacity 0..5 across the top. Empty cells are answers the loop has not reached yet.
Visualize
One cell’s decision
Every cell of the table runs this same decision — does it fit, and is taking it better than skipping?
process decision input / output start / end
Watch it run
Memo vs table
Top-down memo and bottom-up tabulation — the same two problems, solved in opposite directions.
fib_memo.py
1memo ={}
2
3deffib(n):
4if n in memo:
5return memo[n]
6if n <=1:
7return n
8 memo[n]=fib(n -1)+fib(n -2)
9return memo[n]
10
11print(fib(6))
12print(len(memo))
module
memo starts as an empty dict — the cache that will remember every fib answer we pay for once.
Memory
memo= {}
Console
output appears as the program runs…
Step 1/15
Algorithm in motion
Why the loop reverses
Same table, one rolling row — loop capacity the wrong way and item A rides twice.
0
0
cap
0
1
0
2
0
3
0
4
0
5
1 / 13
The whole knapsack table squeezed into one row: dp holds capacities 0..5, all zeros. Item A (weight 2, value 3) is up first — attempted the obvious way, looping capacity upward.
Challenge
Predict the output
A seeded cache, two calls, two prints — count the keys carefully.
predict.py
1cache ={0:0,1:1}
2
3deffib(n):
4if n in cache:
5return cache[n]
6 cache[n]=fib(n -1)+fib(n -2)
7return cache[n]
8
9fib(4)
10fib(6)
11print(len(cache))
12print(cache[5])
Your predicted output — one line per console line
Hint: The two base cases were seeded before any call and they count toward len(cache). fib(4) adds keys 2, 3, 4; fib(6) only needs 5 and 6.
Practice
Concept check
Click a term, then click its meaning to lock the pair.
8 conceptsTap a card to expand
?memoizationdrops into place
?tabulationdrops into place
?overlapping subproblemsdrops into place
?dp[i][w]drops into place
?loop cap downdrops into place
?base casedrops into place
?cache hitdrops into place
?O(n × W)drops into place
Recap
Key takeaways
Key takeaways6 takeaways
01
DP= overlapping subproblems+ optimal substructure: solve each subproblem once, store it, reuse it.
02
Memoization is recursion plus a dict; tabulation is a table filled from the base cases up.