TExcelTAD
Home › Foundations of Algorithm Analysis › When the Master Theorem Fails

When the Master Theorem Fails

The master theorem is fast, but it is not a universal solver. It needs a recurrence of the exact shape aT(n/b)+f(n)aT(n/b) + f(n) with constant a≥1a \ge 1 and b>1b > 1, and even then its three cases do not cover every ff. Knowing where it stops matters as much as knowing how to use it: applying it outside its conditions gives confident wrong answers. This lesson collects the typical failures, solves each one another way and ends with the Akra-Bazzi method, which covers much of what the master theorem misses.

Gap 1: smaller than the watershed, but not polynomially

Take

T(n)=2T(n/2)+nlg⁡n.T(n) = 2T(n/2) + \frac{n}{\lg n}.

The watershed is nlog⁡22=nn^{\log_2 2} = n, and f(n)=n/lg⁡nf(n) = n/\lg n is smaller. But case 1 needs f(n)=O(n1−ϵ)f(n) = O(n^{1 - \epsilon}) for some ϵ>0\epsilon > 0, and n/lg⁡nn / \lg n is not: dividing by lg⁡n\lg n is much less than dividing by any nϵn^\epsilon, since nϵ/lg⁡n→∞n^\epsilon / \lg n \to \infty. Case 2 needs f(n)=Θ(nlg⁡kn)f(n) = \Theta(n \lg^k n) with k≥0k \ge 0, and here k=−1k = -1. Neither applies.

The recursion tree still works. With n=2mn = 2^m, level jj has 2j2^j nodes of size n/2jn/2^j, so it costs

2j⋅n/2jlg⁡(n/2j)=nm−j.2^j \cdot \frac{n/2^j}{\lg(n/2^j)} = \frac{n}{m - j}.

Summing over the levels j=0,…,m−1j = 0, \ldots, m - 1 and adding the nn leaves:

T(n)=n(1m+1m−1+⋯+11)+n=n(Hm+1),T(n) = n \left( \frac{1}{m} + \frac{1}{m - 1} + \cdots + \frac{1}{1} \right) + n = n (H_m + 1),

where Hm=1+12+⋯+1mH_m = 1 + \frac{1}{2} + \cdots + \frac{1}{m} is the mm-th harmonic number, and Hm=ln⁡m+O(1)H_m = \ln m + O(1). Since m=lg⁡nm = \lg n,

T(n)=Θ(nlg⁡lg⁡n).T(n) = \Theta(n \lg \lg n).

That sits strictly between the Θ(n)\Theta(n) of case 1 and the Θ(nlg⁡n)\Theta(n \lg n) of case 2. The same calculation with f(n)=n/lg⁡2nf(n) = n / \lg^2 n gives level costs n/(m−j)2n/(m - j)^2, whose sum converges (to π2/6\pi^2/6 times nn), so that recurrence is Θ(n)\Theta(n).

python · try it yourself
import math

def T(f, m):
    """Exact T(2^m) for T(n) = 2 T(n/2) + f(n), T(1) = 1."""
    t = 1
    for j in range(1, m + 1):
        t = 2 * t + f(2 ** j, j)          # j = lg of the current size
    return t

print("  lg n  [n/lg n]: T/n   1 + H_m    [n/lg^2 n]: T/n")
for m in [8, 16, 32, 64, 128]:
    n = 2 ** m
    a = T(lambda size, j: size / j, m)
    b = T(lambda size, j: size / j**2, m)
    H = sum(1 / i for i in range(1, m + 1))
    print(f"{m:6}  {a / n:12.4f}  {1 + H:8.4f}  {b / n:16.4f}")
print("limit for n/lg^2 n: 1 + pi^2/6 =", round(1 + math.pi**2 / 6, 4))
  lg n  [n/lg n]: T/n   1 + H_m    [n/lg^2 n]: T/n
     8        3.7179    3.7179            2.5274
    16        4.3807    4.3807            2.5843
    32        5.0585    5.0585            2.6142
    64        5.7439    5.7439            2.6294
   128        6.4331    6.4331            2.6372
