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)
Divide-and-conquer algorithm ke teen phases kya hain?
Divide (subproblems mein split karo), Conquer (har ek ko recursively base case tak solve karo), Combine (sub-answers merge karo).
Recurrence T(n)=aT(n/b)+f(n) mein a, b, aur f ka matlab kya hai?
a = subproblems ki sankhya, b = woh factor jisse input shrink hota hai, f(n) = divide + combine ki cost (non-recursive work).
Master Theorem mein "watershed" exponent kya hai?
ccrit=logba, leaves ki cost nlogba; f(n) ko nccrit se compare karo.
Master Theorem Case 2 ki condition aur result kya hai?
Agar f(n)=Θ(nlogba) toh T(n)=Θ(nlogbalogn).
Mergesort Θ(nlogn) kyun deta hai?
a=b=2 toh ccrit=1, f(n)=Θ(n) match karta hai → Case 2 → Θ(n) work ke n⋅logn levels.
Kaunsi teen obligations ek divide-and-conquer algorithm ko induction se correct banati hain?
Progress (subproblems strictly smaller), Base correctness, Combine correctness.
Karatsuba schoolbook multiplication ko kyun beat karta hai?
Yeh 4 ki jagah 3 half-size multiplications use karta hai, a ko 4 se 3 tak giraa ke exponent log24=2 se log23≈1.585 tak le jaata hai.
Master Theorem kab apply NAHI hota?
Jab f(n), nlogba se sirf non-polynomial (jaise log) factor se alag ho, ya splits uneven hon — tab recursion tree ya Akra–Bazzi use karo.
aT(n/b) ke recursion tree mein kitne leaves hote hain?
alogbn=nlogba.
Divide-and-conquer correctness establish karne ke liye kaunsi proof technique hai?
Input size n par strong induction (IH: sabhi sizes <n ke liye correct).
Recall Feynman: ek 12-saal ke bacche ko explain karo
Socho tumhare paas mixed-up cards ka ek giant pile hai aur tumhe unhe sort karna hai. Akele handle karna bahut mushkil hai. Toh tum pile ko aadha split karo aur har aadha ek dost ko de do. Woh apna aadha split karte hain aur aage de dete hain... jab tak kisi ko sirf ek card na mile (jo already "sorted" hai!). Phir sab sorted chhoti piles wapas upar bhejte hain, aur har step par tum sirf do sorted piles ko ek zipper ki tarah zip karte ho — aasaan hai, kyunki dono sides already order mein hain. Splitting free thi, zipping jaldi thi, aur isliye ki tumne baar baar half kiya, tumhe sirf logn rounds chahiye. Isliye yeh itni fast hai!