Structures

The counter that takes the smallest slot

Space-Saving keeps two numbers per key and they bracket the truth from both sides. On the twenty heaviest keys of a stream its mean error is a tenth of one arrival, against a hundred and ten for Misra-Gries at the same bits — and on the keys ranked past a hundred the ordering reverses.

Misra-Gries answers a heavy-hitter query with a number that is never above the truth. Count-Min answers with one that is never below it. Both are useful and both are one-sided, and this collection has measured both.

There is a third counter-based structure that keeps two numbers per key, and the pair brackets the truth from both sides.

Space-Saving, from Metwally, Agrawal and El Abbadi in 2005, works like this. Keep kk counters, always full once kk distinct keys have arrived. A key already in the table is incremented. A key that is not present takes the slot of the smallest counter — and it keeps that counter’s value and adds one to it. The value it inherited is written down beside it as that key’s error, because everything up to it belonged to somebody else.

So the structure holds, for each key, a count and an amount of that count it is not sure about. The true frequency is between count − error and count, always.

Space-Saving's bracket is empty where the answers matter and 937 wide where they do notThe 4 heaviest and the 4 lightest of the 32 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 938 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,250.key 16,726exactkey 23,236exactkey 32,035exactkey 41,440± 2key 3109± 937key 42117± 937key 8402± 937key 14914± 937bracket, with the truth marked · widest 937 · 3 exactcash register · stationary Zipf · 32 countersthe smallest counter is 938
Fig. 1 Each bar runs from a key’s lower bracket to its counter, and the mark is the true count. The heavy keys have no bracket at all — a key that took a slot before the table filled inherited nothing, so its counter is exact — and the wide ones are the keys the structure is about to forget.

The bracket is empty where the answers are

The first surprise is on that plate and it is worth stating on its own: the structure is exact on precisely the keys anybody asks it about.

A key’s bracket is the value it inherited when it last took a slot. A key that has been in the table since before the table filled inherited nothing, so its bracket is zero and its counter is its true count. And the keys that were there from the beginning are, on any skewed stream, the heavy ones.

At sixteen thousand bits over sixty thousand arrivals of a Zipf stream, Space-Saving is exact on nineteen of the twenty heaviest keys and off by one on the twentieth. The bracket at the bottom of the table is 937 wide.

Space-Saving's bracket is empty where the answers matter and 937 wide where they do notThe 8 heaviest of the 32 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 938 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,250.key 16,726exactkey 23,236exactkey 32,035exactkey 41,440± 2key 51,165± 39key 6996± 11key 8689± 937key 12401± 934bracket, with the truth marked · widest 937 · 3 exactcash register · stationary Zipf · 32 countersthe smallest counter is 938
Fig. 2 The eight heaviest counters alone, where almost every bar has collapsed to a point. This is the whole table’s most useful property and it is a consequence of the eviction rule rather than of anything the analysis promises.
Space-Saving's bracket is empty where the answers matter and 937 wide where they do notThe 8 lightest of the 32 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. 0 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 938 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,250.key 15122± 937key 15224± 937key 18624± 937key 26121± 937key 3109± 937key 42117± 937key 8402± 937key 14914± 937bracket, with the truth marked · widest 937 · 0 exactcash register · stationary Zipf · 32 countersthe smallest counter is 938
Fig. 3 And the eight lightest, where every bracket is the full width of the smallest counter. These keys arrived recently, took a slot from somebody, and inherited that somebody’s accumulated count. The structure knows it does not know, which is more than Count-Min manages.

The published guarantee is weaker than this and is the same guarantee for every key: no reported count is below the truth, and no reported count exceeds the truth by more than n/kn/k. That bound is tight only at the bottom of the table. What the structure knows, key by key, is much more precise than what the bound says — and the second number per key is where it is written down.

What that costs, and what it buys

Three counters’ worth of state per key rather than two: a key, a count, and an error. Ninety-six bits against Misra-Gries’s sixty-four, so at a fixed budget Space-Saving holds two-thirds as many keys.

Measured at three budgets on the same stream, against exact counts from a referee neither structure can reach — mean absolute error on the twenty heaviest keys:

bits Space-Saving Misra-Gries Count-Min
4,096 293.0 546.1 757.5
8,192 40.4 264.6 345.3
16,384 0.1 110.0 134.4

