The Master Method (or Master Theorem) is a powerful "cookbook" technique used in algorithm analysis to determine the asymptotic time complexity (Big O, Big Theta, Big Omega) of divide-and-conquer algorithms directly from their recurrence relations.
It applies to recurrence relations of the form:
T(n) = aT(n/b) + f(n)
Where:
T(n): The time complexity for a problem of size n.
a: The number of subproblems in the recursion. (a >= 1)
n/b: The size of each subproblem. (It's assumed that all subproblems are roughly the same size). (b > 1)
f(n): The cost of the work done outside the recursive calls, which includes the cost of dividing the problem and the cost of combining the solutions of the subproblems. (f(n) > 0)
The Master Method compares the function f(n) with n^(log_b a). This n^(log_b a) term can be thought of as a threshold function representing the "critical" amount of work. The relationship between f(n) and this threshold determines the overall complexity.
There are three cases:
Case 1: f(n) is polynomially smaller than n^(log_b a)
- If
f(n) = O(n^(log_b a - ε)) for some constant ε > 0.
- (In simpler terms:
f(n) grows significantly slower than n^(log_b a)).
- Then:
T(n) = Θ(n^(log_b a))
- Intuition: The cost is dominated by the work done at the leaves of the recursion tree.
Case 2: f(n) is asymptotically similar to n^(log_b a)
- If
f(n) = Θ(n^(log_b a))
- (In simpler terms:
f(n) and n^(log_b a) grow at roughly the same rate).
- Then:
T(n) = Θ(n^(log_b a) * log n)
- A more general version of Case 2: If
f(n) = Θ(n^(log_b a) * log^k n) for some constant k >= 0.
- Then:
T(n) = Θ(n^(log_b a) * log^(k+1) n)
- Intuition: The work is spread fairly evenly across all levels of the recursion tree.
Case 3: f(n) is polynomially larger than n^(log_b a) AND a "regularity condition" holds
- If
f(n) = Ω(n^(log_b a + ε)) for some constant ε > 0.
- (In simpler terms:
f(n) grows significantly faster than n^(log_b a)).
- AND if the regularity condition
a * f(n/b) ≤ c * f(n) holds for some constant c < 1 and all sufficiently large n.
- This condition ensures that
f(n) doesn't just grow faster, but that the total work is indeed dominated by the work at the root/top levels.
- Then:
T(n) = Θ(f(n))
- Intuition: The cost is dominated by the work done at the root of the recursion tree (the cost of dividing and combining).
How to Use the Master Method:
- Identify
a, b, and f(n) from the recurrence relation.
- Calculate
log_b a.
- Form the term
n^(log_b a).
- Compare
f(n) with n^(log_b a) asymptotically:
- Is
f(n) polynomially smaller? (Case 1)
- Is
f(n) asymptotically the same? (Case 2)
- Is
f(n) polynomially larger? If yes, check the regularity condition. (Case 3)
- Apply the corresponding case to find
T(n).
- If none of the cases apply, the Master Method cannot be used to solve the recurrence. You'll need other techniques like the Akra-Bazzi method, recursion tree method, or substitution method.
Examples:
-
Merge Sort: T(n) = 2T(n/2) + Θ(n)
a = 2, b = 2, f(n) = Θ(n)
log_b a = log_2 2 = 1
n^(log_b a) = n^1 = n
- Compare
f(n) = Θ(n) with n. They are asymptotically the same.
- This is Case 2 (with
k=0 in the general form).
T(n) = Θ(n^(log_2 2) * log n) = Θ(n log n).
-
Binary Search: T(n) = T(n/2) + Θ(1)
a = 1, b = 2, f(n) = Θ(1)
log_b a = log_2 1 = 0
n^(log_b a) = n^0 = 1
- Compare
f(n) = Θ(1) with 1. They are asymptotically the same.
- This is Case 2 (with
k=0).
T(n) = Θ(n^0 * log n) = Θ(log n).
-
Strassen's Matrix Multiplication: T(n) = 7T(n/2) + Θ(n^2)
a = 7, b = 2, f(n) = Θ(n^2)
log_b a = log_2 7 ≈ 2.807
n^(log_b a) = n^(log_2 7)
- Compare
f(n) = Θ(n^2) with n^(log_2 7). Since 2 < log_2 7, n^2 is polynomially smaller than n^(log_2 7).
f(n) = O(n^(log_2 7 - ε)) where ε = log_2 7 - 2 > 0.
- This is Case 1.
T(n) = Θ(n^(log_2 7)).
-
A Case 3 Example: T(n) = 3T(n/4) + Θ(n log n)
a = 3, b = 4, f(n) = Θ(n log n)
log_b a = log_4 3 ≈ 0.792
n^(log_b a) = n^(log_4 3)
- Compare
f(n) = Θ(n log n) with n^(log_4 3). n log n is polynomially larger than n^(log_4 3) (e.g., n log n = Ω(n^(log_4 3 + ε)) for ε = 1 - log_4 3, since n log n grows faster than n^1).
The Master Method is a very handy tool, but it's crucial to remember its limitations and the precise conditions for each case.