Recursion and Dynamic Programming
DP
Fibonacci with Memoization
Computes fib(n) with top-down memoization. Each subproblem is solved
once and cached; later calls hit the cache instead of recomputing.
Algorithm
Canonical input is fib 6 with an empty memo. The descent fills the
cache {0, 1, 2, 3, 4, 5, 6}; the final value is 8.
top-down memoization
`fib(n)` first consults a `memo` associative array. On hit, return the stored value; on miss, recursively compute `fib(n - 1) + fib(n - 2)`, store, and return.
base cases
`fib(0) = 0` and `fib(1) = 1` populate the cache and stop the recursion.
Basic Implementation
basic.sh
Replay: real traced execution (multi-file project)
#!/usr/bin/env bash
set -euo pipefail
declare -A memo
fib_result=0
fib() {
local n=$1
local key=$n
if [ -n "${memo[$key]+_}" ]; then
fib_result=${memo[$key]}
return
fi
if [ "$n" -lt 2 ]; then
memo[$key]=$n
fib_result=$n
return
fi
fib $((n - 1))
local r1=$fib_result
fib $((n - 2))
local r2=$fib_result
local value=$((r1 + r2))
memo[$key]=$value
fib_result=$value
}
fib 6
echo "$fib_result"
memo ← {}, action ← miss -> descend fib(5)
16fi17fib $((n - 1))18local r1=$fib_resultvalues this step{}memomiss -> descend fib(5)action6nmemo ← {}, action ← miss -> descend fib(4)
16fi17fib $((n - 1))18local r1=$fib_resultvalues this step{}memomiss -> descend fib(4)action5nmemo ← {}, action ← miss -> descend fib(3)
16fi17fib $((n - 1))18local r1=$fib_resultvalues this step{}memomiss -> descend fib(3)action4nmemo ← {}, action ← miss -> descend fib(2)
16fi17fib $((n - 1))18local r1=$fib_resultvalues this step{}memomiss -> descend fib(2)action3nmemo ← {}, action ← miss -> descend fib(1)
16fi17fib $((n - 1))18local r1=$fib_resultvalues this step{}memomiss -> descend fib(1)action2nmemo ← {1: 1}, action ← base 1; memo[1]=1; return
16fi17fib $((n - 1))18local r1=$fib_resultvalues 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
16fi17fib $((n - 1))18local r1=$fib_resultvalues 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
16fi17fib $((n - 1))18local r1=$fib_resultvalues 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
16fi17fib $((n - 1))18local r1=$fib_resultvalues 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
16fi17fib $((n - 1))18local r1=$fib_resultvalues 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
16fi17fib $((n - 1))18local r1=$fib_resultvalues 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
26fib 627echo "$fib_result"values this step8stdout8fib_result
Complexity
- Time: O(n) with memoization (vs. O(2^n) without)
- Space: O(n) for the memo plus O(n) call stack
Implementation notes
- Bash: command substitution
$(fib ...)would spawn a subshell and throw away the memo updates, so the function mutates a globalmemo(declare -A) directly and returns its value through a globalfib_result. Callers readfib_resultimmediately after the call returns. - The
"${memo[$key]+_}"parameter expansion distinguishes a missing key from a key whose value is0, which is what makesmemo[0]=0safe to cache. - The replay describes the descent / cache miss frames first, then the cache fills as the recursion unwinds, so the viewer sees the cache build out from the base cases.