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 `HashMap[Int, Int]` keyed by `n` stores each completed subproblem. Before recursing, check `memo.contains(n)`: a hit returns immediately, a miss descends.
explicit memo state
The memo is threaded through the recursion as `memo: HashMap[Int, Int]` so the lesson stays about caching, not global state.
Basic Implementation
basic.scala
Replay: real traced execution (multi-file project)
import scala.collection.mutable.HashMap
object Main {
def fib(n: Int, memo: HashMap[Int, Int]): Int = {
if (memo.contains(n)) {
return memo(n)
}
if (n < 2) {
memo(n) = n
return n
}
val value = fib(n - 1, memo) + fib(n - 2, memo)
memo(n) = value
value
}
def main(args: Array[String]): Unit = {
val memo = HashMap.empty[Int, Int]
val result = fib(6, memo)
println(result)
}
}
memo ← {}, action ← miss -> descend fib(5)
11}12val value = fib(n - 1, memo) + fib(n - 2, memo)13memo(n) = valuevalues this step{}memomiss -> descend fib(5)action6nmemo ← {}, action ← miss -> descend fib(4)
11}12val value = fib(n - 1, memo) + fib(n - 2, memo)13memo(n) = valuevalues this step{}memomiss -> descend fib(4)action5nmemo ← {}, action ← miss -> descend fib(3)
11}12val value = fib(n - 1, memo) + fib(n - 2, memo)13memo(n) = valuevalues this step{}memomiss -> descend fib(3)action4nmemo ← {}, action ← miss -> descend fib(2)
11}12val value = fib(n - 1, memo) + fib(n - 2, memo)13memo(n) = valuevalues this step{}memomiss -> descend fib(2)action3nmemo ← {}, action ← miss -> descend fib(1)
11}12val value = fib(n - 1, memo) + fib(n - 2, memo)13memo(n) = valuevalues this step{}memomiss -> descend fib(1)action2nmemo ← {1: 1}, action ← base 1; memo(1) = 1; return
11}12val value = fib(n - 1, memo) + fib(n - 2, memo)13memo(n) = valuevalues 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
11}12val value = fib(n - 1, memo) + fib(n - 2, memo)13memo(n) = valuevalues 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
11}12val value = fib(n - 1, memo) + fib(n - 2, memo)13memo(n) = valuevalues 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
11}12val value = fib(n - 1, memo) + fib(n - 2, memo)13memo(n) = valuevalues 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
11}12val value = fib(n - 1, memo) + fib(n - 2, memo)13memo(n) = valuevalues 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
11}12val value = fib(n - 1, memo) + fib(n - 2, memo)13memo(n) = valuevalues 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
19 val result = fib(6, memo)20 println(result)21}values this step8stdout8result
Complexity
- Time: O(n) with memoization (vs. O(2^n) without)
- Space: O(n) memo + O(n) call stack
Implementation notes
- Scala: the recursion takes the memo as a
scala.collection.mutable.HashMap[Int, Int]argument rather than a companion-objectvar, which keeps state explicit without hiding the lesson behind a shared global. Thecontains+applyindexer pair stays parallel to the lesson spec instead of leaning ongetOrElseUpdate. - 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.