Space-Saving wins at every budget and the margin grows: at the largest it is a thousand times more accurate than either, while holding fewer keys than one of them and a quarter as many cells as the other.

That is a large enough gap to be suspicious of, and the resolution is the next essay’s subject. The short version is that it is not a better structure. Space-Saving with kk counters holds exactly the state Misra-Gries with k1k-1 holds, shifted up by a single number. The difference in the table above is entirely in which end of the bracket each structure reports.

The reversal

Which makes a prediction that can be checked, and it is the check that makes the comparison honest rather than flattering.

If the only difference is which end of an interval is reported, then Space-Saving must be worse wherever the truth sits at the lower end — that is, on the light keys, where a slot has changed hands recently and the inherited count is most of what the counter holds.

rank band keys Space-Saving Misra-Gries Count-Min
1–20 20 0.1 110.0 134.4
21–100 80 68.3 94.2 148.7
101–300 200 50.1 31.8 135.2
301–1000 700 15.2 9.2 149.3

The reversal happens at about rank one hundred, and it is exactly where the argument says it should be: above it the truth is near the top of the bracket and below it the truth is near the bottom. Count-Min is worse than both everywhere, and its error is flat across the bands, because its error is a fraction of the whole stream rather than a property of the key.

The right way to read the table is not that one structure is better. It is that one piece of state supports two reporting rules, that they are optimal on different parts of the distribution, and that the structure holding both numbers is the one that could choose — which is what the bracket is for and what neither of the other two has.

Absolute over-count against a key's true count, 4×64 sketchThe same run, drawn as the absolute over-count rather than the relative one. The dashed line is the additive bound e/w × N = 2,548, and nothing is above it. This is the plot the guarantee is about, and it is flat: the error a key suffers has nothing to do with how often that key occurred. Every claim of the form "accurate to within a per cent" is a claim about this axis divided by the other one.1101001,00010³true count of the keyover-counte/w × N = 2,548heaviest keyrarest key4×64 counters · 8,192 bits · Zipf s = 1.1 · 3,528 distinct keys at 1,085 positionsbound 2,548, worst 1,363
Fig. 4 Count-Min’s version of the same picture, from earlier in this field: absolute error against a key’s true frequency. It is flat, because the error is a share of the stream’s L1 norm and does not care which key was asked about — which is exactly why it is the worst of the three at the top of the distribution and competitive nowhere.

The bound, and what the structure actually knows

The published guarantee says the overshoot is at most n/kn/k. The smallest counter is what the overshoot actually is. Those are different numbers and the gap between them is worth measuring, because the first is what a reader is told and the second is what the structure could report.

counters bound n/kn/k smallest counter worst actual overshoot mean overshoot
42 1,429 1,046 1,046 776
85 706 445 444 302
170 353 187 187 120

Two things in the last two columns. The worst overshoot equals the smallest counter exactly, at every size — which is not a coincidence but a restatement of the eviction rule, since the largest inheritance any key can be carrying is the largest value the smallest slot has ever held, and that is the current smallest counter. And the mean overshoot across the whole table is about two-thirds of it, because most keys took their slot when the smallest counter was smaller than it is now.

The bound is between 1.4 and 1.9 times the truth here, which is a good bound as bounds go. What matters is that it is a bound about the structure and the bracket is a fact about the key — and the second is available for free, at query time, from a number the structure is already storing.

This is the same shape as the constant the notation drops with the roles reversed. There, the class was honest and the constant was hidden. Here the bound is honest and it is being quoted in place of something the implementation can compute exactly.

The smallest counter is the whole story

One number decides everything about this structure: the value of its smallest counter. It is the width of every bracket, it is the worst overshoot, it is what a newcomer inherits, and it is the threshold below which a key cannot be distinguished from noise.

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. 5 That number as the stream runs, beside a quantity from a different structure that a reader would expect to be unrelated. They are one line drawn twice, and the next essay is about why.

It grows roughly linearly in the stream on a stationary source, which means the structure’s uncertainty grows without limit as the stream runs, at a rate of about one per kk arrivals. That is not a defect — it is the price of a fixed-size structure over an unbounded stream, and Misra-Gries’s shortfall does exactly the same thing — but it has a consequence worth stating: the useful lifetime of a Space-Saving table is bounded by how large the smallest counter is allowed to get relative to the threshold being asked about.

