Skip to content
Part IA Lent Term

The Master Theorem

The Recurrence Form

Many divide-and-conquer algorithms have running times described by recurrences of the form

T(n)=aT ⁣(nb)+f(n)T(n) = a\,T\!\left(\frac{n}{b}\right) + f(n)

where a1a \ge 1 is the number of subproblems, b>1b > 1 is the factor by which the problem size shrinks, and f(n)f(n) accounts for the work of dividing and combining. We interpret n/bn/b as n/b\lfloor n/b \rfloor or n/b\lceil n/b \rceil; the asymptotic bounds are the same.

The Critical Exponent

The key quantity is the critical exponent

ccrit=logbac_{\text{crit}} = \log_b a

This is the exponent of nn in the time contributed by the leaf level of the recursion tree. The Master Theorem compares f(n)f(n) (the work done at the root and internal levels) against nccritn^{c_{\text{crit}}} (the work done at the leaves).

The Three Cases

Case 1: Leaf-Dominated

If f(n)=O(nccritε)f(n) = O(n^{c_{\text{crit}} - \varepsilon}) for some constant ε>0\varepsilon > 0, then

T(n)=Θ(nccrit)T(n) = \Theta(n^{c_{\text{crit}}})

The leaves dominate: the cost at the bottom of the tree outweighs everything above. Note the ε\varepsilon: f(n)f(n) must be polynomially smaller than nccritn^{c_{\text{crit}}}, not just asymptotically smaller.

Example: T(n)=9T(n/3)+nT(n) = 9\,T(n/3) + n

a=9a = 9, b=3b = 3, ccrit=log39=2c_{\text{crit}} = \log_3 9 = 2. Here f(n)=n=O(n20.5)f(n) = n = O(n^{2 - 0.5}), so Case 1 gives T(n)=Θ(n2)T(n) = \Theta(n^2). The 99 subproblems at size n/3n/3 generate Θ(n2)\Theta(n^2) leaf work, overwhelming the linear f(n)f(n).

Case 2: Balanced

If f(n)=Θ(nccrit)f(n) = \Theta(n^{c_{\text{crit}}}), then

T(n)=Θ(nccritlgn)T(n) = \Theta(n^{c_{\text{crit}}} \lg n)

Each level of the recursion tree contributes the same amount of work (Θ(nccrit)\Theta(n^{c_{\text{crit}}})), and there are logbn\log_b n levels, giving the extra lgn\lg n factor.

Example: T(n)=2T(n/2)+Θ(n)T(n) = 2\,T(n/2) + \Theta(n) (Mergesort)

a=2a = 2, b=2b = 2, ccrit=1c_{\text{crit}} = 1. f(n)=Θ(n)=Θ(n1)f(n) = \Theta(n) = \Theta(n^1), so Case 2: T(n)=Θ(nlgn)T(n) = \Theta(n \lg n).

Case 3: Root-Dominated

If f(n)=Ω(nccrit+ε)f(n) = \Omega(n^{c_{\text{crit}} + \varepsilon}) for some ε>0\varepsilon > 0, and the regularity condition af(n/b)cf(n)a\,f(n/b) \le c\,f(n) holds for some constant c<1c < 1 and all sufficiently large nn, then

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

The root dominates: the top-level work outweighs everything below. The regularity condition ensures the work decreases geometrically down the tree: each level’s work is at most a constant fraction of the level above.

Example: T(n)=3T(n/4)+nlgnT(n) = 3\,T(n/4) + n \lg n

a=3a = 3, b=4b = 4, ccrit=log430.793c_{\text{crit}} = \log_4 3 \approx 0.793. f(n)=nlgn=Ω(n0.793+0.2)f(n) = n \lg n = \Omega(n^{0.793 + 0.2}). Check regularity: 3(n/4)lg(n/4)=34nlgn34nlg434nlgn3 \cdot (n/4)\lg(n/4) = \frac{3}{4}n\lg n - \frac{3}{4}n\lg 4 \le \frac{3}{4}n\lg n. With c=3/4<1c = 3/4 < 1, regularity holds. Case 3: T(n)=Θ(nlgn)T(n) = \Theta(n \lg n).

