Compute fib(n) recursively and cache each fib(k) in a memo array so each subproblem is solved at most once. Turns exponential recursion into a linear walk, the canonical introduction to dynamic programming.

Algorithm

Canonical input n = 6 produces fib(6) = 8. The replay shows the descent for fib(6) -> fib(5) -> ... -> fib(0) then four cache hits on the unwind (one per repeat-call subproblem).

memoization Reuse cached subproblem results instead of re-descending.

Basic Implementation

basic.f90
Replay: real traced execution (multi-file project)
module fib_mod
    implicit none
    integer :: memo(0:6) = -1
contains
    recursive function fib(n) result(v)
        integer, intent(in) :: n
        integer :: v
        if (memo(n) /= -1) then
            v = memo(n)
            return
        end if
        if (n < 2) then
            memo(n) = n
            v = n
            return
        end if
        v = fib(n - 1) + fib(n - 2)
        memo(n) = v
    end function fib
end module fib_mod

program dp_fibonacci_memo
    use fib_mod
    implicit none
    integer :: result, i
    result = fib(6)
    print '(I0)', result
    do i = 0, 6
        print '(A,I0,A,I0)', 'memo(', i, ') = ', memo(i)
    end do
end program dp_fibonacci_memo
  1. memo ← {}, action ← miss -> descend fib(5)

    16end if17v = fib(n - 1) + fib(n - 2)18memo(n) = v
    values this step{}memomiss -> descend fib(5)action6n
  2. memo ← {}, action ← miss -> descend fib(4)

    16end if17v = fib(n - 1) + fib(n - 2)18memo(n) = v
    values this step{}memomiss -> descend fib(4)action5n
  3. memo ← {}, action ← miss -> descend fib(3)

    16end if17v = fib(n - 1) + fib(n - 2)18memo(n) = v
    values this step{}memomiss -> descend fib(3)action4n
  4. memo ← {}, action ← miss -> descend fib(2)

    16end if17v = fib(n - 1) + fib(n - 2)18memo(n) = v
    values this step{}memomiss -> descend fib(2)action3n
  5. memo ← {}, action ← miss -> descend fib(1)

    16end if17v = fib(n - 1) + fib(n - 2)18memo(n) = v
    values this step{}memomiss -> descend fib(1)action2n
  6. memo ← {1: 1}, action ← base 1; memo(1) = 1; return

    16end if17v = fib(n - 1) + fib(n - 2)18memo(n) = v
    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

    16end if17v = fib(n - 1) + fib(n - 2)18memo(n) = v
    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

    16end if17v = fib(n - 1) + fib(n - 2)18memo(n) = v
    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

    16end if17v = fib(n - 1) + fib(n - 2)18memo(n) = v
    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

    16end if17v = fib(n - 1) + fib(n - 2)18memo(n) = v
    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

    16end if17v = fib(n - 1) + fib(n - 2)18memo(n) = v
    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

    26result = fib(6)27print '(I0)', result28do i = 0, 6
    values this step8stdout8result

Complexity

  • Time: O(n) with memoization (vs. O(2^n) without)
  • Space: O(n) memo plus O(n) call stack

Implementation notes

  • Fortran: a 0-indexed integer :: memo(0:6) = -1 is the smallest possible memo for fib(6). The sentinel -1 distinguishes "not computed yet" from a real fib(k) value, which is always >= 0. Pass the memo through the module rather than as an argument to keep the recursive signature compact.
  • The replay shows each fib(k) call and the memo state after, marking the cache-hit steps distinctly so the viewer can count how many descents were avoided.