Counting

Two floors that can be added

Handing back the ten smallest of 65,536 keys has two floors under it and neither is close where they cross — the larger of the two is 63,821 comparisons at k = 4,000 and the best method makes 125,401. They can be added, because a comparison that eliminates a key the caller never sees can never be a comparison that orders two the caller does see. Charged together the floor rises 62%, and the tournament goes from 1.97 times it to 1.21.

The count of the part that was read put two floors under the problem of handing back the kk smallest of nn keys in order, and was careful to say they could not be added.

Every element not handed back must lose a comparison. Each comparison has exactly one loser, so at least nkn - k comparisons are needed. And the answer must be distinguished from every other possible answer, which is one of n!/(nk)!n!/(n-k)! sequences, so at least log2n!/(nk)!\lceil \log_2 n!/(n-k)! \rceil comparisons are needed. Adding them would double-charge, because the comparisons that eliminate the losers also carry information, and at k=1k = 1 the sum exceeds what a scan actually costs.

So that page took the larger of the two, and said where the arrangement is weak: they cross near k=3,900k = 3{,}900, and where two arguments cross each is saying only that the other might be right. The ratio plate peaked there at 1.97 and the page left the question of whose weakness it was.

It is the floor’s. The two arguments can be added — not as they were stated, but after one of them is narrowed — and the narrowing is a sentence about which comparisons can carry which kind of work.

The sentence

Take the kk elements handed back and call them the outputs. The algorithm must establish their total order: it must know which is smallest, which second, and so on.

Suppose it knows a<ba < b for two outputs. Knowledge here is the transitive closure of the comparisons made, so there is a chain

a<x1<x2<<ba < x_1 < x_2 < \cdots < b

of comparisons the algorithm actually performed. Every element of that chain is an output. Each xix_i is known to be less than bb, so in the true order xi<bx_i < b; and bb is among the kk smallest, so anything smaller than bb is among the kk smallest too. A key that is not handed back is larger than every key that is, and can never stand between two of them in a chain.

So the outputs’ order is established entirely by comparisons between two outputs, and there must be at least log2k!\lceil \log_2 k! \rceil of them, since they alone have to distinguish k!k! orderings.

Now count losers. A comparison between two outputs has an output as its loser. A comparison that gives a non-output its required loss has a non-output as its loser. The two sets of comparisons are disjoint, and so

W(n,k)    (nk)+log2k!W(n, k) \;\ge\; (n - k) + \lceil \log_2 k! \rceil

is a floor. Nothing is charged twice: the first term counts comparisons whose loser is not handed back, the second counts comparisons whose loser is.

It is worth checking the sentence against the two ends, because a bound that is wrong is usually wrong at an extreme. At k=1k = 1 there is nothing to order, log21!=0\lceil \log_2 1! \rceil = 0, and the floor is n1n - 1 — which is exactly the cost of finding a minimum, so it is tight rather than merely valid. At k=nk = n nothing is eliminated, nk=0n - k = 0, and the floor is log2n!\lceil \log_2 n! \rceil — which is the floor under every comparison sort, recovered as a special case. A combination that reproduces both known answers at the ends is at least not obviously wrong in the middle.

What it is worth

Where the two old floors cross — near k = 4,000 — each is saying only that the other might be right; charging both at once lifts the floor there by 62%Three floors under handing back the k smallest of 65,536 keys in order, against k, on logarithmic axes. The first counts the elements that must lose a comparison and falls with k; the second counts the bits needed to name one of n!/(n−k)! sequences and rises; the third charges the losers and the comparisons the outputs' own order needs, which are disjoint sets of comparisons. k = 1: losers 65,535, information 16, the two charged together 65,535. k = 4: losers 65,532, information 64, the two charged together 65,537. k = 16: losers 65,520, information 256, the two charged together 65,565. k = 64: losers 65,472, information 1,024, the two charged together 65,768. k = 256: losers 65,280, information 4,096, the two charged together 66,964. k = 1,000: losers 64,536, information 15,989, the two charged together 73,066. k = 2,000: losers 63,536, information 31,956, the two charged together 82,589. k = 4,000: losers 61,536, information 63,821, the two charged together 103,636. k = 8,000: losers 57,536, information 127,266, the two charged together 149,729. k = 16,000: losers 49,536, information 252,921, the two charged together 249,914. k = 32,768: losers 32,768, information 509,783, the two charged together 477,023. k = 65,536: losers 0, information 954,037, the two charged together 954,037.1416642561k2k4k8k16k33k66k11010010³10⁴10⁵10⁶keys handed backcomparisons no method can go underthe losers, and the outputs' ownorderevery element not handed back losesthe answer is one of n!/(n−k)!sequences65,536 keyseach is a claim about every possible method
Fig. 1 Three floors under handing back the k smallest of 65,536 keys in order, against k, on logarithmic axes. The loser count falls from 65,535 to zero; the information count rises from 16 to 954,037; the two charged together run from 65,535 through 73,066 at k = 1,000 and 103,636 at k = 4,000 to 954,037 at k = n. The dotted rule marks where the two old arguments cross.

