Discrete Mathematics and Combinatorics

The Master Theorem for Divide and Conquer Recurrences

Learn the three cases of the master theorem, why they work, how to apply them, and which recurrences fall outside its reach.

Key takeaways

  • The theorem applies to recurrences of the form T(n) = a·T(n/b) + f(n), with constants a ≥ 1 and b > 1.
  • The critical exponent c = log_b a measures how fast the number of smallest subproblems grows with n.
  • If f(n) is polynomially smaller than n^c the leaves dominate, and if it matches n^c you gain a factor of log n.
  • If f(n) is polynomially larger than n^c and passes a regularity check, the top-level work dominates.
  • Uneven splits, subtractive recurrences and some borderline functions need other methods.

The master theorem tells you how fast a function grows when it is defined by splitting a problem into equal smaller copies of itself. If solving a problem of size n takes a subproblems of size n/b plus some extra work f(n), you can usually read off the growth rate by comparing f(n) with one particular power of n.

That power is n raised to log_b a, where log_b a is the logarithm of a in base b. Whichever of the two is clearly bigger wins. If they are evenly matched, you pick up one extra logarithmic factor.

The shape of the recurrence

A recurrence defines each value of a function in terms of earlier or smaller values. A divide and conquer recurrence has this form:

T(n) = a·T(n/b) + f(n)

Here n is the size of the problem, a is the number of subproblems you create, b is the factor by which the size shrinks, and f(n) is the work spent dividing the problem and combining the answers. The constants satisfy a ≥ 1 and b > 1, f(n) is positive for large n, and T(n) is a constant for very small n.

Take merge sort. To sort a list of n items, you sort two halves of size n/2 and then merge them, which takes work proportional to n. So a = 2, b = 2 and f(n) = n.

In real programs the halves are often ⌊n/2⌋ and ⌈n/2⌉, the values rounded down and up. For the standard cases this rounding does not change the growth rate, so you can safely write n/2. The notation is covered in floor and ceiling functions.

Where the critical exponent comes from

Picture the recursion as a tree. The root is the original problem and costs f(n). It has a children, each of size n/b. Each of those has a children of size n/b², and so on until the size reaches a constant.

Level i of the tree contains a^i problems of size n/b^i, so that level costs a^i · f(n/b^i). The tree has about log_b n levels. The number of leaves at the bottom is:

a^(log_b n) = n^(log_b a)     (a standard logarithm identity)

So n^c, with c = log_b a, is the total cost of the leaves up to a constant factor. The total running time is the leaf cost plus the sum of f over all the higher levels.

If f(n) = n^d exactly, level i costs n^d · (a / b^d)^i. Each level is a fixed multiple of the one above, so the level costs form a geometric sequence. A ratio above 1 means costs grow as you go down. A ratio of exactly 1 means every level costs the same. A ratio below 1 means the root outweighs everything beneath it. The sum of a geometric series is dominated by its largest term unless all terms are equal.

The recursion tree for T(n) = a·T(n/b) + f(n)
Root 1 problem of size n, cost f(n) Level 1 a problems of size n/b, cost a·f(n/b) Level i a^i problems of size n/b^i, cost a^i·f(n/b^i) Leaves About n^(log_b a) constant-size problems Root 1 problem of size n, cost f(n) Level 1 a problems of size n/b, cost a·f(n/b) Level i a^i problems of size n/b^i, cost a^i·f(n/b^i) Leaves About n^(log_b a) constant-size problems

The three cases

Let c = log_b a. The notation Θ means "grows at the same rate as, up to constant factors", O means "at most that rate" and Ω means "at least that rate". Big O notation from a mathematical point of view gives the formal definitions.

Case Condition on f(n) Result What dominates
1 f(n) = O(n^(c − ε)) for some ε > 0 T(n) = Θ(n^c) The leaves
2 f(n) = Θ(n^c) T(n) = Θ(n^c · log n) All levels equally
3 f(n) = Ω(n^(c + ε)) for some ε > 0, plus regularity T(n) = Θ(f(n)) The root

The symbol ε (epsilon) stands for some fixed positive number, however small. It demands that f differ from n^c by at least a power of n, which is what "polynomially smaller" or "polynomially larger" means.

Case 3 has an extra regularity condition: there must be a constant k < 1 with a·f(n/b) ≤ k·f(n) for all large n. It says the children together do a fixed fraction less work than their parent. Every plain power f(n) = n^d with d > c satisfies it automatically. It can fail for functions that oscillate wildly.

How to apply it in four steps

  1. Write the recurrence in the standard form and identify a, b and f(n). Check that a and b are constants.
  2. Compute c = log_b a.
  3. Compare f(n) with n^c, looking for a gap of a positive power of n.
  4. Choose the matching case. In case 3, also verify the regularity condition.

