3.1.7Complexity Analysis

Master theorem — solving recurrences T(n) = aT(n - b) + f(n)

1,956 words9 min readdifficulty · medium6 backlinks

What the symbols mean (WHAT)

The number nlogban^{\log_b a} is the total cost of the leaves. Everything depends on comparing your combine-cost f(n)f(n) to it.


Deriving it from scratch (HOW — never just trust the formula)

Unroll the recursion into a tree.

  • Level 0 (root): 1 node, work f(n)f(n).
  • Level 1: aa nodes, each work f(n/b)f(n/b) → total af(n/b)a\,f(n/b).
  • Level ii: aia^i nodes, each size n/bin/b^i, work aif(n/bi)a^i f(n/b^i).
  • Last level: size shrinks to 1 when n/bL=1L=logbnn/b^L = 1 \Rightarrow L = \log_b n.

Now count the leaves. The number of leaves is alogbn=nlogba=nccrit.a^{\log_b n} = n^{\log_b a} = n^{c_{\text{crit}}}. Why? Use alogbn=(blogba)logbn=blogbalogbn=(blogbn)logba=nlogbaa^{\log_b n} = (b^{\log_b a})^{\log_b n} = b^{\log_b a \cdot \log_b n} = (b^{\log_b n})^{\log_b a} = n^{\log_b a}. So leaf cost =Θ(nlogba)= \Theta(n^{\log_b a}).

The sum is essentially a geometric series comparing f(n)f(n) to nlogban^{\log_b a}:

  1. If ff is smaller (f=O(nlogbaϵ)f = O(n^{\log_b a - \epsilon})): the series grows downward, leaves dominate → T(n)=Θ(nlogba)T(n)=\Theta(n^{\log_b a}).
  2. If ff is equal (f=Θ(nlogba)f = \Theta(n^{\log_b a})): every level costs about the same, and there are logbn\log_b n levels → multiply by logn\log n.
  3. If ff is bigger (f=Ω(nlogba+ϵ)f = \Omega(n^{\log_b a + \epsilon})): the root dominates → T(n)=Θ(f(n))T(n)=\Theta(f(n)) (needs a regularity check so the series actually converges to the root).

The three cases (the 20% you must memorize)

Figure — Master theorem — solving recurrences T(n) = aT(n - b) + f(n)

Forecast-then-Verify worked examples


Where it BREAKS (Steel-man your mistakes)


Quick decision recipe (80/20)

  1. Compute p=logbap=\log_b a and the leaf cost npn^p.
  2. Polynomially smaller ff → Case 1 → answer npn^p.
  3. Equal (up to logk\log^k) → Case 2 → answer nplogk+1nn^p\log^{k+1}n.
  4. Polynomially bigger ff + regularity → Case 3 → answer f(n)f(n).
  5. Only a log gap between cases → not basic Master; use extended Case 2.

Recall Feynman: explain to a 12-year-old (click to reveal)

Imagine a chore that you split among friends. Each round you hand each friend a smaller chore, but you also do some sorting work yourself. Two costs fight: the giant pile of tiny chores at the bottom vs the big sorting you do at the top. The Master Theorem is just a referee: count how fast chores multiply (aa) versus how fast they shrink (bb). If tiny chores swamp you, the answer is the leaf cost. If your own sorting swamps you, the answer is the top cost. If they're a tie, you pay the same on every floor of the building, so you multiply by the number of floors (logn\log n).


Connections

  • Recursion Trees — the visual derivation the Master Theorem summarizes.
  • Akra–Bazzi method — generalization for unequal subproblems / non-constant splits.
  • Big-O, Big-Omega, Big-Theta — the asymptotic comparison machinery.
  • Merge Sort, Karatsuba Multiplication, Binary Search — direct applications.
  • Geometric Series — why the three cases exist (increasing/flat/decreasing series).

Flashcards