At k=4,000k = 4{,}000 — the crossing, where the old arrangement was weakest — the larger of the two old floors is 63,821 and the new one is 103,636, sixty-two per cent higher. At k=1,000k = 1{,}000 it is 64,536 against 73,066; at k=10,000k = 10{,}000, 158,839 against 173,995.

It is never lower. The new floor exceeds the loser count by log2k!\lceil \log_2 k! \rceil, which is not negative, and it exceeds the information count whenever nkn - k exceeds log2(nk)\lceil \log_2 \binom{n}{k} \rceil — which holds over most of the range on any large nn, because identifying which kk elements are smallest costs far fewer bits than eliminating the rest costs comparisons. At the two ends the old arguments win and the new floor coincides with them.

The combined floor is one falling term and one rising one: the losers fall from 65,535 to 0 and the outputs' own order rises from 0 to 954,037The two terms of the combined floor against k, on logarithmic axes, for 65,536 keys: the n − k comparisons whose loser is an element not handed back, and the ⌈log₂ k!⌉ comparisons between two of the k handed back that their own order requires. k = 1: 65,535 losers and -0 for the order, 65,535 together. k = 4: 65,532 losers and 5 for the order, 65,537 together. k = 16: 65,520 losers and 45 for the order, 65,565 together. k = 64: 65,472 losers and 296 for the order, 65,768 together. k = 256: 65,280 losers and 1,684 for the order, 66,964 together. k = 1,000: 64,536 losers and 8,530 for the order, 73,066 together. k = 2,000: 63,536 losers and 19,053 for the order, 82,589 together. k = 4,000: 61,536 losers and 42,100 for the order, 103,636 together. k = 8,000: 57,536 losers and 92,193 for the order, 149,729 together. k = 16,000: 49,536 losers and 200,378 for the order, 249,914 together. k = 32,768: 32,768 losers and 444,255 for the order, 477,023 together. k = 65,536: 0 losers and 954,037 for the order, 954,037 together.1416642561k2k4k8k16k33k66k11010010³10⁴10⁵10⁶keys handed backcomparisonsthe two togethercomparisons a non-output losescomparisons the outputs' orderneeds65,536 keysdisjoint sets of comparisons, so they add
Fig. 2 The two terms of the combined floor, against k, on logarithmic axes. The comparisons a non-output must lose fall from 65,535 at k = 1 to zero at k = n. The comparisons the outputs’ own order requires rise from zero to 954,037. Their sum is the floor, and the sum is legitimate because the two count comparisons with different kinds of loser.

The shape is the reason the old arrangement failed where it did. One term falls, the other rises, and taking the larger throws away whichever is smaller — which near the crossing is nearly half of what is available. Charging both keeps it.

What it says about the tournament