limit for n/lg^2 n: 1 + pi^2/6 = 2.6449

The first column grows without bound, but only like ln⁡lg⁡n\ln \lg n; the last column levels off at 1+π2/6≈2.6451 + \pi^2/6 \approx 2.645. Both are invisible to the three cases.

Note: some books extend case 2 to negative kk: f(n)=Θ(nlog⁡balg⁡−1n)f(n) = \Theta(n^{\log_b a} \lg^{-1} n) gives Θ(nlog⁡balg⁡lg⁡n)\Theta(n^{\log_b a} \lg \lg n), and k<−1k < -1 gives Θ(nlog⁡ba)\Theta(n^{\log_b a}). That is exactly what the computation above shows. Use it only when you know which version of the theorem you are quoting.

Gap 2: regularity fails

Case 3 has two conditions, and the second one is not decoration. Consider a=1a = 1, b=2b = 2 and a cost function that jumps between large and small on alternate levels, for n=2mn = 2^m:

f(n)={nif m is even,⌊n⌋if m is odd.f(n) = \begin{cases} n & \text{if } m \text{ is even}, \\ \lfloor \sqrt{n} \rfloor & \text{if } m \text{ is odd}. \end{cases}

The watershed is nlog⁡21=1n^{\log_2 1} = 1, and f(n)≥n/2f(n) \ge \sqrt{n}/2 always, so f(n)=Ω(n0+1/2)f(n) = \Omega(n^{0 + 1/2}): the first condition of case 3 holds with ϵ=1/2\epsilon = 1/2. Regularity asks for f(n/2)≤cf(n)f(n/2) \le c f(n) with c<1c < 1. At an odd mm, that would say n/2≤cnn/2 \le c\sqrt{n}, which fails for large nn. Case 3 would conclude T(n)=Θ(f(n))T(n) = \Theta(f(n)); the computation shows that is false.

python · try it yourself
import math

f = lambda m: 2 ** m if m % 2 == 0 else math.isqrt(2 ** m)

t = 1                                   # T(1)
print(" m       f(n)       T(n)   T(n)/f(n)   T(n)/n")
for m in range(1, 21):
    t = t + f(m)                        # T(2^m) = T(2^(m-1)) + f(2^m)
    if m >= 13:
        print(f"{m:2}  {f(m):9}  {t:9}  {t / f(m):10.2f}  {t / 2**m:7.3f}")
 m       f(n)       T(n)   T(n)/f(n)   T(n)/n
13         90       5637       62.63    0.688
14      16384      22021        1.34    1.344
15        181      22202      122.66    0.678
16      65536      87738        1.34    1.339
17        362      88100      243.37    0.672
18     262144     350244        1.34    1.336
19        724     350968      484.76    0.669
20    1048576    1399544        1.33    1.335

On the odd levels T(n)/f(n)T(n)/f(n) doubles every two steps, so T(n)≠Θ(f(n))T(n) \ne \Theta(f(n)). The last column shows what does hold: T(n)=Θ(n)T(n) = \Theta(n), because the previous even level already cost n/2n/2. For polynomial f(n)=npf(n) = n^p regularity is automatic, so this failure only happens with irregular cost functions, but such functions do turn up when the combine step's cost depends on the input's bit pattern.

Wrong shape: the theorem does not even apply

The master theorem says nothing about recurrences like these:

Recurrence Why it is outside the theorem Answer
T(n)=T(n−1)+nT(n) = T(n - 1) + n the size shrinks by subtraction, not division Θ(n2)\Theta(n^2)
T(n)=2T(n−1)+1T(n) = 2T(n - 1) + 1 subtraction again (Towers of Hanoi) 2n−12^n - 1 with T(1)=1T(1) = 1
T(n)=n T(n)+nT(n) = \sqrt{n}\, T(\sqrt{n}) + n aa is not a constant Θ(nlg⁡lg⁡n)\Theta(n \lg \lg n)
T(n)=12T(n/2)+nT(n) = \frac{1}{2} T(n/2) + n a<1a < 1 Θ(n)\Theta(n)
T(n)=T(n/3)+T(2n/3)+nT(n) = T(n/3) + T(2n/3) + n two different subproblem sizes Θ(nlg⁡n)\Theta(n \lg n)

Subtractive recurrences are solved by unrolling: T(n)=T(n−1)+nT(n) = T(n - 1) + n gives T(n)=T(1)+2+3+⋯+n=Θ(n2)T(n) = T(1) + 2 + 3 + \cdots + n = \Theta(n^2), and the Recurrences lesson solves Hanoi's. The recurrence with n\sqrt{n} falls to a change of variables: dividing by nn and setting S(n)=T(n)/nS(n) = T(n)/n gives S(n)=S(n)+1S(n) = S(\sqrt{n}) + 1, so S(n)=Θ(lg⁡lg⁡n)S(n) = \Theta(\lg \lg n). A recurrence with a<1a < 1 has no algorithmic meaning (a fraction of a recursive call), which is why the theorem excludes it.

The last row is the interesting one, because unequal splits are common, and that is what Akra-Bazzi handles.

A preview of Akra-Bazzi

The Akra-Bazzi theorem (1998) solves recurrences of the form

T(x)=∑i=1kai T(bix)+g(x),T(x) = \sum_{i=1}^{k} a_i\, T(b_i x) + g(x),

with constants ai>0a_i > 0 and 0<bi<10 < b_i < 1, and with gg nonnegative and growing at most polynomially (polynomials, logarithms and their products qualify). It also allows small perturbations of the arguments, such as floors and ceilings. The recipe has two steps.

  1. Find the unique real number pp with ∑i=1kaibi p=1\sum_{i=1}^{k} a_i b_i^{\,p} = 1.
  2. Then
T(x)=Θ(xp(1+∫1xg(u)up+1 du)).T(x) = \Theta\left( x^p \left( 1 + \int_1^x \frac{g(u)}{u^{p+1}} \, du \right) \right).

Example 1. T(n)=T(n/3)+T(2n/3)+nT(n) = T(n/3) + T(2n/3) + n. Here 13+23=1\frac{1}{3} + \frac{2}{3} = 1, so p=1p = 1, and ∫1xuu2 du=ln⁡x\int_1^x \frac{u}{u^2}\,du = \ln x. Hence T(n)=Θ(n(1+ln⁡n))=Θ(nlg⁡n)T(n) = \Theta(n(1 + \ln n)) = \Theta(n \lg n), the answer the recursion tree gave.

Example 2. T(n)=T(n/2)+T(n/4)+nT(n) = T(n/2) + T(n/4) + n. Now pp solves 2−p+4−p=12^{-p} + 4^{-p} = 1. Writing y=2−py = 2^{-p} gives y+y2=1y + y^2 = 1, so y=(5−1)/2y = (\sqrt{5} - 1)/2 and p=lg⁡1+52≈0.694p = \lg \frac{1 + \sqrt{5}}{2} \approx 0.694, less than 1. The integral is ∫1xu−p du=x1−p−11−p\int_1^x u^{-p}\,du = \frac{x^{1-p} - 1}{1 - p}, so T(n)=Θ(np⋅n1−p)=Θ(n)T(n) = \Theta(n^p \cdot n^{1-p}) = \Theta(n): the root's linear cost dominates, because the two children together are only 3/43/4 of the parent.

The program finds pp by bisection (the sum ∑aibi p\sum a_i b_i^{\,p} decreases in pp) and checks example 2 numerically.

python · try it yourself
import math
from functools import lru_cache

def akra_bazzi_p(terms):
    lo, hi = -20.0, 20.0
    for _ in range(100):
        mid = (lo + hi) / 2
        if sum(a * b ** mid for a, b in terms) > 1:
            lo = mid
        else:
            hi = mid
    return lo

print("p for T(n/3) + T(2n/3):", round(akra_bazzi_p([(1, 1/3), (1, 2/3)]), 6))
print("p for T(n/2) + T(n/4): ", round(akra_bazzi_p([(1, 1/2), (1, 1/4)]), 6),
      " lg of the golden ratio:", round(math.log2((1 + math.sqrt(5)) / 2), 6))
print("p for 2T(n/2) + 4T(n/4):", round(akra_bazzi_p([(2, 1/2), (4, 1/4)]), 6))

@lru_cache(None)
def T(n):
    return 1 if n < 4 else T(n // 2) + T(n // 4) + n

for n in [10**3, 10**5, 10**7]:
    print(f"n = {n:8}   T(n) / n = {T(n) / n:.4f}")
p for T(n/3) + T(2n/3): 1.0
p for T(n/2) + T(n/4):  0.694242  lg of the golden ratio: 0.694242
p for 2T(n/2) + 4T(n/4): 1.694242
n =     1000   T(n) / n = 3.3300
n =   100000   T(n) / n = 3.8536
n = 10000000   T(n) / n = 3.9665

The ratio T(n)/nT(n)/n approaches 4, which is what the guess T(n)≈cnT(n) \approx cn predicts: cn=n+cn/2+cn/4cn = n + cn/2 + cn/4 forces c=4c = 4. The third line mixes two subproblem sizes with six calls in all: 2⋅2−p+4⋅4−p=12 \cdot 2^{-p} + 4 \cdot 4^{-p} = 1 has the solution p=lg⁡(1+5)≈1.694p = \lg(1 + \sqrt{5}) \approx 1.694, so for any g(x)=O(x1.6)g(x) = O(x^{1.6}) the integral stays bounded and T(n)=Θ(n1.694…)T(n) = \Theta(n^{1.694\ldots}), a leaves-win answer in the style of case 1.

Test yourself

Why does the master theorem not apply to T(n)=2T(n/2)+n/lg⁡nT(n) = 2T(n/2) + n/\lg n?

  1. Because a=ba = b
  2. Because n/lg⁡nn/\lg n is smaller than nn but not by a polynomial factor nϵn^{\epsilon}, and it is not Θ(nlg⁡kn)\Theta(n \lg^k n) for any k≥0k \ge 0
  3. Because f(n)f(n) is not positive
  4. Because the regularity condition fails

What is the solution of T(n)=T(n−1)+nT(n) = T(n - 1) + n?

  1. Θ(nlg⁡n)\Theta(n \lg n)
  2. Θ(n)\Theta(n)
  3. Θ(n2)\Theta(n^2)
  4. Θ(2n)\Theta(2^n)

In the Akra-Bazzi method for T(n)=T(n/3)+T(2n/3)+nT(n) = T(n/3) + T(2n/3) + n, what is pp and what is the answer?

  1. p=1p = 1, Θ(nlg⁡n)\Theta(n \lg n)
  2. p=2p = 2, Θ(n2)\Theta(n^2)
  3. p=1/2p = 1/2, Θ(n)\Theta(\sqrt{n})
  4. p=1p = 1, Θ(n)\Theta(n)

In the regularity counterexample, f(n)=Ω(n1/2)f(n) = \Omega(n^{1/2}) holds but f(n/2)≤cf(n)f(n/2) \le c f(n) fails at odd levels. What goes wrong?

  1. Nothing, T(n)=Θ(f(n))T(n) = \Theta(f(n)) anyway
  2. T(n)T(n) is not Θ(f(n))\Theta(f(n)): at odd levels T(n)T(n) is about nn while f(n)f(n) is only about n\sqrt{n}
  3. T(n)T(n) becomes exponential
  4. The recursion does not terminate