At k=170k = 170 and a threshold of one per cent, the bracket reaches the threshold at about a million arrivals. Past that, every answer the structure gives about that threshold is cannot tell.

Where the eviction rule comes from

The rule — take the smallest slot, inherit its count — looks arbitrary and is forced.

Suppose a new key took a slot and started from zero. Then the structure would be underestimating that key by however much it had already arrived before taking the slot, with no bound on the amount, and the guarantee that no count is below the truth would be gone. Suppose instead the new key evicted a random slot. Then a heavy key could be evicted while a light one survives, and the guarantee that every key above n/kn/k is in the table would be gone.

Inheriting the smallest count is the unique choice that keeps both. The evicted key’s count was the smallest, so it was at most n/kn/k; the new key’s true count is at least one; and the inherited value is an upper bound on what the new key might already have accumulated unnoticed, because anything arriving more often than the smallest counter would itself be in the table.

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. 6 The table with the arithmetic done. Column two is what Space-Saving reports, column three is the same numbers minus the smallest counter, and column four is a Misra-Gries summary with one fewer counter built from the same stream. The third and fourth are identical on every row.

What the third number is for

There is a use for the bracket that neither of the other structures can offer, and it is the reason to pay the extra thirty-two bits.

A query can come back with a decision rather than an estimate. Asked whether a key is above a threshold ϕn\phi n, the structure answers yes when the lower bracket is above it, no when the counter is below it, and cannot tell in between. The third answer is the useful one: it is the only one of the three structures that can say so, and the width of the uncertain band is the smallest counter, which is a number the structure knows exactly at query time rather than a bound derived from nn and kk.

On the stream above with k=170k = 170, the smallest counter is 187 and the stream is sixty thousand, so at ϕ=0.01\phi = 0.01 — a threshold of six hundred — the uncertain band is a third of the threshold. At ϕ=0.005\phi = 0.005 it is more than half. A structure that reports “cannot tell” on a third of the range around the threshold is more useful than one that reports a number and lets the reader believe it, and that is the whole argument for the third column.

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. 7 The same brackets on a stream where a heavy key stops arriving halfway through. Its slot is not taken away — nothing in this structure expires either, and everything in the essay on windowed counting applies here unchanged — so it keeps an exact, enormous, and entirely historical count.

Two structures, one comparison, and a caution about it

The table of error bands above compares three structures at a fixed number of bits, and it is worth being explicit about what that comparison does and does not settle, because “at equal bits” is a phrase that can hide a lot.

The three structures hold different numbers of things at the same budget: 170 keys with brackets, 256 keys without, and 512 counter cells with no keys at all. Choosing bits as the common axis is a choice, and it is the right one for this collection — state in bits is the unit this field has used since it opened — but it is not the only reasonable one. Compared per counter, Count-Min looks much better and Space-Saving much worse; compared per hash evaluation, Count-Min does four per arrival and the other two do none.

And there is a subtler asymmetry. Space-Saving and Misra-Gries can only answer about keys they hold, so their error on a key that was evicted is the key’s whole count; Count-Min answers about any key at all. The bands above are computed over keys ranked by their true frequency, so the light bands include keys the counter-based structures have never heard of and Count-Min has an estimate for. That is why Count-Min’s error is flat and the others’ fall: the three are not answering the same question at the bottom of the distribution, and a table that did not say so would be comparing a structure that abstains with one that guesses.

The midpoint halves the worst case, for nothing

The reversal is presented above as a reason to keep both reporting rules and choose between them. There is a third rule that needs no choosing and is better than either in the one respect a bound cares about.

Report the midpoint of the bracket. The truth is somewhere inside, so the error of the midpoint is at most half the bracket’s width — and the error of either endpoint is, in the worst case, the whole width. So reporting the middle halves the worst-case error for every key simultaneously, at the cost of one subtraction and one shift at query time.

That is not a marginal improvement in the way a constant usually is, because of where the two endpoints are wrong. Space-Saving’s counter is exactly right at the top of the distribution and maximally wrong at the bottom; Misra-Gries’s lower end is the reverse. The midpoint is never exactly right and never maximally wrong, which for a structure whose output is going to be read by something that does not know which band a key is in is the safer position.

