Skip to main content

Induction, Correctness and Recurrences

12 min readRead first: Sums, Logarithms and Factorials

Recursive algorithms raise two questions: is the algorithm correct, and how long does it take? The answer to both uses the same idea. A recursive algorithm solves a problem by trusting that the smaller calls work; mathematical induction is the tool that makes that trust rigorous, and a recurrence is the equation that describes its cost in terms of the cost of the smaller calls.

This lesson presents induction, its use to prove algorithms correct (including loops, through invariants), and the methods to solve recurrences. The Analyzing Divide and Conquer lesson gives ready-made formulas for the most common recurrences; this lesson explains the techniques behind them and what to do when the formulas do not apply.

What you will learn in this lesson
  • How mathematical induction and strong induction work, and how to prove recursive algorithms correct.
  • How loop invariants (initialization, maintenance, termination) prove iterative algorithms correct.
  • Four general methods to solve recurrences: unrolling, recursion trees, substitution (guess and verify), and characteristic roots.
  • Common pitfalls when using asymptotic notation inside inductive proofs.

Mathematical induction​

To prove that a property P(n)P(n) holds for every integer n≥n0n \ge n_0, it is enough to prove two things:

  1. Base case: P(n0)P(n_0) is true.
  2. Inductive step: for every n>n0n \gt n_0, if P(n−1)P(n - 1) is true, then P(n)P(n) is true.

Like a row of dominoes: the first one falls, and each one knocks down the next, so all of them fall. The assumption "P(n−1)P(n-1) is true" is called the induction hypothesis.

Example. Prove that ∑i=1ni2=n(n+1)(2n+1)6\sum_{i=1}^{n} i^2 = \frac{n(n+1)(2n+1)}{6} for n≥1n \ge 1.

  • Base: for n=1n = 1, the left side is 1 and the right side is 1⋅2⋅36=1\frac{1 \cdot 2 \cdot 3}{6} = 1.
  • Step: assume the formula holds for n−1n - 1. Then
∑i=1ni2=(n−1)n(2n−1)6+n2=n[(n−1)(2n−1)+6n]6=n(2n2+3n+1)6=n(n+1)(2n+1)6\sum_{i=1}^{n} i^2 = \frac{(n-1) n (2n-1)}{6} + n^2 = \frac{n \left[ (n-1)(2n-1) + 6n \right]}{6} = \frac{n (2n^2 + 3n + 1)}{6} = \frac{n(n+1)(2n+1)}{6}

Induction proves a formula, but it does not discover it. The formula must be guessed first (from small cases, or from an approximation such as the integral of x2x^2).

Strong induction​

Sometimes P(n)P(n) depends on several smaller cases, not just on P(n−1)P(n-1). Strong induction allows the inductive step to assume that P(k)P(k) holds for all k<nk \lt n. For example, the Fibonacci numbers (F0=0F_0 = 0, F1=1F_1 = 1, Fn=Fn−1+Fn−2F_n = F_{n-1} + F_{n-2}) satisfy Fn<2nF_n \lt 2^n:

  • Base: F0=0<1F_0 = 0 \lt 1 and F1=1<2F_1 = 1 \lt 2.
  • Step: Fn=Fn−1+Fn−2<2n−1+2n−2<2n−1+2n−1=2nF_n = F_{n-1} + F_{n-2} \lt 2^{n-1} + 2^{n-2} \lt 2^{n-1} + 2^{n-1} = 2^n.

The step uses two previous cases, so it needs two base cases. Divide and conquer algorithms, which call themselves on n/2n/2 rather than on n−1n - 1, are proved correct with strong induction.


Proving recursive algorithms correct​

The structure of a recursive algorithm matches the structure of an inductive proof:

Recursive algorithmInductive proof
Base caseBase case
Recursive calls on smaller inputsInduction hypothesis: the calls return correct results
Combining the resultsInductive step: the combination is correct
The size decreases at every callThe induction is well founded (it reaches the base case)

Example: fast exponentiation.

static long power(long a, int n) { // computes a^n, n >= 0
if (n == 0)
return 1;
long half = power(a, n / 2);
if (n % 2 == 0)
return half * half;
else
return half * half * a;
}

