Exercises — Divide and conquer — template, correctness, recurrence
3.7.2 · D4· Coding › Algorithm Paradigms › Divide and conquer — template, correctness, recurrence
Shuru karne se pehle, woh EK machine pin karein jo baar baar use hoti hai. Parent note ne ise derive kiya tha, isliye yahan hum sirf vocabulary restate karte hain taaki koi bhi symbol bina explanation ke na aaye.
Deep "why" ke liye parent note ka Master Theorem aur Recurrence Relations dekho. Yahan hum practice karte hain.
Level 1 — Recognition
Goal: recurrence se padhna aur case ka naam lena. Koi calculation trickery nahi.
Exercise 1.1
ke liye , , aur batao.
Recall Solution
- (char recursive calls), (har child half-size), .
- .
- ko se compare karo: hai, isliye polynomially chhota hai ⟹ Case 1.
- Answer: .
Exercise 1.2
Mergesort ka merge step teen phases (Divide / Conquer / Combine) mein se kis phase mein aata hai, aur "array ko aadha kato" wala step kis phase mein aata hai?
Recall Solution
- "Array ko aadha kato" Divide hai — yeh do subproblems produce karta hai.
mergeCombine hai — yeh do sorted halves ko ek sorted whole mein glue karta hai.- Do recursive
mergesortcalls Conquer phase hain.
Exercise 1.3
Har algorithm ko uski recurrence se match karo: Binary Search, Mergesort, Karatsuba. Recurrences: ; ; .
Recall Solution
- Binary Search → (ek recursive call, constant compare).
- Mergesort → (do halves, linear merge).
- Karatsuba → (teen half-size multiplications, linear add/shift).
Level 2 — Application
Goal: Master Theorem ko end to end run karo aur bound nikalo.
Exercise 2.1
solve karo.
Recall Solution
- .
- .
- vs : ⟹ Case 1.
- .
Exercise 2.2
solve karo.
Recall Solution
- ; .
- vs : , isliye polynomially bada hai ⟹ candidate Case 3.
- Regularity check: . ke saath ✓.
- Case 3 confirmed: .
Exercise 2.3
solve karo — yeh Strassen Matrix Multiplication ki recurrence hai.
Recall Solution
- ; .
- vs : ⟹ chhota hai ⟹ Case 1.
- .
- Matlab: Strassen naive matrix multiply ko beat karta hai kyunki .
Level 3 — Analysis
Goal: woh recurrences jahan Master Theorem subtle hai ya fail karta hai; recursion tree use karo.
Recursion tree hamaara fallback hai: har level par kaam draw karo, phir levels sum karo. Neeche figure mein wahi teen qualitative shapes hain teen horizontal bar charts ke roop mein, ek har case ke liye. Har row tree ka ek level hai (level 0 = upar root, deeper levels neeche) aur har bar ki length us level par kiya gaya total kaam hai. Left panel (Case 1, coral): bars neeche jaate jaate lambe hote hain — bottom (leaves) sabse zyada kaam karta hai, isliye sum leaves ke dominate hota hai. Middle panel (Case 2, lavender): har bar ek jaisi length ka hai — sab levels tie karte hain, isliye total hai (work per level) (number of levels ). Right panel (Case 3, mint): bars neeche jaate jaate chhhote hote hain — top (root) dominate karta hai. Neeche ke har exercise mein actually yahi poochha ja raha hai: "main in teen pictures mein se kaunsi mein hun?"

