COMPUTER ENGINEER

← BlogDATA STRUCTURES / ALGORITHMSPython example pack ↓

Problems, mathematics and analysis · 03 / 53

Sums, recurrences and the Master theorem

Understand → trace → implement → justify

1 min reading · allow extra time for practice

Separate recursive call count, subproblem size and nonrecursive work. Merge sort obeys T(n)=2T(n/2)+Θ(n). Each level of its recursion tree contains Θ(n) work and the tree has about log₂n levels, giving Θ(n log n).

The Master theorem applies to balanced recurrences of the form aT(n/b)+f(n). Compare f(n) with n^(log_b a), and check the case assumptions. Unequal splits, n−1 reductions and irregular costs require expansion, substitution or induction rather than forcing the theorem.

T(n)=T(n−1)+n expands into a triangular sum. T(n)=T(n/2)+1 has constant work per level. State base cases such as T(1)=Θ(1). Floors and ceilings often preserve asymptotic bounds, but executable boundary handling still matters.

A worked trace

T(8)=2T(4)+8: level work is 8,8,8, followed by constant work at eight leaves. Splitting alone does not account for merging.

Cost and assumptions

2T(n/2)+n: Θ(n log n); T(n−1)+1: Θ(n); T(n/2)+n: Θ(n).

Executable example

Run with Python 3. Assertions check a small example; they do not constitute a general correctness proof. Some advanced examples are explicitly small models or reference oracles, not full production implementations.

Python 3
def halving_depth(n):
    if n < 1:
        raise ValueError("n must be positive")
    depth = 0
    while n > 1:
        n //= 2
        depth += 1
    return depth
assert halving_depth(8) == 3

TEST YOUR UNDERSTANDING

Your turn

Which term dominates T(n)=3T(n/2)+n?

Reveal the reasoning

n^(log₂3), about n^1.585, dominates the linear combining term. The first classical Master case gives Θ(n^log₂3).

Take it further

  1. Write down the input contract and the invariant before changing the example.
  2. Add empty, boundary, duplicate and adversarial cases where they are relevant. Compare with a simple oracle.
  3. Derive the cost under the stated model; measure several input sizes and explain discrepancies.

Primary reading & lecture notes

Original explanations in this course; these references provide proofs and further reading. Source links may cover the broader module.

MIT 6.046J · Design and Analysis ↗Jeff Erickson · Algorithms ↗
Solve the exercise before moving on.