Problem
Given a stream of elements each from a finite domain , the goal of the heavy hitter problem is to find the elements that appear at least times in . Note that there can be at most such elements.
The Misra-Gries Summary
If false positive is allowed, we have a deterministic algorithm that has space overhead.
L = {}
for x in S:
if x in L:
L[x] = L[x] + 1
elif len(L) < k:
L[x] = 1
else:
decrement the value of every elements in L by 1
delete elements in L with value = 0
To see the correctness of the algorithm, we give each element in a token. There are tokens in total. If a element is not added to the list , token are throw away. If an elements appears more than times, it must appears in when the algorithm terminates.
Another way to prove this is that, each time an element is thrown away, it is not in the counter and the sum of counter decremented is . Let be the frequency of an element . Then the maximum number of times is thrown away is bounded by , which implies that .
Count-Min Sketch
An alternate approach is randomized algorithm. In high level, we would like to maintain a set of bins and assign each element in uniformly at random one of the bins. Then we put each number in into the bins according which bin it is assigned to. In expectation, each bin will receive elements. To estimate the number of time an element appears in , we just return the number of elements of the bin is assigned. Note that we might overestimate 's frequency, whose expectation is at most . We might repeat this several time to reduce variance.
The tools to achieve the idea are hash functions . Initially, we have list (denoted as ) where each list has size . Then we go through elements in . For each element , we increment the by 1 for all . Finally for an element , we use the minimum value as its frequency estimation.
By Markov inequality,
Therefore,
which decreases exponentially with respect to the size of .
If we would like the failure probability to be , we need to set .
In conclusion, we can return the estimation of frequency of elements, such that , , with probability at least and space overhead .
Reference
[1] MIT 6.854/18.415: Advanced Algorithms, Spring 2016, Lecture 4