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 ZeroThe Number ZeroCounting to FiveCounting to 10Counting to 20Counting a Set of Objects Up to 20Cardinality: The Last Number CountedMatching Numerals to QuantitiesSubitizing Small QuantitiesAddition Within 10Number Bonds to 10Addition Within 20Doubles and Near DoublesDoubles Facts Within 10Near Doubles Facts Within 20Mental Math Strategies for AdditionMental Math: Adding and Subtracting TensAddition Within 100Repeated Addition as MultiplicationMultiplication as Equal GroupsMultiplication: ArraysBasic Multiplication Facts (0s, 1s, 2s, 5s, 10s)Multiplication Facts Within 100Division as Equal SharingDivision as Grouping (Measurement Division)Division: Grouping (Repeated Subtraction) ModelDivision: Fair Sharing ModelDivision as Equal SharingDivision as GroupingBasic Division FactsDivision Facts Within 100Multiplication and Division Fact FamiliesRelationship Between Multiplication and DivisionDivision Facts as Inverse of MultiplicationRemainders and Quotients in DivisionDivision Word ProblemsMulti-Step Word ProblemsSolving Multi-Step Word ProblemsMultiplication Word ProblemsDivision Word ProblemsIntroduction to Long DivisionFactors and MultiplesPrime and Composite NumbersEquivalent FractionsRelating Fractions and DecimalsDecimal Place ValueIntegers and the Number LineComparing and Ordering IntegersAbsolute ValueAdding IntegersSubtracting IntegersMultiplying IntegersIntroduction to ExponentsOrder of OperationsInteger Order of OperationsVariable ExpressionsThe Distributive PropertyVariables and Expressions ReviewIntroduction to PolynomialsAdding and Subtracting PolynomialsMultiplying PolynomialsFactorialPermutationsCombinationsCounting Principles: Addition and Multiplication RulesIntroduction to Graph TheoryPropositional Logic FoundationsLogical EquivalencesBoolean AlgebraBoolean Type and Truth ValuesComparison Operators and Boolean TestsLogical Operators and Boolean AlgebraConditional StatementsDefining and Calling FunctionsFunctions: Decomposing ProblemsFunction Parameters and Argument PassingReturn ValuesVariable ScopeIntroduction to ClassesObjects and InstancesMethods and AttributesAlgorithm Design BasicsTree Structure and Node PropertiesBinary TreesTree TraversalsDepth-First Search (DFS)Depth-First Search: Implementation and ApplicationsTopological SortDynamic ProgrammingMemoization and Tabulation

Longest path: 90 steps · 482 total prerequisite topics

Prerequisites (4)

Leads To (0)

No topics depend on this one yet.