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 with constant and , and even then its three cases do not cover every . 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
The watershed is , and is smaller. But case 1 needs for some , and is not: dividing by is much less than dividing by any , since . Case 2 needs with , and here . Neither applies.
The recursion tree still works. With , level has nodes of size , so it costs
Summing over the levels and adding the leaves:
where is the -th harmonic number, and . Since ,
That sits strictly between the of case 1 and the of case 2. The same calculation with gives level costs , whose sum converges (to times ), so that recurrence is .
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 ; the last column levels off at . Both are invisible to the three cases.
Note: some books extend case 2 to negative : gives , and gives . 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 , and a cost function that jumps between large and small on alternate levels, for :
The watershed is , and always, so : the first condition of case 3 holds with . Regularity asks for with . At an odd , that would say , which fails for large . Case 3 would conclude ; the computation shows that is false.
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 doubles every two steps, so . The last column shows what does hold: , because the previous even level already cost . For polynomial 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 |
|---|---|---|
| the size shrinks by subtraction, not division | ||
| subtraction again (Towers of Hanoi) | with | |
| is not a constant | ||
| two different subproblem sizes |
Subtractive recurrences are solved by unrolling: gives , and the Recurrences lesson solves Hanoi's. The recurrence with falls to a change of variables: dividing by and setting gives , so . A recurrence with 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
with constants and , and with 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.
- Find the unique real number with .
- Then
Example 1. . Here , so , and . Hence , the answer the recursion tree gave.
Example 2. . Now solves . Writing gives , so and , less than 1. The integral is , so : the root's linear cost dominates, because the two children together are only of the parent.
The program finds by bisection (the sum decreases in ) and checks example 2 numerically.
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 approaches 4, which is what the guess predicts: forces . The third line mixes two subproblem sizes with six calls in all: has the solution , so for any the integral stays bounded and , a leaves-win answer in the style of case 1.
Test yourself
Why does the master theorem not apply to ?
- Because
- Because is smaller than but not by a polynomial factor , and it is not for any
- Because is not positive
- Because the regularity condition fails
What is the solution of ?
In the Akra-Bazzi method for , what is and what is the answer?
- ,
- ,
- ,
- ,
In the regularity counterexample, holds but fails at odd levels. What goes wrong?
- Nothing, anyway
- is not : at odd levels is about while is only about
- becomes exponential
- The recursion does not terminate