Recursion and Dynamic Programming
Fibonacci with Memoization
Compute fib(n) recursively. Cache each fib(k) in a memo array so each
subproblem is solved at most once.
Algorithm
Canonical input n = 6 produces fib(6) = 8. Replay highlights every
memo write and every cache hit.
memoization
A parallel `memo_value[]` / `memo_seen[]` pair indexed by `n` stores each completed subproblem. Before recursing, check `memo_seen[n]`: a hit returns immediately, a miss descends.
explicit memo state
The memo lives in file-scope static arrays so the recursion stays about caching, not parameter passing — C does not need a heap map container for small `n`.
Basic Implementation
basic.c
Replay: real traced execution (multi-file project)
#include <stdio.h>
#include <stdbool.h>
#define MEMO_SIZE 16
static int memo_value[MEMO_SIZE];
static bool memo_seen[MEMO_SIZE];
int fib(int n) {
if (memo_seen[n]) {
return memo_value[n];
}
if (n < 2) {
memo_value[n] = n;
memo_seen[n] = true;
return n;
}
int value = fib(n - 1) + fib(n - 2);
memo_value[n] = value;
memo_seen[n] = true;
return value;
}
int main(void) {
int result = fib(6);
printf("%d\n", result);
printf("{");
bool first = true;
for (int i = 0; i < MEMO_SIZE; ++i) {
if (memo_seen[i]) {
if (!first) printf(", ");
printf("%d: %d", i, memo_value[i]);
first = false;
}
}
printf("}\n");
return 0;
}
memo ← {}, action ← miss -> descend fib(5)
17}18int value = fib(n - 1) + fib(n - 2);19memo_value[n] = value;values this step{}memomiss -> descend fib(5)action6nmemo ← {}, action ← miss -> descend fib(4)
17}18int value = fib(n - 1) + fib(n - 2);19memo_value[n] = value;values this step{}memomiss -> descend fib(4)action5nmemo ← {}, action ← miss -> descend fib(3)
17}18int value = fib(n - 1) + fib(n - 2);19memo_value[n] = value;values this step{}memomiss -> descend fib(3)action4nmemo ← {}, action ← miss -> descend fib(2)
17}18int value = fib(n - 1) + fib(n - 2);19memo_value[n] = value;values this step{}memomiss -> descend fib(2)action3nmemo ← {}, action ← miss -> descend fib(1)
17}18int value = fib(n - 1) + fib(n - 2);19memo_value[n] = value;values this step{}memomiss -> descend fib(1)action2nmemo ← {1: 1}, action ← base 1; memo[1] = 1; return
17}18int value = fib(n - 1) + fib(n - 2);19memo_value[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
17}18int value = fib(n - 1) + fib(n - 2);19memo_value[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
17}18int value = fib(n - 1) + fib(n - 2);19memo_value[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
17}18int value = fib(n - 1) + fib(n - 2);19memo_value[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
17}18int value = fib(n - 1) + fib(n - 2);19memo_value[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
17}18int value = fib(n - 1) + fib(n - 2);19memo_value[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
25int result = fib(6);26printf("%d\n", result);27printf("{");values this step8stdout8result
Complexity
- Time: O(n) with memoization (vs. O(2^n) without)
- Space: O(n) memo + O(n) call stack
Implementation notes
- C: file-scope
int memo_value[MEMO_SIZE]andbool memo_seen[MEMO_SIZE]from<stdbool.h>give the memo a deterministic iteration order keyed byn, which keeps the final printout honest without leaning on a hash-map container the standard library does not provide. - The replay shows the call stack on one side and the memo map on the other so memo writes and cache hits are visually distinct.