Sub topic 6
The Master Theorem for T(n) = aT(n/b) + f(n)
This chapter assumes you can read recursive code and turn it into a recurrence. It also assumes you are comfortable with Big-Theta, Big-O and Big-Omega, and with logarithm identities such as log_b a = ln a / ln b. Recursion trees from the earlier chapter are useful background. Here you will not need to draw one.
Recursion trees and substitution work on almost any recurrence, but they take effort each time. Many divide-and-conquer algorithms produce recurrences with the same shape. The master theorem handles that whole shape at once. You read off three parameters, make one comparison, and get a Theta bound. This chapter states the theorem precisely and teaches the part people most often get wrong: pulling the right a, b and f(n) out of an algorithm. The comparison itself and each case get their own chapters after this one.
The Standard Form
The master theorem applies to recurrences of the form
T(n) = a·T(n/b) + f(n)
where each symbol has a specific meaning and a specific requirement.
| Symbol | Meaning | Requirement |
|---|---|---|
| a | Number of recursive calls made on each invocation | Constant, a ≥ 1 |
| b | Factor by which the input shrinks in each call | Constant, b > 1 |
| n/b | Size of each subproblem | Floor or ceiling allowed |
| f(n) | Work done outside the recursive calls (divide plus combine) | Asymptotically positive |
"Constant" means the value does not depend on n. "Asymptotically positive" means f(n) > 0 for all sufficiently large n. The base case, T(n) = Θ(1) for n below some constant, is usually left implicit. Changing it changes the constant factors and leaves the Theta bound unchanged.
The Statement
Let a ≥ 1 and b > 1 be constants, let f(n) be asymptotically positive, and let T(n) = aT(n/b) + f(n), where n/b may mean ⌊n/b⌋ or ⌈n/b⌉. Define the critical exponent c* = log_b a. Then:
- If f(n) = O(n^(c − ε)) for some constant ε > 0, then T(n) = Θ(n^(c)).
- If f(n) = Θ(n^(c)), then T(n) = Θ(n^(c) log n).
- If f(n) = Ω(n^(c + ε)) for some constant ε > 0, and* a·f(n/b) ≤ k·f(n) for some constant k < 1 and all sufficiently large n, then T(n) = Θ(f(n)).
The function n^(c*) is the cost of the leaves of the recursion tree. The tree has depth logb n and branching factor a, so it has a^(logb n) = n^(log_b a) leaves. Each case answers one question: does the leaf count or the per-call work f(n) grow faster?
- In case 1, f(n) is polynomially smaller than n^(c*), so the leaves win.
- In case 2, the two match, so every level contributes about the same amount and you pay an extra log n factor for the number of levels.
- In case 3, f(n) is polynomially larger, so the root's work dominates. The extra condition, called the regularity condition, makes sure the work really shrinks as you go down the tree.
Identifying a, b, and f(n) From Code
Most errors with the master theorem happen before the comparison, when you read the parameters off the code. Work through each one in order.
a: calls actually made, not calls written
Count the recursive calls that run on a single invocation. Recursive binary search has two recursive calls in its source, but they sit in different branches of an if, and only one runs. So a = 1, not 2. Merge sort runs both of its calls every time, so a = 2.
b: the shrink factor, not the number of pieces
b is defined by subproblem size: each call gets input of size n/b. Karatsuba multiplication splits each number into halves and makes three recursive multiplications on half-size numbers. That gives a = 3 and b = 2. People often assume a = b because merge sort has a = b = 2, but the two parameters are independent.
All subproblems must have the same size n/b, up to floors and ceilings. If the sizes are n/3 and 2n/3, the recurrence is not in master form.
f(n): everything except the recursive calls
f(n) covers the divide step, the combine step and any hidden overhead. Hidden overhead is where people slip up. In Python, xs[:mid] copies mid elements, which costs Θ(n). Merge sort already does Θ(n) merging, so its slices don't change f(n). Binary search is different. If you write it to recurse on xs[mid+1:] instead of passing index bounds, f(n) becomes Θ(n) and the recurrence becomes T(n) = T(n/2) + Θ(n). That solves to Θ(n), which is no faster than a linear scan.
Worked Identifications
| Recurrence | Source | a | b | f(n) | c* = log_b a | In master form? |
|---|---|---|---|---|---|---|
| T(n) = 2T(n/2) + Θ(n) | Merge sort | 2 | 2 | n | 1 | Yes |
| T(n) = T(n/2) + Θ(1) | Binary search | 1 | 2 | 1 | 0 | Yes |
| T(n) = 3T(n/2) + Θ(n) | Karatsuba | 3 | 2 | n | ≈ 1.585 | Yes |
| T(n) = 7T(n/2) + Θ(n²) | Strassen | 7 | 2 | n² | ≈ 2.807 | Yes |
| T(n) = 8T(n/2) + Θ(n²) | Naive recursive matrix multiply | 8 | 2 | n² | 3 | Yes |
| T(n) = T(n−1) + n | Recursive selection sort | No: size shrinks by subtraction, not division | ||||
| T(n) = T(n/3) + T(2n/3) + n | Uneven split | No: subproblems of different sizes | ||||
| T(n) = 0.5·T(n/2) + n | 0.5 | 2 | n | No: a < 1 | ||
| T(n) = 2ⁿ·T(n/2) + 1 | 2ⁿ | 2 | 1 | No: a is not constant |
Rows marked "No" are not hopeless cases. They need a different tool: unrolling for the subtraction case, Akra-Bazzi for the uneven split, and so on. The table only records that the master theorem cannot be applied as stated.
Checking the Parameters Numerically
You can check a parameter identification by evaluating the recurrence directly and dividing by the leaf cost n^(c*). The script below prints the critical exponent for several recurrences. It then evaluates Karatsuba's recurrence for powers of two and shows that T(n) / n^(log₂ 3) settles toward a constant, which is what case 1 predicts.
import math
from functools import lru_cache
def critical_exponent(a, b):
if a < 1 or b <= 1:
raise ValueError("master form requires a >= 1 and b > 1")
return math.log(a) / math.log(b)
examples = {
"merge sort 2T(n/2)+n": (2, 2),
"binary search T(n/2)+1": (1, 2),
"Karatsuba 3T(n/2)+n": (3, 2),
"Strassen 7T(n/2)+n^2": (7, 2),
"naive matmul 8T(n/2)+n^2": (8, 2),
}
for name, (a, b) in examples.items():
print(f"{name:28s} log_b a = {critical_exponent(a, b):.3f}")
def make_T(a, b, f):
@lru_cache(maxsize=None)
def T(n):
if n <= 1:
return 1
return a * T(n // b) + f(n)
return T
a, b = 3, 2
T = make_T(a, b, lambda n: n)
c = critical_exponent(a, b)
for p in range(4, 21, 4):
n = 2 ** p
print(n, T(n), round(T(n) / n ** c, 4))
# Ratios: 2.6049, 2.922, 2.9846, 2.997, 2.9994 -> approaching 3For n = 2^k the exact solution is T(n) = 3·3^k − 2n, so the ratio is 3 − 2n/3^k, which tends to 3. If you had misread Karatsuba as a = 2, you would expect Θ(n log n), and the ratio T(n)/(n log n) would keep growing. That growing ratio would expose the mistake.
What the Theorem Does Not Promise
The master theorem gives a Theta bound for the recurrence. It does not give exact constants, and it bounds only what the recurrence models. If the recurrence describes worst-case time, the result is the worst-case time. Quicksort's best-case recurrence 2T(n/2) + Θ(n) gives Θ(n log n), but that says nothing about quicksort's worst case, which follows T(n) = T(n−1) + Θ(n). The theorem also says nothing about recurrences outside its form. A gap between cases means the theorem does not apply, not that the bound is unknown.
Exercise
A recursive algorithm on an array of size n makes four recursive calls, each on a quarter of the array, passing index bounds rather than copies. Outside the calls it does a single loop over all n elements. Write the recurrence and identify a, b, f(n), and log_b a.
Show answer
T(n) = 4T(n/4) + Θ(n). So a = 4, b = 4, f(n) = n, and log4 4 = 1. Here f(n) has the same order as n^(logb a), which points to case 2 and Θ(n log n).
Exercise
A student implements binary search as return search(xs[mid+1:], target) and so on, slicing the list on every call. They claim the master theorem gives Θ(log n). What is wrong, and what is the correct recurrence?
Show answer
The slice copies about n/2 elements, so the work outside the call is Θ(n), not Θ(1). The recurrence is T(n) = T(n/2) + Θ(n), with a = 1, b = 2, f(n) = n and log_2 1 = 0. f(n) is polynomially larger than n^0, so case 3 gives Θ(n), provided regularity holds: 1·(n/2) ≤ (1/2)·n, so it does.
Exercise
For each recurrence, say whether it is in master form. If it is, give a, b, and f(n). If not, name the requirement it breaks. (i) T(n) = 9T(n/3) + n². (ii) T(n) = T(n/2) + T(n/4) + n. (iii) T(n) = nT(n/2) + n. (iv) T(n) = 2T(n − 2) + 1.
Show answer
(i) Yes: a = 9, b = 3, f(n) = n², with log_3 9 = 2. (ii) No: the two subproblems have different sizes. (iii) No: a = n is not a constant. (iv) No: the size shrinks by subtraction, so there is no b > 1.