Imagine you are watching a billion website requests go by, one per millisecond. You cannot store them all — there is no buffer big enough. But you need to know: which items appear more than 1% of the time? Those are the heavy hitters, and missing even one could mean missing a denial-of-service attack or the next viral trend.
In 1982, Jayadev Misra and David Gries published a deceptively simple answer. Using only k counters — a data structure that fits in a few kilobytes — their algorithm reads the stream once, front to back, and guarantees that every item appearing more than times is still tracked at the end. Nothing that matters slips through.
The trick is a single, brutal operation: whenever the counters fill up, subtract 1 from all of them and discard any that hit zero. It looks destructive. But the math shows that this subtraction can only happen at most times, and any true heavy hitter is too frequent to be eliminated entirely.
This is streaming algorithm design at its best: a worst-case guarantee achieved with constant working memory, in a single pass, with no randomness needed.
Comments
Loading comments...