Algorithms · Complexity analysis

Recurrence relations and the Master Theorem

Recurrences translate the structure of a recursive algorithm into an equation. The Master Theorem quickly solves many divide-and-conquer recurrences, provided that all of its hypotheses are actually satisfied.

Why recurrences appear

A recursive algorithm solves an instance using one or more smaller instances. Its running time is therefore the cost of the recursive calls plus the work performed outside them.

Binary search

T(n)=T(n/2)+Θ(1): one half and constant local work.

Merge sort

T(n)=2T(n/2)+Θ(n): two halves and linear merging.

Linear recursion

T(n)=T(n1)+Θ(n): the problem shrinks by one.

A base case is essential. For example, T(1)=Θ(1). Without a stopping condition, the recurrence does not describe a terminating algorithm.

Reading a divide-and-conquer recurrence

T(n)=aT(n/b)+f(n)

a — subproblems

The number of recursive calls produced by each instance. It must be at least 1.

b — shrink factor

Each subproblem has size n/b; the theorem requires b>1.

f(n) — local work

Splitting, combining and every operation performed outside the recursive calls.

The leaf cost of the recursion tree is governed by

alogbn=nlogba

This is why the Master Theorem compares f(n) with nlogba.

Solution methods

Iteration

Repeatedly expand the recurrence down to the base case, then identify and sum the resulting series.

Recursion tree

Find the cost of each level, the number of levels and the leaf cost. This is especially useful for forming a hypothesis.

Substitution

Guess an asymptotic bound and prove it by induction, choosing constants and the base case carefully.

Master Theorem

Classify balanced recurrences of the form aT(n/b)+f(n).

A short expansion example

For T(n)=T(n1)+n:

T(n)=T(1)+i=2ni=Θ(n2)

The Master Theorem does not apply because the size changes from n to n1, not to n/b.

Master Theorem

Let

T(n)=aT(n/b)+f(n) a1,b>1,c=logba

Compare f(n) with nc. In cases 1 and 3, the gap must be polynomial.

CaseComparisonResultIntuition
1f(n)=O(ncε), some ε>0Θ(nc)Leaves dominate.
2f(n)=Θ(nclogkn), k0Θ(nclogk+1n)All levels contribute.
3f(n)=Ω(nc+ε), some ε>0Θ(f(n))The root dominates.
Case 3 also requires regularity: there must be a constant q<1 such that af(n/b)qf(n) for all sufficiently large n.

Practical procedure

  1. Rewrite the recurrence as aT(n/b)+f(n).
  2. Identify a, b and f(n).
  3. Compute c=logba and nc.
  4. Compare the growth of f(n) and nc, checking for a polynomial gap.
  5. For case 3, explicitly verify regularity.
  6. State the result in Θ notation.

Worked examples

RecurrenceCritical termCaseSolution
T(n)=T(n/2)+112Θ(logn)
T(n)=2T(n/2)+nn2Θ(nlogn)
T(n)=8T(n/2)+n2n31Θ(n3)
T(n)=7T(n/2)+n2nlog271Θ(n2.807)
T(n)=3T(n/4)+nlognn0.7923Θ(nlogn)

When the Master Theorem does not apply

  • Unequal subproblem sizes: T(n)=T(n/3)+T(2n/3)+n.
  • Subtractive shrinkage: T(n)=T(n1)+n.
  • A nonconstant number of subproblems: for example nT(n/2).
  • No polynomial gap: functions such as ncloglogn do not fit the standard three cases.
  • Unverified regularity: case 3 needs more than just a larger f(n).
Use iteration, a recursion tree or substitution in these cases. The Akra–Bazzi theorem can handle more general divide-and-conquer recurrences.

Common mistakes

  • Applying the theorem without checking the required recurrence form.
  • Comparing f(n) with logba instead of nlogba.
  • Using case 1 or 3 when the gap is only logarithmic.
  • Forgetting the extra logarithmic factor in case 2.
  • Omitting splitting or combining work from f(n).

Exercises with solutions

1. Leaf-dominated

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

Show solution

The critical term is n2, which polynomially dominates n. Case 1 gives Θ(n2).

2. Logarithmic factor

Solve T(n)=2T(n/2)+nlogn.

Show solution

Case 2 with k=1 gives Θ(nlog2n).

3. Root-dominated

Solve T(n)=2T(n/2)+n2.

Show solution

The critical term is n; regularity holds because 2f(n/2)=n2/2. Case 3 gives Θ(n2).

4. Recognise the limit

Can the theorem solve T(n)=T(n2)+1?

Show solution

No. The shrinkage is subtractive. Expansion takes about n/2 constant-cost steps, so the result is Θ(n).

Checklist: identify the form, compute nlogba, compare it with f(n), and verify every hypothesis before selecting a case.