Imagine a router that sees a billion packets per second, or a search engine tallying every query typed by every user worldwide. You want to know: which items appear most often? Storing every item is impossible — the stream never ends and memory is finite.
This is the heavy-hitters problem: find every item whose frequency exceeds a fraction of the total stream length, using only a small, fixed amount of memory — far less than the number of distinct items in the stream.
The elegant answer is the Space-Saving algorithm (Metwally, Agrawal, and El Abbadi, 2005). It keeps exactly counters. When a new item arrives and already has a counter, that counter is incremented. When a new item has no counter, it evicts the counter with the smallest count and takes its slot — inheriting that count plus one. That tiny rule is enough to guarantee that every true heavy hitter is always tracked.
This article is about how a handful of counters can outrun a trillion-item flood — and why the algorithm's guarantees are surprisingly tight.
Comments
Loading comments...