Structures

The items that survive k counters

Misra-Gries keeps k counters, decrements all of them on a miss, and never returns a count above the truth — with no hashing, no randomness and no failure probability. At equal state it is more accurate than the randomised sketch on the question both are usually asked, at every size measured.

Keep a table of at most kk keys, each with a counter.

For each item: if its key is in the table, increment it. If not and there is room, insert it with a count of one. If not and the table is full, decrement every counter in the table and drop any that reach zero.

That is the entire algorithm. It was published by Misra and Gries in 1982, rediscovered twice in the 2000s under other names, and it contains no hash function, no random number and no probability.

12 Misra-Gries counters after 60,000 itemsThe bars are what the 12 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 4,163 against a guaranteed bound of N/(k+1) = 4,615. 4 of the 12 keys kept are among the 12 genuinely most frequent. No hashing and no randomness are involved anywhere: the bound holds on every stream, not on most of them.key 05,416short by 4,163key 1462short by 4,154key 31short by 2,099key 71short by 943key 19401short by 3counter held (bar) against true count (tick)12 counters · 768 bits · no randomnessbound N/(k+1) = 4,615
Fig. 1 Twelve counters after sixty thousand items. The bars are what the counters held when the stream stopped; the tick beyond each marks that key’s true count. Every counter is at or below the truth, because the only operations are increments of a key’s own counter and decrements that apply to everything, and the largest shortfall is inside the bound N/(k+1)N/(k+1) that the structure guarantees on every stream.

Why the decrement rule works

The counters do not track counts. They track a quantity that is best described as surplus: how far ahead a key is of the general level of everything competing for a slot.

The invariant is that each decrement step throws away k+1k+1 occurrences at once — one for the arriving item, which is discarded, and one from each of the kk counters. So if there have been DD decrement steps over the whole stream, then D(k+1)ND(k+1) \le N, giving DN/(k+1)D \le N/(k+1).

Now take any key xx. Its counter was incremented once for each of its own occurrences that was not thrown away, and decremented at most DD times. So

fxD    f^x    fx,DNk+1.f_x - D \;\le\; \hat f_x \;\le\; f_x, \qquad D \le \frac{N}{k+1}.

Both halves at once, for every key simultaneously, on every stream. There is no “with probability” anywhere in it and no input that defeats it.

Introsort against an adversary that answers the comparisonsMcIlroy's killer adversary does not choose an array — it decides the order of two elements at the moment it is asked, always so that the pivot turns out to be the largest element seen. With the depth limit removed it drives introsort to 16,793,519 comparisons at n = 8,192, which is 0.500 of n²/2. With the limit in place the same attack costs 399,927 — a factor of 42.0 saved, and growing — because one call to heapsort ends it. The unlimited run's recursion is 4,088 levels deep at that size. Random input is drawn for scale.10³10³10⁴10⁵10⁶10⁷ncomparisonsadversary, no depth limitadversary, as it shipsrandom inputn²/2McIlroy's adversary, answering as it goescomparisons, counted exactly
Fig. 2 What “no input that defeats it” is worth, measured somewhere it can be. In the practice field an adversary constructed against a library sort drives its cost from linearithmic to quadratic, because the guarantee there is about typical inputs. Nothing of that kind exists for this structure: its bound is derived by counting occurrences thrown away, which is an accounting identity rather than a probabilistic argument, so an adversary has nothing to aim at.

On the stream measured here, with k=32k = 32 and N=60,000N = 60{,}000, the bound is 1,818 and the worst shortfall actually suffered by any key is 1,397 — the guarantee is tight to within 30%, which is unusually tight for a bound of this kind and is worth contrasting with what the randomised sketch’s bound turns out to be worth.

The error is one-sided, in the other direction

Count-Min never returns a count below the truth. Misra-Gries never returns one above it. The two structures answer the same question and are wrong in opposite directions, and that is more useful than it sounds.

An upper bound rules keys out: this sketch says at most 40, so the key is not a heavy hitter. A lower bound rules keys in: this table says at least 3,000, so the key certainly is one. Running both gives an interval, and the interval is exact in the sense that the truth is provably inside it. Neither structure alone provides that.

