3.7.2 · HinglishAlgorithm Paradigms

Divide and conquer — template, correctness, recurrence

1,976 words9 min readRead in English

3.7.2 · Coding › Algorithm Paradigms


The Template

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)

Correctness — yeh actually kaam kyun karta hai

Teen obligations hain jo tumhe poori karni hain — ek bhi choot gayi toh proof (aur code) toot jaata hai:

Obligation Yeh kya guarantee karta hai
Progress har recursive call ki size strictly hai → recursion terminate hoti hai
Base correctness sabse chhote cases sahi hain
Combine correctness correct parts ⟹ correct whole

The Recurrence

Recursion tree se solve karna (first principles)

Figure — Divide and conquer — template, correctness, recurrence

The Master Theorem (shortcut)


Jab Master Theorem fail ho jaata hai


Flashcards

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 mein , , aur ka matlab kya hai?
= subproblems ki sankhya, = woh factor jisse input shrink hota hai, = divide + combine ki cost (non-recursive work).
Master Theorem mein "watershed" exponent kya hai?
, leaves ki cost ; ko se compare karo.
Master Theorem Case 2 ki condition aur result kya hai?
Agar toh .
Mergesort kyun deta hai?
toh , match karta hai → Case 2 → work ke 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, ko 4 se 3 tak giraa ke exponent se tak le jaata hai.
Master Theorem kab apply NAHI hota?
Jab , se sirf non-polynomial (jaise log) factor se alag ho, ya splits uneven hon — tab recursion tree ya Akra–Bazzi use karo.
ke recursion tree mein kitne leaves hote hain?
.
Divide-and-conquer correctness establish karne ke liye kaunsi proof technique hai?
Input size par strong induction (IH: sabhi sizes 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 rounds chahiye. Isliye yeh itni fast hai!

Concept Map

divide into a subproblems

conquer recursively

stop at

answers

glue cost C n

justifies

correctness proven by

requires

requires

requires

prevents

classic example

Problem size n

Subproblems size n/b

Recursive solve

Base case brute force

Combine step

Answer for size n

Bet: split plus combine is cheap

Strong induction on n

Progress: size strictly smaller

Base correctness

Combine correctness

Infinite recursion

Mergesort O n log n