What is taught wrongly

Two structures that are one

Space-Saving never underestimates and Misra-Gries never overestimates, and they are taught as rival structures with opposite failure modes. Subtract one number from every Space-Saving counter and what is left is the Misra-Gries table, key for key and count for count, at all twenty thousand prefixes of a stream and at every table size tried.

Run two structures side by side over the same stream.

The first is a Space-Saving summary with sixteen counters. Watch its smallest counter — the number a new key inherits when it takes a slot.

The second is a Misra-Gries summary with fifteen counters. Watch the total it has decremented away — the sum of everything subtracted from its counters over the whole run.

There is no obvious reason for these to be related. They are bookkeeping quantities inside two different algorithms, published seventeen years apart, one of them a running minimum over a table and the other a running total of an operation the first structure does not perform.

The smallest Space-Saving counter, and everything Misra-Gries has thrown awayTwo structures, run side by side over 40,000 arrivals. The first line is the value of the smallest counter in a Space-Saving summary with 32 counters — the number a new key inherits when it takes a slot. The second is the total that a Misra-Gries summary with 31 counters has decremented away over the same stream. A reader would expect these to be two unrelated pieces of bookkeeping in two different algorithms. They are one line drawn twice: 938 against 938 at the end, and equal at every one of the 40,000 prefixes checked, not merely at the end. Subtract the first from every Space-Saving counter and what is left is the Misra-Gries table, key for key and count for count.2585167741,032020,00040,000arrivals so farcountSpace-Saving's floorMisra-Gries's decrementsboth 93840,000 prefixes, 0 disagreementsk = 32 against k = 31
Fig. 1 The two quantities, drawn on the same axes. There is one line here and it has been drawn twice.

They are equal. Not approximately, not asymptotically, and not merely at the end: at every one of twenty thousand prefixes, and at every table size tried.

The conversion is a subtraction

The full statement is stronger than the equality of those two numbers, and it is the thing worth carrying away.

Take a Space-Saving summary with kk counters. Subtract its smallest counter from every one of them and delete the keys whose result is zero. What remains is exactly the Misra-Gries summary with k1k-1 counters over the same stream.

The same state, read twice: subtract 938 and one table becomes the otherThe top 8 keys of a Space-Saving summary with 32 counters over 40,000 arrivals, with its own numbers, those numbers minus its smallest counter, and the table a Misra-Gries summary with 31 counters built from the same stream. The third and fourth columns are identical on every row. They are usually presented as rival structures with opposite failure modes — one never under the truth, the other never over — and the opposition is real; what is not real is that they are two structures. One holds 3,072 bits and the other 1,984, and the difference is the third counter per key that Space-Saving keeps so it can report its own error.keySpace-Saving− 938Misra-Gries, k = 31the truth16,7265,7885,7886,72623,2362,2982,2983,23632,0351,0971,0972,03541,4415035031,44051,1662282281,16561,00163639968939116891293911401cash register · 40,000 arrivals · stationary Zipfthe shift is the smallest Space-Saving counter, 9383,072 bits against 1,984
Fig. 2 The arithmetic done on the top of one table. Column two is what Space-Saving reports, column three is those numbers minus the smallest counter, column four is a Misra-Gries summary with one fewer counter built independently from the same stream. Three and four are the same on every row.

Checked at every prefix rather than at the end, because a claim that holds only at the end is a claim about one number and this one is about a state:

Space-Saving counters Misra-Gries counters prefixes checked disagreements
8 7 20,000 0
16 15 20,000 0
32 31 20,000 0
64 63 20,000 0

The result is due to Agarwal, Cormode, Huang, Phillips, Wei and Yi, in a 2012 paper about merging summaries, where it appears as a lemma on the way to something else. It is not obscure and it is not what these structures are usually taught with.

Why the mechanisms look different

The two update rules are written in vocabularies that share nothing.

Misra-Gries. A key already present is incremented. A key that is absent, when there is a free slot, takes it. A key that is absent when the table is full causes every counter to be decremented by one, and is itself discarded.

Space-Saving. A key already present is incremented. A key that is absent, when the table is full, evicts the smallest counter’s key, takes the slot, and inherits the count.