The Regularity Condition (Case 3)

The regularity condition af(n/b)cf(n)a\,f(n/b) \le c\,f(n) for c<1c < 1 is not a formality. Without it, Case 3 can fail even when f(n)=Ω(nccrit+ε)f(n) = \Omega(n^{c_{\text{crit}} + \varepsilon}). It rules out oscillating functions where the total cost at a child level could exceed the parent level. Most polynomially bounded functions encountered in practice satisfy it automatically.

The Polynomial Gap (ε)

The ε>0\varepsilon > 0 in Cases 1 and 3 means the Master Theorem requires a polynomial separation between f(n)f(n) and nccritn^{c_{\text{crit}}}. A logarithmic gap is not enough.

Example Where the Master Theorem Fails

T(n)=2T ⁣(n2)+nlgnT(n) = 2\,T\!\left(\frac{n}{2}\right) + n \lg n

a=2a = 2, b=2b = 2, ccrit=1c_{\text{crit}} = 1. f(n)=nlgnf(n) = n \lg n grows faster than nn but the ratio f(n)/n=lgnf(n)/n = \lg n is not polynomial in nεn^\varepsilon for any ε>0\varepsilon > 0. So f(n)Ω(n1+ε)f(n) \notin \Omega(n^{1+\varepsilon}), and f(n)O(n1ε)f(n) \notin O(n^{1-\varepsilon}). The Master Theorem cannot resolve this recurrence. We must use the substitution method or the recursion-tree method instead.

This gap between Cases 2 and 3 (or between Cases 1 and 2) arises fairly rarely in practice for standard algorithmic recurrences.

The Recursion-Tree Method

When the Master Theorem cannot be applied, the recursion tree provides a fallback: expand the recurrence level by level and sum the work.

For T(n)=2T(n/2)+nlgnT(n) = 2T(n/2) + n \lg n:

LevelNumber of nodesProblem sizeWork per nodeWork per level
01nnnlgnn \lg nnlgnn \lg n
12n/2n/2(n/2)lg(n/2)(n/2)\lg(n/2)nlg(n/2)n \lg(n/2)
24n/4n/4(n/4)lg(n/4)(n/4)\lg(n/4)nlg(n/4)n \lg(n/4)
\vdots\vdots\vdots\vdots\vdots
kk2k2^kn/2kn/2^k(n/2k)lg(n/2k)(n/2^k)\lg(n/2^k)nlg(n/2k)n \lg(n/2^k)

Total work: k=0lgn1nlg(n/2k)=nk=0lgn1(lgnk)\sum_{k=0}^{\lg n - 1} n \lg(n/2^k) = n \sum_{k=0}^{\lg n - 1} (\lg n - k). This is nn times an arithmetic series summing to Θ(lg2n)\Theta(\lg^2 n), so T(n)=Θ(nlg2n)T(n) = \Theta(n \lg^2 n).

Worked Examples

T(n)=T(n/2)+Θ(1)T(n) = T(n/2) + \Theta(1)

a=1a = 1, b=2b = 2, ccrit=log21=0c_{\text{crit}} = \log_2 1 = 0. f(n)=Θ(1)=Θ(n0)f(n) = \Theta(1) = \Theta(n^0), so Case 2: T(n)=Θ(lgn)T(n) = \Theta(\lg n).

Example 2: Strassen’s Matrix Multiplication

T(n)=7T(n/2)+Θ(n2)T(n) = 7\,T(n/2) + \Theta(n^2)

a=7a = 7, b=2b = 2, ccrit=log272.807c_{\text{crit}} = \log_2 7 \approx 2.807. f(n)=Θ(n2)=O(n2.8070.5)f(n) = \Theta(n^2) = O(n^{2.807 - 0.5}), so Case 1: T(n)=Θ(nlg7)Θ(n2.807)T(n) = \Theta(n^{\lg 7}) \approx \Theta(n^{2.807}).