Exercise 3.1
Explain karo kyun plain three-case Master Theorem par apply nahi hota, phir ise recursion tree se solve karo.
Recall Solution
- , isliye aur . Yahan .
- Kya polynomially bada hai se? Humein chahiye hoga kisi ke liye. Lekin har ke liye se dhire badhta hai, isliye aisa koi exist nahi karta. Gap sirf logarithmic hai: na polynomially chhota hai (Case 1), na (plain Case 2), na polynomially bada (Case 3). Isliye plain three-case statement koi answer nahi deta. (Yeh hamare upar ke definition se generalized Case 2 with mein cover hota hai — aur hum woh answer neeche haath se confirm karte hain.)
- Recursion tree. Level mein nodes hain har ek size ke, work karte hue. Level total:
- se tak sum karo:
- Answer: — generalized Case 2 se match karta hai ().
Exercise 3.2
solve karo (unequal splits — Master Theorem ke cousin ka kaam, lekin tree bounds se karo).
Recall Solution
- Do children alag alag factors se shrink karte hain, isliye plain MT (jise ek clean chahiye) apply nahi hota.
- Pehle ek monotonicity note. Neeche ke sab bounds use karte hain ki nondecreasing hai mein (bada input ⟹ utna hi ya zyada kaam). Yeh hold karta hai kyunki recurrence ka non-recursive term nondecreasing hai aur recursive calls nonnegative sizes par hain, isliye ek standard induction dikhata hai ki jab bhi . Hum yeh poori jagah assume karte hain.
- Upper bound. Kyunki nondecreasing hai, , isliye chhote child ko bade se replace karne par total sirf badh sakta hai: , jo Case 2 hai ⟹ .
- Lower bound. Root akela kaam karta hai, isliye .
- Kyun har level par total input size se multiply hoti hai. Size ka har node do children spawn karta hai sizes aur ke, jinki sizes add karke hoti hai. Isliye agar level par sab nodes ki sizes add karke hain, toh har node par yeh sum karne se milta hai. (root) se shuru karke, milta hai.
- Sharper tree sum. Ek node par non-recursive work uski size ke barabar hai (yeh term linear hai), isliye level- work , ek geometric series:
- Kyunki series converge karti hai, root dominate karta hai: (jo upar ke loose upper bound ko bhi tighten karta hai).
Exercise 3.3
(Mergesort) ke liye, Mathematical Induction se prove karo ki kisi suitable constant ke liye (lo , for , a power of 2).
Recall Solution
- Guess: . Hum find karte hain 2 ki powers par induction se.
- Base : . Humein chahiye , yani .
- Step: assume karo . Tab
- Yeh hai iff , yani . Base ke ke saath mila ke, choose karo.
- Isliye . ∎
Level 4 — Synthesis
Goal: khud divide-and-conquer algorithm design karo aur uski recurrence derive karo.
Exercise 4.1
Divide and conquer use karke unsorted array ka maximum element dhundhne ka algorithm design karo. Teen phases batao, recurrence likho, aur solve karo. Obvious linear scan se compare karo.
Recall Solution
- Header se yaad karo ki non-recursive term hai : divide cost plus combine cost.
- Divide: array ko left aur right halves mein split karo (, ; split sirf index arithmetic hai, isliye ).
- Conquer: har half ka max recursively dhundho.
- Combine: do returned maxima mein se bada return karo — ek comparison, isliye .
- Isliye .
- Base: ek akela element apna khud ka max hai.
- Recurrence: . Tab , ; ⟹ Case 1 ⟹ .
- Comparison: linear scan jaisa hi — divide and conquer yahan kuch nahi kama raha kyunki koi cheap combine advantage nahi hai; har element phir bhi ek baar dekhna padta hai. (Useful lesson: D&C tabhi jeeтта hai jab combine re-solving se sasta ho.)
Exercise 4.2
Tumhare paas do already-sorted arrays hain total length ke aur tumhe unka merged sorted output chahiye. Koi propose karta hai: har ek ke pehle half ko dusre ke pehle half ke saath recursively merge karo aur doosre halves ke saath doosre halves ko (). Kya yeh valid divide-and-conquer merge hai? Agar nahi, toh correct combine cost kya hai, aur standard mergesort merge kaunsi recurrence deta hai?
Recall Solution
- Proposal valid nahi hai: har sorted array ko "aadha" split karke crosswise merge karne par, jo halves tum pick karte ho woh cleanly interleave hone ki guarantee nahi — array A ke left half ka element array B ke right half ke kisi element se bada ho sakta hai, isliye charo merges overlap karte hain. Subproblems independent nahi hain, jo paradigm ki precondition tod deta hai.
- Correct merge ek single linear pass hai do pointers ke saath: , merge ke andar khud koi recursion nahi.
- Mergesort ke andar poori recurrence hai jahan yahi two-pointer merge hai. Case 2 ⟹ .
Exercise 4.3
Karatsuba do -digit numbers ko se multiply karta hai, jo deta hai. Maano ek researcher ek aisa scheme invent karta hai jo har number ko 3 parts mein split karta hai (isliye ) aur sirf 5 multiplications chahiye, linear combine ke saath: . Kya yeh Karatsuba se fast hai?
Recall Solution
- Naya: , .
- ; ⟹ Case 1 ⟹ .
- Karatsuba hai .
- Kyunki , naya scheme asymptotically faster hai. (Yeh real Toom–3 algorithm hai.)
Level 5 — Mastery
Goal: ek general statement prove karo ya woh exact threshold dhundho jahan ek design decision answer flip kar deta hai.
Exercise 5.1
Family consider karo (ek fixed shrink factor , lekin varying number of subproblems ). Har woh value of dhundho jahan asymptotic solution ka form change hota hai, aur har regime mein closed-form bound batao.
Recall Solution
- ; ko se compare karo.
- : , isliye bada hai ⟹ Case 3 (regularity: with ✓) ⟹ .
- : , tie ⟹ Case 2 ⟹ .
- : , leaves bade ⟹ Case 1 ⟹ .
- Single transition point hai : us se neeche root dominate karta hai (), exactly us par har level tie karta hai (), aur us se upar leaves dominate karte hain ().
Exercise 5.2
ke tree ke leaves ki count hai yeh prove karo, aur isliye explain karo kyun leaf work hai jab har leaf cost karta hai. Depth par Mathematical Induction use karo.
Recall Solution
- Tree ki depth hai (har step size ko se divide karta hai jab tak size 1 na ho: ).
- Claim: level par nodes hain. Base : ek root, ✓. Step: agar level par nodes hain aur har ek children spawn karta hai, toh level par ✓. Induction se claim sab ke liye hold karta hai.
- Leaves par baithe hain, isliye unki count hai
- ko ke roop mein likhein. Left side ka natural log lo: . Right side ka natural log lo: . Dono ke barabar hain, aur one-to-one hai, isliye original quantities equal hain:
- Isliye leaves ki sankhya hai. Har leaf kaam karta hai, isliye total leaf work . Yeh exactly parent note se watershed cost hai. ∎
Exercise 5.3
Ek student claim karta hai: "Kyunki zyada subproblems hamesha exponent mein kaam add karte hain, sabse kam subproblems () hamesha fastest hote hain." Ek concrete recurrence do jahan ko 1 se 8 tak badhane par asymptotic time unchanged rahe, aur explain karo kyun.
Recall Solution
- ko poori range mein same case mein rakho. Lo aur , toh ranges from ( par) se ( par) tak.
- ko strictly dominant term rakhne ke liye humein chahiye , yani , yani . Isliye ke liye: polynomially bada hai se ⟹ Case 3 ⟹ , se independent.
- Kyun unchanged: Case 3 mein root work poore sum ko dominate karta hai; subproblems ki sankhya sirf (geometrically chhote) neeche ke levels ko affect karti hai, jo constant mein collapse ho jaate hain. ko root-heavy regime ke andar badhane se asymptotics touch nahi hoti.
- Isliye student galat hai: tabhi matter karta hai jab woh tumhe case boundary ke across push kare (yahan, tak pahunchne par jahan hai jo se tie karta hai aur Case 2 mein flip hota hai, ).
Recall Quick self-check (cloze)
Leaf exponent hai ====. Master Theorem ke plain three cases fail hote hain jab aur ke beech gap sirf logarithmic (not polynomial) ho. Case 3 ke liye extra regularity condition with chahiye.