What three quantities define the Master Theorem recurrence?
aa = number of subproblems, bb = shrink factor (nn/bn\to n/b), f(n)f(n) = work outside recursion (split+combine).
What is the critical exponent and what does nlogban^{\log_b a} represent?
ccrit=logbac_{\text{crit}}=\log_b a; nlogban^{\log_b a} is the total cost of the leaves of the recursion tree.
State Case 1 of the Master Theorem.
If f(n)=O(nlogbaϵ)f(n)=O(n^{\log_b a-\epsilon}), then T(n)=Θ(nlogba)T(n)=\Theta(n^{\log_b a}) (leaves win).
State Case 2 (extended).
If f(n)=Θ(nlogbalogkn)f(n)=\Theta(n^{\log_b a}\log^k n), then T(n)=Θ(nlogbalogk+1n)T(n)=\Theta(n^{\log_b a}\log^{k+1}n).
State Case 3 and its extra condition.
If f(n)=Ω(nlogba+ϵ)f(n)=\Omega(n^{\log_b a+\epsilon}) AND af(n/b)cf(n)a f(n/b)\le c f(n) for some c<1c<1, then T(n)=Θ(f(n))T(n)=\Theta(f(n)).
Why does Case 2 multiply by logn\log n?
Every level costs the same (Θ(nlogba)\Theta(n^{\log_b a})) and there are logbn\log_b n levels, so total = level cost × number of levels.
Solve T(n)=2T(n/2)+nT(n)=2T(n/2)+n.
ccrit=1c_{\text{crit}}=1, f=n=Θ(n1)f=n=\Theta(n^1) → Case 2 → Θ(nlogn)\Theta(n\log n).
Solve T(n)=T(n/2)+1T(n)=T(n/2)+1.
ccrit=0c_{\text{crit}}=0, f=1=Θ(n0)f=1=\Theta(n^0) → Case 2 → Θ(logn)\Theta(\log n) (binary search).
Why can't basic Master Theorem solve T(n)=2T(n/2)+nlognT(n)=2T(n/2)+n\log n?
nlognn\log n exceeds nn only by a log factor, not a polynomial factor — falls in the gap; extended Case 2 gives Θ(nlog2n)\Theta(n\log^2 n).
Why is the number of leaves nlogban^{\log_b a}?
alogbn=(blogba)logbn=nlogbaa^{\log_b n} = (b^{\log_b a})^{\log_b n} = n^{\log_b a}.
When does the Master Theorem NOT apply?
Non-constant a,ba,b; unequal subproblem sizes (e.g. T(n/3)+T(2n/3)+nT(n/3)+T(2n/3)+n); non-polynomial gaps; negative ff.

Concept Map

split into

shrink by

combine cost

defines

defines

gives leaf cost

unroll into

geometric series compares

f smaller, leaves win

tie times log^k

f bigger plus regularity

is the baseline for

is the f in

T of n = a T of n/b + f of n

a subproblems

factor b

f of n

critical exponent log_b a

n^log_b a

recursion tree sum over levels

f vs n^log_b a

Case 1: Theta n^log_b a

Case 2: Theta n^log_b a log^k+1 n

Case 3: Theta f of n

Hinglish (regional understanding)

Intuition Hinglish mein samjho

Dekho, Master Theorem ka funda simple hai: jab bhi koi divide-and-conquer algorithm likhte ho, woh problem ko chhote tukdo mein todta hai. Recurrence aati hai T(n)=aT(n/b)+f(n)T(n)=aT(n/b)+f(n) — yahan aa matlab kitne subproblems banaye, bb matlab har baar input kitna chhota hua (n/bn/b), aur f(n)f(n) matlab divide aur combine ka jo extra kaam karna pada wahi level pe.

Ab pura khel ek race hai. Ek taraf hai leaves ka total cost jo nikalta hai nlogban^{\log_b a}, aur doosri taraf hai tumhara f(n)f(n). Dono ko compare karo. Agar f(n)f(n) chhota hai (polynomially), toh leaves jeet gaye → answer Θ(nlogba)\Theta(n^{\log_b a}) (Case 1). Agar dono barabar hain, toh har level pe same kaam, aur logbn\log_b n levels — isliye ek extra logn\log n multiply hota hai → Case 2. Agar f(n)f(n) bada hai aur regularity condition pass karta hai, toh root jeet gaya → answer Θ(f(n))\Theta(f(n)) (Case 3).

Yaad rakhne ka tarika: "LET" — Leaves, Equal, Top. Merge sort dekho: 2T(n/2)+n2T(n/2)+n, yahan log22=1\log_2 2 = 1, aur f=n=n1f=n=n^1, dono barabar → Case 2 → nlognn\log n. Bas itna dhyaan rakho ki "bada" ya "chhota" ka matlab hai polynomially (yaani nϵn^\epsilon ka farak), sirf log\log ka farak nahi. Agar sirf log ka gap hai (jaise nlognn\log n vs nn), toh basic theorem fail ho jaata hai — extended Case 2 use karna padta hai. Yeh chhoti si galti exams mein bahut log karte hain!

Go deeper — visual, from zero

Test yourself — Complexity Analysis

Connections