One decrements globally and throws the arrival away. The other evicts locally and keeps the arrival. They do not look like the same operation and, taken literally on the stored numbers, they are not.

They are the same operation on the numbers relative to the smallest. A global decrement lowers every counter by one and lowers the minimum by one, so it changes no difference between a counter and the minimum — except for the counter that was at the minimum, which falls to zero and is deleted, freeing a slot. An eviction removes the key at the minimum and puts the new key there at exactly the minimum, so relative to the minimum the new key is at zero and every other counter is unchanged. Both rules, read relatively, do the same two things: retire the key at the bottom, and admit the new one at the bottom.

The one asymmetry is the slot count, and it is the source of the k1k-1.

Ten arrivals, done by hand

The identity is small enough to watch happen. Three Space-Saving counters against two Misra-Gries counters, on the stream 1, 2, 1, 3, 4, 1, 2, 5, 1, 6.

arrival Space-Saving its minimum shifted Misra-Gries decremented so far
1 1:1 0 1:1 1:1 0
2 1:1 2:1 0 1:1 2:1 1:1 2:1 0
1 1:2 2:1 0 1:2 2:1 1:2 2:1 0
3 1:2 2:1 3:1 1 1:1 1:1 1
4 1:2 3:1 4:2 1 1:1 4:1 1:1 4:1 1
1 1:3 3:1 4:2 1 1:2 4:1 1:2 4:1 1
2 1:3 4:2 2:2 2 1:1 1:1 2
5 1:3 4:2 5:3 2 1:1 5:1 1:1 5:1 2
1 1:4 4:2 5:3 2 1:2 5:1 1:2 5:1 2
6 1:4 5:3 6:3 3 1:1 1:1 3

Row four is where the structures first do something different and it is the row to look at. Key 3 arrives; Space-Saving’s table is full, so 3 takes the minimum slot, which held key 2 at 1, and becomes 3:1. Misra-Gries’s table is full, so every counter falls by one — key 2 hits zero and is deleted, key 1 drops from 2 to 1 — and key 3 is discarded without ever being stored.

The two tables now hold different keys. Key 3 is in one and not the other. And the shifted reading matches anyway, because 3’s count of 1 equals the new minimum and the shift takes it to zero, which is exactly the state of a key Misra-Gries has deleted.

That is the mechanism in one row: Space-Saving stores at the minimum the keys Misra-Gries has just thrown away, which costs it a slot and costs it nothing else. Everything the rest of this essay says is that row, repeated.

The last column is the other half of the identity. The minimum climbs 0, 1, 2, 3 and so does the decrement total, in the same steps, on the same arrivals.

Why one counter fewer

Space-Saving’s table is always full once kk distinct keys have arrived; one of its kk counters is always at the minimum, and under the relative reading that counter holds zero. A Misra-Gries table has no such entry, because a counter reaching zero is deleted.

So the relative reading of a kk-counter Space-Saving table is a Misra-Gries table with at most k1k-1 non-zero entries. Space-Saving spends one slot holding the origin.

That is checkable in the direction that matters — the wrong pairing must fail — and the way it fails is informative:

Space-Saving against Misra-Gries with agrees until then
k=8k = 8 8 item 8 never again
k=16k = 16 16 item 22 never again
k=32k = 32 32 item 54 never again
k=64k = 64 64 item 128 never again

The agreement lasts exactly until the tables fill, which is when the two update rules first differ at all: before that, both structures are just counting, and every counter is exact. The moment the first eviction happens the readings separate and never rejoin.

That negative half is why the assertion behind this essay is written as a pair. A check that only confirmed the k1k-1 correspondence would be passed by any two structures agreeing on the heavy keys, which these do; requiring the kk correspondence to fail is what makes the statement about kk rather than about the head of a distribution.

