A topic in the Open Knowledge Graph — a free, open map of 15,290 topics and the order to learn them in.

Memoization and Tabulation

College Depth 89 in the knowledge graph ☐ I know this ☆ Set as goal
482prerequisites beneath it
See this on the map →
Dynamic ProgrammingArrays and Lists+2 more→
memoization tabulation top-down bottom-up DP-implementation

Core Idea

Memoization (top-down DP) augments a recursive solution with a cache so each subproblem is solved only once; it is natural to implement but uses the call stack. Tabulation (bottom-up DP) fills a table iteratively from base cases, avoiding recursion entirely. Both achieve the same asymptotic complexity but differ in which subproblems are computed — memoization computes only subproblems needed for the query, while tabulation computes all subproblems in a fixed order. Tabulation also enables space optimizations by discarding rows of the DP table that are no longer needed.

How It's Best Learned

Implement both approaches for the same 2-3 canonical problems: Fibonacci, coin change, and longest common subsequence. Verify identical results and compare space usage. Then optimize the tabulation version to use O(n) space instead of O(n²) for LCS.

Common Misconceptions

Explainer

Dynamic programming solves problems by reusing solutions to overlapping subproblems, but the way you organize that reuse leads to two fundamentally different implementation strategies. Memoization (top-down) starts from the original problem and recurses downward, caching each subproblem's result the first time it is computed. Tabulation (bottom-up) starts from the smallest base cases and iteratively builds up to the answer. Both eliminate redundant computation, but they differ in control flow, memory usage, and practical tradeoffs.

Consider computing the nth Fibonacci number. A naive recursive implementation recomputes F(3) exponentially many times when calculating F(50). With memoization, you wrap the same recursive function with a lookup — typically a hash map or array. Before computing F(k), check the cache; if the result is there, return it immediately. Otherwise compute it recursively, store the result, and return. The recursion tree that would have exploded into billions of calls collapses to exactly n unique subproblems, each solved once. The structure of your original recursion stays untouched — you are just adding a memory layer on top.

Tabulation takes the opposite approach. You allocate an array of size n, set the base cases (F(0) = 0, F(1) = 1), and fill forward: F(i) = F(i-1) + F(i-2) for each i from 2 to n. There is no recursion, no call stack, and no hash map overhead. Because you control the iteration order explicitly, you can see that each entry depends only on the two previous entries — so you can drop the full array and keep just two variables, reducing space from O(n) to O(1). This kind of space optimization is tabulation's signature advantage. In two-dimensional DP problems like longest common subsequence, the same logic lets you reduce an O(mn) table to two O(n) arrays, since each row depends only on the row before it.

The tradeoff between the two strategies is practical, not theoretical. Memoization is easier to write when the recurrence is complex or the subproblem space is sparse — it naturally avoids computing subproblems that are never needed. Tabulation is preferred when every subproblem will be visited anyway, when you want to eliminate recursion depth limits (Python's default stack, for example, caps at around 1000 frames), or when space optimization matters. In interviews and competitive programming, memoization gets you a correct solution faster; in production systems processing large inputs, tabulation with space optimization is usually the better engineering choice. Mastering both lets you pick the right tool depending on the constraints you face.

Practice Questions 5 questions

Prerequisite Chain

Understanding Zero → The Number Zero → Counting to Five → Counting to 10 → Counting to 20 → Counting a Set of Objects Up to 20 → Cardinality: The Last Number Counted → Matching Numerals to Quantities → Subitizing Small Quantities → Addition Within 10 → Number Bonds to 10 → Addition Within 20 → Doubles and Near Doubles → Doubles Facts Within 10 → Near Doubles Facts Within 20 → Mental Math Strategies for Addition → Mental Math: Adding and Subtracting Tens → Addition Within 100 → Repeated Addition as Multiplication → Multiplication as Equal Groups → Multiplication: Arrays → Basic Multiplication Facts (0s, 1s, 2s, 5s, 10s) → Multiplication Facts Within 100 → Division as Equal Sharing → Division as Grouping (Measurement Division) → Division: Grouping (Repeated Subtraction) Model → Division: Fair Sharing Model → Division as Equal Sharing → Division as Grouping → Basic Division Facts → Division Facts Within 100 → Multiplication and Division Fact Families → Relationship Between Multiplication and Division → Division Facts as Inverse of Multiplication → Remainders and Quotients in Division → Division Word Problems → Multi-Step Word Problems → Solving Multi-Step Word Problems → Multiplication Word Problems → Division Word Problems → Introduction to Long Division → Factors and Multiples → Prime and Composite Numbers → Equivalent Fractions → Relating Fractions and Decimals → Decimal Place Value → Integers and the Number Line → Comparing and Ordering Integers → Absolute Value → Adding Integers → Subtracting Integers → Multiplying Integers → Introduction to Exponents → Order of Operations → Integer Order of Operations → Variable Expressions → The Distributive Property → Variables and Expressions Review → Introduction to Polynomials → Adding and Subtracting Polynomials → Multiplying Polynomials → Factorial → Permutations → Combinations → Counting Principles: Addition and Multiplication Rules → Introduction to Graph Theory → Propositional Logic Foundations → Logical Equivalences → Boolean Algebra → Boolean Type and Truth Values → Comparison Operators and Boolean Tests → Logical Operators and Boolean Algebra → Conditional Statements → Defining and Calling Functions → Functions: Decomposing Problems → Function Parameters and Argument Passing → Return Values → Variable Scope → Introduction to Classes → Objects and Instances → Methods and Attributes → Algorithm Design Basics → Tree Structure and Node Properties → Binary Trees → Tree Traversals → Depth-First Search (DFS) → Depth-First Search: Implementation and Applications → Topological Sort → Dynamic Programming → Memoization and Tabulation

Longest path: 90 steps · 482 total prerequisite topics

Prerequisites (4)

Leads To (0)

No topics depend on this one yet.