What is tabulation?
- Algorithms course
- Advanced
- Lesson 5/56
- 4 min
Fill a table bottom-up to solve sub-problems.
What is tabulation?
Tabulation solves subproblems by filling a table bottom-up.
Loops replace recursion; each cell is a subproblem's result.
Tabulation is the bottom-up counterpart of memoization; no stack overflow risk.
Bottom-up approach
Tabulation solves the smallest subproblems first, then uses them to build larger ones.
There is no recursion; all computation happens in a loop.
This approach prevents stack overflow on deep recursion; it suits large n values.
Practice Fibonacci tabulation
Write fib_tab(n). Compute Fibonacci with a table. Print 55 with print(fib_tab(10)).
Starter code
def fib_tab(n):
# Fibonacci tabulationSample solution
def fib_tab(n):
# Fibonacci tabulation
if n <= 1:
return n
dp = [0] * (n + 1)
dp[1] = 1
for i in range(2, n + 1):
dp[i] = dp[i - 1] + dp[i - 2]
return dp[n]
print(fib_tab(10))Expected output
55
Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 Which direction does tabulation work in?
- Top-down
- Bottom-up Correct answer
- From sides
- Random
Explanation: Bottom-up.
2 Table structure
dp = [0] * (____ + 1)
dp = [0] * (n + 1)
Explanation: n is right.
3 What is tabulation: match the terms
| Term | Meaning |
|---|---|
tabulation |
Bottom-up |
table |
Table |
loop |
Loop |
no recursion |
No recursion |