3.7.8 · D4 · HinglishAlgorithm Paradigms

ExercisesTabulation (bottom-up DP) — iterative

2,816 words13 min read↑ Read in English

3.7.8 · D4 · Coding › Algorithm Paradigms › Tabulation (bottom-up DP) — iterative


Level 1 — Recognition

Tumhe pieces (state, base, order, answer) spot karne chahiye aur ek filled table padhni chahiye.


L1.1 — Table padho

Neeche diya Fibonacci table dekho, dp[6] kya hai? Yaad karo rule hai dp[i]=dp[i-1]+dp[i-2].

i 0 1 2 3 4 5 6
dp[i] 0 1 1 2 3 5 ?
Recall Solution

Rule kehta hai har box = usse pehle ke do boxes ka sum. KYA karte hain: dp[5] aur dp[4] add karo. KYU: recurrence dp[6]=dp[5]+dp[4] — dono pehle se filled hain, isliye hum inhe read kar sakte hain.


L1.2 — Direction batao

Ek student likhta hai: "Main dp[n] se start karta hun aur neechey recurse karta hun, results cache karte hue." Kya ye tabulation hai ya memoization?

Recall Solution

Bade se start karke neechey recurse karna = memoization (top-down). Dekho Memoization (top-down DP). Tabulation iska ulta hai: sabse chhote boxes se start karo aur loop karke upar jao. Dono har subproblem ko ek baar compute karte hain; sirf travel direction alag hoti hai.


L1.3 — Kaun si cell answer hold karti hai?

Climbing Stairs ke liye (dp[i] = step i tak pahunchne ke ways ki count) jab n = 4 ho, kaun si single cell final answer hai, aur base cases kya fill karta hai?

Recall Solution

Answer cell: sabse bada state, dp[4]. Base cases: dp[0]=1 (ek tarika — khade raho) aur dp[1]=1 (ek tarika — ek 1-step). Upar fill karte hue: dp[2]=2, dp[3]=3, dp[4]=5.


Level 2 — Application

Ab tum recipe apply karke scratch se chhote tables build karte ho.


L2.1 — n = 7 ke liye Fibonacci table

Poora table fill karo aur dp[7] batao.

Recall Solution

Seed: dp[0]=0, dp[1]=1. Loop small→large, har box = pichle do ka sum:

i 0 1 2 3 4 5 6 7
dp[i] 0 1 1 2 3 5 8 13


L2.2 — Coin Change (ways count karna)

Coins {1, 2}, target amount 5. dp[a] = amount a banane ke ways ki count (order matter nahi karta). Dekho Coin Change. Standard tabulation coins ko bahar loop karta hai, amounts andar: Table fill karo aur dp[5] batao.

Recall Solution

State: dp[a] = a summing karne wale combinations ki count. Base: dp[0]=1 (0 banane ka ek tarika — kuch mat lo). KYU coins bahar: combinations (permutations nahi) count karne ke liye, humein ek coin denomination ko poori tarah commit karna hoga next se pehle, taaki {1,2} aur {2,1} double-count na ho.

Start dp = [1,0,0,0,0,0].

Coin 1 process karo (har amount ko a-1 se ways milte hain): dp = [1,1,1,1,1,1]. Coin 2 process karo (har a≥2 ko a-2 se ways milte hain):

  • dp[2]+=dp[0]=1 → 2
  • dp[3]+=dp[1]=1 → 2
  • dp[4]+=dp[2]=2 → 3
  • dp[5]+=dp[3]=2 → 3

Final dp = [1,1,2,2,3,3], isliye . Teen tarike: 1+1+1+1+1, 1+1+1+2, 1+2+2. ✅


L2.3 — Minimum steps recurrence

dp[i] = coins {1,3,4} se amount i banane ke fewest coins. Recurrence do, phir dp[6] compute karo.

Recall Solution

Recurrence: har coin ko aakhri use kiya hua maano: KYU min, sum nahi: yahan hum ek quantity optimize kar rahe hain (fewest coins), isliye best last choice chunte hain — contrast karo L2.2 se jahan hum count karte hain aur isliye add karte hain.

Upar build karo:

i 0 1 2 3 4 5 6
dp[i] 0 1 2 1 1 2 2

dp[6] check karo: candidates 1+dp[5]=3, 1+dp[3]=2, 1+dp[2]=3. Minimum hai (yani 3+3).


Level 3 — Analysis

Order, dependencies, aur kyun ek table cell wahi value hold karti hai ke baare mein sochte hain.


L3.1 — 0/1 Knapsack grid trace karo

Items (w,v) = (1,1), (3,4), (4,5), capacity W=4. Use karke grid fill karo aur dp[3][4] batao. Dekho 0-1 Knapsack.

