How Misra-Gries tracks heavy hitters

Short answer: The Misra-Gries summary keeps k − 1 counters and finds every value whose frequency in a stream of length n is greater than n/k. A value that is already counted increments its counter. A value that finds a free counter takes it. When every counter is busy, each counter decrements, and a counter that hits zero becomes free. A second pass counts the surviving candidates exactly. The first pass can keep a value that is not heavy. It cannot drop a value that is. Memory is O(k).


What the problem actually asks

A strict majority is the case k = 2, and the Boyer-Moore vote solves it with one counter. The generalization asks for every value that occupies more than a 1/k fraction of the stream, without storing the stream. At most k − 1 values can clear that bar, because (k) copies of a 1/k fraction would already exceed n. That numeric fact is why k − 1 counters are enough to retain every heavy hitter, even though the algorithm does not know the frequencies yet.

Decrementing every counter at once is a batch of deletions. Each such batch deletes k occurrences, one per counter. Across the whole stream the number of deleted occurrences of a true heavy hitter is too small to empty its counter for good. A light value has no such promise and can survive anyway if it happens to be written into a counter near the end.


What the algorithm guarantees

After one pass, every value with frequency greater than n/k is among the at most k − 1 monitored values. The counter stored for a value is at most its true frequency and at least its true frequency minus n/k. A second pass that counts the survivors against the original stream, or against a stored copy, deletes the false survivors whose real frequency is at most n/k. Time is O(n) for the first pass with a hash map from value to counter, and O(n) for the second pass. Extra memory is O(k).

The algorithm is deterministic. A count-min sketch estimates frequencies with a probabilistic error and can track a different set of questions, including values below the 1/k line, with a different failure story. Misra-Gries does not estimate the frequency of a value it dropped.


The working steps

  1. Choose k. Allocate k − 1 counters, initially free.
  2. For each arriving value, increment its counter if it is monitored. Otherwise assign it to a free counter with count 1 if one exists. Otherwise decrement every counter and free those that reach 0.
  3. At the end of the stream the monitored values include every heavy hitter, plus zero or more light values.
  4. Count each survivor in a second pass. Keep a survivor whose count is greater than n/k.
  5. The k = 2 case is a single counter and is the Boyer-Moore majority vote.

The guarantee is one-sided until the second pass. A count-min sketch is a probabilistic frequency estimate. The Boyer-Moore vote is this algorithm at k = 2. Misra-Gries is the k − 1 counter summary.


Approaches that look simpler and fail

Approach Why it fails when the first-pass candidates are emitted as the answer
Skip the second pass A light value can occupy a counter at the end. The first pass promises only that heavy hitters are present, not that every present value is heavy.
Decrement a single counter instead of every counter The deletion argument counted one removal against each of k slots. Decrementing one slot lets a heavy hitter be paired off against itself and dropped.
Use fewer than k − 1 counters There can be k − 1 values each above n/k. Fewer counters have no room for all of them.
Report the raw counter as the exact frequency The counter is the true frequency minus the deletions, and the deficit can be as large as n/k. The second pass is an exact count.

One majority candidate is the Boyer-Moore vote. A probabilistic frequency table is a count-min sketch. Misra-Gries is the deterministic heavy-hitter summary.


A working example

This Python fragment keeps k−1 = 2 counters, so every value that occurs more than n/3 times survives. The second pass drops a light survivor. In this stream the only heavy hitter is a.

stream = list("aabaabaca")
k = 3
counts = {}
for item in stream:
    if item in counts:
        counts[item] += 1
    elif len(counts) < k - 1:
        counts[item] = 1
    else:
        for key in list(counts):
            counts[key] -= 1
            if counts[key] == 0:
                del counts[key]
n = len(stream)
heavy = [item for item in counts if stream.count(item) > n / k]
assert heavy == ["a"]
print(*heavy)

Where it is used

Misra-Gries tracks heavy hitters in a stream with a fixed number of counters, which is the memory budget of a network monitor. The Space-Saving algorithm is the practical relative. A count-min sketch estimates every frequency, not only the heavy ones.


When you can drop this algorithm

Drop the summary when every frequency, including rare values, must be estimated. Use a sketch or a full table. Keep Misra-Gries when the stream is long, memory holds k counters, and only values above n/k matter.


Why this stays published

We publish this so a search for Misra-Gries returns the k − 1 counters, the simultaneous decrement, and the verifying second pass. Misra-Gries keeps k − 1 counters so that every value occurring more than n/k times survives the first pass, and a second pass confirms the true frequencies.

本文由 HoHo 與 AI 協作整理,最後更新於 2026 年 11 月 13 日。

Leave a Reply

Your email address will not be published. Required fields are marked *