Lesson 10 — Intermediate

Recurrence Relations

Recurrence mein kisi term ko pichhli terms se likhte hain — jaise Fibonacci aur Tower of Hanoi. Is lesson mein seekhenge common patterns kaise pehchante hain, iteration method se closed form kaise nikalte hain, aur characteristic equation se linear recurrence kaise solve karte hain. Ye GATE ka fixed topic hai.

⏱ 16 min• Intermediate• Pigeonhole

Recurrence relation kya hai

WHAT

Recurrence relation mein kisi term ko pichhli terms se likha jaata hai — jaise a_n = a_(n−1) + a_(n−2). Iske saath initial conditions (a_0, a_1) chahiye — tabhi sequence fix hoti hai.

WHEN

Recursive algorithms (divide & conquer), Fibonacci, Tower of Hanoi, aur stair-climbing jaise puzzles — jab "agla pichhla se bane" ho tab recurrence.

WHERE

Algorithm analysis (T(n) = 2T(n/2) + n jaisa), programming recursion, aur GATE ka ek fixed question — recurrence solve karke closed form nikalna.

Recurrence solve ka matlab hai closed form nikalna — a_n ka direct formula jo bina pichhli terms ke chal sake. Ye recursion ko formula mein badalna hai, jise induction se verify bhi kar sakte hain.

Common patterns (pehchano, GATE shortcut)

Common patterns (pehchano, answer turant):

T(n) = T(n − 1) + c    → linear   → T(n) = cn + T(0)
T(n) = aT(n − 1)       → geometric → a^n
T(n) = 2T(n − 1) + 1, T(1) = 1 → Hanoi → 2^n − 1
T(n) = T(n/2) + c      → divide   → O(log n)
T(n) = 2T(n/2) + 1, T(1) = 1 → 2n − 1 → O(n)
T(n) = 2T(n/2) + n     → merge sort → O(n log n)

Multiply wala recurrence = geometric series (a^n)
Add wala recurrence     = arithmetic (cn)
Divide wala (n/2)       = log n
Shortcut: GATE mein recurrence dekho aur pattern pehchano — 2T(n−1) = 2^n, T(n/2) = log n, 2T(n/2)+n = n log n. Numericals 10 second mein solve ho jaate hain. Par har baar chhota sa verify bhi kar lo (n=1,2 daal kar).

Iteration method — Tower of Hanoi solve

Sabse simple method — iteration (baar baar substitute karo). Isse Tower of Hanoi ka closed form nikalte hain:

T(n) = 2T(n − 1) + 1,   T(1) = 1

T(n) = 2T(n − 1) + 1
T(n) = 2[2T(n − 2) + 1] + 1 = 2²T(n − 2) + 2 + 1
T(n) = 2³T(n − 3) + 4 + 2 + 1
  ...
T(n) = 2^(n−1) T(1) + (1 + 2 + ... + 2^(n−2))
     = 2^(n−1) · 1 + (2^(n−1) − 1)   [geometric sum]
     = 2^n − 1

Check: T(1) = 1 ✓,  T(2) = 3 ✓,  T(3) = 7 ✓
3 disks = 7 moves,  64 disks = 2^64 − 1 moves

Geometric sum 1 + 2 + 4 + ... + 2^(n−2) = 2^(n−1) − 1 — ye power ka off-by-one sabse common galti hai. Har step mein ek baar count karke dekh lo.

Characteristic equation method

Linear homogeneous recurrence with constant coefficients ka standard method — characteristic equation banao, roots nikalo:

a_n = 3a_(n−1) − 2a_(n−2),   a_0 = 0, a_1 = 1

Step 1: assume a_n = r^n
        r^n = 3r^(n−1) − 2r^(n−2)
Step 2: divide r^(n−2)  →  r² = 3r − 2
Step 3: r² − 3r + 2 = 0  →  (r − 1)(r − 2) = 0
        roots r = 1, 2
Step 4: general solution a_n = A(1)^n + B(2)^n = A + B·2^n
Step 5: initial conditions se A, B nikalo
        a_0 = A + B = 0   ⇒ A = −B
        a_1 = A + 2B = 1  ⇒ B = 1, A = −1
        ⇒ a_n = 2^n − 1

Verify: a_2 = 3a_1 − 2a_0 = 3 = 2² − 1 ✓
Distinct roots wala rule: Do distinct real roots r1, r2 → a_n = A(r1)^n + B(r2)^n. Repeated root r → a_n = (A + Bn)r^n. GATE mein distinct root wale hi aate hain — bas initial conditions se A, B nikalna aana chahiye.

Exercise

Question: Tower of Hanoi mein 3 disks ke liye kitne minimum moves? Sirf number daalo.

Question: Fibonacci: F(1)=1, F(2)=1. F(6) kitna hai? Sirf number daalo.

Question: Recurrence ka general solution fix karne ke liye kya chahiye? (initial conditions / sirf equation / kuch nahi)

Common mistakes

Desi example: Teesri manzil ki seedhiyan

Mess teesri manzil pe hai. Har waqt mein 1 ya 2 step chadha ja sakta hai — floor n tak kitne tarike? Yahi Fibonacci recurrence hai, khud count karke dekho:

text
T(n) = T(n − 1) + T(n − 2)   (1-step + 2-step wale raste)
T(1) = 1,   T(2) = 2
T(3) = 3,   T(4) = 5,   T(5) = 8
Har floor pichhle do ka sum — Fibonacci (1,1,2,3,5,8...)
Exam mapping (GATE / Algorithm interviews)
  • GATE: recurrence solve — iteration aur characteristic dono method se aata hai.
  • Algorithm analysis: T(n) ka order nikalna (master theorem se pehle yahi).
  • Interview: "Tower of Hanoi mein 10 disks pe kitne moves?" — 2^10 − 1 = 1023.
  • Trap: "T(n) = 2T(n−1) + 1 linear hai" — bilkul, par uska closed form 2^n − 1 exponential time deta hai.
Lesson complete?

Module 2 complete! Recurrence se closed form nikalna aa gaya. Ab Module 3 — Graph Theory — jisme networks, trees aur connections ki poori duniya hai.

Popular courses: Data Science Course in Lucknow · AI Course in Lucknow · Python Course in Lucknow · Power BI Course · Data Analytics Course