Recursion and Dynamic Programming
Fibonacci with Memoization
Compute fib(n) recursively. Cache each fib(k) in a memo map 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 named list `memo` keyed by `as.character(n)` stores each completed subproblem. Before recursing, check `!is.null(memo[[key]])`: a hit returns immediately, a miss descends.
explicit memo state
The memo is threaded through the recursion as the second parameter and returned alongside the value so the lesson stays about caching, not a closure upvalue or a shared environment.
Basic Implementation
basic.R
Replay: real traced execution (multi-file project)
fib <- function(n, memo) {
key <- as.character(n)
if (!is.null(memo[[key]])) {
return(list(value = memo[[key]], memo = memo))
}
if (n < 2) {
memo[[key]] <- n
return(list(value = n, memo = memo))
}
r1 <- fib(n - 1, memo); memo <- r1$memo
r2 <- fib(n - 2, memo); memo <- r2$memo
value <- r1$value + r2$value
memo[[key]] <- value
return(list(value = value, memo = memo))
}
memo <- list()
r <- fib(6, memo)
result <- r$value
cat(result, "\n", sep = "")
memo ← {}, action ← miss -> descend fib(5)
9}10r1 <- fib(n - 1, memo); memo <- r1$memo11r2 <- fib(n - 2, memo); memo <- r2$memovalues this step{}memomiss -> descend fib(5)action6nmemo ← {}, action ← miss -> descend fib(4)
9}10r1 <- fib(n - 1, memo); memo <- r1$memo11r2 <- fib(n - 2, memo); memo <- r2$memovalues this step{}memomiss -> descend fib(4)action5nmemo ← {}, action ← miss -> descend fib(3)
9}10r1 <- fib(n - 1, memo); memo <- r1$memo11r2 <- fib(n - 2, memo); memo <- r2$memovalues this step{}memomiss -> descend fib(3)action4nmemo ← {}, action ← miss -> descend fib(2)
9}10r1 <- fib(n - 1, memo); memo <- r1$memo11r2 <- fib(n - 2, memo); memo <- r2$memovalues this step{}memomiss -> descend fib(2)action3nmemo ← {}, action ← miss -> descend fib(1)
9}10r1 <- fib(n - 1, memo); memo <- r1$memo11r2 <- fib(n - 2, memo); memo <- r2$memovalues this step{}memomiss -> descend fib(1)action2nmemo ← {1: 1}, action ← base 1; memo[[1]] <- 1; return
9}10r1 <- fib(n - 1, memo); memo <- r1$memo11r2 <- fib(n - 2, memo); memo <- r2$memovalues 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}10r1 <- fib(n - 1, memo); memo <- r1$memo11r2 <- fib(n - 2, memo); memo <- r2$memovalues 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}10r1 <- fib(n - 1, memo); memo <- r1$memo11r2 <- fib(n - 2, memo); memo <- r2$memovalues 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}10r1 <- fib(n - 1, memo); memo <- r1$memo11r2 <- fib(n - 2, memo); memo <- r2$memovalues 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}10r1 <- fib(n - 1, memo); memo <- r1$memo11r2 <- fib(n - 2, memo); memo <- r2$memovalues 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}10r1 <- fib(n - 1, memo); memo <- r1$memo11r2 <- fib(n - 2, memo); memo <- r2$memovalues 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
19result <- r$value20cat(result, "\n", sep = "")values this step8stdout8result
Complexity
- Time: O(n) with memoization (vs. O(2^n) without)
- Space: O(n) memo + O(n) call stack
Implementation notes
- R: the recursion takes the memo as a parameter and returns the
updated memo alongside the value rather than mutating an enclosing
environment, which keeps state explicit without leaning on R's
reference-semantic environments (
new.env(parent = emptyenv())). Every recursive call threadsr1$memoforward so the cache shape is visible in the source. !is.null(memo[[key]])is the explicit cache-check predicate; R's named-list default behaviour returnsNULLfor absent keys, so the predicate stays parallel to the lesson spec instead of leaning ontryCatchdefaults orexists()-on-environment checks.- 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.