DSA Notes
Master the bottom-up DP approach with iterative table filling, understand computation order, and learn space optimization techniques.
What is Tabulation?
Tabulation is the iterative approach to DP. Instead of starting with the big problem and recursing down (top-down), you start with the smallest subproblems and build up to the answer. You fill a table (array) from the base cases forward, ensuring that when you compute dp[i], all values it depends on (dp[i-1], dp[i-2], etc.) are already computed.
The Pattern
Fibonacci: Tabulation vs Memoization
Both return the same result. Both are O(n) time, O(n) space. But tabulation has no recursion overhead and no stack overflow risk.
Space Optimization: The Key Advantage of Tabulation
With tabulation, you can often see that dp[i] only depends on the previous 1-2 values. This means you do not need the entire table — just a few variables!
This "rolling variable" technique is unique to tabulation. You cannot do this with memoization because recursive calls access arbitrary past states.
2D Tabulation Example: Unique Paths
Problem: Grid of m×n. Start at top-left, end at bottom-right. Can only move right or down. How many unique paths?
The insight: row i only depends on row i-1. So we only keep two rows at a time, reducing space from O(m×n) to O(n).
Determining Computation Order
The fill order must respect dependencies. Common patterns:
1D, left to right: dp[i] depends on dp[i-1], dp[i-2], etc.
2D, row by row: dp[i][j] depends on dp[i-1][...] and dp[i][j-1]
2D, diagonal (gap-based): dp[i][j] depends on dp[i+1][j-1], dp[i][j-1] (interval DP)
C++ Tabulation Example
Space Optimization Patterns
1D → O(1): When dp[i] depends only on fixed number of previous states
2D → O(n): When dp[i][j] only depends on current and previous row
The reverse iteration trick is crucial for 0/1 knapsack: iterating forward would use the updated (current row) values, effectively allowing items to be picked multiple times.
Tabulation vs Memoization Summary
| Aspect | Memoization (Top-Down) | Tabulation (Bottom-Up) |
|---|---|---|
| Style | Recursive | Iterative |
| Order | Determined by recursion | Must define explicitly |
| Space optimization | Difficult | Natural (rolling array) |
| Stack overflow | Risk for deep recursion | No risk |
| Subproblem coverage | Only solves needed subproblems | Solves all subproblems |
| Debugging | Harder (recursive stack) | Easier (print table) |
| Code style | More intuitive for some | More efficient typically |
Practical Tips
- Start with memoization to verify correctness, then convert to tabulation for optimization
- Always check dependencies before choosing fill order
- Space optimization is often the difference between AC and MLE in contests
- Print the DP table during debugging to verify transitions
- For 2D DP reduced to 1D, watch out for iteration direction (forward vs reverse)
Exam Focus
Revise definitions, diagrams, examples, and short-answer points for Tabulation (Bottom-Up Dynamic Programming).
Interview Use
Prepare one clear explanation, one practical example, and one common mistake for this Data Structures & Algorithms topic.
Search Terms
data-structures-algorithms, data structures & algorithms, data, structures, algorithms, dynamic, programming, tabulation
Related Data Structures & Algorithms Topics