3.7.2Algorithm Paradigms

Divide and conquer — template, correctness, recurrence

2,147 words10 min readdifficulty · medium6 backlinks

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 — why it actually works

Three obligations you must discharge — miss one and the proof (and the code) breaks:

Obligation What it guarantees
Progress every recursive call has size strictly <n< n → recursion terminates
Base correctness the smallest cases are right
Combine correctness correct parts ⟹ correct whole

The Recurrence

Solving by the recursion tree (first principles)

Figure — Divide and conquer — template, correctness, recurrence

The Master Theorem (the shortcut)


When Master Theorem fails


Flashcards

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)T(n)=aT(n/b)+f(n), what do aa, bb, and ff mean?
aa = number of subproblems, bb = factor input shrinks by, f(n)f(n) = cost of divide + combine (non-recursive work).
What is the "watershed" exponent in the Master Theorem?
ccrit=logbac_{\text{crit}}=\log_b a, the cost of the leaves nlogban^{\log_b a}; you compare f(n)f(n) against nccritn^{c_{\text{crit}}}.
Master Theorem Case 2 condition and result?
If f(n)=Θ(nlogba)f(n)=\Theta(n^{\log_b a}) then T(n)=Θ(nlogbalogn)T(n)=\Theta(n^{\log_b a}\log n).
Why does mergesort give Θ(nlogn)\Theta(n\log n)?
a=b=2a=b=2 so ccrit=1c_{\text{crit}}=1, f(n)=Θ(n)f(n)=\Theta(n) matches → Case 2 → nlognn\cdot\log n levels of Θ(n)\Theta(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 aa from 4 to 3 so the exponent falls from log24=2\log_2 4=2 to log231.585\log_2 3\approx1.585.
When does the Master Theorem NOT apply?
When f(n)f(n) differs from nlogban^{\log_b a} 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)aT(n/b) have?
alogbn=nlogbaa^{\log_b n}=n^{\log_b a}.
What proof technique establishes divide-and-conquer correctness?
Strong induction on input size nn (IH: correct for all sizes <n<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\log n rounds. That's why it's so fast!

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

Hinglish (regional understanding)

Intuition Hinglish mein samjho

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 nn 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)T(n)=a\,T(n/b)+f(n), jahan aa = kitne subproblems, bb = size kitne se divide hua, aur f(n)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)f(n) ko nlogban^{\log_b a} se compare karo. Chhota hai toh leaves dominate (Case 1), barabar hai toh extra logn\log n multiply (Case 2), bada hai toh root dominate (Case 3).

Examples se clear hoga: Mergesort 2T(n/2)+n2T(n/2)+nlog22=1\log_2 2=1, f=nf=n match → nlognn\log n. Binary search T(n/2)+1T(n/2)+1 → sirf ek call → logn\log n. Karatsuba ka jugaad: 4 ki jagah sirf 3 multiplications karke exponent 22 se 1.5851.585 tak gira deta hai — yahi divide-and-conquer ki asli power hai, smart combine se kaam bachana.

Go deeper — visual, from zero

Test yourself — Algorithm Paradigms

Connections