The k-minimum-values sketch for the number of distinct elements #
The number of distinct items of a stream is F₀ = #s.toFinset (Cslib.Streaming.freqMoment_zero).
Bar-Yossef, Jayram, Kumar, Sivakumar and Trevisan estimate it in small space
[BarYossefJayramKumarSivakumarTrevisan2002, Theorem 1]. Hash the items by a pairwise independent
family h θ : α → Fin M and keep the t smallest hash values seen. If d distinct items have been
read, the t-th smallest of their d hash values is near t M / d, so t M divided by it
estimates d.
The sketch. Each copy (Cslib.Streaming.kmvSketch) keeps the t smallest keys
(h θ a, a) (Cslib.Streaming.kmvKey) in lexicographic order, selected by Finset.lowest. Each
item is inserted into the selection, and the selection is cut back to t keys. Since selection can
be maintained online (Finset.lowest_insert_lowest), after the stream s the copy holds the t
lowest keys of the distinct items of s (Cslib.Streaming.state_kmvSketch). Ties between hash
values are broken by the item, so collisions never merge two distinct items. While it holds fewer
than t keys, a copy reports their number, which is then exactly F₀. Otherwise it reports
t M / (v + 1), where v is the largest hash value it keeps (Cslib.Streaming.kmvEstimate); hash
values are counted from 0, so v + 1 is the source's t-th smallest value in [1, M]. The
sketch answers the median of the estimates of its copies.
Accuracy. The t lowest hash values of d ≥ t items are all below K exactly when at least
t items hash below K (Finset.forall_mem_lowest_iff). The number of items hashing below K
has mean d K / M and variance at most its mean
(Cslib.Probability.IsPairwiseIndependentHash.dens_le_abs_card_filter_apply_mem_sub_le), and
Chebyshev's inequality bounds each side. For 0 < ε ≤ 1 and d ≤ M, one copy misses d by more
than ε d for at most a 2 / (t ε²) fraction of seeds
(Cslib.Streaming.dens_abs_kmvEstimate_sub_gt_le). The two tails contribute (1 + ε) / (t ε²) and
(1 - ε) / (t ε²); the source bounds each by 1 / 6 with t = ⌈96 / ε²⌉. For 6 ≤ t ε² one
copy thus fails with probability at most 1 / 3. The median trick
(Cslib.Probability.dens_abs_median_sub_gt_le, [Chakrabarti2025, Section 2.4]) brings the failure
probability of the median of 2 r copies down to (8 / 9) ^ r
(Cslib.Streaming.dens_kmvSketch_error_le), so kmv h t (2 r) solves F₀ estimation with
relative error ε and error probability δ ≥ (8 / 9) ^ r on streams of length at most M
(Cslib.Streaming.solves_kmv).
Memory. For fixed seeds, the reachable states are determined by the at most t items behind
each copy's keys. Their number is at most (n + 1) ^ t for a universe of n items. Under the
public-coin convention of Cslib.Streaming.Randomized, every sketch with c copies uses at most
b bits as soon as (n + 1) ^ (c t) ≤ 2 ^ b (Cslib.Streaming.spaceLE_kmv), on streams of any
length.
With the seeds stored as well, (|ι| (n + 1) ^ t) ^ c ≤ 2 ^ b suffices
(Cslib.Streaming.exists_injOn_iUnion_reachable_kmvSketch). With t = ⌈6 / ε²⌉ and c of order
log (1 / δ) this is O(ε⁻² log (1 / δ) log n) bits, the bound of
[BarYossefJayramKumarSivakumarTrevisan2002, Theorem 1] (the source writes m for the size of the
universe).
Differences from the source. The source keeps bare hash values in [1, m³] and pays a 1 / m
probability that h is not injective on the stream. Keeping the item beside the hash removes that
failure, and needs only d ≤ M rather than M = m³. The memory is still O(log n) bits per key,
since the key is determined by the item and the seed. The seeds are drawn as public coins and are
not charged by Cslib.Streaming.spaceLE_kmv.
This upper bound complements the Ω(n) lower bounds for computing F₀ exactly with bounded error
and for approximating it deterministically (Cslib.Streaming.le_of_solves_freqMoment,
Cslib.Streaming.Algorithm.le_of_approx_freqMoment with k = 0): randomness together with
approximation brings the memory down to O(ε⁻² log n) bits for constant error. The matching
Ω(ε⁻²) lower bound is not formalized.
Main definitions #
Cslib.Streaming.kmvKey,Cslib.Streaming.kmvEstimate: the sort key of an item and the estimate of one copy.Cslib.Streaming.kmvSketch: the k-minimum-values sketch with given seeds.Cslib.Streaming.kmv: the randomized sketch with uniform seeds.
Main results #
Cslib.Streaming.state_kmvSketch: each copy holds thetlowest keys of the distinct items.Cslib.Streaming.run_kmvSketch_of_card_lt: with fewer thantdistinct items the answer is exactlyF₀.Cslib.Streaming.dens_abs_kmvEstimate_sub_gt_le: one copy missesdby more thanε dwith probability at most2 / (t ε²).Cslib.Streaming.errorProb_kmv: the error of the randomized sketch is the fraction of bad seeds.Cslib.Streaming.dens_kmvSketch_error_le: the median of2 rcopies missesF₀by more thanε F₀with probability at most(8 / 9) ^ rwhen6 ≤ t ε².Cslib.Streaming.solves_kmv: the randomized sketch solvesF₀estimation.Cslib.Streaming.spaceLE_kmv: its memory under the public-coin convention.Cslib.Streaming.exists_injOn_iUnion_reachable_kmvSketch: its memory with the seeds stored.
References #
The sketch #
The k-minimum-values sketch [BarYossefJayramKumarSivakumarTrevisan2002, Theorem 1] with
seeds ω: each copy i keeps the t lowest keys kmvKey h (ω i) a of the items read, and the
answer is the median over the copies of their estimates Cslib.Streaming.kmvEstimate. The seeds
are written in the initial memory and read back by the update, which does not otherwise depend on
ω (Cslib.Streaming.update_kmvSketch).
The randomized k-minimum-values sketch: the sketch with independent uniform seeds.
The update of the sketch reads the seeds from the memory: it does not depend on ω.
The output of the sketch does not depend on its seeds.
From a memory holding the t lowest keys of the sets T i, reading s leads to the t
lowest keys of T i together with the keys of the items of s.
The state of the sketch: after the stream s, the copy with seed ω i holds the t lowest
keys of the distinct items of s.
The answer of the sketch is the median over the copies of the estimate from the t lowest keys
of the distinct items.
Distinct items have distinct keys, so a set of d items has d keys.
Exactness on few distinct items: on a stream with fewer than t distinct items, every copy
holds all their keys, so the sketch answers F₀ exactly.
Accuracy of one copy #
One copy estimates F₀ [BarYossefJayramKumarSivakumarTrevisan2002, proof of Theorem 1]:
for a pairwise independent family h θ : α → Fin M, a set D of d ≤ M items, 0 < ε ≤ 1 and
t > 0, the estimate from the t lowest keys of D misses d by more than ε d for at most a
2 / (t ε²) fraction of the seeds. The source proves 1 / 6 + 1 / 6 for t = ⌈96 / ε²⌉, plus a
1 / m term for hash collisions, which the tie-breaking keys avoid.
The error of the randomized sketch is the fraction of bad seeds.
Amplification #
The median of 2 r copies misses F₀ by more than ε F₀ for at most an (8 / 9) ^ r
fraction of seeds once 6 ≤ t ε² [BarYossefJayramKumarSivakumarTrevisan2002, Theorem 1]: one
copy fails for at most a third of the seeds, and the median trick
(Cslib.Probability.dens_abs_median_sub_gt_le) amplifies this.
The randomized sketch with 2 r copies solves F₀ estimation with relative error ε and
failure probability δ ≥ (8 / 9) ^ r on streams of length at most M
[BarYossefJayramKumarSivakumarTrevisan2002, Theorem 1].
Memory #
The memory of the randomized sketch [BarYossefJayramKumarSivakumarTrevisan2002,
Theorem 1]: over a universe of n items, every sketch it draws uses at most b bits of memory on
streams of any length m as soon as (n + 1) ^ (c t) ≤ 2 ^ b. Under the public-coin convention of
Cslib.Streaming.Randomized the seeds are not charged; each copy is charged for the at most t
items behind its keys. For the count with the seeds stored see
Cslib.Streaming.exists_injOn_iUnion_reachable_kmvSketch.
The memory of the sketch with its seeds stored [BarYossefJayramKumarSivakumarTrevisan2002,
Theorem 1]: over a universe of n items, the states reachable from all seeds together have a
single injective encoding by b bits as soon as (|ι| (n + 1) ^ t) ^ c ≤ 2 ^ b. Since only the
initial memory depends on the seeds (Cslib.Streaming.update_kmvSketch,
Cslib.Streaming.output_kmvSketch), this is the memory of one algorithm whose initial memory is
random, with the seeds charged.