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)
  1. memo ← {}, action ← miss -> descend fib(5)

    6    return n7value = fib(n - 1, memo) + fib(n - 2, memo)8memo[n] = value
    values this step{}memomiss -> descend fib(5)action6n
  2. memo ← {}, action ← miss -> descend fib(4)

    6    return n7value = fib(n - 1, memo) + fib(n - 2, memo)8memo[n] = value
    values this step{}memomiss -> descend fib(4)action5n
  3. memo ← {}, action ← miss -> descend fib(3)

    6    return n7value = fib(n - 1, memo) + fib(n - 2, memo)8memo[n] = value
    values this step{}memomiss -> descend fib(3)action4n
  4. memo ← {}, action ← miss -> descend fib(2)

    6    return n7value = fib(n - 1, memo) + fib(n - 2, memo)8memo[n] = value
    values this step{}memomiss -> descend fib(2)action3n
  5. memo ← {}, action ← miss -> descend fib(1)

    6    return n7value = fib(n - 1, memo) + fib(n - 2, memo)8memo[n] = value
    values this step{}memomiss -> descend fib(1)action2n
  6. memo ← {1: 1}, action ← base 1; memo[1]=1; return

    6    return n7value = fib(n - 1, memo) + fib(n - 2, memo)8memo[n] = value
    values this step{1: 1}memobase 1; memo[1]=1; returnaction1n
  7. memo ← {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] = value
    values this step{0: 0, 1: 1}memobase 0; memo[0]=0; fib(2)=1; memo[2]=1action0n
  8. memo ← {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] = value
    values this step{0: 0, 1: 1, 2: 1, 3: 2}memohit 1; fib(3)=2; memo[3]=2action1n
  9. memo ← {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] = value
    values this step{0: 0, 1: 1, 2: 1, 3: 2, 4: 3}memohit 1; fib(4)=3; memo[4]=3action2n
  10. memo ← {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] = value
    values this step{0: 0, 1: 1, 2: 1, 3: 2, 4: 3, 5: 5}memohit 2; fib(5)=5; memo[5]=5action3n
  11. memo ← {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] = value
    values this step{0: 0, 1: 1, 2: 1, 3: 2, 4: 3, 5: 5, 6: 8}memohit 3; fib(6)=8; memo[6]=8action4n
  12. stdout ← 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 memo explicitly. Do not use functools.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.