3.7.2 · D3 · HinglishAlgorithm Paradigms

Worked examplesDivide and conquer — template, correctness, recurrence

3,881 words18 min read↑ Read in English

3.7.2 · D3 · Coding › Algorithm Paradigms › Divide and conquer — template, correctness, recurrence

Yeh page ek drill hai. Parent note ne machinery banayi thi — template, recurrence , aur Master Theorem. Yahan hum us machinery par har tarah ka input daalte hain aur answer nikalte hain, taaki jab exam ya koi real problem tumhe ek recurrence de, tum uski shape pehle se dekh chuke ho.

Shuru karne se pehle, ek reminder ki har letter ka kya matlab hai, kyunki hum ise re-derive nahi karenge — hum ise har baar use karenge:

Recall Woh numbers jo tum hamesha pehle padhte ho

Question ::: mein , , kya hain? ::: kitne subproblems har call spawn karta hai ::: woh factor jisse input shrink hota hai (size ho jaati hai) ::: non-recursive work — divide cost plus combine cost ::: derived watershed exponent , yaani leaves ki cost ( se compute hota hai, read off nahi)

Poora game yeh hai: compute karo, phir ko se compare karo. Teen outcomes possible hain — chhota, barabar, bada — aur kuch degenerate recurrences bhi hain jahan yeh comparison apply hi nahi hoti. Yahi hamara matrix hai.


The scenario matrix

Har divide-and-conquer recurrence jo tum miloge, in cells mein se ek mein fit hogi. Columns hain watershed ke relative kaise hai; rows hain special / degenerate inputs jo naive routine ko tod dete hain.

Cell Ise yeh cell kya banata hai Example jo hum solve karte hain
A — Balanced (Case 2) : divide-cost leaf-cost se match karta hai Ex 1 (Mergesort), Ex 8 (word problem)
B — Leaf-heavy (Case 1) se polynomially chhota hai Ex 2 (Karatsuba)
C — Root-heavy (Case 3) polynomially bada hai, plus regularity hold karta hai Ex 3
D — Single subproblem () tree ek chain hai, Ex 4 (Binary Search)
E — Uneven / degenerate split subproblem sizes differ karte hain, ya ek size hai; shrink constant factor nahi hai (), toh floors/ceilings ko wave away nahi kiya ja sakta Ex 5 (bad Quicksort pivot)
F — Basic Master Theorem fails sirf ek log factor watershed se off hai — extended rule ya tree chahiye Ex 6
G — Regularity fails root-heavy lagta hai lekin Ex 7
H — Exam twist shrink / disguised recurrence Ex 8 (word problem), Ex 9

Ab hum har cell ko hit karte hain.


Cell A — Balanced (Master Theorem Case 2)

Figure — Divide and conquer — template, correctness, recurrence

Figure padhne ka tarika: har row recursion ka ek level hai. Black dots subproblems hain; right par red label unka total work dikhata hai. Notice karo punchline — haalaanki dots har level double hote hain aur half shrink hote hain, unki widths cancel ho jaati hain, toh har red label padhta hai. rows hain (left par black double-arrow), aur work per row times rows exactly wahi hai jo humne compute kiya.


Cell B — Leaf-heavy (Master Theorem Case 1)


Cell C — Root-heavy (Master Theorem Case 3)

Figure — Divide and conquer — template, correctness, recurrence

Figure padhne ka tarika: har bar ek recursion level par total work hai, ki units mein. Ex 1 ke flat profile ke unlike, yahan bars geometrically shrink karte hain — har ek pichle ka half hai. Depth par tall red bar root hai, aur kyunki bars itni fast girti hain, poora tower bas us pehle bar ko sum karta hai. "Root dominates" yaise dikhta hai, aur isliye answer hai, sirf root ki cost.


Cell D — Single subproblem ()

Figure — Divide and conquer — template, correctness, recurrence

Figure padhne ka tarika: ki wajah se picture ek single vertical stack hai, branching tree nahi — har level par ek dot, har ek apni shrinking size () aur constant work ke saath labelled. Neeche red dot base case hai, size . Black double-arrow rungs count karta hai: of them, har ek karta hai, toh total hai.


Cell E — Uneven / degenerate split


Cell F — Basic Master Theorem fails (log-factor gap)


Cell G — Regularity fails (sirf shape se root-heavy)


Cell A / H — Real-world word problem


Cell H — Exam twist ( shrink)


Recall Self-test

solves to? ::: , tie karta hai → Case 2 → solves to? ::: Master-able nahi (); unroll karo → Tournament kyun deta hai nahi? ::: depth hai, lekin total work (leaves) dominate karta hai → matches

Related paradigms is same recurrence machinery par build karte hain: Strassen Matrix Multiplication (Karatsuba jaisi Case-1 win), Mathematical Induction (correctness engine), aur Dynamic Programming (jab subproblems overlap karte hain independent hone ki jagah, tab ise use karo).