Data Structures · Visualization
Count-Min Sketch: Introduction
Count-Min Sketch
A Count-Min sketch counts how often items appear using a tiny grid of counters instead of a giant dictionary. Time: O(d) per add/estimate — a few hashes. Space: O(d × w) counters, far below a dictionary.
Big-O: Add/estimate in O(d) hashes; tiny fixed memory.
Count-Min Sketch: Introduction
Count-Min SketchA Count-Min sketch counts how often items appear using a tiny grid of counters instead of a giant dictionary. Each item is hashed once per row and every one of those counters is bumped. To estimate, take the minimum across the rows — collisions only ever inflate counts, so the minimum is the closest to the truth.
How Count-Min Sketch: Introduction Works
- Fix a grid: d rows (hash functions) × w columns (counters).
- Add(x): for each row, counter[row][hash_row(x)] += 1.
- Estimate(x): return the smallest of its d counters.
- Width controls accuracy, depth controls confidence — both tiny.
Count-Min Sketch: Introduction Worked Example
Input: Grid 2 rows × 5 cols; add 'apple' twice, 'mango' once
- 'apple' hashes to (row0:1, row1:3) → bump both, twice.
- 'mango' hashes to (row0:1, row1:0) → shares row0/col1 with apple.
- Estimate 'apple': min(3, 2) = 2 ✓ exact.
- Estimate 'mango': min(3, 1) = 1 ✓ (row0 inflated, row1 saves it).
Output: apple ≈ 2, mango ≈ 1 — never an underestimate
Press ▶ Run to watch it step by step.
def add(sketch, x, w=1):
for r in range(D):
sketch[r][h(r, x)] += w # one cell per row
grid = [[0] * W for _ in range(D)] # tiny, fixed
# example: introCount-Min Sketch: Introduction Time & Space Complexity
When to Use Count-Min Sketch: Introduction
- Heavy-hitter detection in network traffic and ad clicks
- Stream frequency estimates where storing everything is impossible
Recommended resourcesAffiliate links · may earn us a commission
Recommended for learners
Ready for the next step?
See the same ideas in action — visualize the algorithms that operate on these structures, or take a guided course.