Against the larger of the two floors the tournament peaks at 1.96 near k = 4,000; against the combined floor it never exceeds 1.24The knockout tournament's comparisons for the k smallest of 65,536 keys, divided by each floor, on logarithmic axes. k = 1: 65,535 comparisons, 1.000 times the larger of the two floors and 1.000 times the combined one. k = 4: 65,580 comparisons, 1.001 times the larger of the two floors and 1.001 times the combined one. k = 16: 65,760 comparisons, 1.004 times the larger of the two floors and 1.003 times the combined one. k = 64: 66,480 comparisons, 1.015 times the larger of the two floors and 1.011 times the combined one. k = 256: 69,359 comparisons, 1.062 times the larger of the two floors and 1.036 times the combined one. k = 1,000: 80,513 comparisons, 1.248 times the larger of the two floors and 1.102 times the combined one. k = 2,000: 95,493 comparisons, 1.503 times the larger of the two floors and 1.156 times the combined one. k = 4,000: 125,401 comparisons, 1.965 times the larger of the two floors and 1.210 times the combined one. k = 8,000: 184,974 comparisons, 1.453 times the larger of the two floors and 1.235 times the combined one. k = 16,000: 303,255 comparisons, 1.199 times the larger of the two floors and 1.199 times the combined one. k = 32,768: 545,818 comparisons, 1.071 times the larger of the two floors and 1.071 times the combined one. k = 65,536: 965,662 comparisons, 1.012 times the larger of the two floors and 1.012 times the combined one.1416642561k2k4k8k16k33k66k1keys handed backtournament ÷ floorthe larger of the two floorsthe combined floorexact65,536 keys, knockout tournamentone is a method that cannot be improved
Fig. 3 The knockout tournament’s comparisons for the k smallest of 65,536 keys, divided by each floor, on logarithmic axes. Against the larger of the two old floors: 1.000 at k = 1, 1.248 at 1,000, 1.503 at 2,000, a peak of 1.965 at 4,000, then 1.453 at 8,000 and 1.012 at n. Against the combined floor: 1.000, 1.102, 1.156, 1.210, 1.235, 1.012 — no peak, and never above 1.24.

The spike was the floor’s. A method that looked as though it might be doing twice the necessary work at one particular kk turns out to be within 24% of what no method can beat, at every kk from one to all of them.

That is worth stating in the form the earlier page asked for. It wanted to know “whether any method can hand back four thousand keys from 65,536 in substantially fewer than 125,388 comparisons”. The answer is now bounded: no method can do it in fewer than 103,636, so at most seventeen per cent of the tournament’s work at that point can be removed by any method at all, and quite possibly none of it.

Measured against the floor, the tournament stays within 1.96 of it at every k; sorting everything is 14.7 times it for the smallest oneEach method's comparisons for the k smallest of 65,536 divided by the floor at that k, on logarithmic axes; selection and incremental quicksort are means over 16 seeds. sort all, read k: 14.735 at 1, 14.736 at 4, 14.737 at 10, 14.744 at 40, 14.757 at 100, 14.825 at 400, 14.963 at 1,000, 15.131 at 4,000, 6.079 at 10,000, 1.565 at 40,000, 1.012 at 65,536. build a heap, pop k: 1.885 at 1, 1.886 at 4, 1.889 at 10, 1.904 at 40, 1.933 at 100, 2.080 at 400, 2.377 at 1,000, 3.802 at 4,000, 2.643 at 10,000, 2.063 at 40,000, 1.986 at 65,536. select k, sort those: 1.885 at 1, 1.887 at 4, 1.887 at 10, 1.892 at 40, 1.918 at 100, 1.978 at 400, 2.258 at 1,000, 3.260 at 4,000, 1.990 at 10,000, 1.245 at 40,000, 1.109 at 65,536. incremental quicksort: 1.885 at 1, 1.887 at 4, 1.887 at 10, 1.892 at 40, 1.911 at 100, 1.975 at 400, 2.234 at 1,000, 3.321 at 4,000, 2.167 at 10,000, 1.428 at 40,000, 1.337 at 65,536. keep the best k while scanning: 1.000 at 1, 1.001 at 4, 1.006 at 10, 1.041 at 40, 1.116 at 100, 1.505 at 400, 2.252 at 1,000, 5.411 at 4,000, 4.057 at 10,000, 1.971 at 40,000, 1.159 at 65,536. knockout tournament: 1.000 at 1, 1.001 at 4, 1.002 at 10, 1.010 at 40, 1.024 at 100, 1.098 at 400, 1.248 at 1,000, 1.965 at 4,000, 1.351 at 10,000, 1.049 at 40,000, 1.012 at 65,536.1101001,00010,00011.52351015k, the elements the caller readscomparisons, in multiples of the floorsort all, read kbuild a heap, pop kselect k, sort thoseincremental quicksortkeep the best k while scanningknockout tournamentlabels at k = 1,00065,536 random distinct keys1 = the floor
Fig. 4 The earlier page’s plate, for comparison: the same six methods against the old floor. Its peak at k = 4,000 is the feature this page is about, and it is now known to belong to the denominator — every line on that plate is closer to optimal than it appears, and the tournament’s line is the one most affected.

