Count-Min Sketch
A d × w grid of counters that estimates how many times an item has appeared in a stream, using sub-linear memory relative to the number of distinct items.
How it works
The sketch is a grid with d rows and w columns. Each row has its own independent hash function mapping an item to one of the w columns. add(item) increments one cell per row: d increments total.
estimate(item) reads the same d cells and returns the minimum value across rows. Why the minimum? Hash collisions can only ever make a counter too high (by adding some other item's count into the same cell), never too low. So across d independent rows, the smallest observed value is the one least corrupted by collisions, giving an estimate that's always ≥ the true count, with bounded expected error.
Width w controls the error magnitude (ε ≈ e/w); depth d controls the confidence that the error bound holds (failure probability δ ≈ e-d). Add the same item many times below and compare the sketch's estimate to the true count tracked alongside it.
Interactive demo
API surface
| Operation | Description | Complexity |
|---|---|---|
| new CountMinSketch(w, d) | Allocate a d × w counter grid. | O(w × d) space |
| add(item, amount?): void | Increment one cell per row (d cells total). | O(d) time |
| estimate(item): number | Return the minimum across the d cells for item. | O(d) time |
| widthForEpsilon(ε): number | Width needed for error bound ε (ceil(e/ε)). | O(1) |
| depthForDelta(δ): number | Depth needed for failure probability δ (ceil(ln(1/δ))). | O(1) |
References
Original papers
- An Improved Data Stream Summary: The Count-Min Sketch and its ApplicationsGraham Cormode, S. Muthukrishnan · 2005
- Finding Frequent Items in Data Streams (Count-Sketch)Moses Charikar, Kevin Chen, Martin Farach-Colton · 2002
Library implementations
Real-world use cases
- Redis CMS.INITBYDIM / CMS.INCRBY / CMS.QUERY for approximate item-frequency counting
- Apache DataSketches and Spark frequency-estimation utilities
- Network traffic "heavy hitter" flow detection; DB query-optimizer cardinality estimation