Induction, Correctness and Recurrences
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.
- 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 holds for every integer , it is enough to prove two things:
- Base case: is true.
- Inductive step: for every , if is true, then 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 " is true" is called the induction hypothesis.
Example. Prove that for .
- Base: for , the left side is 1 and the right side is .
- Step: assume the formula holds for . Then
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 ).
Strong induction
Sometimes depends on several smaller cases, not just on . Strong induction allows the inductive step to assume that holds for all . For example, the Fibonacci numbers (, , ) satisfy :
- Base: and .
- Step: .
The step uses two previous cases, so it needs two base cases. Divide and conquer algorithms, which call themselves on rather than on , are proved correct with strong induction.
Proving recursive algorithms correct
The structure of a recursive algorithm matches the structure of an inductive proof:
| Recursive algorithm | Inductive proof |
|---|---|
| Base case | Base case |
| Recursive calls on smaller inputs | Induction hypothesis: the calls return correct results |
| Combining the results | Inductive step: the combination is correct |
| The size decreases at every call | The 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 for every (ignoring overflow).
- Base case: for it returns .
- Step (strong induction): for , (integer division) is smaller than , so by the hypothesis
half. If is even, and . If is odd, and . - Termination: 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 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:
- Initialization: the invariant holds before the first iteration.
- Maintenance: if it holds before an iteration, it holds after it.
- 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 . Invariant: before processing position , the subarray a[0..i-1] contains the original first elements, in order.
- Initialization: for ,
a[0..0]has a single element, which is trivially sorted. - Maintenance: the iteration inserts
a[i]into its place withina[0..i-1], shifting the larger elements one position to the right, soa[0..i]is sorted. - Termination: the loop ends with , 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.
Recurrences
A recurrence expresses the cost of a recursive algorithm for size 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.
| Algorithm | Recurrence | Solution |
|---|---|---|
| Recursive factorial, sequential search | ||
| Selection sort written recursively | ||
| Binary search, fast exponentiation | ||
| Mergesort | ||
| Towers of Hanoi | ||
| Naive Fibonacci | , |
There are four main ways to solve them.
Unrolling
Substitute the recurrence into itself repeatedly until a pattern appears, then sum.
For the Towers of Hanoi, with and move per call:
For 10 disks, 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:
The two children of a node together have the same size as the node, so every complete level of the tree costs . The shortest branch (always taking the third) has levels and the longest (always taking the two thirds) has levels. The total is therefore between and : . Computing the recurrence exactly, is 1.15 for and 1.12 for , consistent with . This is why Quicksort is still when every partition is uneven but proportional, such as 10% and 90%.
Another case outside the formulas is a non-recursive term that is not a power of :
Level has nodes of size , costing in total. Summing over the levels gives about : , slightly more than Mergesort.
Substitution: guess and verify
Guess the form of the solution, then prove it by induction. For Mergesort, guess for some constant and . Assuming it holds for :
"Prove" that Mergesort is : assume ; then . The error: induction must prove exactly the same statement, with the same , and is not at most . The constant grows at each level of the recursion, times. Never use inside an inductive proof; work with explicit constants.
Linear recurrences with several terms
Recurrences such as Fibonacci's, , have exact solutions of the form . Substituting gives , whose roots are (the golden ratio) and . The solution that fits the initial values is
and since the second term is smaller than in absolute value, is simply rounded to the nearest integer.
This explains precisely why the naive recursive Fibonacci is exponential. Its number of calls, with , is exactly : 25 calls for , 21,891 for and 2,692,537 for . It grows like , about 1.6 times more calls for each unit added to . This repeated work is what dynamic programming eliminates.
Self-check
1. Base cases. Why does the proof that need two base cases?
Because the inductive step for uses both and . With only checked, the step for would need , 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 , max is the maximum of a[0..i-1]. It holds for , each iteration keeps it, and at the end () max is the maximum of the whole array.
3. Unrolling. Solve with , for a power of 2.
. It is the number of iterations of binary search: .
4. Uneven splits. Quicksort always splits the array into 1% and 99% of the elements. What is its complexity?
. The recursion tree has at most levels (a large constant times ), and each level costs at most . Only splits that leave a constant number of elements on one side (such as 0 and ) make it quadratic.
5. Find the error. A student proves is : "assume ; then ". What is wrong?
The step must show with the same , but is about , which is larger than . The constant doubles at every step. Unrolling gives the true answer, .
- Proving the inductive step and forgetting the base case. Without it, nothing is proved.
- Using 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 inside the induction), or, for linear recurrences, characteristic roots.
- Proportional splits, however uneven, keep ; the naive Fibonacci makes exactly calls, which grows like .
Errors, suggestions and public questions open a form on GitHub, visible to everyone.