Example 3: T(n)=2T(n/2)+Θ(n3)T(n) = 2\,T(n/2) + \Theta(n^3)

a=2a = 2, b=2b = 2, ccrit=1c_{\text{crit}} = 1. f(n)=Θ(n3)=Ω(n1+1)f(n) = \Theta(n^3) = \Omega(n^{1 + 1}). Regularity: 2(n/2)3=14n3cn32 \cdot (n/2)^3 = \frac{1}{4} n^3 \le c \cdot n^3 for c=1/4c = 1/4. Case 3: T(n)=Θ(n3)T(n) = \Theta(n^3).

Example 4: T(n)=3T(n/2)+n2T(n) = 3\,T(n/2) + n^2

a=3a = 3, b=2b = 2, ccrit=log231.585c_{\text{crit}} = \log_2 3 \approx 1.585. f(n)=n2=Ω(n1.585+0.3)f(n) = n^2 = \Omega(n^{1.585 + 0.3}). Regularity: 3(n/2)2=34n234n23(n/2)^2 = \frac{3}{4}n^2 \le \frac{3}{4} n^2. Case 3: T(n)=Θ(n2)T(n) = \Theta(n^2).

Example 5: T(n)=8T(n/2)+Θ(n2)T(n) = 8\,T(n/2) + \Theta(n^2)

a=8a = 8, b=2b = 2, ccrit=log28=3c_{\text{crit}} = \log_2 8 = 3. f(n)=Θ(n2)=O(n30.5)f(n) = \Theta(n^2) = O(n^{3 - 0.5}), Case 1: T(n)=Θ(n3)T(n) = \Theta(n^3).

Systematic Approach to Applying the Theorem

  1. Identify aa, bb, and f(n)f(n) from the recurrence.
  2. Compute ccrit=logbac_{\text{crit}} = \log_b a.
  3. Compare f(n)f(n) against nccritn^{c_{\text{crit}}}.
  4. If f(n)f(n) is polynomially smaller → Case 1, result Θ(nccrit)\Theta(n^{c_{\text{crit}}}).
  5. If f(n)f(n) is asymptotically equal → Case 2, result Θ(nccritlgn)\Theta(n^{c_{\text{crit}}} \lg n).
  6. If f(n)f(n) is polynomially larger → check regularity; if satisfied, Case 3, result Θ(f(n))\Theta(f(n)).
  7. If none of the above applies (the gap case) → use the recursion tree or substitution method.

Summary

CaseConditionSolutionIntuition
1f(n)=O(nccritε)f(n) = O(n^{c_{\text{crit}} - \varepsilon})Θ(nccrit)\Theta(n^{c_{\text{crit}}})Leaves dominate
2f(n)=Θ(nccrit)f(n) = \Theta(n^{c_{\text{crit}}})Θ(nccritlgn)\Theta(n^{c_{\text{crit}}} \lg n)All levels equal
3f(n)=Ω(nccrit+ε)f(n) = \Omega(n^{c_{\text{crit}} + \varepsilon}) and regularity holdsΘ(f(n))\Theta(f(n))Root dominates
Gapf(n)/nccritf(n)/n^{c_{\text{crit}}} is polylogarithmicNot applicableUse tree method
Example recurrencea,ba,bccritc_{\text{crit}}f(n)f(n)CaseResult
T(n)=2T(n/2)+nT(n) = 2T(n/2) + n2,21nn2Θ(nlgn)\Theta(n \lg n)
T(n)=9T(n/3)+nT(n) = 9T(n/3) + n9,32nn1Θ(n2)\Theta(n^2)
T(n)=T(2n/3)+1T(n) = T(2n/3) + 11,1.50112Θ(lgn)\Theta(\lg n)
T(n)=3T(n/4)+nlgnT(n) = 3T(n/4) + n \lg n3,4\approx0.793nlgnn \lg n3Θ(nlgn)\Theta(n \lg n)
T(n)=2T(n/2)+nlgnT(n) = 2T(n/2) + n \lg n2,21nlgnn \lg nGapΘ(nlg2n)\Theta(n \lg^2 n)