It also settles an argument about which is “safer” that has no general answer. A monitoring system that must never miss a heavy hitter wants the upper bound; one that must never raise a false alarm wants the lower. Choosing is a statement about which mistake costs more, and it is the same shape of decision as a Bloom filter’s — a filter allowed to be wrong sets it out in the membership setting.

At equal bits, the deterministic one wins

The field is usually presented with the randomised structures as the powerful ones and the counter table as the simple predecessor. On the question both answer, at equal state, the measurement goes the other way at every size tried.

The comparison has to be set up carefully. Misra-Gries holds kk keys and kk counters, which at 32 bits each is 64k64k bits. Count-Min holds d×wd \times w counters at 32 bits each. Setting d=4d = 4 and w=2kw = 2k makes the two the same size, and both are then asked about the ten genuinely most frequent keys.

state Misra-Gries, worst error on the top ten Count-Min, worst error on the top ten
1,024 bits 3,053 5,370
2,048 bits 1,397 2,286
4,096 bits 618 854

The bounds go the same way and by more. At 2,048 bits the counter table promises a shortfall of at most 1,818; the sketch promises an over-count of at most 10,194, which on a stream of sixty thousand is 17% of everything that went past.

The reason is not mysterious. Count-Min spends its state on being able to answer about any key, including the 836 keys in this stream that occurred exactly once. Misra-Gries spends all of its state on the keys that survived competition, and answers zero for everything else. When the question is “what were the frequent items”, the second allocation is simply the better one, and the sketch’s generality is being paid for and not used.

What it cannot do

Three things, and they are why the randomised structure exists.

It cannot be merged into a fixed answer. Two Misra-Gries tables can be combined and the result satisfies the same bound, which is a real and non-obvious theorem — but the merged table is not the table the concatenated stream would have produced. It holds a different set of keys. The summaries that add measures the difference and separates it from the exact mergeability the register-based structures have.

It cannot handle decrements. The invariant counts occurrences thrown away, and an occurrence that is removed from the stream has no meaning in that accounting. There is no turnstile version.

It cannot answer about a key that is not in the table with any precision. It returns zero, and zero is a lower bound and nothing more. For a key that occurred N/(k+1)N/(k+1) times — just under the threshold — the answer is zero and the truth is over a thousand. The sketch, for all its looseness, returns a number with the right order of magnitude for such a key, because its error is additive rather than a cliff.

The threshold reading

The clean way to state what the structure delivers is as a threshold rather than as an accuracy.

Any key occurring more than N/(k+1)N/(k+1) times must be in the final table. If it were not, it would have been decremented out, which takes at least as many decrement steps as it had occurrences, which is more than DD can be. So setting k=1/ϕ1k = \lceil 1/\phi \rceil - 1 guarantees that every key with more than a ϕ\phi share of the stream is present.

That is a very strong statement for such a small structure: to catch every key carrying more than 1% of a stream of any length, ninety-nine counters suffice, forever. It comes with the necessary caveat that the table also contains keys that do not qualify — the guarantee is one-directional — so a second pass, or a Count-Min sketch alongside, is needed to say which of the survivors genuinely cleared the bar.

The two-pass version is worth naming because it is what production implementations do and because it steps outside this field’s rules to do it. Pass one finds the candidates; pass two counts them exactly. That is not a streaming algorithm, and its accuracy is not comparable with anything on this page.

24 Misra-Gries counters after 200,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 6,883 against a guaranteed bound of N/(k+1) = 8,000. 8 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 020,705short by 6,883key 15,813short by 6,883key 21,384short by 6,880key 39short by 5,994key 41short by 4,689key 91short by 2,158key 121short by 1,613key 141short by 1,439key 251short by 768key 411short by 471key 681short by 262key 3221short by 50key 26791short by 6key 32121short by 2key 59841short by 5key 88521short by 0key 93251short by 0key 98561short by 2counter held (bar) against true count (tick)24 counters · 1,536 bits · no randomnessbound N/(k+1) = 8,000
Fig. 3 Twenty-four counters on a stream of two hundred thousand over a universe of sixty-five thousand. The bound is N/(k+1)=8,000N/(k+1) = 8{,}000, and the structure holds 1,536 bits against the 1,124,475 an exact hash map of this stream takes. What the picture shows is the shape of the answer rather than a set of counts — the keys are in roughly the right order and their counters are all short by roughly the same amount, which is what a uniform decrement does.

