Documentation

CslibFrontier.Computability.Streaming.MisraGries

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 #

Main results #

References #

def Cslib.Streaming.MisraGries.step {α : Type u_1} [DecidableEq α] (k : ℕ) (c : Multiset α) (a : α) :

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.

def Cslib.Streaming.misraGries {α : Type u_1} [DecidableEq α] (k : ℕ) :
Algorithm (Multiset α) α (α → ℕ)

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.

theorem Cslib.Streaming.MisraGries.state_le {α : Type u_1} [DecidableEq α] {k : ℕ} (s : List α) :
(misraGries k).state s ≤ ↑s

The Misra–Gries summary of a stream is part of the stream.

The Misra–Gries summary uses at most k counters.

theorem Cslib.Streaming.MisraGries.run_le_count {α : Type u_1} [DecidableEq α] {k : ℕ} (s : List α) (a : α) :

The Misra–Gries estimate never exceeds the frequency.

theorem Cslib.Streaming.MisraGries.mul_count_sub_run_add_card_le {α : Type u_1} [DecidableEq α] {k : ℕ} (s : List α) (a : α) :
(k + 1) * (List.count a s - (misraGries k).run s a) + ((misraGries k).state s).card ≤ s.length

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.

theorem Cslib.Streaming.MisraGries.count_sub_div_le_run {α : Type u_1} [DecidableEq α] {k : ℕ} (s : List α) (a : α) :
↑(List.count a s) - ↑s.length / (↑k + 1) ≤ ↑((misraGries k).run s a)

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].

theorem Cslib.Streaming.MisraGries.mem_state_of_length_lt_mul_count {α : Type u_1} [DecidableEq α] {k : ℕ} {s : List α} {a : α} (h : s.length < (k + 1) * List.count a s) :

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.

theorem Cslib.Streaming.MisraGries.toFinset_state_one_eq {α : Type u_1} [DecidableEq α] {s : List α} {a : α} (h : s.length < 2 * List.count a s) :

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.

theorem Cslib.Streaming.MisraGries.spaceLE_misraGries {α : Type u_1} [DecidableEq α] {k : ℕ} [Fintype α] {m b : ℕ} (hb : (Fintype.card α * (m + 1) + 1) ^ k ≤ 2 ^ b) :

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.