The Master Theorem

Calculate the time complexity of devide-and-conquer algorithms.

· Updated
Other languages: 中文

We often encounter divide-and-conquer algorithms. When analyzing their time complexity, we may face recurrences of the form:

T(n)=aT(nb)+f(n)T(n) = aT\left(\frac{n}{b}\right) + f(n)

The Master Theorem gives us a powerful tool to solve these recurrences mechanically. But rather than memorizing the formula, let’s build intuition from the ground up.

A Concrete Example

Let’s start with the classic Merge Sort algorithm:

  1. Divide: Split an array of nn elements into two halves (n2\frac{n}{2} each)
  2. Conquer: Recursively sort each half
  3. Combine: Merge the two sorted halves in O(n)O(n) time The recurrence is:
T(n)=2T(n2)+nT(n) = 2T\left(\frac{n}{2}\right) + n

with base case T(1)=Θ(1)T(1) = \Theta(1).

Build The Recursion Tree By Hand

Let’s expand this recurrence manually to see the pattern:

  • Level 0: T(n)=n+2T(n2)T(n) = n + 2T\left(\frac{n}{2}\right)
  • Level 1: 2T(n2)=2[n2+2T(n4)]=n+4T(n4)2T\left(\frac{n}{2}\right) = 2\left[\frac{n}{2} + 2T\left(\frac{n}{4}\right)\right] = n + 4T\left(\frac{n}{4}\right)
  • Level 2: 4T(n4)=4[n4+2T(n8)]=n+8T(n8)4T\left(\frac{n}{4}\right) = 4\left[\frac{n}{4} + 2T\left(\frac{n}{8}\right)\right] = n + 8T\left(\frac{n}{8}\right)
T(n) = nT(n/2) = n/2T(n/4) = n/4T(n/8) = n/8T(n/8) = n/8T(n/4) = n/4T(n/8) = n/8T(n/8) = n/8T(n/2) = n/2T(n/4) = n/4T(n/8) = n/8T(n/8) = n/8T(n/4) = n/4T(n/8) = n/8T(n/8) = n/8

Continuing this pattern:

T(n)=n+n+n++2kT(n2k)T(n) = n + n + n + \cdots + 2^k T\left(\frac{n}{2^k}\right)

The recursion stops when n2k=1\frac{n}{2^k} = 1, i.e., k=log2nk = \log_2 n. At that level, we have 2log2n=n2^{\log_2 n} = n subproblems, each with cost Θ(1)\Theta(1).

Total cost per level:

  • Level 0: nn
  • Level 1: 2n2=n2 \cdot \frac{n}{2} = n
  • Level 2: 4n4=n4 \cdot \frac{n}{4} = n
  • Level ii: 2in2i=n2^i \cdot \frac{n}{2^i} = n
  • Level log2n\log_2 n: n1=nn \cdot 1 = n (base case)

Total cost:

T(n)=n+n++nlog2n+1 levels=n(log2n+1)=Θ(nlogn)T(n) = \underbrace{n + n + \cdots + n}_{\log_2 n + 1 \text{ levels}} = n(\log_2 n + 1) = \Theta(n \log n)

This is the familiar complexity of merge sort!

Generalizing the Pattern

Now let’s generalize. Consider the recurrence:

T(n)=aT(nb)+f(n)T(n) = aT\left(\frac{n}{b}\right) + f(n)

where:

  • a1a \geq 1 is the number of subproblems
  • b>1b > 1 is the factor by which problem size decreases
  • f(n)f(n) is the cost of dividing and combining

Building the Recursion Tree:

  • Level 0: f(n)f(n)
  • Level 1: af(nb)a \cdot f\left(\frac{n}{b}\right)
  • Level 2: a2f(nb2)a^2 \cdot f\left(\frac{n}{b^2}\right)
  • Level ii: aif(nbi)a^i \cdot f\left(\frac{n}{b^i}\right)

The tree has depth logbn\log_b n, and at the leaves we have alogbna^{\log_b n} subproblems of size Θ(1)\Theta(1).

And alogbn=(blogba)logbn=blogbalogbn=nlogbaa^{\log_b n} = (b^{\log_b a})^{\log_b n} = b^{\log_b a \cdot \log_b n} = n^{\log_b a}.

The total cost is:

T(n)=i=0logbn1aif(nbi)+Θ(nlogba)T(n) = \sum_{i=0}^{\log_b n - 1} a^i f\left(\frac{n}{b^i}\right) + \Theta(n^{\log_b a})

The behavior depends on how f(n)f(n) compares to nlogban^{\log_b a}.

The Master Theorem

Master Theorem Let T(n)=aT(n/b)+f(n)T(n) = aT(n/b) + f(n) where a1a \geq 1, b>1b > 1, and f(n)f(n) is asymptotically positive. Let c=logbac = \log_b a.

Case 1: If f(n)=O(ncϵ)f(n) = O(n^{c - \epsilon}) for some ϵ>0\epsilon > 0, then T(n)=Θ(nc)=Θ(nlogba)T(n) = \Theta(n^c) = \Theta(n^{\log_b a})

