Skip to main content

DSAPath13 minintermediate

Memoization vs Tabulation

Memoization is top-down dynamic programming with a cache; tabulation is bottom-up dynamic programming that fills states in dependency order.

DifficultyIntermediate
TierTier 1
ModuleComparisons
LanguagesPython

Why This Matters

Memoization and tabulation solve many of the same dynamic programming problems, but they shape the code differently. The right choice depends on state reachability, recursion depth, and fill order.

Decision Rule

Use memoization when the recursive definition is clear and only some states may be reached. Use tabulation when you know the dependency order and want iterative control over memory and stack usage.

Comparison Table

FeatureMemoizationTabulation
Directiontop-downbottom-up
Control flowrecursion plus cacheloops over table
Computes unreachable statesoften avoids themoften fills all planned states
Stack riskrecursion depthno recursion stack
Common fittree-shaped choices, sparse statesgrid DP, sequence DP, memory optimization

Worked Example

For Fibonacci, memoization starts with fib(n) and recursively asks for fib(n-1) and fib(n-2), caching answers.

Tabulation starts with fib(0) and fib(1), then fills fib(2), fib(3), up to fib(n).

Both remove repeated exponential work. They differ in direction and implementation shape.

Common Mistakes

MistakeCorrection
Thinking memoization and DP are separate topics.Memoization is a top-down DP technique.
Forgetting base cases in memoized recursion.The cache does not replace the recurrence definition.
Filling tabulation states before dependencies exist.Bottom-up DP needs a valid dependency order.

Diagnostic Questions

QuestionAnswer signal
Which style uses recursion?Memoization.
Which style is usually easier to memory-optimize with rolling arrays?Tabulation.
Why can memoization be better for sparse state spaces?It computes only states reached by recursive calls.

Exercises

  • Beginner: Write memoized Fibonacci in Python.
  • Beginner: Write tabulated Fibonacci in Python.
  • Beginner: Explain why naive recursive Fibonacci repeats work.
  • Intermediate: Convert a memoized grid-path recurrence to a table.
  • Intermediate: Identify a valid fill order for edit distance.
  • Challenge: Optimize a tabulated 2D DP to one row and explain the dependency condition.

References