Count–Min Sketch
The Count–Min sketch is a sublinear probabilistic frequency-estimation data structure invented by Graham Cormode and S. Muthukrishnan in 2005 that uses a d × w counter array to bound point-query overestimation by ε‖a‖₁ with probability 1 − δ.
Count–Min Sketch (CM Sketch) is a sublinear-space probabilistic streaming data structure and randomized algorithm in computation and algorithms that summarizes a high-velocity stream of frequency updates and answers point frequency queries, range queries, inner-product queries, and heavy-hitter (frequent-item) queries in O(d) constant time and O(d · w) fixed space. Introduced by Graham Cormode and S. Muthukrishnan in 2005 (first presented at LATIN 2004 and published in Journal of Algorithms), the Count–Min sketch functions as a compact, general-purpose linear projection of an implicit frequency vector a ∈ ℝU over a massive key universe U.
Whereas a Bloom filter answers binary set-membership queries (ax > 0 vs. ax = 0) and HyperLogLog estimates the total number of distinct keys (‖a‖0), the Count–Min sketch estimates the multiplicity or cumulative weight ax of any individual item x ∈ U. By maintaining a two-dimensional d × w grid of integer counters indexed by d pairwise-independent hash functions, a Count–Min sketch with width w = ⌈e / ε⌉ and depth d = ⌈ln(1 / δ)⌉ uses only O((1 / ε) ln(1 / δ)) counters—independent of the universe cardinality |U|—to return an estimate âx satisfying the one-sided error bound ax ≤ âx ≤ ax + ε ‖a‖1 with probability at least 1 − δ.
Matrix Architecture, Update and Point-Query Operations
The d × w Counter Array and Pairwise-Independent Hashing
Consider a data stream of updates (xt, ct) targeting an implicit frequency vector a = [a1, a2, ..., a|U|] initialized to zero, where each arrival (xt, ct) increments coordinate axt ← axt + ct. In the standard cash-register (non-negative turnstile) model, all increments are non-negative (ct ≥ 0), so the ℓ1 norm of the stream is ‖a‖1 = ∑y∈U ay = N.
Given a target additive error factor ε ∈ (0, 1) and failure probability δ ∈ (0, 1), a Count–Min sketch allocates a two-dimensional array C[1 .. d][0 .. w − 1] of d rows and w columns initialized to 0, where:
Each row i ∈ {1, 2, ..., d} is associated with a hash function hi : U → {0, 1, ..., w − 1} drawn independently from a pairwise-independent (2-universal) hash family H (for example, hi(x) = ((αix + βi) mod p) mod w for a prime p > |U|), satisfying Pr[hi(x) = hi(y)] ≤ 1 / w for all x ≠ y:
- Update(x, c): For each row i = 1, 2, ..., d, add c to the hashed counter in row i:
- Point Query Estimate(x): Return the minimum counter value across all d rows:
Markov Inequality Proof of the (ε, δ) Error Bound
One-Sided Overestimation and Expected Collision Noise
In the non-negative stream model (ay ≥ 0 for all y ∈ U), every counter C[i][hi(x)] contains the true count ax plus the counts of all other elements y ≠ x that happened to collide with x in row i:
Because Xi,x ≥ 0 in every row i, taking the minimum across the d rows can never underestimate the true count (âx ≥ ax). By linearity of expectation and pairwise independence of hi (E[1[hi(x) = hi(y)]] ≤ 1 / w), the expected collision noise in any single row i is bounded by:
Exponential Tail Decay via d Independent Rows
Applying Markov's inequality to the non-negative random variable Xi,x in row i yields the probability that row i overestimates ax by more than ε ‖a‖1:
Because the d hash functions h1, ..., hd are mutually independent, the minimum estimate âx = mini (ax + Xi,x) exceeds ax + ε ‖a‖1 if and only if every single one of the d rows simultaneously suffers a collision error exceeding ε ‖a‖1:
For example, choosing w = 2,719 (ε = 0.001 = 0.1% of total stream volume) and d = 5 rows (δ = e−5 ≈ 0.0067 = 99.33% confidence) requires just 5 × 2,719 = 13,595 counters (54.4 KB using 32-bit integers) regardless of whether the stream contains a million or a trillion distinct keys.
| Streaming Sketch / Structure | Query Supported | Error Guarantee (with Prob ≥ 1 − δ) | Space Complexity (Counters) | Update / Query Time | Primary Comparison vs. Count–Min Sketch |
|---|---|---|---|---|---|
| Count–Min Sketch (2005) | Point frequency ax, inner product, heavy hitters | One-sided: ax ≤ âx ≤ ax + ε‖a‖1 | O( (1/ε) ln(1/δ) ) | O(ln(1/δ)) | Linear dependence on 1/ε; simple min estimator |
| Conservative Update CM-CU (Estan & Varghese, 2002) | Non-negative point frequency ax | ax ≤ âx ≤ ax + ε‖a‖1 (~2×–5× lower empirical error) | O( (1/ε) ln(1/δ) ) | O(ln(1/δ)) | Increments only minimal counters; no deletions |
| Count Sketch (Charikar et al., 2002) | Point frequency ax (ℓ2 heavy tails) | Two-sided: |âx − *ax*| ≤ ε‖a‖2 | O( (1/ε2) ln(1/δ) ) | O(ln(1/δ)) | Uses ±1 Rademacher signs & median; ℓ2 bound |
| Misra–Gries / Space-Saving | Top-k heavy hitters (deterministic) | Deterministic: ax − ‖a‖1/k ≤ âx ≤ ax | O(k) key-counter pairs | O(1) | Stores explicit keys; not a linear sketch |
| HyperLogLog (2007) | Distinct cardinality F0 = |{ x : ax > 0 }| | Relative error ±1.04 / √m | O(ε−2 log log N) bits | O(1) | Counts distinct items rather than per-item multiplicity |
Conservative Updates, Dyadic Range Queries, and Bioinformatics k-mer Counting
When deletions (ct < 0) are not required, Count–Min with Conservative Update (CM-CU) (adapted from Estan & Varghese's Multistage Filters, 2002) reduces empirical overestimation error by a factor of 2× to 5× without adding a single bit of memory: to insert (x, c) with c > 0, first compute the current estimate âx = minj C[j][hj(x)], and then update each row i via:
In other words, a counter that already exceeds âx + c due to prior collisions from other keys is not unnecessarily incremented further.
Because the standard Count–Min sketch is a linear sketch (C(αa + βb) = αC(a) + βC(b)), sketches computed on distributed shards or network switches can be added or subtracted elementwise, and inner products a · b are estimated via mini ∑j=0..w−1 *Ca*[i][j] *Cb*[i][j]. In Caffeine (the high-performance Java caching library powering Cassandra, Finagle, and Neo4j), a 4-bit TinyLFU Count–Min sketch tracks access frequencies of millions of cache keys in 8 bytes per cached entry to achieve near-optimal admission control. In bioinformatics, the khmer library (Zhang et al., 2014) uses Count–Min sketches to count DNA k-mer abundances across terabase-scale metagenomic sequencing reads in fixed RAM alongside cuckoo hashing and locality-sensitive hashing indexes.
Frequently Asked Questions
How does the Count–Min sketch differ from the Count Sketch (Charikar, Chen, & Farach-Colton, 2002)?
In the Count–Min sketch, every row increments C[i][hi(x)] by +c and queries take the minimum, giving a one-sided error O(ε ‖a‖1) using O(1/ε) columns. In the Count Sketch, each row multiplies c by a second pairwise-independent random sign si(x) ∈ {−1, +1} so collision noise cancels in expectation (E[si(x)*si*(y)] = 0), and queries take the median across rows, achieving a two-sided ℓ2 error bound O(ε ‖a‖2) at the cost of O(1/ε2) columns.
How can a Count–Min sketch answer 1D range queries a[l .. r] = ∑k=l..r ak in logarithmic time?
Summing r − l + 1 individual point queries would accumulate(r − l + 1)ε ‖a‖1 error. Instead, Cormode and Muthukrishnan maintain log2 |U| parallel Count–Min sketches corresponding to the levels of a binary dyadic interval tree over [0, |U| − 1]. Each update increments O(log |U|) dyadic intervals containing x, and any range [l, r] decomposes into the disjoint union of at most 2 log2 |U| canonical dyadic intervals, bounding total range error by 2(log2 |U|)ε ‖a‖1.
What happens to the query procedure when negative updates (deletions ct < 0) are allowed in the strict turnstile model?
In the strict turnstile model, individual updates ct may be positive or negative (representing insertions and deletions), provided the net cumulative frequency of every item remains non-negative (ay ≥ 0 at query time). Because ay ≥ 0 for all y, the collision noise Xi,x = ∑y≠x ay 1[hi(x) = hi(y)] remains non-negative, so the exact same minimum estimator âx = mini C[i][hi(x)] continues to hold with one-sided error ≤ ε ‖a‖1 (whereas if net frequencies ay can become negative in the general turnstile model, the median across rows is used with width w = ⌈3 / ε⌉).
References
- Cormode, G., & Muthukrishnan, S. (2005). "An improved data stream summary: the count-min sketch and its applications." Journal of Algorithms, 55(1), 58–75. https://doi.org/10.1016/j.jalgor.2003.12.001
- Estan, C., & Varghese, G. (2002). "New directions in traffic measurement and accounting." ACM SIGCOMM Computer Communication Review, 32(4), 323–336. https://doi.org/10.1145/964725.633056
- Charikar, M., Chen, K., & Farach-Colton, M. (2002). "Finding frequent items in data streams." Proceedings of the 29th International Colloquium on Automata, Languages and Programming (ICALP 2002), LNCS Vol. 2380, 693–703. https://doi.org/10.1007/3-540-45465-9_59
- Zhang, Q., Pell, J., Canino-Koning, R., Howe, A. C., & Brown, C. T. (2014). "These are not the k-mers you are looking for: efficient online k-mer counting using a probabilistic data structure." PLOS ONE, 9(7), e101271. https://doi.org/10.1371/journal.pone.0101271
- Einziger, G., Friedman, R., & Manes, B. (2017). "TinyLFU: A highly efficient cache admission policy." ACM Transactions on Storage, 13(4), 35:1–35:31. https://doi.org/10.1145/3149371