3.7.7 · D1Algorithm Paradigms

Foundations — Memoization (top-down DP) — recursive + memo dict

2,651 words12 min readBack to topic

This page is self-contained: it builds every symbol and idea from nothing, so you can read it in isolation. Where a word links out (like Recursion), that link is a place to go deeper later — you do not need it to follow this page. By the end you'll have the full mental toolkit for the "Memoization" topic.

Recall What is the "parent topic" I keep hearing about?

This page is a foundations companion to a main topic note called Memoization (top-down DP). That main note applies these ideas to real problems. Everything you need to understand memoization is defined right here — the main note is where you'll use it.


0. What is a function that calls itself?

The whole topic sits on top of one idea: a rule that is written in terms of itself.

Look at the picture below. Reaching fib(5) means first reaching fib(4) and fib(3). Those in turn split again. The splitting stops only at the bottom, where the answer needs no further work.

Figure — Memoization (top-down DP) — recursive + memo dict
  • The red boxes with a thick double border are where the machine stops splitting — the base cases.
  • The boxes marked with a ★ star (fib3) appear twice, and the ones marked with a ● dot (fib2) appear three times. That repetition — flagged by shape and colour so it reads even in greyscale — is the enemy the whole topic is built to kill.

1. The base case — where the splitting stops


2. What is a recurrence?

The star example is Fibonacci:

Now decode every symbol in that line:


3. The blow-up — the pain we're curing

Why bolt a memory on at all? Because plain recursion is wildly wasteful when subproblems repeat.

Figure — Memoization (top-down DP) — recursive + memo dict

The figure counts the boxes in the call tree as grows.


4. The cache — a dictionary that remembers

The "notebook" the recursion writes in is a dictionary.

Figure — Memoization (top-down DP) — recursive + memo dict

5. Putting it together — the memoized algorithm

You now own every piece. Here is the whole method, as plain step-by-step pseudocode — the "how" this page exists to earn. It's the pattern C-B-R-S: Check, Base, Recurse, Store.

Trace it on Fibonacci so the abstract steps become concrete:


6. The two costs — time and memory

A memo buys speed, but it isn't free — it spends memory to save time. Name both costs honestly.


7. Function arguments and default values

Memoized code passes the memo around between calls, so you need one last vocabulary pair.


How the foundations feed the topic

Read this map top to bottom — it is the exact order of the sections above, showing what depends on what:

  • Function (Sec. 0) is the raw machine.
  • Recursion (Sec. 0) is a function calling itself; it needs a base case (Sec. 1) to stop.
  • Recursion that references earlier values is a recurrence (Sec. 2).
  • A recurrence with repeated subproblems causes the exponential blow-up (Sec. 3) — the pain.
  • A dictionary (Sec. 4) keyed by state (Sec. 4) gives free lookup (Sec. 6).
  • Combine them with the C-B-R-S algorithm (Sec. 5), mind the memory cost (Sec. 6) and the default-argument safety (Sec. 7) — and you have memoization.

Function: input to output

Recursion: function calls itself

Base case: where splitting stops

Recurrence: value from smaller values

Blow-up: repeated subproblems

Dictionary: key to value

Cache keyed by state

State: full subproblem id

O of 1 lookup

Memory cost O of states

C-B-R-S algorithm

MEMOIZATION

Default argument memo none

Each arrow means "the lower box uses the idea in the upper box." Trace any path to MEMOIZATION and you're retracing why each foundation had to come first.


Equipment checklist

Cover the right side and check you can answer each before moving on.

What does it mean for a function to be recursive?
It calls itself on a smaller input, shrinking toward a base case.
What is a base case and why is it essential?
The smallest input whose answer is known outright; it's the STOP sign that ends the recursion.
In , what does mean?
The Fibonacci machine's output for input — the -th number in the sequence.
Why is plain recursive Fibonacci exponential, and what is its exact growth rate?
The lopsided call tree recomputes repeated subproblems; the true node count grows like with (bounded above by ).
What does mean?
Constant time — the same tiny cost regardless of input size, like a dictionary lookup.
What is a dictionary / hash map?
A store of key→value pairs with near-instant lookup by key.
What is a state in memoization?
The full description of the current subproblem — it becomes the cache key.
What are the four steps of the memoized algorithm?
C-B-R-S: Check cache → Base case → Recurse → Store (store before returning).
What TWO resources does memoization cost, and how much?
Time drops to , but memory rises to for the cache plus for the stack.
Why is def f(n, memo={}) dangerous?
The {} is created once and shared across all calls, leaking stale cached answers between invocations.
One-sentence definition of memoization?
Recursion that stores each distinct subproblem's answer in a cache so it's computed exactly once.

Connections

  • Recursion — the engine underneath everything here
  • Dynamic Programming — the umbrella these ideas live under
  • Time Complexity Analysis — where and come from
  • Hash Maps / Dictionaries — the cache's data structure
  • Divide and Conquer — recursion without overlap (no memo needed)
  • Tabulation (Bottom-up DP) — the iterative twin you'll meet next
  • lru_cache decorator — Python's built-in auto-cache