A quick shortcut works when f(n) = n^d. Compare a with b^d. If a > b^d, you are in case 1. If a = b^d, case 2. If a < b^d, case 3.

Worked examples

Binary search. You discard half of a sorted list with one comparison: T(n) = T(n/2) + 1. Here a = 1, b = 2 and c = 0. Since f(n) = 1 = Θ(n⁰), case 2 gives Θ(log n).

Merge sort. T(n) = 2T(n/2) + n has c = 1 and f(n) = Θ(n), so T(n) = Θ(n log n). You can confirm this exactly with T(1) = 1:

T(2) = 2·1 + 2 = 4
T(4) = 2·4 + 4 = 12
T(8) = 2·12 + 8 = 32
n·log₂ n + n at n = 8:  8·3 + 8 = 32     (matches)

Multiplying large integers. Splitting two n-digit numbers into halves in the obvious way needs four half-size products plus linear work: T(n) = 4T(n/2) + n. Then c = 2, case 1 applies and T(n) = Θ(n²), no better than schoolbook multiplication. Karatsuba's method rearranges the algebra so that three products are enough: T(n) = 3T(n/2) + n. Now c = log₂ 3 ≈ 1.585, and the time drops to about n^1.585.

Strassen's matrix multiplication. For two n × n matrices, Strassen's method uses seven half-size products plus work proportional to n²: T(n) = 7T(n/2) + n². Since c = log₂ 7 ≈ 2.807 exceeds 2, case 1 gives about n^2.807.

A heavy root. Consider T(n) = 2T(n/2) + n². Here c = 1 and f(n) = n² is larger by a full power. Regularity holds because 2·(n/2)² = n²/2, so k = 1/2 works. Therefore T(n) = Θ(n²).

Saving a single subproblem changes the exponent, while shaving the combine step often changes nothing in case 1. That is a useful guide when you try to speed up a recursive method.

Gaps and recurrences it cannot handle

The three cases do not cover every possibility. Consider T(n) = 2T(n/2) + n / log n. The function f is smaller than n, but only by a logarithmic factor, which is less than any positive power of n. Neither case 1 nor case 2 applies. Analyzing the tree directly shows that T(n) = Θ(n log log n).

A common extension fills one such gap. If f(n) = Θ(n^c · (log n)^k) for some k ≥ 0, then T(n) = Θ(n^c · (log n)^(k+1)). For example, T(n) = 2T(n/2) + n log n solves to Θ(n (log n)²).

Other recurrences are outside the theorem altogether:

  • Subtractive recurrences such as T(n) = T(n − 1) + n, which solves to Θ(n²). The size shrinks by subtraction, not by division. See solving linear recurrence relations and the Tower of Hanoi.
  • Unequal splits such as T(n) = T(n/3) + T(2n/3) + n. A recursion tree shows this is Θ(n log n). A more general result, the Akra–Bazzi theorem, handles many recurrences of this kind.
  • A varying number of subproblems, such as a = n. The constants must really be constant.

Common mistakes

  • Comparing without a power gap. The fact that n log n grows faster than n does not put it in case 3. There is no ε > 0 with n log n ≥ n^(1+ε) for all large n.
  • Swapping a and b. The number of subproblems a goes inside the logarithm, and the shrink factor b is its base.
  • Assuming a = b. You might split into halves yet recurse on only one of them (a = 1) or on overlapping pieces (a = 3).
  • Forgetting the combine cost. If copying the subarrays takes linear time, that time belongs in f(n).
  • Reading Θ as exact. The theorem gives a growth rate. It says nothing about the constant factor, which can matter a great deal for inputs of ordinary size.

Checking an answer by induction

When you are unsure whether the theorem applies, guess a bound and prove it. Suppose T(n) = 2T(n/2) + n and you claim T(n) ≤ 2n log₂ n for powers of two n ≥ 2, given T(1) = 1. The base case is T(2) = 4 ≤ 4. For the step, assume the claim for n/2:

T(n) ≤ 2·(2·(n/2)·log₂(n/2)) + n     (induction hypothesis)
     = 2n·(log₂ n − 1) + n
     = 2n·log₂ n − n
     ≤ 2n·log₂ n

This method, a form of mathematical induction, works for many recurrences the theorem cannot touch. The recursion tree suggests the guess, and induction certifies it.

Written for 4Mathematics with AI writing tools and not reviewed line by line by a person. Check anything you rely on against a textbook or primary source. Report an error.

Keep reading

All 45 articles in Discrete Mathematics and Combinatorics →

Latest news on this topic