Why it is in the structures field

It sits with heaps and hash tables rather than with the sketches, and the placement is a claim.

Everything else in this phase is a projection: the state is a lossy image of the frequency vector, and the key never appears in it. Misra-Gries keeps keys. Its table is an ordinary associative structure with an eviction policy, its state is legible, and the operations on it are the ones any cache performs. What makes it belong to the streaming field’s argument is the policy — decrement everything rather than evict the smallest — and what makes it belong here is that it is a data structure in the sense the rest of this site uses the word.

The eviction-policy framing is also the clearest way to see why the odd-looking rule is the right one. Evicting the minimum on a miss is the obvious alternative and it has no bound at all: a stream that alternates between two keys with a table of size one evicts on every item and reports nothing, forever. The decrement rule charges the arriving item against every incumbent, so the incumbents pay for their own protection, and the accounting closes.

Both structures are worth drawing once more at a different width, because the whole comparison is a comparison at equal bits and a width is what buys the bits.

A 4×64 Count-Min table after 40,000 itemsEach cell is a counter and each row has its own hash, so a key lands in exactly one cell per row — the four ringed cells are where key 0 went. The darker a cell, the larger its count. Key 0 truly occurred 6,793 times; its four cells hold 7,025, 7,009, 7,009, 7,060, and the estimate is the smallest of them, 7,009. Every cell is the key's count plus whatever else collided there, so every cell is an overestimate and the minimum is the least bad one. The whole table is 8,192 bits against 86,265 for an exact counter.hash rowh1h2h3h4h1 → cell 62: 7,025h2 → cell 29: 7,009h3 → cell 19: 7,009h4 → cell 36: 7,060key 0 occurred 6,793 times · the minimum of the four is 7,009 · over by 216the additive bound at this width is e/w × N = 1,6994×64 counters · 8,192 bits · Zipf s = 1.1exact would take 86,265 bits
Fig. 4 A 4×64 Count-Min table after forty thousand items, twice as wide as the one above. Key 0 truly occurred 6,793 times; its four cells hold 7,025, 7,009, 7,009 and 7,060, and the estimate is the smallest of them. Every cell is the key’s count plus whatever collided there, so every cell is an overestimate and the minimum is the least bad one. The table is 8,192 bits against 86,265 for an exact counter.
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. 5 Twenty-four Misra-Gries counters on the same stream. Every counter is at or below the truth — the decrement rule can only take away — and the largest shortfall is 1,261 against a guaranteed bound of N/(k+1)=1,600N/(k+1) = 1{,}600. Six of the twenty-four keys kept are among the twenty-four genuinely most frequent, and no hashing and no randomness are involved anywhere.

What the rule costs per item

A decrement step touches every counter, so a naive implementation is O(k)O(k) in the worst case and the worst case happens on every miss. On a stream with no skew that is O(k)O(k) per item, which for k=99k = 99 is a hundred operations to process one.

It is avoidable and the fix is worth knowing because it is a nice piece of ordinary data-structure work. Keep the counters grouped by value in a doubly linked list of buckets, so that “decrement everything” is a single operation on the lowest bucket rather than kk operations on kk counters. Then every step is O(1)O(1) and the structure’s per-item cost is a constant, matching Count-Min’s dd hashes rather than losing to it by a factor of kk.

The measurement here does not use that implementation, because the quantity these essays report is state and error rather than time, and the two implementations hold identical state and produce identical answers. What the choice would change is a number this field does not print. It is recorded here so the comparison in the table above is not read as a comparison of speeds, which it is not.

