1. Gyanastra
  2. Algorithms
  3. Master Theorem and Solving Recurrences: All Cases and Traps
  4. What a Recurrence Is and Why We Solve It
Free lesson, no account needed 7 min read
Asked in past papers3 times
  • GATE CSE2021
  • ISRO2017
  • UGC NET2017

Algorithms · Master Theorem and Solving Recurrences: All Cases and Traps

What a Recurrence Is and Why We Solve It

This chapter takes you from "what is a recurrence" to the exact, exam-ready mastery of the Master Theorem — every case, the extended case 2, the situations where the theorem silently fails, twelve fully worked recurrences, the substitution method with its classic wrong-proof trap, and the change-of-variable trick. Every arithmetic step is shown and rechecked.


A recurrence expresses the cost T(n)T(n) of an algorithm on input size nn in terms of its cost on smaller inputs. For divide-and-conquer algorithms the standard shape is

T(n)=a T(n/b)+f(n)T(n) = a\,T(n/b) + f(n)

with symbols defined as:

SymbolMeaningConstraint
nnsize of the (sub)problemn>0n > 0
aanumber of recursive subproblemsa≥1a \ge 1, constant
bbfactor by which input shrinksb>1b > 1, constant
f(n)f(n)cost of dividing + combining (the "non-recursive" work)f(n)≥0f(n) \ge 0

The critical exponent is

p=log⁡ba=ln⁡aln⁡b.p = \log_b a = \frac{\ln a}{\ln b}.

This single number pp is the whole game: the Master Theorem compares f(n)f(n) against npn^{p}.


The Master Theorem — All Three Cases

Let T(n)=aT(n/b)+f(n)T(n) = aT(n/b) + f(n), a≥1a \ge 1, b>1b > 1, and p=log⁡bap = \log_b a.

Case 1 — leaves dominate (recursion dominates)

If f(n)=O(np−ε)f(n) = O(n^{p-\varepsilon}) for some constant ε>0\varepsilon > 0, then

T(n)=Θ(np)=Θ(nlog⁡ba).T(n) = \Theta(n^{p}) = \Theta(n^{\log_b a}).

Intuition: the combine work is polynomially smaller than the leaf work, so the leaves (there are alog⁡bn=nlog⁡baa^{\log_b n} = n^{\log_b a} of them) dominate.

Case 2 — balanced (all levels cost the same)

If f(n)=Θ(np)f(n) = \Theta(n^{p}), then

T(n)=Θ(nplog⁡n)=Θ(nlog⁡balog⁡n).T(n) = \Theta(n^{p}\log n) = \Theta(n^{\log_b a}\log n).

Intuition: every one of the log⁡bn\log_b n levels costs the same Θ(np)\Theta(n^p), so multiply by the number of levels.

Case 3 — root dominates (combine dominates)

If f(n)=Ω(np+ε)f(n) = \Omega(n^{p+\varepsilon}) for some ε>0\varepsilon > 0 AND the regularity condition holds:

a f(n/b)≤c f(n)for some constant c<1 and all sufficiently large n,a\,f(n/b) \le c\,f(n) \quad \text{for some constant } c < 1 \text{ and all sufficiently large } n,

then

T(n)=Θ(f(n)).T(n) = \Theta(f(n)).

Intuition: the top-level combine work is so large it swamps everything below; the regularity condition guarantees the geometric series of combine costs converges.

The regularity condition — what it really checks

For a polynomial f(n)=nqf(n) = n^{q}, the regularity condition becomes

a(nb)q≤c nq  ⟺  abq≤c<1  ⟺  a<bq  ⟺  q>log⁡ba=p.a\left(\frac{n}{b}\right)^{q} \le c\,n^{q} \;\Longleftrightarrow\; \frac{a}{b^{q}} \le c < 1 \;\Longleftrightarrow\; a < b^{q} \;\Longleftrightarrow\; q > \log_b a = p.

So for polynomial ff, Case 3's Ω\Omega condition and the regularity condition are the same statement (q>pq > p). The regularity condition only needs separate checking when ff is not a clean polynomial (e.g. f(n)=n2/log⁡nf(n) = n^2/\log n or f(n)=n2sin⁡2nf(n)=n^2\sin^2 n). In GATE, ff is almost always polynomial, so "Case 3 applies" ⇔\Leftrightarrow "ff grows polynomially faster than npn^p."

