Sub topic 12
Gaps and Non-Examples: When the Master Theorem Does Not Apply
This chapter assumes you know the statement of the master theorem, its three cases, the case 3 regularity condition, and the extended case 2 with a log^k n factor. Everything here is about the edges of that theorem. You will see where its hypotheses fail, why each failure matters, and which tool to use instead.
The master theorem solves recurrences of the form T(n) = aT(n/b) + f(n), where a ≥ 1 and b > 1 are constants and f(n) is asymptotically positive. Write c = log_b a for the critical exponent. A recurrence can fall outside the theorem in two ways:
- Shape failures. The recurrence is not of the form aT(n/b) + f(n) at all.
- Comparison failures. The shape is right, but f(n) does not fit any case.
Shape Failures
The theorem needs exactly one kind of subproblem. There must be a fixed number of copies, and each copy must have size n divided by a fixed constant. Each item below breaks one of those requirements.
| Recurrence | What breaks | Alternative |
|---|---|---|
| T(n) = T(n-1) + n | Subtracts from n instead of dividing it | Unrolling: Θ(n²) |
| T(n) = T(n-1) + T(n-2) + 1 | Subtractive, two terms | Characteristic equation: Θ(φⁿ) |
| T(n) = T(n/3) + T(2n/3) + n | Two different split ratios | Recursion tree or Akra-Bazzi: Θ(n log n) |
| T(n) = 2ⁿ T(n/2) + n | a is not a constant | Unroll directly |
| T(n) = 0.5 T(n/2) + n | a < 1, fewer than one subproblem | Not a sensible algorithm cost; unroll |
| T(n) = 2T(n/2) − n | f(n) is negative | Solve exactly; the theorem says nothing |
| T(n) = 2T(√n) + log n | Divides the exponent, not n | Change of variables |
Subtractive recurrences come from algorithms that peel off one element at a time, such as insertion sort's recursive form or naive Fibonacci. The recursion tree has depth n instead of log n, so the geometric structure the theorem relies on is missing. Later chapters cover the characteristic equation for these.
Uneven splits such as T(n/3) + T(2n/3) come from algorithms like quicksort with a fixed-ratio pivot, or median-of-medians selection. The recursion tree is lopsided: its leaves sit at different depths. A recursion tree still gives the bound here, since every full level sums to n and there are Θ(log n) levels. The Akra-Bazzi chapter gives a general formula.
Rescuing a Recurrence by Changing Variables
Some recurrences look wrong but turn into master-theorem form after a substitution. Take T(n) = 2T(√n) + log₂ n. Let m = log₂ n, so n = 2^m and √n = 2^(m/2). Define S(m) = T(2^m). Then:
S(m) = 2S(m/2) + m
This is merge sort's recurrence, so S(m) = Θ(m log m). Substituting back gives T(n) = Θ(log n · log log n).
Comparison Failures: The Gaps Between Cases
The three basic cases need a polynomial margin:
- Case 1: f(n) = O(n^(c−ε)) for some constant ε > 0.
- Case 2: f(n) = Θ(n^c).
- Case 3: f(n) = Ω(n^(c+ε)) for some ε > 0, plus regularity.
A function can grow faster than n^c by less than any polynomial factor, or slower than n^c by less than any polynomial factor. Such a function lands in a gap.
The Upper Gap: n^c log^k n
For T(n) = 2T(n/2) + n log n, we have c = 1, and f(n) = n log n is not Ω(n^(1+ε)) for any ε > 0, because log n grows more slowly than every n^ε. Basic case 3 does not apply. The extended case 2 covers this region: with f(n) = Θ(n^c log^k n) and k ≥ 0, T(n) = Θ(n^c log^(k+1) n). Here that gives Θ(n log² n).
The Lower Gap: n^c / log n
Now take T(n) = 2T(n/2) + n / log n. Again c = 1. The function f(n) = n / log n is smaller than n, but not by a polynomial factor, so case 1 fails. It is not Θ(n), so case 2 fails too. The extended case 2 as usually stated needs k ≥ 0, and here k = −1.
A recursion tree solves it. At depth i there are 2^i nodes, each costing (n/2^i) / log(n/2^i). With n = 2^p, level i contributes n / (p − i). Summing over i = 0 to p − 1 gives n(1/p + 1/(p−1) + … + 1/1) = n·Hp, where Hp is the p-th harmonic number. Since H_p = Θ(log p) and p = log n:
T(n) = Θ(n log log n)
The following code checks this numerically. It evaluates the recurrence on powers of two and divides by n · ln(log₂ n). The ratio drifts slowly toward 1, which matches the harmonic-sum analysis.
import math
def T(p):
# T(2**p) for T(n) = 2T(n/2) + n / log2(n), with T(1) = 1
if p == 0:
return 1.0
n = 2 ** p
return 2 * T(p - 1) + n / p
for p in (8, 16, 32, 64, 128):
n = 2 ** p
print(p, round(T(p) / (n * math.log(p)), 4))The recursion depth is only p, so this runs instantly even for n = 2^128.
Regularity Failures in Case 3
Case 3 needs a·f(n/b) ≤ k·f(n) for some constant k < 1 and all large n. (This constant is usually written c, but here c already names the critical exponent.) Regularity is what guarantees that the per-level costs shrink geometrically down the tree. Smooth polynomial-times-log functions always satisfy it. Oscillating functions can fail it.
Consider T(n) = T(n/2) + n(2 − cos n). Here a = 1, b = 2, c = 0, and f(n) lies between n and 3n, so f(n) = Ω(n^(0+ε)) with ε = 1. The growth test passes. Regularity requires f(n/2) ≤ k·f(n). Near n = 2π(2j+1), cos n ≈ 1 while cos(n/2) ≈ −1, so f(n/2) ≈ 3n/2 and f(n) ≈ n. The ratio approaches 3/2, which is above 1.
import math
def f(n):
return n * (2 - math.cos(n))
# Largest value of f(n/2) / f(n) over real n in [1, 2000)
worst = max(f(x / 2) / f(x) for x in (i * 0.01 for i in range(100, 200000)))
print(round(worst, 3)) # close to 1.5, so no constant k < 1 worksThe true answer here is still Θ(n): each level costs at most 3n/2^i, and that sum converges to at most 6n. So the theorem has not given a wrong answer. It cannot certify any answer, and you need a direct recursion-tree sum to get one.
A Decision Procedure
When you meet a recurrence, work through these checks in order:
- Is it of the form aT(n/b) + f(n) with constants a ≥ 1, b > 1, and f(n) positive for large n? If the argument shrinks by a root, try a change of variables first. If it is subtractive, use unrolling or the characteristic equation. If the splits are uneven, use a recursion tree or Akra-Bazzi.
- Compute c = log_b a and compare f(n) with n^c.
- If f(n) = Θ(n^c log^k n), use case 2 or the extended case 2.
- If f(n) differs from n^c by a polynomial factor, use case 1 or case 3. For case 3, check regularity explicitly when f oscillates or is defined piecewise.
- If f(n) sits in a gap, or regularity fails, draw the recursion tree and sum the levels. Then confirm the bound with substitution if you need a rigorous proof.
Exercise
Does the master theorem (basic three cases) apply to T(n) = 4T(n/2) + n² / log n? If not, find the bound with a recursion tree.
Show answer
No. With c = log2 4 = 2, f(n) = n²/log n is smaller than n² by only a log factor, so it lies in the gap between case 1 and case 2. At depth i there are 4^i nodes, each costing (n/2^i)² / log(n/2^i), so level i costs n² / (log n − i). Summing over the levels gives n²·H(log n) = Θ(n² log log n).
Exercise
Solve T(n) = 2T(√n) + 1.
Show answer
Let m = log₂ n and S(m) = T(2^m). Then S(m) = 2S(m/2) + 1. Here c = 1 and f(m) = 1 = O(m^(1−ε)), so case 1 gives S(m) = Θ(m). Therefore T(n) = Θ(log n).
Exercise
For each recurrence, say whether the master theorem applies, and why or why not: (a) T(n) = T(n−2) + n, (b) T(n) = 3T(n/3) + n log² n, (c) T(n) = 0.5T(n/2) + 1/n.
Show answer
(a) No. It is subtractive, not a division by b. Unrolling gives Θ(n²). (b) Yes, through the extended case 2 with c = 1 and k = 2, which gives Θ(n log³ n). (c) No. It has a < 1, and f(n) = 1/n is a decreasing cost, so the hypotheses fail.