Skip to content

Top-Down vs Bottom-Up

Lesson 2 of 20 · 5 min

Top-Down vs Bottom-Up

Step 1 of 12

Top-down starts at the full problem — fib(4) — and recurses only into what it actually needs.

The Idea

Two directions, one recurrence. Top-down starts at the full problem, recurses, and caches whatever it meets — so it touches only the subproblems it actually needs. Bottom-up starts at the base cases and fills a table forward, so every dependency is ready before it is read. Top-down is easier to derive from the recursion; bottom-up avoids deep call stacks.

Real-World Example

Revising for an exam. Top-down means opening the hardest past paper and looking things up only when a question demands them. Bottom-up means working the syllabus chapter by chapter, so nothing is ever missing when you reach the paper — though you also study pages the paper never asks about.

The Code

Your turn

Put the steps in the right order.

  1. Fill the remaining cells in dependency order
  2. Decide what a single table cell will mean
  3. Read the final cell as the answer
  4. Write the base-case cells in directly

Mini quiz

1 / 3

Top-down DP is best described as: