Divide and conquer algorithms give recurrences of one common shape. The Master theorem solves all of them at once without expansion.
The General Form
Many algorithms split a problem of size n into a sub-problems of size n/b, solve them recursively and spend f(n) time dividing and combining. Their time is T(n) = a·T(n/b) + f(n), where a ≥ 1 and b > 1 are constants. The quantity n^(log_b a) represents the total work done at the leaves of the recursion tree, and the theorem compares it with f(n), the work at the root.
The Three Cases
Let c = log_b a. Case 1: if f(n) = O(n^(c−ε)) for some ε > 0, the leaves dominate and T(n) = Θ(n^c). Case 2: if f(n) = Θ(n^c), the work is spread evenly over the levels and T(n) = Θ(n^c · log n). Case 3: if f(n) = Ω(n^(c+ε)) for some ε > 0, and a·f(n/b) ≤ k·f(n) for some k < 1 (the regularity condition), the root dominates and T(n) = Θ(f(n)). In simple terms: compare n^c with f(n); if f(n) is polynomially smaller, take n^c; if equal, multiply by log n; if polynomially larger, take f(n).
Using the Theorem
First identify a, b and f(n). Then compute c = log_b a and compare n^c with f(n). Examples: for merge sort a = 2, b = 2, f(n) = n, c = 1, f(n) = Θ(n¹), Case 2, T(n) = Θ(n log n). For binary search a = 1, b = 2, f(n) = 1, c = 0, f(n) = Θ(n⁰), Case 2, T(n) = Θ(log n). For Strassen a = 7, b = 2, f(n) = n², c = log₂ 7 ≈ 2.81, f(n) smaller, Case 1, T(n) = Θ(n^2.81). For T(n) = 3T(n/4) + n log n, c = log₄ 3 ≈ 0.79 and n log n is larger, Case 3, T(n) = Θ(n log n).
Limits of the Theorem
The theorem does not apply when the recurrence is not of the form aT(n/b) + f(n): for example T(n) = T(n − 1) + n, where the size shrinks by subtraction, or when the sub-problems have unequal sizes such as T(n) = T(n/3) + T(2n/3) + n. It also fails when f(n) lies between cases, such as f(n) = n^c / log n, which is smaller than n^c but not polynomially smaller. Then recursion trees or substitution are needed.
Definition: Master theorem. A result giving the asymptotic solution of recurrences of the form T(n) = a·T(n/b) + f(n) by comparing f(n) with n raised to log_b a.
Table 1.6: Applying the Master theorem
| Algorithm | a, b, f(n) | Case | Result |
|---|---|---|---|
| Binary search | 1, 2, 1 | 2 | Θ(log n) |
| Merge sort | 2, 2, n | 2 | Θ(n log n) |
| Strassen | 7, 2, n² | 1 | Θ(n^2.81) |
| T(n) = 3T(n/4) + n log n | 3, 4, n log n | 3 | Θ(n log n) |
Example 1.6: Solving with the Master theorem
Problem: Solve T(n) = 4T(n/2) + n² and T(n) = 4T(n/2) + n.
Solution:
- First: a = 4, b = 2, c = log₂ 4 = 2, and f(n) = n² = Θ(n²), so Case 2: T(n) = Θ(n² log n).
- Second: a = 4, b = 2, c = 2, f(n) = n = O(n^(2−1)), so Case 1: T(n) = Θ(n²).
- Check numerically for n = 1024 with T(1) = 1: the first grows about 1.05 million × 10 and the second about 1.05 million × 2.
Result: First is Θ(n² log n); second is Θ(n²).
import math
def classify(a, b, k):
c = math.log(a, b)
if abs(c - k) < 1e-9:
return f"case 2: Theta(n^{k} log n)"
return (f"case 1: Theta(n^{c:.2f})" if k < c
else f"case 3: Theta(n^{k})")
cases = [(2, 2, 1), (1, 2, 0), (7, 2, 2), (4, 2, 2),
(4, 2, 1), (2, 2, 2)]
for a, b, k in cases:
print(f"T(n)={a}T(n/{b})+n^{k} ->", classify(a, b, k))
Output
T(n)=2T(n/2)+n^1 -> case 2: Theta(n^1 log n)
T(n)=1T(n/2)+n^0 -> case 2: Theta(n^0 log n)
T(n)=7T(n/2)+n^2 -> case 1: Theta(n^2.81)
T(n)=4T(n/2)+n^2 -> case 2: Theta(n^2 log n)
T(n)=4T(n/2)+n^1 -> case 1: Theta(n^2.00)
T(n)=2T(n/2)+n^2 -> case 3: Theta(n^2)
Common mistakes
- Applying the theorem to T(n) = T(n − 1) + n, which is not of the divide form.
- Comparing f(n) with n instead of with n^(log_b a).
- Using Case 3 without checking that f(n) is polynomially larger and satisfies regularity.
- Forgetting the log n factor in Case 2.
Key takeaways
- T(n) = aT(n/b) + f(n); compare f(n) with n^(log_b a).
- Smaller f gives Θ(n^c); equal gives Θ(n^c log n); larger gives Θ(f(n)).
- Merge sort is Case 2 and gives Θ(n log n); binary search is Case 2 and gives Θ(log n).
- The theorem does not cover subtraction recurrences or unequal splits.
Practice questions
- Solve T(n) = 2T(n/2) + n using the Master theorem.
- What does Case 1 of the Master theorem mean?
- Name a recurrence the theorem cannot solve.
Answers to the practice questions
- a = 2, b = 2, c = 1, f(n) = n = Θ(n^c), Case 2: Θ(n log n).
- The leaf work n^c dominates, so T(n) = Θ(n^c).
- T(n) = T(n − 1) + n.
Sources
- Official BCA syllabus Design and Analysis of Algorithms, course code 26BCA404DS03, Unit I
