Skip to main content

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 3n2+5nlog⁡n+73n^2 + 5n \log n + 7 to O(n2)O(n^2) without hesitation. These rules justify it. Throughout, all functions are positive for large nn, and f∈O(g)f \in O(g) means that there are constants c>0c \gt 0 and n0n_0 with f(n)≤c g(n)f(n) \le c \, g(n) for every n≥n0n \ge n_0.

Constant factors. If f∈O(g)f \in O(g) and k>0k \gt 0 is a constant, then kf∈O(g)k f \in O(g).

Proof. If f(n)≤c g(n)f(n) \le c \, g(n) for n≥n0n \ge n_0, then kf(n)≤(kc) g(n)k f(n) \le (k c) \, g(n) for the same nn. The constant kck c works. ■\blacksquare

Sum rule. If f1∈O(g1)f_1 \in O(g_1) and f2∈O(g2)f_2 \in O(g_2), then f1+f2∈O(max⁡(g1,g2))f_1 + f_2 \in O(\max(g_1, g_2)).

Proof. Take c1,n1c_1, n_1 for the first bound and c2,n2c_2, n_2 for the second. For n≥max⁡(n1,n2)n \ge \max(n_1, n_2):

f1(n)+f2(n)≤c1g1(n)+c2g2(n)≤(c1+c2)max⁡(g1(n),g2(n))f_1(n) + f_2(n) \le c_1 g_1(n) + c_2 g_2(n) \le (c_1 + c_2) \max(g_1(n), g_2(n))

■\blacksquare

This is why a sequence of program fragments costs as much as its most expensive fragment.

Product rule. If f1∈O(g1)f_1 \in O(g_1) and f2∈O(g2)f_2 \in O(g_2), then f1⋅f2∈O(g1⋅g2)f_1 \cdot f_2 \in O(g_1 \cdot g_2).

Proof. For n≥max⁡(n1,n2)n \ge \max(n_1, n_2), multiplying the two inequalities (all terms are positive) gives f1(n)f2(n)≤(c1c2) g1(n)g2(n)f_1(n) f_2(n) \le (c_1 c_2) \, g_1(n) g_2(n). ■\blacksquare

This is why a loop that runs O(f)O(f) times with a body of cost O(g)O(g) costs O(f⋅g)O(f \cdot g).

Transitivity. If f∈O(g)f \in O(g) and g∈O(h)g \in O(h), then f∈O(h)f \in O(h).

Proof. For nn larger than both thresholds, f(n)≤c1g(n)≤c1c2 h(n)f(n) \le c_1 g(n) \le c_1 c_2 \, h(n). ■\blacksquare

Polynomials. If p(n)=aknk+ak−1nk−1+⋯+a0p(n) = a_k n^k + a_{k-1} n^{k-1} + \dots + a_0 with ak>0a_k \gt 0, then p∈Θ(nk)p \in \Theta(n^k).

Proof. Upper bound: for n≥1n \ge 1, every power nin^i with i≤ki \le k is at most nkn^k, so p(n)≤(∣ak∣+⋯+∣a0∣) nkp(n) \le (\lvert a_k \rvert + \dots + \lvert a_0 \rvert) \, n^k. Lower bound: write p(n)=nk(ak+ak−1n+⋯+a0nk)p(n) = n^k \left( a_k + \frac{a_{k-1}}{n} + \dots + \frac{a_0}{n^k} \right). The terms in parentheses other than aka_k tend to 0, so for nn large enough their sum is at least −ak/2-a_k / 2, and p(n)≥ak2nkp(n) \ge \frac{a_k}{2} n^k. ■\blacksquare

The limit criterion​

A practical way to compare two functions is to compute the limit of their quotient:

L=lim⁡n→∞f(n)g(n)L = \lim_{n \to \infty} \frac{f(n)}{g(n)}
  • If L=0L = 0, then f∈O(g)f \in O(g) but g∉O(f)g \notin O(f): ff grows strictly more slowly.
  • If 0<L<∞0 \lt L \lt \infty, then f∈Θ(g)f \in \Theta(g).
  • If L=∞L = \infty, then g∈O(f)g \in O(f) but f∉O(g)f \notin O(g).

