Recursion and Dynamic Programming
Fibonacci with Memoization
Compute fib(n) recursively and cache each fib(k) in a memo map so each
subproblem is solved at most once. Turns exponential recursion into a
linear walk, the canonical introduction to dynamic programming.
Algorithm
Canonical input n = 6 produces fib(6) = 8. The replay shows the
descent for fib(6) -> fib(5) -> ... -> fib(0) then four cache hits on
the unwind (one per repeat-call subproblem).
memoization
Reuse cached subproblem results instead of re-descending.
Basic Implementation
basic.py
Replay: real traced execution (multi-file project)
def fib(n, memo):
if n in memo:
return memo[n]
if n < 2:
memo[n] = n
return n
value = fib(n - 1, memo) + fib(n - 2, memo)
memo[n] = value
return value
memo = {}
result = fib(6, memo)
print(result)
print(memo)
memo ← {}, action ← miss -> descend fib(5)
6 return n7value = fib(n - 1, memo) + fib(n - 2, memo)8memo[n] = valuevalues this step{}memomiss -> descend fib(5)action6nmemo ← {}, action ← miss -> descend fib(4)
6 return n7value = fib(n - 1, memo) + fib(n - 2, memo)8memo[n] = valuevalues this step{}memomiss -> descend fib(4)action5nmemo ← {}, action ← miss -> descend fib(3)
6 return n7value = fib(n - 1, memo) + fib(n - 2, memo)8memo[n] = valuevalues this step{}memomiss -> descend fib(3)action4nmemo ← {}, action ← miss -> descend fib(2)
6 return n7value = fib(n - 1, memo) + fib(n - 2, memo)8memo[n] = valuevalues this step{}memomiss -> descend fib(2)action3nmemo ← {}, action ← miss -> descend fib(1)
6 return n7value = fib(n - 1, memo) + fib(n - 2, memo)8memo[n] = valuevalues this step{}memomiss -> descend fib(1)action2nmemo ← {1: 1}, action ← base 1; memo[1]=1; return
6 return n7value = fib(n - 1, memo) + fib(n - 2, memo)8memo[n] = valuevalues this step{1: 1}memobase 1; memo[1]=1; returnaction1nmemo ← {0: 0, 1: 1}, action ← base 0; memo[0]=0; fib(2)=1; memo[2]=1
6 return n7value = fib(n - 1, memo) + fib(n - 2, memo)8memo[n] = valuevalues this step{0: 0, 1: 1}memobase 0; memo[0]=0; fib(2)=1; memo[2]=1action0nmemo ← {0: 0, 1: 1, 2: 1, 3: 2}, action ← hit 1; fib(3)=2; memo[3]=2
6 return n7value = fib(n - 1, memo) + fib(n - 2, memo)8memo[n] = valuevalues this step{0: 0, 1: 1, 2: 1, 3: 2}memohit 1; fib(3)=2; memo[3]=2action1nmemo ← {0: 0, 1: 1, 2: 1, 3: 2, 4: 3}, action ← hit 1; fib(4)=3; memo[4]=3
6 return n7value = fib(n - 1, memo) + fib(n - 2, memo)8memo[n] = valuevalues this step{0: 0, 1: 1, 2: 1, 3: 2, 4: 3}memohit 1; fib(4)=3; memo[4]=3action2nmemo ← {0: 0, 1: 1, 2: 1, 3: 2, 4: 3, 5: 5}, action ← hit 2; fib(5)=5; memo[5]=5
6 return n7value = fib(n - 1, memo) + fib(n - 2, memo)8memo[n] = valuevalues this step{0: 0, 1: 1, 2: 1, 3: 2, 4: 3, 5: 5}memohit 2; fib(5)=5; memo[5]=5action3nmemo ← {0: 0, 1: 1, 2: 1, 3: 2, 4: 3, 5: 5, 6: 8}, action ← hit 3; fib(6)=8; memo[6]=8
6 return n7value = fib(n - 1, memo) + fib(n - 2, memo)8memo[n] = valuevalues this step{0: 0, 1: 1, 2: 1, 3: 2, 4: 3, 5: 5, 6: 8}memohit 3; fib(6)=8; memo[6]=8action4nstdout ← 8
12result = fib(6, memo)13print(result)14print(memo)values this step8stdout8result
Complexity
- Time: O(n) with memoization (vs. O(2^n) without)
- Space: O(n) memo plus O(n) call stack
Implementation notes
- Python: pass
memoexplicitly. Do not usefunctools.lru_cache; it would hide the memo write and the cache-hit branch. - The replay shows each
fib(k)call and the memo state after, marking the cache-hit steps distinctly so the viewer can count how many descents were avoided.