Simple Algorithm
Algorithms & Data Structures · Optimal · 28 min

The Master Theorem & Akra–Bazzi

Solving divide-and-conquer recurrences, with proofs.

In 1980 Bentley, Haken and Saxe gave one recipe for the recurrences of divide-and-conquer algorithms, and textbooks turned it into the master theorem. Underneath, it is a statement about a geometric series. Once you see the series, you can prove the theorem, extend it to 2T(n/2)+n/log⁡n2T(n/2)+n/\log n, and go beyond it to the unequal splits of median of medians.

What you’ll learn

  • State the master theorem exactly, with its ε\varepsilon, its log⁡k\log^k factor and the regularity condition, and apply it to recurrences from real algorithms.
  • Prove the theorem for exact powers of bb from the recursion tree, and derive the extended case 2 for every real kk.
  • Explain how floors and ceilings are handled, and why the regularity condition is sufficient but not necessary.
  • Solve recurrences with unequal splits by the Akra–Bazzi theorem, and sketch its proof.

The theorem, exactly

The master theorem asks how ff compares with nlog⁡ban^{\log_b a} polynomially: in cases 1 and 3 one must beat the other by a factor nεn^{\varepsilon}. Case 2 allows extra log factors. Older editions stated it only for k=0k=0, which left 2T(n/2)+nlog⁡n2T(n/2)+n\log n unsolved; with k=1k=1 it is Θ(nlog⁡2n)\Theta(n\log^2 n).

The regularity condition is rarely a burden: for f(n)=nclog⁡knf(n)=n^c\log^k n with c>log⁡bac>\log_b a, a f(n/b)≤(a/bc) f(n)a\,f(n/b)\le(a/b^c)\,f(n) and a/bc<1a/b^c<1. It exists to exclude driving functions that wobble, as you will see.

Recurrencelog⁡ba\log_b aComparisonSolution
9T(n/3)+n9T(n/3)+n22n=O(n2−ε)n=O(n^{2-\varepsilon}): case 1Θ(n2)\Theta(n^2)
T(2n/3)+1T(2n/3)+1001=Θ(n0)1=\Theta(n^0): case 2, k=0k=0Θ(log⁡n)\Theta(\log n)
4T(n/2)+n2log⁡n4T(n/2)+n^2\log n22case 2, k=1k=1Θ(n2log⁡2n)\Theta(n^2\log^2 n)
3T(n/4)+nlog⁡n3T(n/4)+n\log n≈0.79\approx0.79case 3; 3 n4log⁡n4≤34 nlog⁡n3\,\tfrac n4\log\tfrac n4\le\tfrac34\,n\log nΘ(nlog⁡n)\Theta(n\log n)
2T(n/2)+n/log⁡n2T(n/2)+n/\log n11no case: k=−1k=-1 is not allowedΘ(nlog⁡log⁡n)\Theta(n\log\log n), see the proof
Check your understanding

Which case of the master theorem solves T(n)=2T(n/4)+nT(n)=2T(n/4)+\sqrt n, and what is the answer?

  1. Case 1: Θ(n)\Theta(\sqrt n)
  2. Case 3: Θ(n)\Theta(\sqrt n)
  3. Case 2: Θ(nlog⁡n)\Theta(\sqrt n\log n)
  4. No case applies
Show the answer

C. Case 2: Θ(nlog⁡n)\Theta(\sqrt n\log n)

log⁡42=12\log_4 2=\tfrac12, so the watershed function is n1/2n^{1/2}, exactly f(n)f(n): case 2 with k=0k=0 gives Θ(nlog⁡n)\Theta(\sqrt n\log n). Every level of the tree costs 2jn/4j=n2^j\sqrt{n/4^j}=\sqrt n.

The proof, from the recursion tree

Take n=bmn=b^m so that every division is exact, and let T(1)=Θ(1)T(1)=\Theta(1). Level jj of the recursion tree has aja^j nodes of size n/bjn/b^j, and the bottom level has am=alog⁡bn=nlog⁡baa^m=a^{\log_b n}=n^{\log_b a} leaves. Summing level by level gives the lemma everything rests on:

T(n) = Θ(nlog⁡ba) + ∑j=0m−1ajf ⁣(nbj)⏟g(n),m=log⁡bnT(n)\ =\ \Theta\big(n^{\log_b a}\big)\ +\ \underbrace{\sum_{j=0}^{m-1}a^j f\!\Big(\frac{n}{b^j}\Big)}_{g(n)},\qquad m=\log_b n
The leaves cost Θ(nlog⁡ba)\Theta(n^{\log_b a}); g(n)g(n) is the work of all the internal nodes. Each case bounds gg.
DerivationBounding g(n) in the three cases
  1. g(n) ≤ C nlog⁡ba−ε∑j=0m−1(bε)j = C nlog⁡ba−ε bεm−1bε−1 = O(nlog⁡ba)g(n)\ \le\ C\,n^{\log_b a-\varepsilon}\sum_{j=0}^{m-1}\big(b^{\varepsilon}\big)^j\ =\ C\,n^{\log_b a-\varepsilon}\,\frac{b^{\varepsilon m}-1}{b^{\varepsilon}-1}\ =\ O\big(n^{\log_b a}\big)

    Case 1. With f(x)≤C xlog⁡ba−εf(x)\le C\,x^{\log_b a-\varepsilon}, and a/blog⁡ba=1a/b^{\log_b a}=1, each term is a geometric term with ratio bε>1b^{\varepsilon}>1:

  2. g(n) = Θ(nlog⁡ba∑i=1mik) = Θ(nlog⁡ba mk+1) = Θ(nlog⁡balog⁡k+1n)(k≥0)g(n)\ =\ \Theta\Big(n^{\log_b a}\sum_{i=1}^{m}i^{k}\Big)\ =\ \Theta\big(n^{\log_b a}\,m^{k+1}\big)\ =\ \Theta\big(n^{\log_b a}\log^{k+1}n\big)\quad(k\ge0)

    So the leaves dominate: T(n)=Θ(nlog⁡ba)T(n)=\Theta(n^{\log_b a}). Case 2. Now ajf(n/bj)=Θ(nlog⁡balog⁡k(n/bj))a^jf(n/b^j)=\Theta\big(n^{\log_b a}\log^k(n/b^j)\big), and log⁡(n/bj)=(m−j)log⁡b\log(n/b^j)=(m-j)\log b. Substitute i=m−ji=m-j:

  3. f(n) ≤ g(n) ≤ f(n)1−c⟹T(n)=Θ(f(n))f(n)\ \le\ g(n)\ \le\ \frac{f(n)}{1-c}\qquad\Longrightarrow\qquad T(n)=\Theta(f(n))

    Case 3. Regularity applied jj times gives ajf(n/bj)≤cjf(n)a^jf(n/b^j)\le c^jf(n), a decreasing geometric series. The root alone already contributes f(n)f(n), and f(n)=Ω(nlog⁡ba+ε)f(n)=\Omega(n^{\log_b a+\varepsilon}) outweighs the leaves:

  4. f(n)=Θ(nlog⁡balog⁡kn) ⟹ T(n)={Θ(nlog⁡balog⁡k+1n)k>−1Θ(nlog⁡balog⁡log⁡n)k=−1Θ(nlog⁡ba)k<−1f(n)=\Theta\big(n^{\log_b a}\log^k n\big)\ \Longrightarrow\ T(n)=\begin{cases}\Theta\big(n^{\log_b a}\log^{k+1}n\big) & k>-1\\[2pt] \Theta\big(n^{\log_b a}\log\log n\big) & k=-1\\[2pt] \Theta\big(n^{\log_b a}\big) & k<-1\end{cases}

    Every real kk. Case 2's proof never used k≥0k\ge0 except in summing ∑i≤mik\sum_{i\le m}i^k, which is Θ(mk+1)\Theta(m^{k+1}) for k>−1k>-1, the harmonic number Hm=Θ(log⁡m)H_m=\Theta(\log m) for k=−1k=-1, and a convergent series for k<−1k<-1. That is the extended case 2:

Think first Use the extended case 2 to solve T(n)=2T(n/2)+n/log⁡2nT(n)=2T(n/2)+n/\log^2 n. Which levels of the recursion tree carry most of the work?

Here k=−2<−1k=-2<-1, so T(n)=Θ(n)T(n)=\Theta(n): the log factor disappears. Level jj costs n/log⁡22(n/2j)=n/(m−j)2n/\log_2^2(n/2^j)=n/(m-j)^2, so the levels near the leaves, where m−jm-j is small, carry most of the weight, and all internal levels together add at most n∑i1/i2=(π2/6) nn\sum_i1/i^2=(\pi^2/6)\,n. As in case 1 the leaves set the order of growth, but the level costs now shrink only polynomially toward the root instead of geometrically.

Floors, ceilings and wobbly driving functions

Real code splits nn into ⌊n/2⌋\lfloor n/2\rfloor and ⌈n/2⌉\lceil n/2\rceil, so nn is rarely a power of bb. The fix is to bound the sizes. With ceilings, the size njn_j at depth jj satisfies nj≤nj−1/b+1n_j\le n_{j-1}/b+1, so

nj ≤ nbj+∑i≥0b−i = nbj+bb−1,n_j\ \le\ \frac{n}{b^j}+\sum_{i\ge0}b^{-i}\ =\ \frac{n}{b^j}+\frac{b}{b-1},

and with floors nj≥n/bj−b/(b−1)n_j\ge n/b^j-b/(b-1). Down to depth ⌊log⁡bn⌋\lfloor\log_b n\rfloor every size is within an additive constant of n/bjn/b^j, a constant number of extra levels reaches the base cases, and for polynomially bounded ff such as nclog⁡knn^c\log^k n one has f(nj)=Θ(f(n/bj))f(n_j)=\Theta(f(n/b^j)). The three sums of the proof reappear with different constants, and the theorem holds for every nn.

The same comparison principle rescues many near misses: if f1≤f≤f2f_1\le f\le f_2 and the recurrences driven by f1f_1 and f2f_2 have the same order of growth, so does yours. When the gap is real, as between nn and n/log⁡nn/\log n, go back to the level sums.

Akra–Bazzi: unequal splits

Median of medians splits nn elements into recursive calls on about n/5n/5 and 7n/107n/10 elements; an unbalanced partition splits into n/3n/3 and 2n/32n/3. No single bb describes these, and the master theorem is silent. Akra and Bazzi found the general rule, and Leighton gave a cleaner statement with a short inductive proof.

The exponent pp exists and is unique because ∑iaibi p\sum_ia_ib_i^{\,p} falls continuously and strictly from ∞\infty to 00 as pp grows. With one term, ab−p=1ab^{-p}=1 gives p=log⁡bap=\log_b a, and g(u)=ucg(u)=u^c makes the integral Θ(1)\Theta(1), Θ(log⁡x)\Theta(\log x) or Θ(xc−p)\Theta(x^{c-p}) as cc is below, equal to or above pp: the three cases of the master theorem, with floors, ceilings and small shifts included, and no regularity condition. The Akra–Bazzi theorem needs only polynomial growth of gg.

RecurrenceSourceppSolution
T(n/5)+T(7n/10)+nT(n/5)+T(7n/10)+nmedian of medians≈0.84\approx0.84Θ(n)\Theta(n)
T(n/3)+T(2n/3)+nT(n/3)+T(2n/3)+nan uneven partition11Θ(nlog⁡n)\Theta(n\log n)
2T(n/4)+3T(n/6)+nlog⁡n2T(n/4)+3T(n/6)+n\log ntwo kinds of subproblems11Θ(nlog⁡2n)\Theta(n\log^2n)
2T(⌊n/2⌋+17)+n2T(\lfloor n/2\rfloor+17)+n, base cases up to 34merge sort with padding11Θ(nlog⁡n)\Theta(n\log n)
In the last row the shift h(n)h(n) is bounded, far inside O(n/log⁡2n)O(n/\log^2n). The base cases must reach up to 34, because ⌊34/2⌋+17=34\lfloor34/2\rfloor+17=34 would make no progress.
Predict before you look

For T(n)=2T(n/2)+8T(n/4)+n2T(n)=2T(n/2)+8T(n/4)+n^2, find the Akra–Bazzi exponent pp, the solution of 2⋅2−p+8⋅4−p=12\cdot2^{-p}+8\cdot4^{-p}=1.

Show the answer

2

Put y=2−py=2^{-p}: then 2y+8y2=12y+8y^2=1, so y=1/4y=1/4 and p=2p=2. The integral is ∫1nu2/u3 du=ln⁡n\int_1^n u^2/u^3\,du=\ln n, so T(n)=Θ(n2log⁡n)T(n)=\Theta(n^2\log n): the driving function matches the watershed npn^p, the analogue of case 2.

DerivationProof sketch: why x^p(1 + ∫ g/u^(p+1)) is the right guess
  1. ∑iaiG(bix) = G(x) − xp∑iaibi p∫bixxg(u)up+1 du\sum_i a_iG(b_ix)\ =\ G(x)\ -\ x^p\sum_i a_ib_i^{\,p}\int_{b_ix}^{x}\frac{g(u)}{u^{p+1}}\,du

    Let G(x)=xp(1+∫1xg(u)u−p−1du)G(x)=x^p\big(1+\int_1^x g(u)u^{-p-1}du\big), and ignore the hih_i for now. Split each integral at bixb_ix and use ∑iaibi p=1\sum_ia_ib_i^{\,p}=1:

  2. ∑iaiG(bix)+g(x) = G(x) − Θ(g(x)) + g(x)\sum_i a_iG(b_ix)+g(x)\ =\ G(x)\ -\ \Theta\big(g(x)\big)\ +\ g(x)

    On [bix,x][b_ix,x], polynomial growth gives g(u)=Θ(g(x))g(u)=\Theta(g(x)), also u−p−1=Θ(x−p−1)u^{-p-1}=\Theta(x^{-p-1}), and the interval has length Θ(x)\Theta(x). So the subtracted term is Θ(g(x))\Theta(g(x)), and GG satisfies the recurrence up to an error of the same order as gg:

  3. c′ G(x) ≤ T(x) ≤ c G(x)c'\,G(x)\ \le\ T(x)\ \le\ c\,G(x)

    Induction. If T(y)≤c G(y)T(y)\le c\,G(y) for smaller yy, then T(x)≤c∑iaiG(bix)+g(x)=c G(x)−c Θ(g(x))+g(x)≤c G(x)T(x)\le c\sum_ia_iG(b_ix)+g(x)=c\,G(x)-c\,\Theta(g(x))+g(x)\le c\,G(x) once cc is large enough for c Θ(g(x))c\,\Theta(g(x)) to absorb g(x)g(x). The lower bound T≥c′GT\ge c'G is symmetric with c′c' small. The perturbations hih_i are absorbed by strengthening the hypothesis with a factor such as 1±1/log⁡x1\pm1/\log x, which is where ∣hi(x)∣=O(x/log⁡2x)\abs{h_i(x)}=O(x/\log^2x) is used.

Exact solutions, random splits, and history

Θ\Theta hides structure. Merge sort's worst-case number of comparisons satisfies W(n)=W(⌈n/2⌉)+W(⌊n/2⌋)+n−1W(n)=W(\lceil n/2\rceil)+W(\lfloor n/2\rfloor)+n-1 with W(1)=0W(1)=0, and its exact solution is

W(n)=n⌈log⁡2n⌉−2⌈log⁡2n⌉+1.W(n)=n\lceil\log_2n\rceil-2^{\lceil\log_2n\rceil}+1.

Writing θ=⌈log⁡2n⌉−log⁡2n∈[0,1)\theta=\lceil\log_2n\rceil-\log_2n\in[0,1) gives W(n)=nlog⁡2n+n(θ−2θ)+1W(n)=n\log_2n+n(\theta-2^{\theta})+1: the coefficient of nn is not a constant but a periodic function of log⁡2n\log_2n, oscillating between −1-1 and about −0.914-0.914. For n=1000n=1000, W=8977W=8977. Precise analyses of divide-and-conquer recurrences, by Mellin transforms (Flajolet and Golin, 1994) and by the discrete master theorem of Drmota and Szpankowski (2013), explain when such periodic terms appear.

Randomized divide and conquer brings recurrences with random subproblem sizes, as in quickselect, where a random pivot leaves a random fraction of the array. Karp (1994) gave a cookbook for recurrences of the form T(x)=a(x)+T(H(x))T(x)=a(x)+T(H(x)) with a random size H(x)H(x): under mild monotonicity conditions, if E[H(x)]≤m(x)\E[H(x)]\le m(x), let τ\tau solve the deterministic recurrence τ(x)=a(x)+τ(m(x))\tau(x)=a(x)+\tau(m(x)); then the random cost exceeds τ(x)+w a(x)\tau(x)+w\,a(x) only with a probability that decays exponentially in ww. Replacing the random size by its expectation is safe, with high probability.

Key takeaways

  • The master theorem compares f(n)f(n) with the watershed nlog⁡ban^{\log_b a}: polynomially smaller gives Θ(nlog⁡ba)\Theta(n^{\log_b a}), equal up to log⁡kn\log^k n gives Θ(nlog⁡balog⁡k+1n)\Theta(n^{\log_b a}\log^{k+1}n), polynomially larger and regular gives Θ(f(n))\Theta(f(n)).
  • The proof is the recursion-tree identity T(n)=Θ(nlog⁡ba)+∑jajf(n/bj)T(n)=\Theta(n^{\log_b a})+\sum_j a^jf(n/b^j): an increasing geometric series, a flat one, or a decreasing one.
  • The same sum yields the extended case 2 for every real kk: log⁡log⁡n\log\log n appears at k=−1k=-1, and for k<−1k<-1 the log factor disappears.
  • Akra–Bazzi handles unequal splits and small shifts: pick pp with ∑iaibi p=1\sum_ia_ib_i^{\,p}=1, then T(x)=Θ(xp(1+∫1xg(u)u−p−1du))T(x)=\Theta\big(x^p(1+\int_1^xg(u)u^{-p-1}du)\big), with no regularity condition.
  • Θ\Theta hides periodic lower-order terms, such as the oscillating linear term of merge sort's exact cost, and random splits need probabilistic recurrences such as Karp's.

Check yourself

In the interactive lesson these are scored with your confidence, and what you miss comes back for review.

Question 1

Why does case 3 of the master theorem need the regularity condition a f(n/b)≤c f(n)a\,f(n/b)\le c\,f(n) with c<1c<1?

  1. To make the level costs a decreasing geometric series, so that the root's cost f(n)f(n) dominates
  2. To guarantee that ff is polynomially larger than nlog⁡ban^{\log_b a}
  3. To handle floors and ceilings
  4. Because without it T(n)T(n) may not be defined
Show the answer

A. To make the level costs a decreasing geometric series, so that the root's cost f(n)f(n) dominates

Iterating it gives ajf(n/bj)≤cjf(n)a^jf(n/b^j)\le c^jf(n), so the internal levels sum to at most f(n)/(1−c)f(n)/(1-c). It is sufficient, not necessary: T(n)=T(n/2)+n(2−cos⁡n)T(n)=T(n/2)+n(2-\cos n) violates it, yet n≤f(n)≤3nn\le f(n)\le3n shows T(n)=Θ(n)T(n)=\Theta(n) by comparison.

Question 2

What is the solution of T(n)=4T(n/2)+n2/log⁡nT(n)=4T(n/2)+n^2/\log n?

  1. Θ(n2)\Theta(n^2)
  2. Θ(n2log⁡n)\Theta(n^2\log n)
  3. Θ(n2log⁡log⁡n)\Theta(n^2\log\log n)
  4. Θ(n2/log⁡n)\Theta(n^2/\log n)
Show the answer

C. Θ(n2log⁡log⁡n)\Theta(n^2\log\log n)

The watershed function is nlog⁡24=n2n^{\log_2 4}=n^2 and f(n)=n2log⁡−1nf(n)=n^2\log^{-1}n: the extended case 2 with k=−1k=-1. The levels cost n2/(m−j)n^2/(m-j) in turn, which sum to n2Hmn^2H_m, so T(n)=Θ(n2log⁡log⁡n)T(n)=\Theta(n^2\log\log n).

Question 3

For T(n)=T(n/5)+T(7n/10)+nT(n)=T(n/5)+T(7n/10)+n, the Akra–Bazzi exponent pp solves (1/5)p+(7/10)p=1(1/5)^p+(7/10)^p=1. Why is T(n)=Θ(n)T(n)=\Theta(n)?

  1. Because p=1p=1, so the answer is Θ(nlog⁡n)\Theta(n\log n) divided by log⁡n\log n
  2. Because 1/5+7/10<11/5+7/10<1, so p<1p<1, and then the driving function nn dominates
  3. Because the master theorem applies with a=2a=2, b=10/7b=10/7
  4. Because both subproblems are smaller than nn
Show the answer

B. Because 1/5+7/10<11/5+7/10<1, so p<1p<1, and then the driving function nn dominates

At p=1p=1 the sum is 0.9<10.9<1, and the sum decreases in pp, so p≈0.84<1p\approx0.84<1. Then ∫1nu/up+1 du=Θ(n1−p)\int_1^n u/u^{p+1}\,du=\Theta(n^{1-p}), and T(n)=Θ(np⋅n1−p)=Θ(n)T(n)=\Theta\big(n^p\cdot n^{1-p}\big)=\Theta(n). In the recursion tree, the level costs shrink by the factor 0.90.9: a decreasing geometric series dominated by the root.

Question 4

Merge sort's worst-case comparison count is exactly W(n)=n⌈log⁡2n⌉−2⌈log⁡2n⌉+1W(n)=n\lceil\log_2n\rceil-2^{\lceil\log_2n\rceil}+1. Which statement is true?

  1. W(n)=nlog⁡2n+O(1)W(n)=n\log_2n+O(1)
  2. W(n)−nlog⁡2nW(n)-n\log_2n is Θ(n)\Theta(n), with a coefficient of nn that oscillates periodically in log⁡2n\log_2n instead of converging
  3. W(n)/(nlog⁡2n)W(n)/(n\log_2n) oscillates forever without a limit
  4. The formula contradicts the master theorem, which predicts Θ(nlog⁡n)\Theta(n\log n)
Show the answer

B. W(n)−nlog⁡2nW(n)-n\log_2n is Θ(n)\Theta(n), with a coefficient of nn that oscillates periodically in log⁡2n\log_2n instead of converging

With θ=⌈log⁡2n⌉−log⁡2n\theta=\lceil\log_2n\rceil-\log_2n, W(n)=nlog⁡2n+n(θ−2θ)+1W(n)=n\log_2n+n(\theta-2^{\theta})+1, and θ−2θ\theta-2^{\theta} wanders between −1-1 and about −0.914-0.914 as nn grows. The master theorem's Θ(nlog⁡n)\Theta(n\log n) is correct; it just hides this periodic second-order term.

Further reading

  • T. H. Cormen, C. E. Leiserson, R. L. Rivest & C. Stein, Introduction to Algorithms, 4th ed. (MIT Press, 2022), chapter 4.
  • J. L. Bentley, D. Haken & J. B. Saxe, "A general method for solving divide-and-conquer recurrences," SIGACT News 12(3) (1980), 36–44.
  • M. Akra & L. Bazzi, "On the solution of linear recurrence equations," Computational Optimization and Applications 10(2) (1998), 195–210.
  • T. Leighton, "Notes on better master theorems for divide-and-conquer recurrences," MIT lecture notes (1996).