Tabulation (bottom-up DP) — iterative
3.7.8· Coding › Algorithm Paradigms
WHAT hai tabulation?
YEH KAAM KYU KARTA HAI? Ek DP problem mein optimal substructure hoti hai (answer sub-answers se banta hai) aur overlapping subproblems hote hain (same sub-answers baar baar chahiye hote hain). Agar hum har subproblem ko ek aise order mein ek baar compute karein jahan uski dependencies pehle se done hon, toh hum kabhi recompute nahi karte aur kabhi recurse nahi karte.
HOW banayein koi bhi tabulation (the recipe)
Canonical example derive karna: Fibonacci
Hame chahiye jahan .
Step 1 — State. dp[i] = -va Fibonacci number.
Step 2 — Recurrence. Definition se .
Yeh step kyu? Har Fibonacci number literally pehle wale do ka sum hota hai — yahi optimal substructure IS.
Step 3 — Base cases. dp[0]=0, dp[1]=1.
Step 4 — Order. Increasing : jab hum dp[i] compute karte hain, dp[i-1] aur dp[i-2] dono pehle se exist karte hain. ✅
Step 5 — Answer. dp[n].
def fib(n):
if n < 2: return n
dp = [0]*(n+1)
dp[1] = 1
for i in range(2, n+1):
dp[i] = dp[i-1] + dp[i-2] # read smaller, already filled
return dp[n]
Worked example 2 — Climbing Stairs (paths count karna)
Tum stairs chadh rahe ho, ek baar mein 1 ya 2 steps lete ho. Kitne distinct ways hain?
State. dp[i] = step tak pahunchne ke ways ki sankhya.
Recurrence. Step pe land karne ke liye, aakhri move ya toh 1-step tha ( se) ya 2-step ( se):
Yeh step kyu? Ways ka set last action ke basis pe clearly split hota hai; dono cases disjoint hain, isliye hum unhe add karte hain.
Base. dp[0]=1 (ek way: khadhe raho), dp[1]=1.
Answer. dp[n].
def climb(n):
dp = [0]*(n+1)
dp[0] = dp[1] = 1
for i in range(2, n+1):
dp[i] = dp[i-1] + dp[i-2]
return dp[n]Fibonacci jaisi hi shape hai — bahut saare DP problems ek hi skeleton share karte hain.
Worked example 3 — 0/1 Knapsack (2D table)
Items hain weights , values ke saath, capacity hai. Value maximize karo, har item zyada se zyada ek baar use ho.
State. dp[i][c] = pehle items aur capacity ke saath best value.
Recurrence. Item ke liye (1-indexed, weight , value ):
Yeh step kyu? Har item ya toh andar hai ya bahar — yahi binary choice substructure hai. Agar hum lete hain, capacity se ghatti hai aur value se badhti hai, ek chhota subproblem dp[i-1][c-w_i] bachta hai.
Base. dp[0][c]=0 (koi items nahi → koi value nahi).
Order. Increasing (rows), pe koi bhi order, kyunki har read row use karta hai jo poori tarah done hai.
def knapsack(w, v, W):
n = len(w)
dp = [[0]*(W+1) for _ in range(n+1)]
for i in range(1, n+1):
for c in range(W+1):
dp[i][c] = dp[i-1][c] # skip item i
if w[i-1] <= c:
dp[i][c] = max(dp[i][c],
v[i-1] + dp[i-1][c-w[i-1]]) # take it
return dp[n][W]Common mistakes (steel-manned)
Recall Feynman: ek 12-saal ke bacche ko explain karo
Socho LEGO tower banana hai. 4tha brick lage bina 5va nahi rakh sakte. Toh neeche se shuru karo: brick 1 rakhna, phir 2 brick 1 use karke, phir 3 brick 2 use karke… Har brick yaad rakhta hai neeche kya hai, isliye tum kabhi kuch dobara nahi banate. Tabulation exactly yahi hai: chhote answers boxes mein likhte jao, phir unke upar bade answers stack karte jao — ek loop, koi magic nahi, koi peeche jaana nahi.
Active recall
Tabulation DP ki kaun si direction hai?
Tabulation recursion overhead kyu avoid karta hai?
DP/tabulation ke liye ek problem mein kaunsi do properties honi chahiye?
Table kis order mein fill karna chahiye?
Fibonacci tabulation recurrence aur base cases?
Climbing stairs recurrence — yeh max ki jagah sum kyu hai?
0/1 knapsack take-vs-skip recurrence?
Space-optimized 1D knapsack mein capacity high→low kyu loop karte hain?
Memoization aur tabulation mein kya farq hai?
Tabulation table mein final answer usually kahan hota hai?
Connections
- Memoization (top-down DP) — is note ka recursive dual
- Dynamic Programming — parent paradigm
- Optimal Substructure aur Overlapping Subproblems — prerequisites
- 0-1 Knapsack, Longest Common Subsequence, Coin Change — classic tabulation problems
- Space Optimization in DP — 2D tables se rolling arrays
- Recurrence Relations — recurrence step ke neeche ka math