Extended Case 2 (the log⁡kn\log^k n refinement)

When f(n)=Θ(nplog⁡kn)f(n) = \Theta(n^{p}\log^{k} n) for a constant k≥0k \ge 0:

T(n)=Θ ⁣(nplog⁡k+1n).T(n) = \Theta\!\left(n^{p}\log^{k+1} n\right).

This is the "polylog" extension. The three sub-cases people quote:

f(n)f(n)ResultNote
Θ(nplog⁡kn)\Theta(n^p \log^{k} n), k>0k > 0Θ(nplog⁡k+1n)\Theta(n^p \log^{k+1} n)one extra log
Θ(nplog⁡0n)=Θ(np)\Theta(n^p \log^{0} n) = \Theta(n^p)Θ(nplog⁡n)\Theta(n^p \log n)ordinary Case 2 (k=0k=0)
Θ(np/log⁡n)\Theta(n^p / \log n) i.e. k=−1k=-1Θ(nplog⁡log⁡n)\Theta(n^p \log\log n)"one log less"
Θ(np/log⁡kn)\Theta(n^p / \log^{k} n), k>1k>1Θ(np)\Theta(n^p)extended (Akra–Bazzi) result; classical Case 1 does not apply

Careful with the k=−1k=-1 and k<−1k<-1 rows. These are not part of the classical Master Theorem; they come from the Akra–Bazzi / extended treatment. The clean, safe statement for exams: if f(n)=np/log⁡nf(n) = n^p/\log n, the answer is Θ(nplog⁡log⁡n)\Theta(n^p\log\log n); if f(n)=np/log⁡knf(n) = n^p/\log^{k}n with k>1k>1, the answer is Θ(np)\Theta(n^p) (this follows from Akra–Bazzi; classical Case 1 does not apply, because np/log⁡knn^p/\log^k n is not O(np−ε)O(n^{p-\varepsilon}) for any ε>0\varepsilon>0). GATE rarely tests k=−1k=-1 directly, but know it exists.


When the Master Theorem Does NOT Apply

The theorem has hypotheses. Break any one and you must fall back on substitution / recursion tree. GATE loves these.

FailureExampleWhy it failsWhat to do
a<1a < 1 (not a valid branching count)T(n)=12T(n/2)+nT(n) = \tfrac12 T(n/2) + na=12<1a=\tfrac12<1; not a real algorithmsubstitution: T(n)=Θ(n)T(n)=\Theta(n)
aa not constantT(n)=n T(n/2)+nT(n) = n\,T(n/2) + naa depends on nnexpand: T(n)=2Θ(log⁡2n)T(n)=2^{\Theta(\log^2 n)} (super-polynomial); use tree
bb not constant / b≤1b \le 1T(n)=2T(n/2)+nT(n)=2T(n/2)+n is fine, but T(n)=2T(n)+1T(n)=2T(\sqrt n)+1 has b=nb=\sqrt nshrink factor is not constantchange of variable
Non-polynomial gapT(n)=2T(n/2)+n/log⁡nT(n) = 2T(n/2) + n/\log nf=n/log⁡nf = n/\log n is o(n)o(n) but not O(n1−ε)O(n^{1-\varepsilon}) for any ε>0\varepsilon>0extended / substitution → Θ(nlog⁡log⁡n)\Theta(n\log\log n)
ff negative somewhereT(n)=2T(n/2)−nT(n)=2T(n/2)-nf(n)≥0f(n)\ge0 violatednot a cost recurrence; handle directly
ff oscillatingf(n)=n2sin⁡2nf(n)=n^2\sin^2 nregularity condition may failcheck regularity explicitly

The non-polynomial gap is the #1 GATE trap. n/log⁡nn/\log n sits between n0n^0 and n1n^{1} but is not polynomially separated from n1n^1: for every ε>0\varepsilon>0, n/log⁡n≠O(n1−ε)n/\log n \ne O(n^{1-\varepsilon}). So Case 1 does not apply even though ff is "smaller" than nn. The correct answer for 2T(n/2)+n/log⁡n2T(n/2)+n/\log n is Θ(nlog⁡log⁡n)\Theta(n\log\log n) (extended case, k=−1k=-1).


Pages 1–2 of 2