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.
Basic Implementation
basic.pl
use strict; use warnings;
sub fib {
my ($n, $memo_ref) = @_;
if (exists $memo_ref->{$n}) {
return $memo_ref->{$n};
}
if ($n < 2) {
$memo_ref->{$n} = $n;
return $n;
}
my $value = fib($n - 1, $memo_ref) + fib($n - 2, $memo_ref);
$memo_ref->{$n} = $value;
return $value;
}
my %memo = ();
my $result = fib(6, \%memo);
print "$result\n";
Complexity
- Time: O(n) with memoization (vs. O(2^n) without)
- Space: O(n) memo + O(n) call stack
Implementation notes
- Perl: the recursion takes the memo as a hash reference argument
rather than an
our %memopackage global, which keeps state explicit without hiding the lesson behind a shared global. Theexists+ arrow-deref pair stays parallel to the lesson spec instead of leaning on Perl's autovivification (which would create the slot during a probe and collapse the hit / miss branch). - 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.
memoization
A hash reference keyed by `$n` stores each completed subproblem. Before recursing, check `exists $memo_ref->{$n}`: a hit returns immediately, a miss descends.
explicit memo state
The memo is threaded through the recursion as a hash reference argument rather than a package-level `our` global, which keeps the lesson about caching, not shared state.