The same state, read twice: subtract 2,144 and one table becomes the otherThe top 8 keys of a Space-Saving summary with 16 counters over 40,000 arrivals, with its own numbers, those numbers minus its smallest counter, and the table a Misra-Gries summary with 15 counters built from the same stream. The third and fourth columns are identical on every row. They are usually presented as rival structures with opposite failure modes — one never under the truth, the other never over — and the opposition is real; what is not real is that they are two structures. One holds 1,536 bits and the other 960, and the difference is the third counter per key that Space-Saving keeps so it can report its own error.keySpace-Saving− 2,144Misra-Gries, k = 15the truth16,7264,5824,5826,72623,2361,0921,0923,23632,15612122,03542,145111,44062,1451199682,14511689142,14511379152,14511359cash register · 40,000 arrivals · stationary Zipfthe shift is the smallest Space-Saving counter, 2,1441,536 bits against 960
Fig. 3 The same conversion on a smaller table, where the shift is a larger share of every count and the identity is under more strain. It does not move, because it does not depend on the size of anything.

What follows, and it is not nothing

Once the structures are one, several things that are usually discussed as differences between them stop being differences.

“Which has better accuracy” is not a question about structures. It is a question about which end of an interval to report, and the previous essay measured the answer: the upper end is better above about rank one hundred and the lower end is better below it, on one stream, from one state. There is no configuration in which one structure is more accurate, because there is only one structure.

The two one-sidednesses are the two ends of one bracket. Space-Saving is never below the truth; Misra-Gries is never above it. Both statements are true of the same table, and the width between them is the smallest counter. Presented as properties of rival algorithms they look like a design trade-off; presented as an interval they look like what they are, which is a single quantity the structure knows and reports half of.

The bounds are the same bound. Misra-Gries’s shortfall is at most n/(k+1)n/(k+1); Space-Saving’s overshoot is at most n/kn/k. Substituting kk1k \mapsto k-1 makes the second exactly the first, which is a sanity check the correspondence had to pass and does.

And a merge rule proved for one is a merge rule for the other. That is the use Agarwal and colleagues were making of it: the difficult part of merging these summaries had been done for Misra-Gries and needed doing for Space-Saving, and the lemma made the second free.

How the check is written

An identity like this is the kind of claim that is easy to assert and easy to assert falsely, so it is worth saying what would have to happen for the assertion behind it to pass while being wrong.

The check runs both structures, converts one, and compares the tables — at every prefix, both keys and counts, sizes included. Three ways it could be vacuous, and what stops each:

It could compare at the end only. Then two structures that converge on the heavy keys would pass, and there are several such pairs. Comparing at every prefix means the check fails the first time a single arrival is handled differently, which on a twenty-thousand-item stream is a strong condition.

It could compare only the counts of keys present in both. Then a structure holding extra keys would pass. The sizes are compared first.

It could be satisfied by any two similar structures. This is the real risk and it is why the wrong pairing is required to fail. Without that clause, an implementation in which the shift was computed from the Misra-Gries side — or one in which both structures were secretly the same object — would sail through.

And one rejection is carried beside it: a conversion performed without the subtraction. Reading Space-Saving’s raw counters as a Misra-Gries table is what a plausible reading of both papers would suggest, and the check requires it to differ, by exactly the minimum.

What is genuinely different

Three things survive, and it is worth being precise about them because “the same structure” is a claim that invites overreach.

The third counter. Space-Saving stores an error per key — the value it inherited — which Misra-Gries does not. That is real state, thirty-two bits a slot, and it is what makes the per-key bracket available at query time rather than only the global bound. Nothing in the correspondence produces it: the conversion needs only the minimum, and the minimum is one number rather than kk.

The reporting rule. Which is the whole of the accuracy difference and is not a property of the state.

The work per arrival. Misra-Gries’s decrement touches every counter, so an arrival that misses costs Θ(k)\Theta(k); Space-Saving’s eviction touches one slot plus whatever it costs to find the minimum, which with the right heap is Θ(logk)\Theta(\log k) and in the implementation measured here is a linear scan. The stored numbers are the same and the number of operations performed to maintain them is not. This is the second count disagreeing with the first again, and it is the practical reason Space-Saving is the one that gets implemented — a fact that has nothing to do with accuracy and is usually explained in terms of it.