The reason it is not the standard rule is worth stating rather than glossing, because it is a real objection. Both endpoints are one-sided, and one-sidedness is a property some callers need: a filter that must never discard a genuine heavy hitter wants the upper end, and one that must never raise a false alarm wants the lower. The midpoint is two-sided and gives up both, so it is the right rule exactly when the output is an estimate to be read and the wrong one when the output is a decision to be acted on.

Which is the same division this collection keeps arriving at — an estimate and a guarantee are different products — and here, unusually, the structure holds enough to supply all three from one table.

Sizing for a decision rather than for a count

The threshold reading above says the structure answers yes, no or cannot tell, and that the uncertain band is as wide as the smallest counter. That turns sizing into arithmetic, and the arithmetic gives a number several times larger than the usual rule.

The smallest counter is about n/kn/k. To ask about a threshold of ϕn\phi n and have the uncertain band be a fraction β\beta of it:

nk    βϕnk    1βϕ\frac{n}{k} \;\le\; \beta \phi n \qquad\Longrightarrow\qquad k \;\ge\; \frac{1}{\beta\phi}

The familiar rule of thumb is k1/ϕk \approx 1/\phi — ninety-nine counters to catch everything above one per cent — and that is the value at which the uncertain band is as wide as the threshold itself, which is to say the structure can never answer anything but cannot tell near the boundary. To get the band down to a tenth of the threshold takes ten times as many counters: a thousand, not a hundred.

So the standard sizing rule sizes for the guarantee and not for the answer, and the two differ by whatever resolution somebody actually wants at the threshold. That is the same relationship this collection found between a sketch’s width and the question it is being asked — what fraction of the stream is the smallest thing worth being right about — and the resolution is the parameter neither structure’s usual account contains.

It also gives the honest lifetime figure. The band grows at about one per kk arrivals, so a table sized for a resolution β\beta at threshold ϕ\phi delivers it until the stream reaches whatever length made n/kn/k equal to βϕn\beta\phi n — which is to say never expires, because both sides scale with nn. The lifetime worry raised above applies to an absolute threshold, not a proportional one, and the two cases want different answers: a proportional threshold is stable forever and an absolute one has a shelf life.

What is not measured here

Ordering. Space-Saving is usually recommended for top-kk rather than for point queries, and the quantity that matters there is whether the reported ordering is right rather than whether the counts are. The measurements above are all point errors. The ordering on this stream is correct in the top twenty and it would be a different essay to establish where it stops being.

The stream shapes that break it. On a uniform stream there are no heavy hitters, every arrival evicts somebody, and the whole table churns — the brackets are then the full width of the smallest counter on every key, and the structure reports kk arbitrary keys with equal confidence. That case is measured in the essay that introduced Misra-Gries and it applies here identically, because the two structures are the same one.

The same state, read twice: subtract 2,036 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,036Misra-Gries, k = 15the truth16,1694,1334,1336,16974,3792,3432,3433,20022,9739379372,97332,037111,87142,037111,30862,0371193482,03711641142,03711343cash register · 40,000 arrivals · one key floods a stretchthe shift is the smallest Space-Saving counter, 2,0361,536 bits against 960
Fig. 8 The conversion again on a stream with a burst in it and a smaller table, so that the shift is a larger share of every count. The identity does not depend on the stream, and running it on four different streams is how that was established rather than assumed.

A merge. Two Space-Saving tables can be combined, and the rule is the same subtraction the next essay is about, applied twice. It is not measured here, and the reason is that the merged structure’s bracket is the sum of the two smallest counters rather than either of them — which makes merging strictly worse than building over the concatenation, exactly as it is for Misra-Gries and exactly as it is not for Count-Min.

Deletions. Every structure in this family assumes each update adds. Under a model where counts can decrease, as the turnstile essay measures, the eviction rule has no meaning — there is no smallest counter to inherit from when counters can go down — and none of the guarantees above survives.

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.

Count-MINGuaranteeHeavy hitterMeasurementMisra–GriesOne-sided errorSpace-savingState bitsSummaryTight boundTrade offZipf