Foundations/Recurrences and the Master Theorem

Lesson 1.31,738 words

Recurrences and the Master Theorem

Recursive and divide-and-conquer algorithms describe their own running time with a recurrence: T(n)T(n) in terms of TT on smaller inputs. We solve recurrences three ways — drawing the recursion tree, guessing-and-verifying by induction, and applying the Master Theorem — using merge sort as the running example.

╌╌╌╌

When an algorithm solves a problem by calling smaller copies of itself, its running time obeys an equation that refers to itself: the cost on an input of size is some local work plus the cost of the recursive calls on smaller inputs. Such an equation is a recurrence. Counting loops, as in the previous lesson, no longer suffices; we need techniques to turn a recurrence into a closed -bound. This lesson develops three, in increasing order of power and precision.

From a recursive algorithm to a recurrence

is the paradigm all three books use to introduce recurrences. It has three steps: divide the instance into subproblems, conquer them by recursion, and combine their solutions. Merge sort splits the array in half, sorts each half recursively, and merges the two sorted halves.

Algorithm 1:Merge-Sort(A,p,r)\textsc{Merge-Sort}(A, p, r) — sort A[p..r]A[p..r]
  1. 1
    if p<rp < r then
  2. 2
    q(p+r)/2q \gets \floor{(p + r) / 2}
    midpoint
  3. 3
    call Merge-Sort(A,p,q)\textsc{Merge-Sort}(A, p, q)
    sort left half
  4. 4
    call Merge-Sort(A,q+1,r)\textsc{Merge-Sort}(A, q+1, r)
    sort right half
  5. 5
    call Merge(A,p,q,r)\textsc{Merge}(A, p, q, r)
    combine halves
  6. 6
    return AA

The subroutine walks the two sorted halves with two pointers, repeatedly copying the smaller front element into the output. It touches each of the elements a constant number of times, so it costs .

Now read the cost off the structure. On an array of size :

  • Divide is computing the midpoint, .
  • Conquer is two recursive calls, each on elements, costing .
  • Combine is the merge, .

Adding these (and noting that a one-element array is sorted at no cost) gives the recurrence

We can write this compactly, and as an inequality (since the combine step costs at most linear), as

and the bulk of the work is justifying that implication. This is the equation we must solve. (We freely write rather than and ; the floors and ceilings change the answer by lower-order amounts that the asymptotics absorb. Skiena and CLRS both justify dropping them.1) Throughout we also assume a constant base case, which lets us ignore the boundary condition when finding the asymptotic order.

Method 1: the recursion tree

The most intuitive method draws the recurrence. Each node is a subproblem labeled with the non-recursive work it does; its children are the subproblems it spawns. Summing all node labels gives .

For merge sort, the root does work and has two children of size . Each of those does work and has two children of size , and so on, until the leaves are size- subproblems.

Recursion tree for merge sort, with each level summing to .

The key observation: every level sums to . The root level is ; the next level is ; the level below is . The subproblem sizes shrink by half each level, so the tree has levels (from size down to size ), and the bottom level holds the size- leaves. Therefore

This is the result: merge sort runs in time, strictly better than insertion sort's .2 The recursion tree also exposes why: the per-level work stays flat at while the depth is only logarithmic.