A 4×16 Count-Min table after 60,000 itemsEach cell is a counter and each row has its own hash, so a key lands in exactly one cell per row — the four ringed cells are where key 0 went. The darker a cell, the larger its count. Key 0 truly occurred 9,579 times; its four cells hold 12,468, 12,419, 12,093, 12,103, and the estimate is the smallest of them, 12,093. Every cell is the key's count plus whatever else collided there, so every cell is an overestimate and the minimum is the least bad one. The whole table is 2,048 bits against 162,288 for an exact counter.hash rowh1h2h3h4h1 → cell 14: 12,468h2 → cell 13: 12,419h3 → cell 3: 12,093h4 → cell 4: 12,103key 0 occurred 9,579 times · the minimum of the four is 12,093 · over by 2,514the additive bound at this width is e/w × N = 10,1944×16 counters · 2,048 bits · Zipf s = 1.1exact would take 162,288 bits
Fig. 6 The randomised structure at the state the head-to-head compares against: four rows of sixteen counters, 2,048 bits, the same as thirty-two Misra-Gries counters. Sixty-four cells are holding a summary of 3,528 distinct keys, so each cell carries about fifty-five keys’ counts, and the estimate for any key is the smallest of the four piles it happens to be in. The picture is worth setting beside the counter table’s: the same bits, spent on being able to answer about anything rather than on remembering which keys mattered.

Space-Saving, named and not measured

There is a close relative that keeps kk counters and overestimates rather than underestimates. On a miss with the table full it takes the smallest counter, replaces its key with the arriving one, and increments — so the new key inherits the evicted key’s count as a head start.

That gives an upper bound rather than a lower one, with the same N/(k+1)N/(k+1)-shaped error, and it has a property Misra-Gries lacks: the table always holds exactly kk keys, so the structure degrades more gracefully when the stream has fewer heavy hitters than the table has slots.

It is named here and not implemented, on the same principle the coding field applied to arithmetic coding’s successors: quoting its measured accuracy against numbers produced here by a different structure would be asserting a comparison rather than making one, and the variants differ in exactly the ways that decide such a comparison. What can be said without measuring anything is structural — one gives an upper bound, the other a lower, and the interval between two structures of kk counters each is narrower than either alone.

The floor of each shard, computed from its histogram and then measured8 shards of 40,000 arrivals of stationary Zipf — the control, where a window is a smaller sample, partitioned round-robin: every key on every shard, each summarised by 32 counters. The pale bar is the naive estimate: the mass outside the shard's top 32 keys divided by 32. The mid bar is the fixed point, which divides the same mass by the number of slots that are actually churning. The dark bar is the smallest counter the structure settled at. The fixed point is within 0.6% of it on average; the naive estimate is low by about a factor of 1.8.shard 1 · 120 → 119shard 2 · 121 → 120shard 3 · 120 → 120shard 4 · 121 → 120shard 5 · 119 → 118shard 6 · 124 → 123shard 7 · 121 → 120shard 8 · 122 → 121floor, in arrivalsnaive: tail ÷ kfixed pointmeasuredstationary Zipf · round · k = 321.006× the measured floor
Fig. 7 The smallest counter each of eight tables settles at, beside two numbers computed from the arrival frequencies before any table exists. Only one of the two is right, and the gap between them is the counters that hold keys heavy enough never to be evicted.

The one counter it does not keep

The structure can bracket its own answers, exactly, for the price of a single extra integer — and it does not, which is worth pointing out because the omission is universal and the repair is a line of code.

The shortfall for any key is at most DD, the number of decrement steps performed. DD is not a quantity that has to be bounded or estimated: it is a thing the structure does, and counting it costs one increment per decrement step. With it in hand, every answer becomes an interval rather than a number:

f^x    fx    f^x+D\hat f_x \;\le\; f_x \;\le\; \hat f_x + D

and the truth is provably inside, with no probability attached anywhere.

