Home / Learn / The Master theorem
Learn

The Master theorem

Compare d with log base b of a.

The idea

Divide-and-conquer algorithms give recurrences like T(n) = a·T(n/b) + Θ(nd): solve a subproblems of size n/b, plus nd work to split and combine. The Master theorem solves these in seconds.

The rule

Compare the exponent d with logb(a).

  • If d < logb(a), the leaves dominate: T(n) = Θ(nlogb a).
  • If d = logb(a), every level costs the same: T(n) = Θ(nd log n).
  • If d > logb(a), the top level dominates: T(n) = Θ(nd).

Worked examples

1. Merge sort: T(n) = 2T(n/2) + Θ(n)

a = 2, b = 2, d = 1. log2(2) = 1, which equals d.

Answer: Θ(n log n)

2. T(n) = 4T(n/2) + Θ(n)

a = 4, b = 2, d = 1. log2(4) = 2, which is bigger than d, so the leaves dominate.

Answer: Θ(n2)

3. T(n) = 2T(n/2) + Θ(n2)

a = 2, b = 2, d = 2. log2(2) = 1, which is smaller than d, so the top level dominates.

Answer: Θ(n2)

4. Binary search: T(n) = T(n/2) + Θ(1)

a = 1, b = 2, d = 0. log2(1) = 0, which equals d.

Answer: Θ(log n)

Common mistakes

  • Comparing a with b instead of d with logb(a).
  • Forgetting the log n factor in the equal case.
  • Applying the theorem to recurrences that do not split into equal-sized pieces.

Practise this topic

Try questions on this topic with instant explanations, or test yourself against the world in the ranked exam.