Proof of the middle case. By the definition of limit, for nn large enough the quotient lies between L/2L/2 and 2L2L, that is, L2g(n)≤f(n)≤2L g(n)\frac{L}{2} g(n) \le f(n) \le 2L \, g(n): both bounds of Θ\Theta hold. The other cases are similar. ■\blacksquare


Logarithms grow more slowly than any power​

Claim. For every constant ε>0\varepsilon \gt 0, log⁡n∈O(nε)\log n \in O(n^{\varepsilon}), and nε∉O(log⁡n)n^{\varepsilon} \notin O(\log n).

Proof. Apply the limit criterion and L'Hôpital's rule (with natural logarithms, which only changes a constant factor):

lim⁡n→∞ln⁡nnε=lim⁡n→∞1/nε nε−1=lim⁡n→∞1ε nε=0\lim_{n \to \infty} \frac{\ln n}{n^{\varepsilon}} = \lim_{n \to \infty} \frac{1/n}{\varepsilon \, n^{\varepsilon - 1}} = \lim_{n \to \infty} \frac{1}{\varepsilon \, n^{\varepsilon}} = 0

■\blacksquare

The same argument, applied kk times, shows that (log⁡n)k∈O(nε)(\log n)^k \in O(n^{\varepsilon}) for every kk: any power of a logarithm is eventually beaten by any power of nn. Similarly, nk/cn→0n^k / c^n \to 0 for every c>1c \gt 1, 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 a0,…,an−1a_0, \dots, a_{n-1} with n≥2n \ge 2, the method returns the maximum product of two elements at different positions.

Loop invariant. After processing the first ii elements (i≥2i \ge 2), 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 −1-1, smaller than any element. After the first element a0a_0, max1 =a0= a_0 and max2 =−1= -1 (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 i=2i = 2.

Maintenance. Let the next element be xx.

  • If x>x \gt max1, then xx is the new maximum, and the old maximum becomes the largest of the rest: the code assigns exactly max2 = max1; max1 = x.
  • If x≤x \le max1 but x>x \gt max2, the maximum does not change and xx is the new second largest: max2 = x.
  • Otherwise, xx is not larger than either, and nothing changes.

The strict comparison in the first case matters: when xx 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 nn elements, max1 and max2 are the two largest values (counting repetitions). Since all values are non-negative, any product aiaja_i a_j with i≠ji \ne j has one factor at most max1 and the other at most max2, so aiaj≤a_i a_j \le max1 ⋅\cdot max2, and this product is achieved. ■\blacksquare

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 Ω(n)\Omega(n), and this one is optimal.


Claim. If the value is in the array and is equally likely to be at any of the nn positions, sequential search makes n+12\frac{n+1}{2} comparisons on average.

Proof. If the value is at position ii (counting from 1), the search makes exactly ii comparisons. Each position has probability 1/n1/n, so the expected number is

∑i=1n1n⋅i=1n⋅n(n+1)2=n+12\sum_{i=1}^{n} \frac{1}{n} \cdot i = \frac{1}{n} \cdot \frac{n(n+1)}{2} = \frac{n+1}{2}

■\blacksquare


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. ■\blacksquare


Binary search needs a logarithmic number of iterations​

Claim. On an array of n≥1n \ge 1 elements, binary search performs at most ⌊log⁡2n⌋+1\lfloor \log_2 n \rfloor + 1 iterations.

Proof. Let mm be the size of the interval at the start of an iteration. The central position splits the other m−1m - 1 elements into two parts of sizes ⌊(m−1)/2⌋\lfloor (m-1)/2 \rfloor and ⌈(m−1)/2⌉\lceil (m-1)/2 \rceil, and the next interval is one of them, so its size is at most ⌊m/2⌋\lfloor m/2 \rfloor. Starting from nn, after tt iterations the size is at most ⌊n/2t⌋\lfloor n / 2^t \rfloor. The loop can only continue while the size is at least 1, that is, while 2t≤n2^t \le n, or t≤log⁡2nt \le \log_2 n. So the number of iterations is at most ⌊log⁡2n⌋+1\lfloor \log_2 n \rfloor + 1. ■\blacksquare

For n=106n = 10^6 that is 20 iterations, and for n=109n = 10^9, 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 tt comparisons at most 3t3^t different answers can be told apart, and n+1n + 1 answers are possible (each position, or "not present"). Hence t≥log⁡3(n+1)t \ge \log_3 (n + 1), which is also Ω(log⁡n)\Omega(\log 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.