Claim: power(a, n) returns ana^n for every n≥0n \ge 0 (ignoring overflow).

  • Base case: for n=0n = 0 it returns 1=a01 = a^0.
  • Step (strong induction): for n>0n \gt 0, n/2n / 2 (integer division) is smaller than nn, so by the hypothesis half =a⌊n/2⌋= a^{\lfloor n/2 \rfloor}. If nn is even, n=2⌊n/2⌋n = 2 \lfloor n/2 \rfloor and half2=anhalf^2 = a^n. If nn is odd, n=2⌊n/2⌋+1n = 2 \lfloor n/2 \rfloor + 1 and half2⋅a=anhalf^2 \cdot a = a^n.
  • Termination: nn strictly decreases while it is positive, so the base case is always reached.

The proof also tells us what can go wrong. If the recursive call were power(a, n - 2), the size would decrease, but an odd nn would skip 0 and never reach the base case. Checking that the recursion always reaches a base case is as important as checking the combination.


Loop invariants​

Iterative algorithms are proved correct with a loop invariant: a property that is true before the loop starts and remains true after every iteration. It is induction on the number of iterations. A proof has three parts:

  1. Initialization: the invariant holds before the first iteration.
  2. Maintenance: if it holds before an iteration, it holds after it.
  3. Termination: when the loop ends, the invariant (together with the exit condition) implies that the result is correct.

Example: insertion sort. The outer loop processes position i=1,2,…,n−1i = 1, 2, \dots, n-1. Invariant: before processing position ii, the subarray a[0..i-1] contains the original first ii elements, in order.

  • Initialization: for i=1i = 1, a[0..0] has a single element, which is trivially sorted.
  • Maintenance: the iteration inserts a[i] into its place within a[0..i-1], shifting the larger elements one position to the right, so a[0..i] is sorted.
  • Termination: the loop ends with i=ni = n, so a[0..n-1], the whole array, is sorted.

Example: binary search. Invariant: if the target is in the array, it is in a[left..right]. It holds initially (the whole array) and is maintained because each comparison discards only a half where the target cannot be, given that the array is sorted. When the range becomes empty, the invariant says the target is not in the array. Writing this invariant explicitly is the most reliable way to get the boundaries right, which is notoriously easy to get wrong: a version that uses right = mid instead of right = mid - 1 in the wrong place can loop forever.

Loop invariants of insertion sort and of binary searchinsertion sort2051729344851667a[0..i−1]: the first i elements, sortedinot processed yetbinary search37111518222731leftrightif the target is in the array, it is in a[left..right]; the gray parts are discarded
The two invariants in the middle of a run. In insertion sort, the prefix before position i is always sorted; in binary search, the interval from left to right always contains the target, if it is present.

Recurrences​

A recurrence expresses the cost of a recursive algorithm for size nn in terms of its cost for smaller sizes. To obtain it, look at one call: count the recursive calls and their sizes, and the work done outside them.

AlgorithmRecurrenceSolution
Recursive factorial, sequential searchT(n)=T(n−1)+cT(n) = T(n-1) + cΘ(n)\Theta(n)
Selection sort written recursivelyT(n)=T(n−1)+c⋅nT(n) = T(n-1) + c \cdot nΘ(n2)\Theta(n^2)
Binary search, fast exponentiationT(n)=T(n/2)+cT(n) = T(n/2) + cΘ(log⁡n)\Theta(\log n)
MergesortT(n)=2T(n/2)+c⋅nT(n) = 2T(n/2) + c \cdot nΘ(nlog⁡n)\Theta(n \log n)
Towers of HanoiT(n)=2T(n−1)+cT(n) = 2T(n-1) + cΘ(2n)\Theta(2^n)
Naive FibonacciT(n)=T(n−1)+T(n−2)+cT(n) = T(n-1) + T(n-2) + cΘ(φn)\Theta(\varphi^n), φ≈1.618\varphi \approx 1.618

There are four main ways to solve them.

Unrolling​

Substitute the recurrence into itself repeatedly until a pattern appears, then sum.

T(n)=T(n−1)+n=T(n−2)+(n−1)+n=⋯=T(0)+1+2+⋯+n=T(0)+n(n+1)2T(n) = T(n-1) + n = T(n-2) + (n-1) + n = \dots = T(0) + 1 + 2 + \dots + n = T(0) + \frac{n(n+1)}{2}

For the Towers of Hanoi, with T(0)=0T(0) = 0 and c=1c = 1 move per call:

T(n)=2T(n−1)+1=4T(n−2)+2+1=⋯=2nT(0)+2n−1+⋯+2+1=2n−1T(n) = 2T(n-1) + 1 = 4T(n-2) + 2 + 1 = \dots = 2^n T(0) + 2^{n-1} + \dots + 2 + 1 = 2^n - 1

For 10 disks, 210−1=10232^{10} - 1 = 1023 moves, which is exactly what the recursive solution performs.

The recursion tree​

Draw the calls as a tree, write the non-recursive work at each node, add each level, and add the levels. This is how the formulas of the division scheme are derived: the levels form a geometric series, and the ratio decides which part of the tree dominates (see geometric series).

The tree also works when the formulas do not. Consider an algorithm that splits its input into unequal parts, one third and two thirds:

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

The two children of a node together have the same size as the node, so every complete level of the tree costs nn. The shortest branch (always taking the third) has log⁡3n\log_3 n levels and the longest (always taking the two thirds) has log⁡3/2n\log_{3/2} n levels. The total is therefore between nlog⁡3nn \log_3 n and nlog⁡3/2nn \log_{3/2} n: Θ(nlog⁡n)\Theta(n \log n). Computing the recurrence exactly, T(n)/(nlog⁡2n)T(n) / (n \log_2 n) is 1.15 for n=103n = 10^3 and 1.12 for n=106n = 10^6, consistent with Θ(nlog⁡n)\Theta(n \log n). This is why Quicksort is still O(nlog⁡n)O(n \log n) when every partition is uneven but proportional, such as 10% and 90%.

Recursion tree of T(n) = T(n/3) + T(2n/3) + n: every complete level costs nnn/32n/3n/92n/92n/94n/9⋮nn/3 + 2n/3 = nn/9 + 2n/9 + 2n/9 + 4n/9 = nshortest branch (always n/3): log₃ n levelslongest branch (always 2n/3): log₃/₂ n levelstotal: Θ(n log n)
The recursion tree of an uneven division. Each complete level costs n, and the number of levels is between log₃ n and log₃/₂ n, so the total is Θ(n log n).

Another case outside the formulas is a non-recursive term that is not a power of nn:

T(n)=2T(n/2)+nlog⁡nT(n) = 2T(n/2) + n \log n

Level ii has 2i2^i nodes of size n/2in / 2^i, costing nlog⁡(n/2i)=n(log⁡n−i)n \log (n/2^i) = n (\log n - i) in total. Summing over the log⁡n\log n levels gives about 12nlog⁡2n\frac{1}{2} n \log^2 n: Θ(nlog⁡2n)\Theta(n \log^2 n), slightly more than Mergesort.

Substitution: guess and verify​

Guess the form of the solution, then prove it by induction. For Mergesort, guess T(n)≤c nlog⁡2nT(n) \le c \, n \log_2 n for some constant cc and n≥2n \ge 2. Assuming it holds for n/2n/2:

T(n)=2T(n/2)+n≤2cn2log⁡2n2+n=c nlog⁡2n−c n+n≤c nlog⁡2nif c≥1T(n) = 2T(n/2) + n \le 2 c \frac{n}{2} \log_2 \frac{n}{2} + n = c \, n \log_2 n - c \, n + n \le c \, n \log_2 n \quad \text{if } c \ge 1
A classic mistake

"Prove" that Mergesort is O(n)O(n): assume T(n/2)≤c⋅n/2T(n/2) \le c \cdot n/2; then T(n)≤2cn2+n=cn+n=(c+1)n=O(n)T(n) \le 2 c \frac{n}{2} + n = c n + n = (c + 1) n = O(n). The error: induction must prove exactly the same statement, T(n)≤cnT(n) \le c n with the same cc, and (c+1)n(c+1) n is not at most cnc n. The constant grows at each level of the recursion, log⁡n\log n times. Never use O(⋅)O(\cdot) inside an inductive proof; work with explicit constants.

Linear recurrences with several terms​

Recurrences such as Fibonacci's, Fn=Fn−1+Fn−2F_n = F_{n-1} + F_{n-2}, have exact solutions of the form rnr^n. Substituting Fn=rnF_n = r^n gives r2=r+1r^2 = r + 1, whose roots are φ=1+52≈1.618\varphi = \frac{1 + \sqrt{5}}{2} \approx 1.618 (the golden ratio) and 1−52≈−0.618\frac{1 - \sqrt{5}}{2} \approx -0.618. The solution that fits the initial values is