24 Misra-Gries counters after 40,000 itemsThe bars are what the 24 counters held when the stream stopped; the tick beyond each marks that key's true count. Every counter is at or below the truth — the decrement rule can only take away — and the largest shortfall here is 1,261 against a guaranteed bound of N/(k+1) = 1,600. 6 of the 24 keys kept are among the 24 genuinely most frequent. No hashing and no randomness are involved anywhere: the bound holds on every stream, not on most of them.key 05,532short by 1,261key 11,994short by 1,261key 2772short by 1,261key 3151short by 1,261key 416short by 1,217key 61short by 755key 221short by 190key 341short by 126key 431short by 108key 691short by 62key 721short by 56key 791short by 52key 1211short by 42key 1291short by 42key 6771short by 3counter held (bar) against true count (tick)24 counters · 1,536 bits · no randomnessbound N/(k+1) = 1,600
Fig. 4 One of the two structures, drawn as this collection has drawn it since the field opened. Every statement on this plate is also a statement about the other one, shifted by a number that is not on it.

What the third counter is worth, and where

The error field is listed above as one of three genuine differences, at thirty-two bits a slot, and it is worth pricing rather than noting — because the conversion needs one number and Space-Saving stores kk of them, which invites the reading that k1k-1 of them are redundant.

They are not, and the hand-worked table says why. A key’s error field is the minimum at the moment that key entered, not the minimum now. Those are different numbers and the gap between them is the whole value of the field.

Follow key 1 down the ten-arrival table. It enters at the first arrival, when the table is empty and the minimum is zero, so its error is zero and stays zero. By the tenth arrival its counter reads 4 and the table’s minimum has climbed to 3. The global bound therefore permits key 1’s true count to be anywhere from 1 to 4; the error field says it is exactly 4.

That is the pattern rather than a lucky row. A heavy key enters early, when the minimum is small, and is never evicted afterwards — so its error is frozen at whatever the minimum was when the stream was young, while the global floor keeps climbing on everybody else’s behalf. A light key enters late, at the current minimum, and carries the whole of it.

So the thirty-two bits a slot buy a bracket that is tightest exactly where a heavy-hitter query looks. The global floor is the worst case over the table and is carried by the keys nobody asked about; the per-key error is near zero for the top of the distribution and near the floor for the bottom. On the ten-arrival table the difference between the two readings for key 1 is the difference between an interval three wide and an exact answer.

Two consequences worth having.

The identity does not make the extra state redundant. The conversion needs the current minimum because it is converting the table, and a table is a state at an instant. The error fields are a record of the table’s history, which the shift discards, and history is what makes a per-key bracket possible at all. Two structures with the same state can carry different amounts of information about how they got there.

And it explains which structure gets deployed, again. The essay’s third difference — that Misra-Gries pays Θ(k)\Theta(k) per missed arrival and Space-Saving pays Θ(logk)\Theta(\log k) — is one practical reason Space-Saving is the implemented one. This is a second, and it is about the answer rather than the work: a count that is never under is a useful guarantee, and never under and provably exact on the keys that matter is a considerably more useful one. Neither is available from the shifted reading, and both come from a field the correspondence has no use for.

Which sharpens rather than weakens the identity. The two structures hold one state; one of them also holds an annotation on it; and the annotation is worth nothing to a merge, nothing to the bound, and a great deal to a query about the top of the distribution — which is exactly what the counter that takes the smallest slot measured when it found the upper reading better above rank one hundred and worse below it.

The shape this is an instance of

A shared name is not a shared function, and this collection has made a point of it. The reverse case is rarer and is what happened here: two different names, two different pseudocodes, two literatures, and one function.

The thing that hid it is that the difference between the two states is a constant offset, and an offset is invisible in every way these structures are usually looked at. It does not change the ordering, so a top-kk comparison shows nothing. It does not change the ratio between counters, so a plot of the distribution shows nothing. It does not change which keys are present, except at the single boundary key. It changes the numbers, which is exactly what a reader compares — and it changes all of them by the same amount, which is exactly what a reader does not check for.

