Best on a larger screen — the animation and controls have more room to breathe.
Inputs

Fibonacci & Memoization

Memoization caches a function's results so repeated calls with the same argument return without recomputing. The Fibonacci sequence (fib(n) = fib(n-1) + fib(n-2), with fib(0) = 0 and fib(1) = 1) is its standard demonstration. Plain recursion recomputes subproblems exponentially often; adding one cache reduces the cost from roughly O(2ⁿ) to O(n).

Picture a clerk recomputing a subtotal from scratch every time someone asks: that is naive recursion. Memoization hands the clerk a notebook so each result is computed once, written down, and looked up on later requests. Dynamic programming applies that reuse systematically, and this lesson runs the same computation three ways (naive recursion, top-down memoization, and bottom-up tabulation) so you can compare the repeated work directly.

How Memoization Works

Naive recursion (the default view) translates the recurrence directly into a recursive function and builds a recursion tree. fib(6) calls fib(5) and fib(4), and each splits again down to the base cases fib(1) and fib(0). Nothing is remembered, so an entire subtree such as fib(4) is rebuilt from scratch every time it is requested.

Top-down memoization keeps the same recursion and adds a memo dictionary. The first time fib(4) is computed, the result is stored. Every later request becomes an O(1) lookup (a cache hit), so that subtree is never expanded again and the tree prunes to a thin spine.

Bottom-up tabulation removes recursion and fills a table dp[] in order from left to right: dp[0] = 0, dp[1] = 1, then each dp[i] = dp[i-1] + dp[i-2]. Every cell is written once using only the two preceding cells, with no call stack and no waiting child calls because each dependency is already filled when needed.

All three methods produce the same answer; they differ in how they reuse known subproblems. Naive recursion ignores prior results, memoization saves them lazily on demand, and tabulation computes them eagerly in order.

Fibonacci Step by Step

The default run is naive recursion with n = 6, so the target is fib(6) = 8.

  1. Call fib(6). It needs fib(5) + fib(4) and recurses into fib(5) first.
  2. Drill down the left spine. Recursion descends fib(5) → fib(4) → fib(3) → fib(2) until it reaches the base cases fib(1) = 1 and fib(0) = 0.
  3. Combine upward. With both children resolved, a combine beat performs the addition (fib(2) = 1 + 0 = 1), and a separate return beat commits the value and passes it up. The same sequence yields fib(3) = 1 + 1 = 2, fib(4) = 2 + 1 = 3, and fib(5) = 3 + 2 = 5.
  4. Recompute fib(4) from scratch. Back at the root, fib(6) still needs its second child. Without a cache, the entire fib(4) subtree (nine calls) is rebuilt node by node, and the status line notes that this argument has already been seen.
  5. Finish. fib(6) = 5 + 3 = 8, after 25 calls in total.

Switch the mode selector to Memoization and the same fib(6) resolves in 11 calls: the second requests for fib(2), fib(3), and fib(4) appear as cache-hit stubs that read straight from the memo panel instead of expanding subtrees. Switch to Bottom-up and there is no tree; the table fills 0, 1, 1, 2, 3, 5, 8 in three beats per cell: focus the empty slot, read the two preceding cells, and write the sum.

On screen, the tree reveals one node per call rather than appearing all at once; the two children light up as summands on each combine beat, and a value chip turns the node green when it returns. In memoized mode, the left-hand memo panel displays the cache: a row fills on every store and flashes on every hit. The running call counter in the header tracks the call savings directly: 25 calls versus 11.

Time and Space Complexity

ApproachTimeSpaceWhat you see
Naive recursionO(φⁿ) ≈ O(2ⁿ)O(n) stackfib(6) costs 25 calls; fib(7) costs 41
Top-down memoizationO(n)O(n) memo + O(n) stackeach fib(k) with k ≥ 2 solved once, then looked up; bases return directly
Bottom-up tableO(n)O(n) tableone write per cell; O(1) if you keep only the last two values

The exponential base is the golden ratio φ ≈ 1.618, because the call count of naive fib(n) grows in proportion to the Fibonacci numbers themselves. Reducing that runtime to O(n) is what makes larger inputs practical: fib(50) finishes immediately with memoization or tabulation, whereas naive recursion becomes unusable.

Edge Cases

  • n = 0 and n = 1 are base cases. They return immediately with no recursion. The lesson's slider therefore starts at n = 2, the smallest input with a recurrence step, and caps at n = 7, where the naive tree already holds 41 nodes.
  • Base cases are never cache hits. The displayed code checks n < 2 before consulting the memo, so fib(0) and fib(1) never enter the cache because returning the argument directly needs no lookup.
  • Python's memo={} default argument persists across calls. That is convenient in a small demo, but a common bug whenever separate calls should not share a cache.
  • Large n overflows fixed-width integers in many languages long before O(n) runtime becomes slow.

Common Mistakes

  • Shipping the naive version. It is correct and passes small tests, then times out as n grows because exponential cost remains small around n = 10.
  • Reordering the cache check and the base case, then expecting the same trace. This lesson returns bases first, so they never reach the memo. Another consistent order can also be correct, but it changes which calls hit, store, and exit through which line.
  • Off-by-one in the table. dp needs length n + 1, and both dp[0] and dp[1] must be written before the loop, or the first iteration reads an empty cell.
  • Believing memoization changes the answer. All three approaches return the same fib(n). Caching reduces the number of calls without changing the result.