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.
Basic Implementation
basic.sh
#!/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"
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.
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.