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.
Basic Implementation
basic.R
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 = "")
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.
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.