#📝 Note: Count-Min Sketch

#Overview

Count-Min Sketch (CMS) is a probabilistic data structure that estimates the frequency of items in a stream using a fixed-size 2D array of counters. It trades a small, controllable overcount error for dramatically reduced memory — O(1/ε × log(1/δ)) space vs O(N) for exact counting.

#Core Idea

#Parameter Selection

Width  w = ceil(e / ε)    → controls error magnitude (e ≈ 2.718)
Depth  d = ceil(ln(1/δ))  → controls error probability

Example: ε=0.001, δ=0.01
  w = ceil(2.718 / 0.001) = 2718 counters per row
  d = ceil(ln(100))       = 5 rows
  Total: 5 × 2718 = 13,590 integers ≈ 54 KB (32-bit counters)
  → Estimates frequency within 0.1% of total events, 99% of the time

#Comparison: Exact vs Probabilistic Counting

Aspect HashMap (Exact) Count-Min Sketch
Memory O(unique items) — could be GBs O(1/ε × log 1/δ) — often KBs
Error None ε × N overcount (no undercount)
Merge Trivial Trivially addable across workers
Distributed Hard (full map sync) Easy (add tables element-wise)
Deletions Supported Not supported (standard CMS)

#When to Use

#When NOT to Use

#Trade-offs

#Used In This Repo