Case 2: If f(n)=Θ(nc)f(n) = \Theta(n^c), then T(n)=Θ(nclogn)=Θ(nlogbalogn)T(n) = \Theta(n^c \log n) = \Theta(n^{\log_b a} \log n)

Case 3: If f(n)=Ω(nc+ϵ)f(n) = \Omega(n^{c + \epsilon}) for some ϵ>0\epsilon > 0, and af(n/b)kf(n)af(n/b) \leq kf(n) for some k<1k < 1 and sufficiently large nn (regularity condition), then T(n)=Θ(f(n))T(n) = \Theta(f(n))

Case 1:Leaf-DominatedCase 2:BalancedCase 3:Root-DominatedWork done at leavesdominates total cost.Example: T(n) = 2T(n/2) + 1Cost: Θ(n)Work is evenly distributedacross all levels.Example: T(n) = 2T(n/2) + nCost: Θ(n log n)Work done at rootdominates total cost.Example: T(n) = 2T(n/2) + n²Cost: Θ(n²)f(n) growsf(n) grows faster

Understanding the Cases

Case 1: Leaf Dominated

Example: T(n)=4T(n/2)+nT(n) = 4T(n/2) + n

Here a=4a = 4, b=2b = 2, so c=log24=2c = \log_2 4 = 2.

We have f(n)=n=O(n2ϵ)f(n) = n = O(n^{2 - \epsilon}) for ϵ=1\epsilon = 1.

Verification by expansion:

T(n)=n+4n2+16n4++4log2nΘ(1)=n+2n+4n++n2Θ(1)=n(1+2+4++2log2n1)+Θ(n2)=nΘ(n)+Θ(n2)=Θ(n2)\begin{aligned} T(n) &= n + 4 \cdot \frac{n}{2} + 16 \cdot \frac{n}{4} + \cdots + 4^{\log_2 n} \cdot \Theta(1) \\ &= n + 2n + 4n + \cdots + n^2 \cdot \Theta(1) \\ &= n(1 + 2 + 4 + \cdots + 2^{\log_2 n - 1}) + \Theta(n^2) \\ &= n \cdot \Theta(n) + \Theta(n^2) = \Theta(n^2) \end{aligned}

The leaves contribute Θ(n2)\Theta(n^2), which dominates the Θ(n)\Theta(n) work at the root.

Case 2: Balanced

Example: T(n)=4T(n/2)+n2T(n) = 4T(n/2) + n^2

Here c=2c = 2 and f(n)=n2=Θ(n2)f(n) = n^2 = \Theta(n^2).

Verification by expansion:

Level ii contributes: 4i(n2i)2=4in24i=n24^i \cdot \left(\frac{n}{2^i}\right)^2 = 4^i \cdot \frac{n^2}{4^i} = n^2

There are log2n\log_2 n levels, each contributing n2n^2.

T(n)=n2+n2++n2log2n levels=Θ(n2logn)T(n) = \underbrace{n^2 + n^2 + \cdots + n^2}_{\log_2 n \text{ levels}} = \Theta(n^2 \log n)

Case 3: Root Dominated

Example: T(n)=4T(n/2)+n3T(n) = 4T(n/2) + n^3 Here c=2c = 2 and f(n)=n3=Ω(n2+ϵ)f(n) = n^3 = \Omega(n^{2 + \epsilon}) for ϵ=1\epsilon = 1.

Check regularity condition:

af(n/b)=4(n2)3=4n38=n32=12f(n)af(n/b) = 4 \cdot \left(\frac{n}{2}\right)^3 = 4 \cdot \frac{n^3}{8} = \frac{n^3}{2} = \frac{1}{2}f(n)

So k=12<1k = \frac{1}{2} < 1, satisfying the condition.

Verification:

  • Level 0: n3n^3
  • Level 1: 4(n/2)3=n3/24 \cdot (n/2)^3 = n^3/2
  • Level 2: 16(n/4)3=n3/416 \cdot (n/4)^3 = n^3/4

Total:

T(n)=n3+n32+n34+=n3i=012i=2n3=Θ(n3)T(n) = n^3 + \frac{n^3}{2} + \frac{n^3}{4} + \cdots = n^3 \sum_{i=0}^{\infty} \frac{1}{2^i} = 2n^3 = \Theta(n^3)

The geometric series converges, so the root cost dominates.

The Akra-Bazzi Method

The Master Theorem has limitations. It doesn’t cover:

  • Non-constant aa or bb
  • f(n)f(n) with logarithmic factors
  • Subtractions in the recurrence

The Akra-Bazzi Theorem

Akra-Bazzi Theorem: Let T(x)T(x) satisfy:

T(x)=g(x)+i=1kaiT(bix+hi(x))T(x) = g(x) + \sum_{i=1}^{k} a_i T(b_i x + h_i(x))

