Learn
Lesson · Sorting and Selection · Selection, bounds and scale

Adversary Arguments

A lower bound cannot be measured, because a measurement is about one algorithm and a bound is about all of them. It is proved instead by an adversary who holds no array and answers each comparison so as to keep the largest number of outcomes possible. That argument gives n − 1 comparisons for the maximum, ⌈3n/2⌉ − 2 for the minimum and maximum together, and n + ⌈log₂ n⌉ − 2 for the second largest, and each of the three is met exactly.

the adversary holds no array; it answers to keep the most outcomes alive maximum n − 1 every element but one must lose minimum and maximum ⌈3n/2⌉ − 2 at n = 8: 10, met exactly second largest n + ⌈log₂ n⌉ − 2 at n = 8: 9, met exactly and Discrete Mathematics' ⌈log₂ n!⌉ is the same kind of claim, for sorting
Three bounds, each proved against every algorithm and met by one

Core ideas

What this lesson establishes

The hard idea is the quantifier. Everything else on this course measured one algorithm; this proves something about all of them at once.

1

A lower bound is about every algorithm, so it cannot be measured

Running an algorithm tells you what that algorithm did on that input. A lower bound says no algorithm can do better on its worst input, which quantifies over a set no lab can enumerate. The adversary supplies the quantifier: because it answers whatever it is asked, its argument covers every strategy, including ones nobody has written.

2

The adversary's rule is to keep the most candidates alive

It never commits to an array. It keeps a set of arrays consistent with every answer it has given, and each new answer is chosen to leave the larger set. For the maximum, it answers so that exactly one element is eliminated per comparison: any algorithm stopping before n − 1 comparisons has left two elements unbeaten, and the adversary can still name an array making either of them the maximum, so the answer was not determined.

3

A bound is tight only when an algorithm meets it

The adversary gives a floor; an algorithm gives a ceiling; when they coincide the question is closed. All three bounds here are met. Pairing the elements before scanning finds both ends of eight keys in exactly 10 comparisons, and the tournament finds the second largest of eight in exactly 9 — and the lab verifies the second of those on every one of the 40 320 arrangements.

The material

Three bounds, proved by the same manoeuvre

How an adversary argument works, the three bounds it gives here, and the algorithms that meet them.

Definition · Adversary argument

An adversary argument proves a lower bound on the number of comparisons any algorithm needs. The adversary answers each comparison the algorithm makes, without fixing the input in advance, subject only to consistency: there must always remain at least one input agreeing with every answer given. If, after fewer than B comparisons, two different answers are still consistent with everything said, the algorithm cannot have determined the result, so B is a lower bound.

Theorem · Finding the maximum needs n − 1 comparisons

Any comparison-based algorithm that reports the maximum of n elements makes at least n − 1 comparisons in the worst case.

Call an element unbeaten if it has not yet lost a comparison. Initially all n are unbeaten. The algorithm can only report a maximum when exactly one element is unbeaten: if two were, the adversary could consistently make either of them the largest, so the reported answer would not be determined.

The adversary answers every comparison between two unbeaten elements arbitrarily but consistently, and every other comparison in whatever way keeps its options open. Each comparison eliminates at most one element from the unbeaten set, since only the loser leaves it.

Going from n unbeaten elements to one therefore takes at least n − 1 comparisons. A single scan makes exactly that many, so the bound is tight.

□

Theorem · Both ends together need ⌈3n/2⌉ − 2

Finding the minimum and the maximum of n elements requires at least ⌈3n/2⌉ − 2 comparisons in the worst case, and an algorithm achieves it.

The algorithm is the more instructive half. Pair the elements and compare within each pair, which costs ⌊n/2⌋ comparisons and settles, for each pair, which member can still be the minimum and which can still be the maximum. Then find the minimum among the ⌈n/2⌉ losers and the maximum among the winners, at ⌈n/2⌉ − 1 comparisons each. A pair therefore costs three comparisons for two ends rather than four, and at n = 8 the total is 4 + 3 + 3 = 10 against the 14 of two independent scans.

Theorem · The second largest needs n + ⌈log₂ n⌉ − 2

Any algorithm reporting the second largest of n elements makes at least n + ⌈log₂ n⌉ − 2 comparisons in the worst case, and the tournament meets it.

The second largest must have lost to the maximum, because it lost to nobody else. So the algorithm has to find the maximum first, at n − 1 comparisons, and then find the largest among those the maximum beat. A knockout tournament beats the fewest possible: the winner plays ⌈log₂ n⌉ rounds, so only ⌈log₂ n⌉ elements ever lost to it, and picking the best of those costs ⌈log₂ n⌉ − 1 more. At n = 8 that is 7 + 3 − 1 = 9, which is the lab's measured count, and it never exceeds it on any of the 40 320 arrangements the sweep runs.

The misconception the tournament breaks is the natural one: scan the array keeping the best two, which compares each new element with the second best and sometimes with the best as well, and costs up to 2n − 3 — 13 at n = 8, measured at 11 on the lab's array. The tournament's saving is structural rather than clever: it remembers who lost to the winner, so the candidate set is ⌈log₂ n⌉ instead of n − 1.

One row of the lab's table is different in kind from every other figure on this course. At sizes up to eight it runs the tournament on every arrangement of 1 … n — 40 320 of them at n = 8 — and reports the worst count found, whether any exceeded the bound, and whether any returned the wrong element. None exceeded and none was wrong, and the worst equals the bound. That is a claim about every input of that size, which is as close to a quantifier as a measurement gets, and it is still not a claim about every algorithm.

The bound this course cites and does not reprove

