Proving a cost cannot be beaten: lower bounds
An upper bound is a promise about every input. A worst-case lower bound needs only one bad input for each size, and finding that family is a skill of its own.
So far every argument has been an upper bound: the procedure does at most this much work. The opposite direction is a different kind of claim and it is argued in a different way. Saying a procedure sometimes does at least this much work is what stops a hopeful reader from believing a faster version exists when it does not.
Start with the practical version of the question. You have timed insertion sort on a few thousand shuffled numbers and it looked acceptable. Is there an input of that size on which it does far more work, and how would you know you had found the worst one rather than merely a bad one?
The way to know is to build the bad input deliberately rather than to search for it. A family of inputs is one input chosen for each size , described by a rule rather than listed. For insertion sort, the family is the array that runs strictly downwards: . Run it and compare the count against .
Replace list(range(n, 0, -1)) with list(range(1, n + 1)), which is the same numbers going upwards, and run it again. The counts collapse to n - 1, which is why a lower bound has to name the input it is about.
What the definition asks for
Write for the largest number of operations the procedure performs over all inputs of size . The claim unfolds, by the definition from two lessons ago, into: there are fixed and with for every .
Now notice what that lets you get away with. is a maximum, so any single input of size gives a value that is at least as large as. One input per size is therefore enough, and that is the whole reason lower bounds on a named algorithm are provable by hand.
The insertion sort bound, every step written out
Take the family , the array of the first whole numbers in decreasing order. Fix a pass number , somewhere between 1 and . The value at position is , and the values at positions 0 to are , every one of them larger than .
The inner loop compares the value being placed with the entries to its left and keeps going while they are larger. Since all of them are larger, it compares against all and only stops by running off the left end. So pass costs exactly comparisons.
Add the passes up. The passes are , and pass costs , so the total is , which by the sum from the counting lesson is . For that is , which is what the snippet printed.
The last inequality deserves its own line. The claim becomes, after multiplying both sides by 4, , that is , that is , that is . Both factors are non-negative exactly when , so the inequality holds from onwards.
The definition wanted two numbers and now both are in hand: and . So for insertion sort. Put that beside the upper bound proved earlier and the two together give : the worst case is quadratic, not merely at most quadratic.
A lower bound on the problem, not on one procedure
The bound above is about insertion sort and says nothing about what some cleverer procedure might do. A stronger kind of claim covers every possible algorithm for a task, and the argument for it uses an opponent rather than a family.
Take the maximum problem from the first lesson. The claim is that any correct algorithm must read every one of the entries, so no algorithm for it can cost less than . Here is a procedure that tries to save one read, and what happens to it.
Change the loop so it skips the entry at position 1 instead, and change which entry the second line rewrites to match. The procedure fails again, which is the point: it is not the last position that matters, it is that a position was skipped.
The general argument is that demonstration with the particular procedure removed. Suppose some algorithm claims to solve the maximum problem and, on the input of zeros, finishes without ever reading position . Call its answer , which must be 0, since 0 is the maximum of that input.
Now build a second input: zeros again, except that position holds 1. Every cell the algorithm reads holds exactly what it held before, because position is the only cell that changed and that is the one cell it does not read. An algorithm whose every read returns the same value takes the same path and produces the same answer, so it answers 0 again. But the maximum of the second input is 1, so the answer is wrong.
The algorithm was assumed correct, so this cannot happen, and the assumption that it skipped a position must be false. It therefore reads all positions, which costs at least operations. With and that is exactly , and it holds for every algorithm anyone will ever write for this problem.
That kind of opponent, who is allowed to decide what the untouched part of the input holds after seeing what the algorithm did, is called an adversary. The maximum scan from the first lesson costs comparisons, which is , so the problem's lower bound and that algorithm's upper bound meet. When they meet, the problem is settled and no cleverness can help.
| Claim | What it is about | How it is proved | What it rules out |
|---|---|---|---|
| Upper bound, O | one named algorithm | an argument covering every input of size n | nothing; a better algorithm may exist |
| Worst-case lower bound, Omega, on an algorithm | one named algorithm | one family of bad inputs, counted | improving that algorithm's worst case |
| Lower bound on a problem | every possible algorithm | an adversary who rewrites what was not read | any algorithm at all beating the bound |
A colleague times a sorting procedure on a thousand random arrays of size 5000, finds the slowest run took 40 milliseconds, and says its worst case is Omega of that. What is missing?
A worst-case lower bound needs an input constructed to be bad, not the worst of a sample. Random arrays are overwhelmingly likely to miss the expensive family entirely: for insertion sort, a random array of 5000 entries costs about a quarter of n squared, while the reversed array costs about half, and a family designed against a quicksort implementation can be thousands of times worse than anything random. Measurement can only ever show that some input is at least this bad. To claim the worst case, you have to name the family and count it on paper.