Proofs: Principles of Algorithmics
This page collects the proofs behind the claims of the chapter. The lessons state the results and use them; here they are justified. Each section is self-contained, and the lessons link to the section they need. The proof techniques (induction and loop invariants) are explained in Induction, Correctness and Recurrences.
Rules of the big O notation
The lessons simplify expressions such as to without hesitation. These rules justify it. Throughout, all functions are positive for large , and means that there are constants and with for every .
Constant factors. If and is a constant, then .
Proof. If for , then for the same . The constant works.
Sum rule. If and , then .
Proof. Take for the first bound and for the second. For :
This is why a sequence of program fragments costs as much as its most expensive fragment.
Product rule. If and , then .
Proof. For , multiplying the two inequalities (all terms are positive) gives .
This is why a loop that runs times with a body of cost costs .
Transitivity. If and , then .
Proof. For larger than both thresholds, .
Polynomials. If with , then .
Proof. Upper bound: for , every power with is at most , so . Lower bound: write . The terms in parentheses other than tend to 0, so for large enough their sum is at least , and .
The limit criterion
A practical way to compare two functions is to compute the limit of their quotient:
- If , then but : grows strictly more slowly.
- If , then .
- If , then but .
Proof of the middle case. By the definition of limit, for large enough the quotient lies between and , that is, : both bounds of hold. The other cases are similar.
Logarithms grow more slowly than any power
Claim. For every constant , , and .
Proof. Apply the limit criterion and L'Hôpital's rule (with natural logarithms, which only changes a constant factor):
The same argument, applied times, shows that for every : any power of a logarithm is eventually beaten by any power of . Similarly, for every , so every polynomial is eventually beaten by every exponential.
The linear maximum pairwise product is correct
The optimal linear solution scans the numbers once, keeping max1 and max2.
Claim. For non-negative numbers with , the method returns the maximum product of two elements at different positions.
Loop invariant. After processing the first elements (), max1 is the largest of them and max2 is the largest of the remaining ones, that is, the second largest counting repetitions (if the maximum appears twice, max2 equals max1).
Initialization. Both variables start at , smaller than any element. After the first element , max1 and max2 (there is no second element yet). After the second, one of the two cases of the loop places the larger in max1 and the smaller in max2. The invariant holds for .
Maintenance. Let the next element be .
- If
max1, then is the new maximum, and the old maximum becomes the largest of the rest: the code assigns exactlymax2 = max1; max1 = x. - If
max1butmax2, the maximum does not change and is the new second largest:max2 = x. - Otherwise, is not larger than either, and nothing changes.
The strict comparison in the first case matters: when equals max1, it goes to the second case and becomes max2, which is correct, because the two equal values occupy different positions.
Termination. After the elements, max1 and max2 are the two largest values (counting repetitions). Since all values are non-negative, any product with has one factor at most max1 and the other at most max2, so max1 max2, and this product is achieved.
Optimality. Any correct algorithm must read every element: if an element were never read, changing it to a huge value would change the answer without the algorithm noticing. So no algorithm can be faster than , and this one is optimal.
The average case of sequential search
Claim. If the value is in the array and is equally likely to be at any of the positions, sequential search makes comparisons on average.
Proof. If the value is at position (counting from 1), the search makes exactly comparisons. Each position has probability , so the expected number is
Binary search is correct
Claim. If list is sorted in ascending order, the binary search of the lesson returns true if and only if value is in list.
Loop invariant. If value is in list, then it is in the interval list[left..right].
Initialization. At the start the interval is the whole array.
Maintenance. Let k be the central position. If list[k] == value, the method returns true, which is correct. If value > list[k], then, since the array is sorted, every element in list[left..k] is at most list[k], hence smaller than value; if value is in the interval, it must be in list[k+1..right], which is the new interval after left = k + 1. The case value < list[k] is symmetric.
Termination. Each iteration either returns or makes the interval strictly smaller (it removes at least position k), so the loop ends. If it ends because left > right, the interval is empty, and by the invariant value is not in the array: returning false is correct.
Binary search needs a logarithmic number of iterations
Claim. On an array of elements, binary search performs at most iterations.
Proof. Let be the size of the interval at the start of an iteration. The central position splits the other elements into two parts of sizes and , and the next interval is one of them, so its size is at most . Starting from , after iterations the size is at most . The loop can only continue while the size is at least 1, that is, while , or . So the number of iterations is at most .
For that is 20 iterations, and for , 30. And no algorithm that only compares elements can do better in the worst case: each comparison with an element has at most three outcomes, so with comparisons at most different answers can be told apart, and answers are possible (each position, or "not present"). Hence , which is also .
Errors, suggestions and public questions open a form on GitHub, visible to everyone.