The interval is a good deal tighter than the guarantee. On the stream measured above with k=32k = 32, the bound N/(k+1)N/(k+1) is 1,818 and the measured worst shortfall is 1,397 — but DD itself is what the structure actually did, which is at most 1,397 and is known exactly rather than bounded. The guarantee is a worst case over streams and DD is a measurement of this one, so reporting DD converts a promise into a fact, and the fact is always at least as good.

That closes the gap this essay’s comparison leaves open in an uncomfortable place. Count-Min’s number arrives with an additive bound that is a design parameter — 10,194 at the state compared above, and the realised errors are far below it with no way to know by how much. Misra-Gries’s number can arrive with an additive bound computed from the run, and every key’s interval is the same width because the decrements were applied to everything.

It is the same move a quantile summary can make and does not: the structure holds enough to say how much it does not know, and reports a point estimate instead. The difference here is that the accounting is exact rather than approximate, and the cost is one counter.

The order is exact even though the counts are not

There is a second property hidden in the uniformity of the decrement, and it is the one that matters most for the question the structure is usually asked.

Every decrement step subtracts one from every counter. So two keys that have both been in the table since before the first decrement have both been decremented exactly DD times, and their counters differ by exactly what their true counts differ by:

f^xf^y  =  fxfy\hat f_x - \hat f_y \;=\; f_x - f_y

Their relative order is exact, and so is the gap between them. The counts are all wrong by the same amount and a shared error cancels in a difference.

That is a much stronger statement than the bound suggests, and it is the statement a top-kk query actually needs. A monitoring system asking which five endpoints are busiest does not need any of the five counts to be right; it needs the ranking, and the ranking of the long-lived incumbents is not approximate at all.

The qualification is the phrase since before the first decrement. A key that entered the table late has missed some of the decrements, so its shortfall is smaller and its counter is closer to the truth than its neighbours’ — which biases it upward relative to them. So a newcomer can outrank an incumbent it has not really overtaken, and the error is one-directional: recent arrivals look better than they are.

That gives the practical reading. The table’s ordering is trustworthy among keys that have been there a while and optimistic about keys that have just appeared, which is exactly the behaviour a reader would want from a structure designed to find persistent heavy hitters and exactly the wrong behaviour for detecting a sudden burst. The structure is a summary of the whole stream, and something looking for a change needs a window rather than a better counter rule.

The measurement that settles it

The claim this essay is built on — that the deterministic structure is more accurate at equal bits — is exactly the kind that is easy to arrange by choosing the comparison. Three things were fixed in advance to keep it honest.

The state is counted in bits, from the shape of both structures, including the key stored beside each Misra-Gries counter. Leaving the keys out would have halved its apparent cost and won the comparison by bookkeeping.

The question is the one both are designed for: the ten genuinely most frequent keys, taken from the exact truth. Asking about the tail would have won it for Count-Min, which returns a number there, and asking about a single heavy key would have been one sample.

And the direction of each error is reported rather than its magnitude alone, since the two are wrong in opposite ways and an unsigned comparison would hide it.

What the comparison does not show, and is worth stating so the result is not over-read, is anything about streams with no skew. On a uniform stream there are no heavy hitters, the counter table’s contents are arbitrary, and both structures are equally uninformative. The result is about the streams these structures are deployed on, which are skewed, and it holds because skew is what a kk-counter table is built to exploit.

Nor does it show that the randomised structure was a mistake. Count-Min answers a strictly larger set of questions, merges without loss, survives decrements, and gives an upper bound where this gives a lower one. What the comparison shows is narrower and still worth having: on the one question both are built for, at the same number of bits, the structure with no randomness in it is the more accurate of the two, and its bound is five times tighter. A field that presented the randomised structures as the advance would have that the wrong way round, and the way to find out which it is was to count the bits on both sides and ask them the same question.

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

The 8 essays that link to this one and share the most of its objects, of 13 that link here.

The objects this essay names

Each one links to every other essay that touches it.

Additive errorCount-Min sketchDeterministic algorithmEstimatorGuaranteeHeavy hitterMisra–GriesOne-sided errorSketchState bitsTrade offZipf distribution