Space-Saving's bracket is empty where the answers matter and 400 wide where they do notThe 5 heaviest and the 5 lightest of the 64 counters in a Space-Saving summary over 40,000 arrivals. Each bar runs from the key's lower bracket — its counter minus the value it inherited when it took a slot — to the counter itself, and the mark is the true count. The structure holds both ends and reports the upper one, which is why it is described as never underestimating; the other end is a thing Count-Min does not have at all. 5 of these 10 rows have no bracket: a key that took a slot before the table filled inherited nothing, so its counter is its exact count. The wide ones are at the bottom, where a key inherited the 401 that the evicted key had accumulated. So the structure is exact precisely on the keys anybody asks it about, and uncertain on the ones it is about to forget. Misra-Gries's shortfall bound over the same stream is 625.key 16,726exactkey 23,236exactkey 32,035exactkey 41,440exactkey 51,165exactkey 12983± 400key 13755± 400key 16412± 400key 17483± 400key 19673± 400bracket, with the truth marked · widest 400 · 5 exactcash register · stationary Zipf · 64 countersthe smallest counter is 401
Fig. 5 The bracket, which is the offset drawn as a width. Every bar on this plate is the distance between what one structure reports and what the other does, on the same key, from the same state.
The smallest Space-Saving counter, and everything Misra-Gries has thrown awayTwo structures, run side by side over 40,000 arrivals. The first line is the value of the smallest counter in a Space-Saving summary with 32 counters — the number a new key inherits when it takes a slot. The second is the total that a Misra-Gries summary with 31 counters has decremented away over the same stream. A reader would expect these to be two unrelated pieces of bookkeeping in two different algorithms. They are one line drawn twice: 921 against 921 at the end, and equal at every one of the 40,000 prefixes checked, not merely at the end. Subtract the first from every Space-Saving counter and what is left is the Misra-Gries table, key for key and count for count.2535077601,013020,00040,000arrivals so farcountSpace-Saving's floorMisra-Gries's decrementsboth 92140,000 prefixes, 0 disagreementsk = 32 against k = 31
Fig. 6 The same two quantities on a stream whose popular keys drift, so that the table churns and the minimum climbs in steps rather than smoothly. The two lines follow each other through the steps as well as through the straight stretches, which is what makes this an identity rather than a coincidence of averages.

A drifting stream is one way for the table to churn; a bursty one is the other, and it produces the opposite shape. Where drift keeps the minimum climbing steadily because every arrival is a new contender, a burst is one key arriving over and over into a slot it already holds — so nothing is evicted, the minimum does not move, and the correspondence has a flat stretch to be checked on.

The smallest Space-Saving counter, and everything Misra-Gries has thrown awayTwo structures, run side by side over 40,000 arrivals. The first line is the value of the smallest counter in a Space-Saving summary with 24 counters — the number a new key inherits when it takes a slot. The second is the total that a Misra-Gries summary with 23 counters has decremented away over the same stream. A reader would expect these to be two unrelated pieces of bookkeeping in two different algorithms. They are one line drawn twice: 1,249 against 1,249 at the end, and equal at every one of the 40,000 prefixes checked, not merely at the end. Subtract the first from every Space-Saving counter and what is left is the Misra-Gries table, key for key and count for count.3436871,0301,374020,00040,000arrivals so farcountSpace-Saving's floorMisra-Gries's decrementsboth 1,24940,000 prefixes, 0 disagreementsk = 24 against k = 23
Fig. 7 And on a stream with a burst in it, where the minimum stops climbing for the length of the burst — a flood of one key evicts nobody, because that key is in the table — and resumes afterwards. The decrement total does the same thing for the same reason.

Why it stayed hidden for seventeen years

Misra and Gries published in 1982; Metwally, Agrawal and El Abbadi in 2005; the correspondence in 2012. Two of those gaps are long enough to be worth a sentence about what makes a fact like this hard to notice.

The two papers are about different things. Misra and Gries were solving a decision problem — find the elements occurring more than n/kn/k times — and their structure is a means to it, described in terms of a “candidate set” rather than as a summary anybody would query. Metwally and colleagues were building a system for network monitoring, and their structure is described as a data structure with an API. Neither paper’s abstract is about the object the other paper’s is about, and a reader looking for one would not find the other.

And the empirical route is blocked by the offset. Anybody running both structures on the same stream and comparing them would see two tables of different sizes holding overlapping key sets with systematically different numbers — which reads as “two different approximations”, because that is what two different approximations look like. Seeing the identity requires subtracting a quantity nobody has a reason to compute, and neither structure’s interface exposes it: Space-Saving’s minimum is an implementation detail of its eviction, and Misra-Gries’s decrement total is not stored at all in the usual implementation.