The other five lines move too, and not equally. A method already far above the floor — sorting everything, at 14.7 times it for the smallest ten — is barely changed by a floor that rises 62% in the middle of the range. The bounded heap, which collapses past k=1,000k = 1{,}000, keeps its collapse. Only the tournament, which was close everywhere except at the crossing, turns out to have been close everywhere.

Where the improvement comes from, in one number

The two old floors and the new one can be compared term by term at the crossing, and the arithmetic says where the sixty-two per cent lives.

At k=4,000k = 4{,}000 of 65,536 keys:

  • eliminating the 61,536 keys nobody sees: 61,536 comparisons;
  • ordering the 4,000 that are seen: log24000!=\lceil \log_2 4000! \rceil = 42,100 comparisons;
  • and the information floor’s total, log2(nk)+log2k!\lceil \log_2 \binom{n}{k} \rceil + \lceil \log_2 k! \rceil, is 21,721 + 42,100 = 63,821.

The two accounts agree exactly about the second term — both charge log2k!\log_2 k! for the outputs’ order, because both are counting the same bits — and differ entirely about the first. The information argument charges 21,721 comparisons to identify which 4,000 keys are smallest; the loser argument charges 61,536 to eliminate the other 61,536. The second is nearly three times the first, and the old arrangement was throwing it away whenever the information total happened to be the larger of the two.

That also says why the crossing is where the loss is worst. To the left of it the loser count is the maximum and the outputs’ order is nearly free, so the old floor was already charging almost everything. To the right the information count is the maximum and it contains the outputs’ order in full, so again little is lost. Only in the middle is each account holding something large that the other does not, and only in the middle does taking the maximum discard it.

How far the new floor is from the truth

A floor is a claim about every possible method, and the way to see how much of it is slack is to compute the truth somewhere.

Computed exactly at 6 elements, the best any method can do is 5, 7, 9, 10, 10, 10 comparisons for k = 1, 2, 3, 4, 5, 6 — never below the floor and up to 2 above itThe exact worst-case comparisons to hand back the k smallest in order, found by a minimax against an adversary that answers each comparison the way that costs most, beside the two floors. Three of these are known answers and agree: 6 − 1 for the smallest, 6 − 2 + ⌈log₂ 6⌉ for the smallest two, and the sorting optimum when k is every element. n = 4, k = 1: optimum 3, the larger of the two floors 3, the combined floor 3. n = 4, k = 2: optimum 4, the larger of the two floors 4, the combined floor 4. n = 4, k = 3: optimum 5, the larger of the two floors 5, the combined floor 5. n = 4, k = 4: optimum 5, the larger of the two floors 5, the combined floor 5. n = 5, k = 1: optimum 4, the larger of the two floors 4, the combined floor 4. n = 5, k = 2: optimum 6, the larger of the two floors 5, the combined floor 5. n = 5, k = 3: optimum 7, the larger of the two floors 6, the combined floor 6. n = 5, k = 4: optimum 7, the larger of the two floors 7, the combined floor 7. n = 5, k = 5: optimum 7, the larger of the two floors 7, the combined floor 7. n = 6, k = 1: optimum 5, the larger of the two floors 5, the combined floor 5. n = 6, k = 2: optimum 7, the larger of the two floors 5, the combined floor 5. n = 6, k = 3: optimum 9, the larger of the two floors 7, the combined floor 7. n = 6, k = 4: optimum 10, the larger of the two floors 9, the combined floor 9. n = 6, k = 5: optimum 10, the larger of the two floors 10, the combined floor 10. n = 6, k = 6: optimum 10, the larger of the two floors 10, the combined floor 10.k = 15 · floor 5k = 27 · floor 5k = 39 · floor 7k = 410 · floor 9k = 510 · floor 10k = 610 · floor 10comparisons, at n = 6exact by minimax at n = 4, 5, 6light: the floor · dark: what is left above it
Fig. 5 The exact worst-case comparisons to hand back the k smallest of six elements in order, found by minimax against an adversary that answers each comparison the way that costs most: 5, 7, 9, 10, 10 and 10 for k = 1 to 6, against floors of 5, 5, 7, 9, 10 and 10. The light part of each bar is the floor and the dark part is what no argument here accounts for.

