def solve(P): # 1. BASE CASE — small enough to answer directly if size(P) <= threshold: return brute_force(P) # 2. DIVIDE — break into a subproblems of size n/b subproblems = split(P) # cost = D(n) # 3. CONQUER — recurse on each results = [solve(sub) for sub in subproblems] # 4. COMBINE — glue answers return combine(results) # cost = C(n)
What are the three phases of a divide-and-conquer algorithm?
Divide (split into subproblems), Conquer (solve each recursively to a base case), Combine (merge sub-answers).
In the recurrence T(n)=aT(n/b)+f(n), what do a, b, and f mean?
a = number of subproblems, b = factor input shrinks by, f(n) = cost of divide + combine (non-recursive work).
What is the "watershed" exponent in the Master Theorem?
ccrit=logba, the cost of the leaves nlogba; you compare f(n) against nccrit.
Master Theorem Case 2 condition and result?
If f(n)=Θ(nlogba) then T(n)=Θ(nlogbalogn).
Why does mergesort give Θ(nlogn)?
a=b=2 so ccrit=1, f(n)=Θ(n) matches → Case 2 → n⋅logn levels of Θ(n) work.
What three obligations make a divide-and-conquer algorithm correct by induction?
Progress (subproblems strictly smaller), Base correctness, Combine correctness.
Why does Karatsuba beat schoolbook multiplication?
It uses 3 half-size multiplications instead of 4, dropping a from 4 to 3 so the exponent falls from log24=2 to log23≈1.585.
When does the Master Theorem NOT apply?
When f(n) differs from nlogba only by a non-polynomial (e.g. log) factor, or splits are uneven — use recursion tree or Akra–Bazzi.
How many leaves does the recursion tree of aT(n/b) have?
alogbn=nlogba.
What proof technique establishes divide-and-conquer correctness?
Strong induction on input size n (IH: correct for all sizes <n).
Recall Feynman: explain it to a 12-year-old
Imagine you have a giant pile of mixed-up cards and you must sort them. Too big to handle alone. So you split the pile in half and hand each half to a friend. They each split their half and hand it on... until someone gets just one card (already "sorted"!). Then everyone hands sorted little piles back up, and at each step you just zip two sorted piles together like a zipper — easy, because both sides are already in order. Splitting was free, zipping is quick, and because you halved again and again, you only needed about logn rounds. That's why it's so fast!
Divide and conquer ka funda simple hai: ek bada problem solve karna mushkil hai, toh use chhote-chhote same type ke subproblems mein tod do, har ek ko recursively solve karo, aur phir unke answers ko combine kar do. Teen steps yaad rakho — Divide, Conquer, Combine. Sabse important baat: ek base case zaroor hona chahiye (jaise array of size 1 already sorted hota hai), warna recursion kabhi rukega hi nahi aur stack overflow ho jayega.
Correctness ka proof hum strong induction se karte hain. Maan lo ki size n se chhote sabhi inputs pe algorithm sahi answer deta hai (yeh hamari assumption — IH hai). Phir agar divide chhote valid subproblems banata hai, toh recursive calls IH se sahi answer denge, aur agar combine sahi parts se sahi whole banata hai — bas, proof done. Mergesort mein hum halves ko dobara check nahi karte; IH bolta hai woh already sorted hain, sirf merge ka correctness prove karna padta hai.
Time nikalne ke liye recurrence likho: T(n)=aT(n/b)+f(n), jahan a = kitne subproblems, b = size kitne se divide hua, aur f(n) = divide + combine ka cost. Recursion tree banao — har level pe kitna kaam ho raha hai add karo. Master Theorem shortcut deta hai: f(n) ko nlogba se compare karo. Chhota hai toh leaves dominate (Case 1), barabar hai toh extra logn multiply (Case 2), bada hai toh root dominate (Case 3).
Examples se clear hoga: Mergesort 2T(n/2)+n → log22=1, f=n match → nlogn. Binary search T(n/2)+1 → sirf ek call → logn. Karatsuba ka jugaad: 4 ki jagah sirf 3 multiplications karke exponent 2 se 1.585 tak gira deta hai — yahi divide-and-conquer ki asli power hai, smart combine se kaam bachana.