Tabulation
25 min · Intermediate
Need help? Ask a tutor on WhatsApp (opens in a new tab)Tabulation iteratively computes dynamic programming tables from foundational base cases up to the desired target state.
The bottom-up table filling paradigm
Tabulation fills a table of subproblem answers from the smallest up to the full problem with loops, without recursion (bottom-up DP).
Tabulation eliminates recursive call stacks by computing subproblems iteratively. Instead of starting from target n and recursing down, tabulation begins at base cases. It fills an array sequentially from left to right.
Step by step
Step 1 of 5
- idx
- 1
- val
- 1
Example 1 · Bottom-Up Tabulated Fibonacci
public class TabulationFibonacci { public static int fib(int n) { if (n <= 1) return n; int[] dp = new int[n + 1]; dp[0] = 0; dp[1] = 1; for (int i = 2; i <= n; i++) { dp[i] = dp[i - 1] + dp[i - 2]; } return dp[n]; }
public static void main(String[] args) { System.out.println("Fib(5): " + fib(5)); System.out.println("Fib(8): " + fib(8)); System.out.println("Fib(12): " + fib(12)); }}Fib(5): 5 Fib(8): 21 Fib(12): 144
What this code does
- The array dp allocates n + 1 cells to hold all subproblem values from 0 to n.
- Base cases dp[0] and dp[1] seed the iteration before the loop begins.
- Each iteration computes the next value from the two preceding array cells.
Example 2 · Bottom-Up Min Cost Climbing Stairs
public class MinCostClimbingTabulation { public static int minCost(int[] cost) { int n = cost.length; int[] dp = new int[n + 1]; dp[0] = 0; dp[1] = 0; for (int i = 2; i <= n; i++) { dp[i] = Math.min(dp[i - 1] + cost[i - 1], dp[i - 2] + cost[i - 2]); } return dp[n]; }
public static void main(String[] args) { int[] cost1 = {10, 15, 20}; System.out.println("Min cost 1: " + minCost(cost1)); int[] cost2 = {1, 100, 1, 1, 1, 100, 1, 1, 100, 1}; System.out.println("Min cost 2: " + minCost(cost2)); }}Min cost 1: 15 Min cost 2: 6
What this code does
- Reaching floor 0 or floor 1 incurs zero cost because you can start on either.
- Reaching step i chooses the cheaper transition between step i - 1 and step i - 2.
- Outputs 15 and 6 demonstrate optimal multi-step paths through the cost arrays.
Every cell in the table represents the solution to a specific subproblem. Before computing cell i, all prerequisite values must already exist in the table. This guarantees that each transition executes in O(1) time. Bottom-up iteration produces superior CPU memory throughput. Sequential array access leverages hardware prefetching and CPU cache lines. Programs avoid the overhead of function call dispatch and stack allocation.
Determining dependency ordering and loop directions
The core challenge in tabulation is identifying the correct traversal direction. Subproblems form a directed acyclic graph of dependencies. Your loops must visit nodes in a valid topological order.
Example 3 · Coin Change Fewest Coins Table
import java.util.Arrays;
public class CoinChangeTabulation { public static int coinChange(int[] coins, int amount) { int max = amount + 1; int[] dp = new int[amount + 1]; Arrays.fill(dp, max); dp[0] = 0;
for (int i = 1; i <= amount; i++) { for (int coin : coins) { if (coin <= i) { dp[i] = Math.min(dp[i], dp[i - coin] + 1); } } } return dp[amount] > amount ? -1 : dp[amount]; }
public static void main(String[] args) { int[] coins = {1, 2, 5}; System.out.println("Coins for 11: " + coinChange(coins, 11)); System.out.println("Coins for 3: " + coinChange(coins, 3)); int[] coins2 = {2}; System.out.println("Coins for 3 with [2]: " + coinChange(coins2, 3)); }}Coins for 11: 3 Coins for 3: 2 Coins for 3 with [2]: -1
What this code does
- The table initializes with an unreachable sentinel value amount + 1.
- dp[0] = 0 seeds the table, and each dp[i] reads a smaller amount dp[i - coin], so the loop runs forward.
- Amounts 11 and 3 yield 3 and 2 coins, while impossible amount 3 returns -1.
When state i depends on smaller indices, loop forward from smallest to largest. When state i depends on larger indices, loop backward from largest to smallest. Reversing loop direction without updating dependencies produces uninitialized or incorrect values. Always verify table boundaries and sizing before writing loops. An array for target n often requires n + 1 elements. Miscounting array sizing causes ArrayIndexOutOfBoundsException on edge inputs.
Fix the error
Fix the table size
The program crashes. Choose the replacement for line 2.
climb(5) should return 8, but it crashes. Choose the line that fixes it.
Error
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 5 out of bounds for length 5
Tabulation versus memoization trade-offs
Tabulation and memoization achieve identical asymptotic time complexities. However, their runtime performance profiles differ in important ways. Tabulation runs faster in practice because it avoids call stack operations.
Tabulation eliminates all risks of StackOverflowError on deep recursions. In addition, modern compilers optimize iterative loops through vectorization and unrolling. Space optimization techniques also apply more directly to bottom-up tables.
However, tabulation computes every table entry regardless of necessity. If only a small subset of states is reachable, memoization can run faster. Choose memoization for sparse state graphs and tabulation for dense state domains.
Next step
Check your understanding
10 quick questions