That is worth generalising, because it is the same failure this collection keeps finding in other forms. A relationship that is a constant offset is invisible to every comparison that is scale-free. Ratios, orderings, distributions, plots on log axes: all of them are unchanged by adding the same number to everything, and all of them are what a reader reaches for when comparing two structures. The one comparison that would have shown it — subtract and see if the difference is constant — is the one nobody makes, because there is no reason to expect a constant.

The same state, read twice: subtract 921 and one table becomes the otherThe top 8 keys of a Space-Saving summary with 32 counters over 40,000 arrivals, with its own numbers, those numbers minus its smallest counter, and the table a Misra-Gries summary with 31 counters built from the same stream. The third and fourth columns are identical on every row. They are usually presented as rival structures with opposite failure modes — one never under the truth, the other never over — and the opposition is real; what is not real is that they are two structures. One holds 3,072 bits and the other 1,984, and the difference is the third counter per key that Space-Saving keeps so it can report its own error.keySpace-Saving− 921Misra-Gries, k = 31the truth19222,0161,0951,0951,17817941,9039829821,15616661,8329119111,16214101,7328118111,21015381,7298088081,13912821,6477267261,19811541,5436226221,1528981,4475265261,190cash register · 40,000 arrivals · the popular keys driftthe shift is the smallest Space-Saving counter, 9213,072 bits against 1,984
Fig. 8 The conversion on a stream whose popular keys drift, where the tables churn completely over the course of the run and no key survives from beginning to end. The subtraction still turns one into the other, because it is about the state rather than about which keys happen to be in it.

What is not measured here

The general merge. The correspondence transports Misra-Gries’s merge rule to Space-Saving, and this collection has measured that rule for Misra-Gries only. Whether the transported rule is the one implementations actually use is a question about implementations rather than about the structures.

Weighted updates. Both structures generalise to updates carrying a weight rather than counting one, and the correspondence is stated here for unit updates because that is what is measured. Nothing in the argument obviously requires unit weights and nothing here establishes that it survives them.

Ties. When several counters share the minimum value, which one is evicted is a choice, and the implementation here breaks the tie by key so that a run is reproducible. Misra-Gries’s decrement deletes all of them at once. The correspondence survives it here — a tie means several keys sitting at relative zero, and the shifted reading has none of them either way — but a Space-Saving implementation whose tie-break made a heavy key evictable would not be the same structure any more, and that is a property of the tie-break rather than of either algorithm.

Space-Saving's bracket is empty where the answers matter and 1,158 wide where they do notThe 4 heaviest and the 4 lightest of the 24 counters in a Space-Saving summary over 40,000 arrivals. Each bar runs from the key's lower bracket — its counter minus the value it inherited when it took a slot — to the counter itself, and the mark is the true count. The structure holds both ends and reports the upper one, which is why it is described as never underestimating; the other end is a thing Count-Min does not have at all. 3 of these 8 rows have no bracket: a key that took a slot before the table filled inherited nothing, so its counter is its exact count. The wide ones are at the bottom, where a key inherited the 1,159 that the evicted key had accumulated. So the structure is exact precisely on the keys anybody asks it about, and uncertain on the ones it is about to forget. Misra-Gries's shortfall bound over the same stream is 1,667.key 16,841exactkey 20495,565exactkey 22,655exactkey 31,710± 5key 18622± 1,158key 42114± 1,158key 8402± 1,158key 14914± 1,158bracket, with the truth marked · widest 1,158 · 3 exactcash register · a heavy hitter that stops · 24 countersthe smallest counter is 1,159
Fig. 9 And the brackets on a stream where a heavy key stops arriving. Neither structure expires anything, so the departed key keeps an exact and entirely historical count; the identity between them is undisturbed by that, because both are wrong in the same way about the same thing.

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.

Constant factorCounterexampleFalsificationGuaranteeHeavy hitterMeasurementMergeable summaryMergingMisra–GriesOne-sided errorReproducibilitySpace-saving