The Misra–Gries summary #
The Misra–Gries summary with k counters [MG82] reads a stream of items and keeps a
counter for at most k distinct items. On reading a, it increments the counter of a if a has
one or if fewer than k counters are in use, and otherwise decrements every counter, discarding the
counters that reach zero. A point query reports the counter of the queried item, or zero.
We store the counters as a multiset c : Multiset α with at most k distinct elements
(Cslib.Streaming.MisraGries.card_toFinset_state_le), the counter of a being c.count a. A
decrement removes one copy of each of the k stored items together with the arriving item, that is
k + 1 distinct items of the stream, so after a stream of m items there were at most
m / (k + 1) decrements. Hence the estimate f̂_a of the frequency f_a = count a s satisfies
f_a - m / (k + 1) ≤ f̂_a ≤ f_a (Cslib.Streaming.MisraGries.run_le_count,
Cslib.Streaming.MisraGries.count_sub_div_le_run), in the sharper form
(k + 1) (f_a - f̂_a) + m̂ ≤ m where m̂ is the sum of the counters
(Cslib.Streaming.MisraGries.mul_count_sub_run_add_card_le). In particular every item occurring
more than m / (k + 1) times keeps a counter
(Cslib.Streaming.MisraGries.mem_state_of_length_lt_mul_count): this is the deterministic
heavy-hitters guarantee. With k = 1 the single counter holds the majority item whenever there is
one, which solves the MAJORITY problem of [Chakrabarti2025, Section 1.1] up to a check of the
candidate (Cslib.Streaming.MisraGries.toFinset_state_one_eq).
Memory. On streams of at most m items over a universe of n items each counter is at most
m, so the summary is determined by k slots each holding nothing or an item with a counter, and
there are at most (n (m + 1) + 1) ^ k reachable summaries
(Cslib.Streaming.MisraGries.spaceLE_misraGries): O(k (log n + log m)) bits.
Chakrabarti's notes [Chakrabarti2025, Algorithm 1 and Theorem 1.3.1] use a parameter k with at
most k - 1 counters and error m / k; our misraGries k is their algorithm with parameter
k + 1.
Main definitions #
Cslib.Streaming.misraGries: the Misra–Gries summary withkcounters.
Main results #
Cslib.Streaming.MisraGries.run_le_count,Cslib.Streaming.MisraGries.mul_count_sub_run_add_card_le,Cslib.Streaming.MisraGries.count_sub_div_le_run: the estimate errs by at mostm / (k + 1), from below.Cslib.Streaming.MisraGries.mem_state_of_length_lt_mul_count: frequent items are kept.Cslib.Streaming.MisraGries.card_toFinset_state_le: at mostkcounters.Cslib.Streaming.MisraGries.spaceLE_misraGries: the memory bound.Cslib.Streaming.MisraGries.toFinset_state_one_eq: one counter finds the majority item.
References #
One step of the Misra–Gries summary with k counters, stored as a multiset whose multiplicities
are the counters: add the item a if it has a counter or fewer than k counters are in use, and
otherwise decrement every counter.
The Misra–Gries summary with k counters [MG82]: the memory is a multiset c
with at most k distinct elements, whose multiplicity c.count a is the counter of a, and a
point query a is answered with the counter of a.
The Misra–Gries summary of a stream is part of the stream.
The Misra–Gries summary uses at most k counters.
The Misra–Gries estimate never exceeds the frequency.
The Misra–Gries error bound [Chakrabarti2025, Theorem 1.3.1 and Exercise 1-1]: after a
stream of m items, the estimate f̂_a of the frequency f_a of a satisfies
(k + 1) (f_a - f̂_a) + m̂ ≤ m, where m̂ is the sum of the counters.
The Misra–Gries error bound [Chakrabarti2025, Theorem 1.3.1]: after a stream of m items
the estimate of the frequency f_a of a lies in [f_a - m / (k + 1), f_a].
Frequent items are kept [Chakrabarti2025, Section 1.4]: an item occurring more than
m / (k + 1) times in a stream of m items has a positive counter.
The majority vote [Chakrabarti2025, Section 1.1]: with one counter, an item occurring in more than half of the stream is the only item with a counter.
The memory of the Misra–Gries summary [Chakrabarti2025, Theorem 1.3.1]: over a universe
of n items, the summary with k counters uses at most b bits of memory on streams of at most
m items when (n (m + 1) + 1) ^ k ≤ 2 ^ b, that is O(k (log n + log m)) bits.