Figure — Tabulation (bottom-up DP) — iterative
Recall Solution

Rows = consider kiye gaye items (0 = koi nahi); columns = capacity c 0 se 4 tak. Row 0 sab zeros hai (koi item nahi → koi value nahi).

Row 1 — item (1,1): c≥1 par le sakte hain. dp[1][c]=max(0, 1+dp[0][c-1])=1. [0, 1, 1, 1, 1]

Row 2 — item (3,4): jab c≥3 ho tab le sakte hain.

  • c=0,1,2: bahut heavy → upar se copy karo → 0,1,1
  • c=3: max(dp[1][3]=1, 4+dp[1][0]=4)=4
  • c=4: max(dp[1][4]=1, 4+dp[1][1]=5)=5 [0, 1, 1, 4, 5]

Row 3 — item (4,5): sirf c=4 par le sakte hain.

  • c=0..3: upar se copy karo → 0,1,1,4
  • c=4: max(dp[2][4]=5, 5+dp[2][0]=5)=5 [0, 1, 1, 4, 5]

Do optimal packings value 5 par tie karte hain: sirf item 3, ya items 1+2 (1+4).


L3.2 — Rows large→small kyun loop nahi kar sakte?

Knapsack loop for i in 1..n mein, kya hum i ko n se 1 tak neeche loop kar sakte hain? Explain karo kya toot ta hai.

Recall Solution

Har entry dp[i][c] row i-1 padhta hai. Agar hum pehle row n fill karein, row n-1 abhi bhi sab zeros hai → hum garbage read karte hain. KYA toot ta hai: dependencies read ki jaati hain pehle unke exist karne se. Rule: iterate karo taaki har cell ke inputs pehle se filled hon. Kyunki dp[i] depends karta hai dp[i-1] par, humein small i → large i jaana hoga. Order recurrence ke arrows se dictate hota hai, taste se nahi.


L3.3 — Space-optimized 1D knapsack direction

Hum 2D grid ko ek row dp[0..W] mein collapse karte hain jo har item ke liye reuse hoti hai. Kya capacity c ko high→low ya low→high loop karna chahiye? Item (2,3), W=4 use karke ek chhota counterexample se apni choice prove karo.

Recall Solution

Sahi: high→low. Dekho Space Optimization in DP.

1D update hai dp[c] = max(dp[c], v + dp[c-w]), jahan dp[c-w] ka matlab previous row hona chahiye (item abhi use nahi hua).

Low→high (GALAT): start karo dp=[0,0,0,0,0], item (2,3).

  • c=2: dp[2]=max(0,3+dp[0])=3
  • c=4: dp[4]=max(0,3+dp[2])=3+3=6 ← item do baar use hua! (unbounded, 0/1 nahi)

High→low (SAHI):

  • c=4: dp[4]=max(0,3+dp[2]=3+0)=3
  • c=2: dp[2]=max(0,3+dp[0])=3
  • final dp=[0,0,3,0,3] → best value 3, item ek baar use hua. ✅

High→low jaana guarantee karta hai ki dp[c-w] is round mein abhi overwrite nahi hua, isliye woh abhi bhi previous row hold karta hai.


Level 4 — Synthesis

Ek naye problem ke liye recipe steps khud combine karke ek table design karo.


L4.1 — Longest Common Subsequence

Strings A = "ABCBDAB", B = "BDCAB". Define karo dp[i][j] = A ke pehle i letters aur B ke pehle j letters ki LCS ki length. Recurrence batao, phir dp[7][5] compute karo. Dekho Longest Common Subsequence.

Figure — Tabulation (bottom-up DP) — iterative
Recall Solution

State: dp[i][j] = A[0..i) aur B[0..j) ki LCS length. Base: dp[0][j]=dp[i][0]=0 (empty string kuch share nahi karta). Recurrence: KYU: agar aakhri letters match karte hain, toh woh ek common subsequence end kar sakte hain, isliye hum unse pehle ki sab cheez ki LCS lete hain aur 1 add karte hain. Agar nahi milte, toh do aakhri letters mein se kam se kam ek unused hai → dono drop karke dekho aur bada rakho.

Grid fill karte hue (rows = A ke letters, cols = B ke letters), final cell hai: Length 4 ka ek aisa LCS hai "BCAB" (ya "BDAB").


L4.2 — State khud choose karo

"Houses ki ek row par costs [2,7,9,3,1] diye hain, ek subset choose karo jisme koi do adjacent houses na hon, total maximize karo. n=5." dp define karo, recurrence do aur answer batao.

Recall Solution

