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.dart
Replay: real traced execution (multi-file project)
int fib(int n, Map<int, int> memo) {
final cached = memo[n];
if (cached != null) {
return cached;
}
if (n < 2) {
memo[n] = n;
return n;
}
final value = fib(n - 1, memo) + fib(n - 2, memo);
memo[n] = value;
return value;
}
void main() {
final memo = <int, int>{};
final result = fib(6, memo);
print(result);
print(memo);
}
memo ← {}, action ← miss -> descend fib(5)
9}10final value = fib(n - 1, memo) + fib(n - 2, memo);11memo[n] = value;values this step{}memomiss -> descend fib(5)action6nmemo ← {}, action ← miss -> descend fib(4)
9}10final value = fib(n - 1, memo) + fib(n - 2, memo);11memo[n] = value;values this step{}memomiss -> descend fib(4)action5nmemo ← {}, action ← miss -> descend fib(3)
9}10final value = fib(n - 1, memo) + fib(n - 2, memo);11memo[n] = value;values this step{}memomiss -> descend fib(3)action4nmemo ← {}, action ← miss -> descend fib(2)
9}10final value = fib(n - 1, memo) + fib(n - 2, memo);11memo[n] = value;values this step{}memomiss -> descend fib(2)action3nmemo ← {}, action ← miss -> descend fib(1)
9}10final value = fib(n - 1, memo) + fib(n - 2, memo);11memo[n] = value;values this step{}memomiss -> descend fib(1)action2nmemo ← {1: 1}, action ← base 1; memo[1] = 1; return
9}10final value = fib(n - 1, memo) + fib(n - 2, memo);11memo[n] = value;values 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
9}10final value = fib(n - 1, memo) + fib(n - 2, memo);11memo[n] = value;values 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
9}10final value = fib(n - 1, memo) + fib(n - 2, memo);11memo[n] = value;values 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
9}10final value = fib(n - 1, memo) + fib(n - 2, memo);11memo[n] = value;values 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
9}10final value = fib(n - 1, memo) + fib(n - 2, memo);11memo[n] = value;values 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
9}10final value = fib(n - 1, memo) + fib(n - 2, memo);11memo[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] = 8action4nstdout ← 8
17final result = fib(6, memo);18print(result);19print(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
- Dart: pass
memoexplicitly as aMap<int, int>. Reading viafinal cached = memo[n];keeps the cache hit branch visible. TheMap.updateshortcut would hide the memo write. - 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.