Problem

Given a stream of elements S={x1,x2,...,xn}S = \{ x_1, x_2, ..., x_n \} each from a finite domain UU, the goal of the heavy hitter problem is to find the elements that appear at least n/(k+1)+1n / (k + 1) + 1 times in SS. Note that there can be at most kk such elements.

The Misra-Gries Summary

If false positive is allowed, we have a deterministic algorithm that has O(k)O(k) 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 SS a token. There are nn tokens in total. If a element is not added to the list LL, k+1k + 1 token are throw away. If an elements appears more than n/(k+1)n / (k + 1) times, it must appears in LL 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 kk. Let fxf_x be the frequency of an element xx. Then the maximum number of times xx is thrown away is bounded by kfx≤n−fxk f_x \le n - f_x, which implies that fx≤nk+1f_x \le \frac{n}{k + 1}.

Count-Min Sketch

An alternate approach is randomized algorithm. In high level, we would like to maintain a set of mm bins and assign each element in UU uniformly at random one of the bins. Then we put each number in SS into the mm bins according which bin it is assigned to. In expectation, each bin will receive nm\frac{n}{m} elements. To estimate the number of time an element xx appears in SS, we just return the number of elements of the bin xx is assigned. Note that we might overestimate xx's frequency, whose expectation is at most nm\frac{n}{m}. We might repeat this several time to reduce variance.

The tools to achieve the idea are tt hash functions h1,h2,...,ht:S→[m]h_1, h_2, ..., h_t : S \rightarrow [m]. Initially, we have ll list (denoted as L1,L2,..,LtL_1, L_2, .., L_t) where each list has size mm. Then we go through elements in SS. For each element xx, we increment the Li[hi(x)]L_i[h_i(x)] by 1 for all i∈[t]i \in [t]. Finally for an element xx, we use the minimum value min⁡i{Li[hi(x)]}\min_i \{ L_i[h_i(x)] \} as its frequency estimation.

E[Li[hi(x)]]=fx+∑y∈S,y≠xE[hi(y)=hi(x)]=fx+(n−fx)m≤fx+nm \begin{aligned} \mathbb{E}[L_i[h_i(x)]] &= f_x + \sum_{y \in S, y \neq x} \mathbb{E} [h_i (y) = h_i (x) ]\\ &= f_x + \frac{(n - f_x)}{m} \\ &\le f_x + \frac{n}{m} \end{aligned}

By Markov inequality,

Pr⁡[Li[hi(x)]−fx>2nm]≤E[Li[hi(x)]−fx]2nm≤nm2nm≤12. \Pr [ L_i[h_i(x)] - f_x > 2 \frac{n}{m}] \le \frac{\mathbb{E}[L_i[h_i(x)] - f_x]}{ 2 \frac{n}{m} } \le \frac{ \frac{n}{m} }{ 2 \frac{n}{m} } \le \frac{1}{2}.

Therefore,

Pr⁡[min⁡i{Li[hi(x)]}−fx>2nm]≤12t, \Pr [ \min_i \{ L_i[h_i(x)] \} - f_x > 2 \frac{n}{m}] \le \frac{1}{2^t},

which decreases exponentially with respect to the size of tt.

If we would like the failure probability to be δ\delta, we need to set t=O(log⁡1δ)t = O(\log \frac{1}{\delta}).

In conclusion, we can return the estimation of frequency of elements, such that ∀x∈U\forall x \in U, fx~≤fx+2nm\tilde{f_x} \le f_x + 2\frac{n}{m}, with probability at least 1−δ1 - \delta and space overhead O(mlog⁡1δ)O(m \log \frac{1}{\delta}).

Reference

[1] MIT 6.854/18.415: Advanced Algorithms, Spring 2016, Lecture 4