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"
  1. memo ← {}, action ← miss -> descend fib(5)

    16fi17fib $((n - 1))18local r1=$fib_result
    values this step{}memomiss -> descend fib(5)action6n
  2. memo ← {}, action ← miss -> descend fib(4)

    16fi17fib $((n - 1))18local r1=$fib_result
    values this step{}memomiss -> descend fib(4)action5n
  3. memo ← {}, action ← miss -> descend fib(3)

    16fi17fib $((n - 1))18local r1=$fib_result
    values this step{}memomiss -> descend fib(3)action4n
  4. memo ← {}, action ← miss -> descend fib(2)

    16fi17fib $((n - 1))18local r1=$fib_result
    values this step{}memomiss -> descend fib(2)action3n
  5. memo ← {}, action ← miss -> descend fib(1)

    16fi17fib $((n - 1))18local r1=$fib_result
    values this step{}memomiss -> descend fib(1)action2n
  6. memo ← {1: 1}, action ← base 1; memo[1]=1; return

    16fi17fib $((n - 1))18local r1=$fib_result
    values this step{1: 1}memobase 1; memo[1]=1; returnaction1n
  7. memo ← {0: 0, 1: 1}, action ← base 0; memo[0]=0; fib(2)=1; memo[2]=1

    16fi17fib $((n - 1))18local r1=$fib_result
    values this step{0: 0, 1: 1}memobase 0; memo[0]=0; fib(2)=1; memo[2]=1action0n
  8. memo ← {0: 0, 1: 1, 2: 1, 3: 2}, action ← hit 1; fib(3)=2; memo[3]=2

    16fi17fib $((n - 1))18local r1=$fib_result
    values this step{0: 0, 1: 1, 2: 1, 3: 2}memohit 1; fib(3)=2; memo[3]=2action1n
  9. memo ← {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_result
    values this step{0: 0, 1: 1, 2: 1, 3: 2, 4: 3}memohit 1; fib(4)=3; memo[4]=3action2n
  10. memo ← {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_result
    values this step{0: 0, 1: 1, 2: 1, 3: 2, 4: 3, 5: 5}memohit 2; fib(5)=5; memo[5]=5action3n
  11. memo ← {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_result
    values this step{0: 0, 1: 1, 2: 1, 3: 2, 4: 3, 5: 5, 6: 8}memohit 3; fib(6)=8; memo[6]=8action4n
  12. stdout ← 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 global memo (declare -A) directly and returns its value through a global fib_result. Callers read fib_result immediately after the call returns.
  • The "${memo[$key]+_}" parameter expansion distinguishes a missing key from a key whose value is 0, which is what makes memo[0]=0 safe 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.