The tree is a derivation, not yet a proof — it asks us to trust the level sums and the level count. When a recurrence is irregular (unequal splits, work that isn't a clean power of ), the tree still gives a reliable guess, which we then certify with the next method.

Method 2: substitution (guess and verify)

The substitution method is the rigorous one: guess the form of the answer, then prove it by induction on . It is the only method that always works, and the only one that produces a complete proof.

We verify the guess for the merge-sort recurrence . We claim there is a constant with for all .

Inductive hypothesis. Assume the bound holds for all sizes smaller than ; in particular .

Inductive step. Substitute into the recurrence:

This is exactly when . So choosing any (and picking the base-case constant large enough to cover ) completes the induction. Hence .

The substitution method: assume the bound on smaller inputs, substitute into the recurrence, and check that the leftover residual term lets the same bound re-emerge for .

A symmetric argument with the inequality reversed gives , and together they yield , confirming the tree.

Two warnings the books repeat:

  • Guess the right form. Substitution verifies a guess; it cannot invent one. Use the recursion tree (or the Master Theorem below) to find the candidate.
  • Beware the off-by-a-term trap. A guess like for a recurrence will seem to almost work but leave an unkillable residual term. Strengthening the hypothesis (subtracting a lower-order term, as in ) is the standard fix.

A second example: counting inversions

A second divide-and-conquer problem makes the point sharply. Its recurrence has the same shape as merge sort but a different combine cost, and the combine cost is exactly the thing you must get right. An inversion of a list is a pair with but ; the number of inversions measures how far from sorted the list is (a sorted list has , a reversed list has ). The task: given , return .

The brute-force algorithm compares every pair and runs in . To beat it, mimic merge sort: split in half, recursively count inversions inside each half, then count the cross inversions, the pairs with one element in the left half and one in the right. That gives a recurrence of the merge-sort form,

The three kinds of inversion partition cleanly along the split. On , the inversions within each half are counted by recursion; the cross pairs, a left element greater than a right element, are exactly what the combine step must tally.

Cross inversions on split into halves and . Each red arc is a cross inversion (a left element bigger than a right one): . Within-half inversions are handled by recursion; the combine step counts only these crossing pairs.

Counting cross inversions naively, with a double loop over the two halves, costs , so the recurrence becomes . Feed that to the recursion tree: the per-level work is now , which shrinks geometrically, so the root dominates and the tree sums to . That is no improvement. The split bought us nothing because the combine step is as expensive as the brute force.

Naive counting-inversions tree: unlike merge sort's flat tree, per-level work shrinks geometrically, so the root dominates and the sum is .

The lesson the recurrence teaches is therefore a demand: the combine step must run in , not . If we can count cross inversions in linear time, which one can, by counting them while merging the two sorted halves, the recurrence collapses to , the merge-sort recurrence, and we get . The recurrence both predicts the running time and tells you precisely how fast the combine step has to be for divide-and-conquer to pay off.

Method 3: the Master Theorem

Merge sort's recurrence is one instance of a common pattern. The Master Theorem solves every recurrence of the form

where and are constants and is the divide-and-combine work. Here is the number of subproblems, is each subproblem's size, and is the work done outside the recursion.

The theorem compares against the watershed function, the total cost of the leaves, which equals the number of leaves times the constant base-case cost. Which of the two dominates determines the answer.

The Master Theorem weighs the root's combine work against the leaves' total cost, the watershed .

The intuition matches the recursion tree. Compare the work at the root, , to the work at the leaves, . In Case 1 the tree is leaf-heavy and the answer is the leaf count. In Case 3 the root work dwarfs everything below it and the answer is . In Case 2 the work is spread evenly across all levels, as we saw for merge sort, giving the extra factor.

Where the work concentrates in the Master Theorem's three cases.

Each panel stacks the per-level work from root (top) to leaves (bottom); the bar width is the work at that level. The case is decided by which end is heavier.

A caution from CLRS: the cases do not cover every recurrence.3 There are gaps. When is larger than the watershed but not polynomially larger (e.g. ), or when the regularity condition fails, the Master Theorem simply does not apply. Fall back to the recursion tree or substitution.

Worked examples

Example 1, merge sort. . Here , , so . And , which is Case 2. Therefore

recovering exactly what the tree and substitution gave.

Example 2, binary search.: one subproblem of half size, constant work to pick the side. Here , , so . Then , Case 2 again, and

Example 3, leaf-dominated. . Now , , so . The combine work (take ) is polynomially smaller than the watershed, which is Case 1, so

The recursion has so many leaves ( of them) that they dominate the modest linear work per level.

Example 4, root-dominated. . Here , , watershed . The combine work is polynomially larger, a Case 3 candidate. Check regularity: with . Regularity holds, so

The root's quadratic work swamps the tree beneath it.

Choosing a method

The three methods are complementary, and Erickson in particular urges fluency with all of them:4

  • Recursion tree: fastest for building intuition and guessing the answer; shows where the work concentrates.
  • Master Theorem: fastest for getting the answer when the recurrence fits the template; no derivation needed, but it has gaps.
  • Substitution: the rigorous method that always works and produces a proof; use it to certify a guess, or when the other two do not apply (uneven splits, recurrences like ).

In practice: sketch the tree to guess, apply the Master Theorem if it fits, and reach for substitution whenever you need a guarantee rather than a hunch.

Takeaways

  • A recursive algorithm induces a recurrence: = local work + cost of recursive calls on smaller inputs. Merge sort gives .
  • The recursion tree sums the per-node work; for merge sort every level costs across levels, giving .
  • Substitution guesses the form and proves it by induction; it is the only always-applicable, fully rigorous method. Strengthen the hypothesis if a residual term blocks the step.
  • The combine cost drives the answer. Counting inversions has the merge-sort shape , but a naive combine gives overall, for no gain. Only a linear combine recovers .
  • The Master Theorem solves by comparing to the watershed : leaves win (Case 1), they tie (Case 2, extra ), or the root wins (Case 3, needs regularity).
  • The cases have gaps; when is only non-polynomially separated from the watershed, fall back to the tree or substitution.

Footnotes

  1. Skiena, §2.7–2.10 — Logarithms, Recurrences, Divide-and-Conquer: justification for dropping floors and ceilings in recurrences since they perturb the answer by lower-order amounts.
  2. CLRS, Ch. 4 — Divide-and-Conquer: the recursion-tree derivation that merge sort runs in time.
  3. CLRS, Ch. 4 — Divide-and-Conquer: the Master Theorem for and the gaps where it does not apply.
  4. Erickson, Algorithms, Ch. 1–2 — Recursion; Backtracking & Divide-and-Conquer: the case for fluency with recursion trees, substitution, and the Master Theorem as complementary methods.
Practice

╌╌ END ╌╌