for xx0x \geq x_0, where:

  • ai>0a_i > 0 and 0<bi<10 < b_i < 1 are constants
  • hi(x)=O(x/log2x)|h_i(x)| = O(x / \log^2 x) (small perturbation)
  • g(x)g(x) is polynomially bounded

Let pp be the unique real number satisfying:

i=1kaibip=1\sum_{i=1}^{k} a_i b_i^p = 1

Then:

T(x)=Θ(xp(1+1xg(u)up+1du))T(x) = \Theta\left(x^p \left(1 + \int_{1}^{x} \frac{g(u)}{u^{p+1}} du\right)\right)

Example: Strassen’s Algorithm

Strassen’s matrix multiplication:

T(n)=7T(n/2)+Θ(n2)T(n) = 7T(n/2) + \Theta(n^2)

By Master Theorem (Case 1): c=log272.81c = \log_2 7 \approx 2.81, f(n)=O(n2.810.81)f(n) = O(n^{2.81 - 0.81}).

T(n)=Θ(nlog27)Θ(n2.81)T(n) = \Theta(n^{\log_2 7}) \approx \Theta(n^{2.81})

This beats the naive O(n3)O(n^3)!

Example: When Master Theorem Doesn’t Apply

Consider:

T(n)=2T(n/2)+nlognT(n) = 2T(n/2) + \frac{n}{\log n}

Here f(n)=n/logn=o(n)f(n) = n/\log n = o(n) but not O(n1ϵ)O(n^{1-\epsilon}) for any ϵ>0\epsilon > 0.

Using Akra-Bazzi: a1=2a_1 = 2, b1=1/2b_1 = 1/2, so 2(1/2)p=12 \cdot (1/2)^p = 1, giving p=1p = 1.

1nu/loguu2du=1n1ulogudu=loglogn\int_{1}^{n} \frac{u/\log u}{u^2} du = \int_{1}^{n} \frac{1}{u \log u} du = \log\log n

Therefore:

T(n)=Θ(n(1+loglogn))=Θ(nloglogn)T(n) = \Theta\left(n \left(1 + \log\log n\right)\right) = \Theta(n \log\log n)

Variations and Extensions

Ceiling and Floor Functions

The Master Theorem applies to:

T(n)=aT(n/b)+f(n)T(n) = aT(\lceil n/b \rceil) + f(n) T(n)=aT(n/b)+f(n)T(n) = aT(\lfloor n/b \rfloor) + f(n)

The asymptotic bounds remain unchanged because the ceiling/floor introduces only O(1)O(1) perturbations.

The Substitution Method (Verification)

Always verify your Master Theorem result using substitution.

Example: Verify T(n)=2T(n/2)+n=Θ(nlogn)T(n) = 2T(n/2) + n = \Theta(n \log n).

Upper bound guess: T(n)cnlognT(n) \leq cn \log n

T(n)=2T(n/2)+n2cn2logn2+n=cn(logn1)+n=cnlogncn+ncnlogn for c1\begin{aligned} T(n) &= 2T(n/2) + n \leq 2 \cdot c \cdot \frac{n}{2} \cdot \log\frac{n}{2} + n \\ &= cn(\log n - 1) + n = cn \log n - cn + n \\ &\leq cn \log n \text{ for } c \geq 1 \end{aligned}

Lower bound: Similar argument with T(n)cnlognT(n) \geq cn \log n.

The Iteration Method

For complex recurrences, unrolling is often illuminating:

T(n)=T(n/3)+T(2n/3)+nT(n) = T(n/3) + T(2n/3) + n

This splits unevenly, but the recursion tree has depth log3/2n\log_{3/2} n, and each level sums to nn.

Level 0Level 1Level 2nn/32n/3n/92n/92n/94n/9

At each level, the sum is nn. The longest path is n2n/34n/9n \to 2n/3 \to 4n/9 \to \cdots with depth log3/2n\log_{3/2} n.

T(n)=Θ(nlogn)T(n) = \Theta(n \log n)

Common Pitfalls

The Gap Between Cases

Does not apply: T(n)=2T(n/2)+nlognT(n) = 2T(n/2) + n \log n

Here f(n)=nlognf(n) = n \log n is not polynomially larger or smaller than n1n^1.

Solution: Use the recursion tree or Akra-Bazzi:

T(n)=Θ(nlog2n)T(n) = \Theta(n \log^2 n)

Forgetting the Regularity Condition

Case 3 requires af(n/b)kf(n)af(n/b) \leq kf(n) for k<1k < 1.

Example where Case 3 fails:

T(n)=2T(n/2)+n(2+cosn)T(n) = 2T(n/2) + n(2 + \cos n)

Here f(n)=Ω(n1+ϵ)f(n) = \Omega(n^{1+\epsilon}) sometimes, but oscillates. The regularity condition fails because cosn\cos n doesn’t allow a uniform k<1k < 1.

Non-constant aa or bb

Does not apply: T(n)=T(n/2)+T(n/4)+nT(n) = T(n/2) + T(n/4) + n

The Master Theorem requires constant aa and bb. Use Akra-Bazzi instead.

Further Reading