Discrete Mathematics' “Searching and Sorting” proves that sorting n elements by comparisons needs at least ⌈log₂ n!⌉ of them, by counting the leaves a decision tree must have. That is the same kind of statement as the three above and it is used here as a thing to reduce to rather than reproved: every panel on this course prints it beside a measured count, and Geometric Algorithms will later reduce sorting to convex hull in order to inherit it.

Interactive

A lower bound is an argument, not a measurement

Answer comparisons to keep the most outcomes alive, then meet each bound with an algorithm

Play the algorithm; the lab plays the adversaryevery answer is chosen to keep the most outcomes alive
still unbeatenhas lostthe comparison you askedthe bound

Method

Proving a bound by playing against it

Take the adversary's side. The question is never what your algorithm does, but what it has not yet ruled out.

1

Name the quantity the answer depends on

For the maximum it is the number of elements that have not yet lost. For the second largest it is the number of elements that could still be second. The bound comes from how fast a single comparison can move that quantity, which for the maximum is by one.

2

Fix the adversary's answering rule and check it is consistent

The rule must never contradict an earlier answer: there has to remain at least one array agreeing with everything said. An adversary that cheats proves nothing, and the lab's is consistent by construction — at any point it can name an array matching every answer it gave.

3

Count how many comparisons the algorithm needs to finish

It can only stop when one candidate is left. Divide the distance by the most one comparison can cover, and that is the bound. Play the lab's game and watch the counter rise to n − 1 however cleverly you choose your pairs.

4

Then find the algorithm that meets it

A bound with no matching algorithm is a floor of unknown height. Pair-then-scan meets ⌈3n/2⌉ − 2; the tournament meets n + ⌈log₂ n⌉ − 2. Run each and check the measured count against the bound on the panel.

5

Say which of your numbers is a measurement and which is a proof

The count you ran up is a measurement of one game. The bound is a proof about every algorithm. The sweep over all arrangements is neither: it is a complete statement about one algorithm at one size, and the lab labels it as the one figure on the page that is not about a single input.

Bounds, and who they are about

Score 0 / 0

Both ends of eight keys in ten comparisons

The array is the lab's seeded shuffle, 2 5 4 3 7 8 6 1. The strategy is to pair first, so that one comparison per pair decides which member can still be the minimum and which can still be the maximum.

array 2 5 4 3 7 8 6 1 step 1, pair and compare within each pair 4 comparisons (2, 5) → low 2, high 5 (4, 3) → low 3, high 4 (7, 8) → low 7, high 8 (6, 1) → low 1, high 6 step 2, minimum among the lows 2 3 7 1 3 comparisons 2 vs 3 → 2; 2 vs 7 → 2; 2 vs 1 → 1 minimum = 1 step 3, maximum among the highs 5 4 8 6 3 comparisons 5 vs 4 → 5; 5 vs 8 → 8; 8 vs 6 → 8 maximum = 8 total 4 + 3 + 3 = 10 the bound ⌈3 · 8/2⌉ − 2 = 10 met exactly two scans (n − 1) + (n − 1) = 14 4 comparisons wasted

The saving is entirely in step one. Without it, every element is a candidate for both ends and each has to be tested against both running extremes. With it, one comparison per pair halves both candidate sets at once, so two elements cost three comparisons rather than four — 3n/2 rather than 2n, and the − 2 is the two extremes that need no final comparison.

Now switch the lab to the second-largest game at the same size. The tournament costs 9, the bound is 8 + 3 − 2 = 9, and keeping the best two in one scan costs 11 against a worst case of 2n − 3 = 13. Only three elements ever lost to the winner, which is ⌈log₂ 8⌉, and that is the whole reason the second largest is nearly free once the largest is known.

For a faded rehearsal, play the maximum game at n = 8 and try to finish in six comparisons. The supplied first move is the invariant to watch: the panel's unbeaten count starts at 8 and falls by at most one per comparison. Say, before you start, how many comparisons you will need, then verify that no ordering of your questions changes it.

Common mistakes

1
Assuming the second largest costs about 2nThat is the cost of the obvious algorithm, which keeps the best two as it scans and compares each new element with the second best. 2n − 3 is 13 at eight keys and the tournament pays 9. The difference is that the tournament remembers which elements lost to the winner, leaving only ⌈log₂ n⌉ candidates instead of n − 1.
2
Trying to prove a lower bound by testing algorithmsNo number of algorithms establishes that a better one does not exist. The adversary argument is the technique that quantifies over strategies, and it does it by answering rather than by enumerating. A measurement can refute a claimed upper bound; nothing measured can establish a lower one.
3
Suspecting the adversary of cheatingIt never contradicts itself: at every point there is at least one array consistent with all its answers, and at the end it can produce one. That is exactly why the bound is real — the algorithm was playing against a legitimate input all along, it just was not chosen yet. An adversary allowed to contradict an earlier answer would prove nothing at all.

Check your understanding

✓
Finish when you can state an adversary argument and then meet its bound with an algorithm.

You should be able to give the unbeaten-set argument for n − 1, describe the pairing strategy and count it to ⌈3n/2⌉ − 2, explain why only the winner's losers can be second, and distinguish a sweep over all inputs from a claim about all algorithms.

Note: “k-Way Merging and Sorting in Practice” changes the cost model. Everything so far counted comparisons, because a comparison was the only operation the model charged for. When the data does not fit in memory, the unit becomes a pass over the data, and an algorithm making more comparisons can be the right one, which is exactly what happens there.

Kept in this browser only, for you. Nothing is sent anywhere and nothing checks it. Carry it to another device.

Feedback and recommendations

Something to change about this lesson? Write it down, tick it off when it is handled, and export the list when you want to act on it. Kept in this browser only: nothing is sent anywhere, and nobody is notified.

No recommendations yet for this lesson.