The search is certified by three answers that are already known, and it agrees with all three: n1n - 1 comparisons for the smallest, n2+log2nn - 2 + \lceil \log_2 n \rceil for the smallest two — seven, at six elements — and the sorting optimum of ten when kk is every element. At four elements the floor is exact at every kk. At five and six it is short by one or two.

So the combined floor is a floor and it is not the answer, which is the honest half. Two comparisons of slack at n=6n = 6, k=2k = 2 is not a small share of seven, and nothing here says how that share behaves as nn grows. What the exact values do establish is that the slack is small and bounded at the sizes where it can be seen, which the old arrangement’s slack was not: at n=6n = 6, k=2k = 2 the old floor is 5 and the truth is 7.

The peak was not a property of this size

The worst the tournament ever looks: 1.90, 1.88, 1.82, 1.96 times the larger of the two floors, and 1.35, 1.30, 1.26, 1.23 times the combined oneThe largest ratio of the knockout tournament's comparisons to each floor, over k from one to n, against n, on logarithmic axes. n = 1,024: worst 1.904 against the larger of the two, at k = 86; worst 1.346 against the combined floor, at k = 221. n = 4,096: worst 1.883 against the larger of the two, at k = 354; worst 1.300 against the combined floor, at k = 566. n = 16,384: worst 1.818 against the larger of the two, at k = 906; worst 1.265 against the combined floor, at k = 2,320. n = 65,536: worst 1.959 against the larger of the two, at k = 3,712; worst 1.235 against the combined floor, at k = 9,502. The first does not fall with n and the second does.1,0244,09616,38465,5361keysworst ratio of the tournament to the floorthe larger of the two floorsthe combined floorexactknockout tournament, k from 1 to nthe worst point of each curve
Fig. 6 The largest ratio of the tournament’s comparisons to each floor, over k from one to n, against n, on logarithmic axes. Against the larger of the two old floors: 1.90, 1.88, 1.82 and 1.96 at 1,024, 4,096, 16,384 and 65,536 keys. Against the combined floor: 1.35, 1.30, 1.26 and 1.23.

The old peak is near two at every size and does not fall. The new one is under 1.35 at every size and does fall — 1.346 at a thousand keys, 1.235 at sixty-five thousand — which is the shape a slack that is a lower-order term should have.

That is the strongest evidence that the new floor is the right shape rather than merely a larger number. A floor that were simply an arbitrary improvement would not have a residual that shrinks with nn; one that has captured the leading terms would.

What the earlier page could have noticed and could not

There is a reading of all this that is unkind to the earlier page and it is worth refusing explicitly, because the refusal is the interesting part.

The sentence this page turns on — a non-output cannot lie between two outputs — is available to anyone who writes down the partial order, and it takes a paragraph. Why did the page that drew the peak not find it?

Because it was measuring methods and the peak is a property of a bound. Every plate on that page compares a method against a floor, and a plate of that shape cannot distinguish a method doing unnecessary work from a floor failing to demand necessary work. The page said so — “that peak is a statement about the floor, not about the tournament” — and then had nothing to put in the denominator.

The thing that would have settled it is the one this page ran: compute the truth somewhere. At n=6n = 6, k=2k = 2 the old floor is 5 and the optimum is 7, so the old floor was known to be loose before any new argument existed. A single exhaustive computation at a size small enough to afford it converts “one of these two is at fault” into “the floor is at fault, by at least this much” — and it needs no insight at all, only the patience to write a minimax.

That is the transferable habit, and it is cheaper than the insight it replaces: writing a minimax over sets of orderings took less care than proving the chain sentence, and it is the half of the work that could have been done first. The worst case found by climbing is the same move in the other direction: a search certified at eight elements, where every answer can be checked, and then run where it cannot.

What a floor is for

The whole exercise is a demonstration of something that recurs everywhere a lower bound is quoted and is rarely stated so cleanly.