State: dp[i] = no-two-adjacent rule ke under houses 0..i use karke best total. Recurrence: house i ke liye, ya toh use skip karo (dp[i-1]) ya use lo (cost[i] + dp[i-2], kyunki i-1 ab forbidden hai): Base: dp[0]=cost[0]=2, dp[1]=max(cost[0],cost[1])=7.

i 0 1 2 3 4
cost 2 7 9 3 1
dp 2 7 11 11 12
  • dp[2]=max(7, 9+2)=11
  • dp[3]=max(11, 3+7)=11
  • dp[4]=max(11, 1+11)=12


Level 5 — Mastery

Prove karo, generalise karo, ya ek subtle bug pakdo.


L5.1 — Base-case bug hunt

Ek student ka Coin Change (min coins) galat answers return kar raha hai. Unka code dp = [0]*(amount+1) initialise karta hai. Bug kya hai aur fix kya hai?

Recall Solution

Bug: unhone har cell ko 0 se initialise kiya, lekin sirf dp[0] hi 0 hona chahiye. Har doosri cell ko "impossible" = ek bada sentinel se start karna chahiye (jaise amount+1 ya float('inf')). Kyun sahi lagta hai: 0 natural empty value hai aur sahi dp[0] se match karta hai. Kya toot ta hai: recurrence dp[i]=1+min(dp[i-coin]) dusre cells padhta hai; agar woh 0 hain, toh min galat maanta hai ki koi bhi amount almost bina coins ke reachable hai, sab kuch poison karte hue. Fix: Ek cell jo end mein bhi hai matlab "amount unreachable". Base cases table ko seed karte hain; ek galat seed utna hi fatal hai jitna ek missing ek.


L5.2 — Prove karo ki recomputation nahi hoti

Argue karo kyun tabulation har subproblem ko exactly ek baar compute karta hai, naive recursion ke unlike.

Recall Solution

Naive recursion alag call paths se ek hi subproblem mein baar baar enter karta hai (Overlapping Subproblems property), isliye F(n) calls spawn kar sakta hai. Tabulation har cell ko single loop pass mein visit karta hai. Formally: outer loop index apni range mein har value ek baar leta hai; har cell exactly ek baar written hoti hai (apni loop iteration mein) aur uske baad sirf read hoti hai. Kyunki hum loop is tarah order karte hain ki dependencies cell se pehle aayein (Optimal Substructure guarantee karta hai ki dependencies chhote states hain), koi bhi cell dubara compute karne ke liye revisit nahi hoti. Result: total work = (cells ki count) × (har cell ki cost). Fibonacci ke liye woh hai vs exponential recursion.


L5.3 — Loop direction rule generalise karo

Ek single rule batao jo kisi bhi in-place DP ke liye safe iteration direction decide kare, covering both Fibonacci (koi bhi order safe jab do vars use ho) aur 1D knapsack (must go high→low).

Recall Solution

Rule: Iterate karo taaki har cell jo tum ek update ke dauran read karte ho, intended prior stage ki value hold kare — koi aisi value nahi jo is pass mein pehle se overwrite ho chuki ho.

  • Agar reads strictly earlier, never-overwritten cells reference karte hain (rolling vars ke saath Fibonacci) → direction free hai.
  • Agar ek read dp[c-w] aur write dp[c] us same array mein hain jo is round mein overwrite ho raha hai, woh direction choose karo jo read location ko write location ke baad visit kare, taaki read abhi bhi purani value dekhe. Knapsack ke liye, c-w < c, isliye high→low likhna dp[c-w] ko baad tak untouched rakhta hai. ✅

Ye exactly Space Optimization in DP safety condition hai, aur ye parent note ki har galti ko cover karta hai.


Recall Feynman recap

Yahan har problem wahi chaar moves thi: box ka naam batao (state), fill rule likho (recurrence), woh boxes haath se fill karo jahan koi rule nahi pahunchi (base), aur loop karo taaki ek box ke ingredients use karne se pehle ready hon (order). Counting problems add karte hain; best/least problems min/max lete hain; in-place tricks loop direction se jeeti ya harti hain. Yahi poora ladder hai.

Connections

  • Tabulation (bottom-up DP) — iterative — parent topic
  • Coin Change, 0-1 Knapsack, Longest Common Subsequence — yahan drill kiye gaye problems
  • Space Optimization in DP — L3.3 aur L5.3 direction rule
  • Optimal Substructure, Overlapping Subproblems — L5 proofs mein invoke kiye gaye
  • Recurrence Relations — add-vs-min reasoning
  • Memoization (top-down DP) — L1.2 mein contrast kiya gaya
  • Dynamic Programming — the paradigm