Beyond Worst-Case Analysis
Average-case, smoothed and instance-optimal analysis of algorithms.
The simplex method for linear programming takes exponentially many steps on the Klee–Minty cubes, yet it has solved industrial problems quickly for over seventy years. Worst-case analysis calls it bad, practice calls it excellent, and both are right. This lesson is about the analyses that explain the gap: averages over inputs, small random perturbations of the worst inputs, comparisons with the best possible algorithm on each instance, and algorithms that use predictions.
What you’ll learn
- Define average-case complexity, explain how it can mislead, and how randomization replaces it with expected bounds.
- Define smoothed complexity, and state Spielman and Teng's theorem on the simplex method.
- Define output sensitivity, adaptivity, instance optimality and universal optimality, with convex hulls, sorting and Dijkstra's algorithm as examples.
- Analyze an algorithm with predictions by its consistency and robustness, and prove the bound for searching from a predicted position.
Worst case, and the inputs nobody sends
Worst-case analysis guarantees a bound for every input, which is its strength and its blind spot. It judges an algorithm by its single worst input of each size, even if no one ever sends that input, and it cannot tell apart two algorithms with the same worst case and very different typical behavior.
Average-case analysis fixes a distribution over the inputs of size and studies . Over uniformly random permutations, quicksort with the last element as pivot makes comparisons on average, and insertion sort about . The catch is the distribution: real inputs are often sorted, nearly sorted or adversarial, and a uniform random permutation is none of these.
The standard escape from distributional assumptions is to move the randomness into the algorithm. Quicksort with a uniformly random pivot makes comparisons in expectation on every input: no input is bad, only some coin flips are. Average-case analysis stays valuable where inputs really are random, as with hashing random keys, but it is a statement about a distribution, and only as good as that distribution.
Smoothed analysis
Spielman and Teng asked a different question: are the worst inputs fragile? Take any input, the adversary's worst included, and perturb it slightly at random. If the expected cost of the perturbed input is small, then bad inputs are isolated points that noise destroys, and the algorithm is fast near every input.
No noise. With the expectation disappears and the definition is the worst case:
Huge noise. When dwarfs , the perturbed input is essentially , and an algorithm whose cost does not depend on the scale of its input sees a random Gaussian input: an average case.
In between. A bound polynomial in and says that the inputs of exponential cost fill so little of the input space that a random nudge of relative size escapes them, in expectation, at polynomial cost.
The same lens explains other algorithms that are fast in practice despite bad worst cases, among them the -means method (Arthur, Manthey & Röglin), the 2-opt heuristic for the traveling salesman (Englert, Röglin & Vöcking), and knapsack-like problems that have pseudo-polynomial algorithms (Beier & Vöcking). Smoothed bounds have limits of their own: they need a meaningful notion of perturbation, which discrete inputs such as graphs and strings often lack.
Output sensitivity and instance optimality
Another way past the worst case is to measure cost against a finer parameter than , or against the best possible cost on each instance.
Output sensitivity. The convex hull of points in the plane takes time in the worst case. But if only of the points lie on the hull, gift wrapping (Jarvis's march) takes , and Kirkpatrick and Seidel (1986), and later Chan (1996) with a much simpler algorithm, achieve , which is optimal in the algebraic decision-tree model for every and .
Adaptivity. A sort is adaptive if it is fast on inputs that are almost sorted. Insertion sort runs in time, where counts the inversions, which is linear when . Natural merge sort runs in time on an input made of sorted runs, matching the lower bound for that measure, and Python's sort, with the Powersort merge policy, adapts to the lengths of the runs.
For many problems that definition is too strong, since an algorithm tailored to one instance could simply guess the answer. Afshani, Barbay and Chan (2009; journal version 2017) found the right weakening for geometry: compare costs on the same set of points, each taken in its worst order of arrival. In this order-oblivious sense they gave instance-optimal algorithms for the planar convex hull and related problems, at a cost governed by an entropy-like measure of the instance's structure, which ranges from about to .
Universal optimality is the graph version of the idea: be optimal on every graph structure, against its worst edge weights. Haeupler, Hladík, Rozhoň, Tarjan and Tětek (FOCS 2024, best paper) proved that Dijkstra's algorithm, with a heap that has the working-set property, is universally optimal for ordering vertices by distance; the lesson Dijkstra's Algorithm in Depth tells that story.
Sorting reduces to the convex hull, so the hull needs time in the worst case. Yet Chan's algorithm takes time for hull vertices. Why is there no contradiction?
- Chan's algorithm is randomized, while the lower bound is deterministic
- The reduction from sorting produces inputs with all points on the hull, so it says nothing about inputs with small
- The lower bound is in the comparison model, and Chan's algorithm uses arithmetic
- is the same as
Show the answer
B. The reduction from sorting produces inputs with all points on the hull, so it says nothing about inputs with small
Sorting reduces to the hull of the points on a parabola, all of which are hull vertices, so . The bound is the case of the finer bound , whose lower half Kirkpatrick and Seidel also proved.
Algorithms with predictions
Machine-learned models can often predict something useful about an input: where a key sits in a sorted array, when a cached page will be requested again, how many days you will ski. Learning-augmented algorithms use such predictions without trusting them, and two numbers describe one:
- consistency, its performance when the predictions are perfect;
- robustness, its worst-case guarantee when they are arbitrarily wrong.
The ideal is a smooth degradation in between, as a function of the prediction error . Searching a sorted array from a predicted position is the cleanest example.
def search_from(a, x, guess): # a sorted, nonempty; first i with a[i] >= x
n = len(a)
g = min(max(guess, 0), n - 1)
if a[g] < x: # the answer is right of g: gallop right
lo, step = g + 1, 1
while lo + step - 1 < n and a[lo + step - 1] < x:
lo += step
step *= 2
hi = min(lo + step - 1, n)
else: # the answer is g or left of it: gallop left
hi, step = g, 1
while hi - step >= 0 and a[hi - step] >= x:
hi -= step
step *= 2
lo = max(hi - step + 1, 0)
while lo < hi: # binary search in [lo, hi)
mid = lo + (hi - lo) // 2
if a[mid] < x:
lo = mid + 1
else:
hi = mid
return loIt gallops away from the prediction with doubling steps until it passes , then binary searches inside the last gap. Tested against bisect_left for every target and every guess on arrays of up to 1,024 items.
Galloping. One comparison checks the guess. Then the probes sit at distances from it, and galloping stops at the first probe that lands past the answer. If the answer is places away, that happens by probe
Finishing. The last gap between probes holds fewer than candidates, so the binary search needs at most comparisons. In total:
Consistency and robustness. A perfect prediction costs 2 comparisons. A useless one has and costs at most about twice plain binary search, so a bad predictor can never hurt by more than a constant factor:
The prediction is off by positions in a sorted array of a billion items. At most how many comparisons can search_from make?
Show the answer
20
, whatever the size of the array; plain binary search on items needs up to 30. The bound is tight: some target and guess at distance 1000 take exactly 20.
The same pattern runs through the field. Lykouris and Vassilvitskii (2018) made caching use predicted next-request times while staying -competitive however wrong the predictions; Purohit, Svitkina and Kumar (2018) did the same for ski rental (see Online Algorithms & Competitive Analysis); and learned index structures (Kraska et al., 2018) replace the top of a search tree with a model that predicts positions, then correct the prediction with a local search much like the one above.
Key takeaways
- Average-case analysis is only as good as its input distribution; randomizing the algorithm turns it into an expected bound that holds for every input.
- Smoothed complexity, , interpolates between the worst and the average case, and explains why the simplex method is fast in practice (Spielman & Teng).
- Finer parameters refine the worst case: convex hulls in , adaptive sorting in for runs; instance and universal optimality ask for the best possible cost on each instance or each graph.
- Learning-augmented algorithms are judged by consistency and robustness: a search from a prediction off by takes comparisons, and never more than about .
Check yourself
In the interactive lesson these are scored with your confidence, and what you miss comes back for review.
Which expression is the smoothed complexity of an algorithm?
- over random inputs
- : the worst input, slightly perturbed at random
- : the best input
- : the worst perturbation of the worst input
Show the answer
B. : the worst input, slightly perturbed at random
An adversary picks the input, nature adds small random noise, and the cost is averaged over the noise only. With this is the worst case; with huge noise it approaches an average over random inputs.
Quicksort with the last element as pivot makes about comparisons on average over random permutations. Why is it still a poor choice for a library sort?
- Real inputs are often sorted or nearly sorted, where it makes about comparisons; its average assumes a distribution that real data does not follow
- Its average is far worse than merge sort's worst case
- It is not stable
- Its random pivot choices might be unlucky
Show the answer
A. Real inputs are often sorted or nearly sorted, where it makes about comparisons; its average assumes a distribution that real data does not follow
An average over uniform random permutations says nothing about the inputs real programs send, and sorted data is common. Choosing the pivot at random gives the same bound in expectation for every input.
Which bound for the planar convex hull is optimal for every number of points and every number of hull vertices?
Show the answer
D.
Kirkpatrick and Seidel (1986) and Chan (1996) achieve , and Kirkpatrick and Seidel proved a matching lower bound in the algebraic decision-tree model, so it is optimal as a function of both and .
A search starts from a predicted position, gallops outward, then binary searches. What happens when the prediction is terrible?
- The search can take comparisons
- It may return a wrong index
- It still takes comparisons, at most about twice plain binary search
- It falls back to a linear scan
Show the answer
C. It still takes comparisons, at most about twice plain binary search
The error is at most , so the bound is at most about . That is robustness: a good prediction helps a lot, and a bad one hurts by at most a constant factor.
Further reading
- T. Roughgarden (ed.), Beyond the Worst-Case Analysis of Algorithms (Cambridge University Press, 2021).
- D. A. Spielman & S.-H. Teng, "Smoothed analysis of algorithms: Why the simplex algorithm usually takes polynomial time," Journal of the ACM 51(3) (2004).
- R. Fagin, A. Lotem & M. Naor, "Optimal aggregation algorithms for middleware," Journal of Computer and System Sciences 66(4) (2003).
- P. Afshani, J. Barbay & T. M. Chan, "Instance-optimal geometric algorithms," Journal of the ACM 64(1) (2017).
- T. Lykouris & S. Vassilvitskii, "Competitive caching with machine learned advice," Journal of the ACM 68(4) (2021).