A lower bound is a claim about every possible method, and a weak one is indistinguishable from a method being bad. The earlier page reported a method at 1.97 times its floor and could not say which of the two was at fault. It said so, which is the right thing to do with a number one cannot attribute — and the attribution turned out to be entirely on one side.

And two arguments combine only when the things they count are disjoint. The reason the original page was right not to add its two floors is that they both counted the same comparisons. The reason this page may add its two is a sentence about the structure of the partial order: a non-output cannot lie between two outputs. That sentence is where the whole improvement lives, and it is not about counting at all.

That is the same discipline the count is not the time applies to a measurement rather than to a bound: a number is trustworthy only alongside a statement of what it is a number of, and a floor’s statement is the argument that produced it.

The floor moves when the question does made the general point that a bound belongs to a question. This adds the finer one: a bound also belongs to an argument, and a question can have several arguments whose combination is a better bound than any of them, when their charges fall on different objects.

What is not measured here

One method against the floors. The tournament is the one the ratios are drawn for, because it is the method the earlier page found closest. The other five are on the reproduced plate and are not recomputed against the new floor point by point.

Nothing here is about the average. Both floors bound the worst case. A method could beat either of them on most inputs and pay on one, and a distribution computed rather than sampled is the page that measures what a distribution of counts looks like when the worst case is not the whole story. The tournament happens to be the wrong method to raise that objection about, since its count is nearly the same on every input.

A bound, not an algorithm. The floor says no method goes under 103,636 at the crossing and says nothing about how a method might reach it. How close anything gets to the floor is the standing measurement of that gap, and this page narrows one entry in it without closing it.

Distinct keys. The chain argument uses “anything smaller than an output is an output”, which needs no two keys equal. With ties the outputs are a multiset and the argument needs restating; the floor when the values repeat is what happens to the information floor in that case and the same care would be needed here.

Worst case, not expected. Everything here is a worst-case bound against an adversary, and the tournament’s measured counts are on one random arrangement. The two are comparable only because the tournament’s count barely depends on its input — the count of the part that was read measured it moving by thirteen comparisons across sorted, random and reversed input.

Exact only to six elements. The minimax is a search over sets of orderings and costs 127,743 states at six elements; seven would be several hundred times that, since the states are subsets of a set that itself grows by a factor of seven. The slack of one or two comparisons is what can be seen, and two points are not a measurement of how it grows.

No account of where the slack sits. At n=6n = 6, k=2k = 2 the floor demands 5 and the truth is 7, and nothing here says what the missing two comparisons are doing. The last section proposes the obvious candidate and does not measure it.

And the floor is not claimed to be the best known. The literature on selection has sharper bounds for particular kk, and for k=2k = 2 the exact answer n2+log2nn - 2 + \lceil \log_2 n \rceil beats this floor by log2n1\lceil \log_2 n \rceil - 1. What is claimed is that this one is provable in a paragraph, computable at every kk, and enough to attribute the peak.

Still open: the third kind of comparison

The argument divides comparisons by their loser: a non-output loses, or an output loses. It then charges the first set for elimination and the second for ordering, and stops.

There is a third thing a comparison can do that neither charge counts. A comparison between two non-outputs eliminates one of them and also tells the algorithm something about the other, and a comparison in which an output beats a non-output identifies part of the boundary between the two groups — which is work the algorithm must do, since it has to know which kk elements are smallest, and neither term above pays for it.

The measurement that follows asks what that boundary costs. It counts, in an optimal decision tree at sizes where one can be computed, how many comparisons have a non-output as their loser beyond the nkn - k the first term demands, and whether that excess tracks log2(nk)\lceil \log_2 \binom{n}{k} \rceil — the bits needed to name the set — or something smaller. If it tracks the set’s own information content, the floor becomes three terms rather than two, and the slack at n=6n = 6, k=2k = 2 has an explanation rather than a size.

What this makes readable

Essays that name this one as a prerequisite.

Named alongside this one

Essays reaching for the same objects. Nobody chose these; they are what the concept index makes visible.

What links here

Every essay whose body links to this one.

The objects this essay names

Each one links to every other essay that touches it.

Adversary argumentComparison countExhaustive searchFalsificationHonest limitInformation floorLower boundOutput-sensitivePartial orderSelectionWorst case