Fn=φn−(1−φ)n5F_n = \frac{\varphi^n - (1 - \varphi)^n}{\sqrt{5}}

and since the second term is smaller than 1/21/2 in absolute value, FnF_n is simply φn/5\varphi^n / \sqrt{5} rounded to the nearest integer.

This explains precisely why the naive recursive Fibonacci is exponential. Its number of calls, C(n)=C(n−1)+C(n−2)+1C(n) = C(n-1) + C(n-2) + 1 with C(0)=C(1)=1C(0) = C(1) = 1, is exactly 2Fn+1−12 F_{n+1} - 1: 25 calls for n=6n = 6, 21,891 for n=20n = 20 and 2,692,537 for n=30n = 30. It grows like φn\varphi^n, about 1.6 times more calls for each unit added to nn. This repeated work is what dynamic programming eliminates.


Self-check​

1. Base cases. Why does the proof that Fn<2nF_n \lt 2^n need two base cases?

Because the inductive step for nn uses both n−1n - 1 and n−2n - 2. With only F0F_0 checked, the step for n=1n = 1 would need F−1F_{-1}, which does not exist. The base cases must cover every value the step cannot reach.

2. Invariant. State an invariant for the loop that finds the maximum of an array: max = a[0]; for (i = 1; i < n; i++) if (a[i] > max) max = a[i];.

Before the iteration for ii, max is the maximum of a[0..i-1]. It holds for i=1i = 1, each iteration keeps it, and at the end (i=ni = n) max is the maximum of the whole array.

3. Unrolling. Solve T(n)=T(n/2)+1T(n) = T(n/2) + 1 with T(1)=1T(1) = 1, for nn a power of 2.

T(n)=T(n/4)+2=⋯=T(1)+log⁡2n=log⁡2n+1T(n) = T(n/4) + 2 = \dots = T(1) + \log_2 n = \log_2 n + 1. It is the number of iterations of binary search: Θ(log⁡n)\Theta(\log n).

4. Uneven splits. Quicksort always splits the array into 1% and 99% of the elements. What is its complexity?

Θ(nlog⁡n)\Theta(n \log n). The recursion tree has at most log⁡100/99n\log_{100/99} n levels (a large constant times log⁡n\log n), and each level costs at most nn. Only splits that leave a constant number of elements on one side (such as 0 and n−1n - 1) make it quadratic.

5. Find the error. A student proves T(n)=2T(n−1)+1T(n) = 2T(n-1) + 1 is O(n)O(n): "assume T(n−1)≤c(n−1)T(n-1) \le c(n-1); then T(n)≤2c(n−1)+1=O(n)T(n) \le 2c(n-1) + 1 = O(n)". What is wrong?

The step must show T(n)≤cnT(n) \le c n with the same cc, but 2c(n−1)+12c(n-1) + 1 is about 2cn2cn, which is larger than cncn. The constant doubles at every step. Unrolling gives the true answer, 2n−12^n - 1.

Common mistakes
  • Proving the inductive step and forgetting the base case. Without it, nothing is proved.
  • Using OO inside an inductive proof. Use explicit constants; the hidden constant could grow at every step.
  • Not showing that the loop invariant holds before the first iteration. Initialization is part of the proof.

Key ideas​

  • Induction: base case plus inductive step. Strong induction may assume all smaller cases; it fits divide and conquer.
  • A recursive algorithm is correct if its base cases are correct, its combination is correct assuming the calls are, and every call reaches a base case.
  • Loop invariants prove iterative algorithms: initialization, maintenance, termination.
  • Recurrences are solved by unrolling, the recursion tree, guess and verify (with explicit constants, never OO inside the induction), or, for linear recurrences, characteristic roots.
  • Proportional splits, however uneven, keep Θ(nlog⁡n)\Theta(n \log n); the naive Fibonacci makes exactly 2Fn+1−12F_{n+1} - 1 calls, which grows like 1.618n1.618^n.
Saved only in your browser
Was this lesson helpful?
Questions, or something wrong?

Errors, suggestions and public questions open a form on GitHub, visible to everyone.