Distinct Elements in Streams: An Algorithm for the (Text) Book

Sourav Chakraborty, N. V. Vinodchandran, Kuldeep S. Meel

TL;DR

Estimating the number of distinct elements in massive data streams typically requires sophisticated machinery like universal hashing and limited independence, making it difficult to teach to undergraduates. This paper presents a remarkably simple, sampling-based algorithm (often called the CVM algorithm) whose analysis relies solely on basic probability and standard Chernoff bounds. While slightly sub-optimal compared to theoretical space lower bounds, it achieves an $(\varepsilon, \delta)$-approximation using only basic coin flips and small memory.

What Problem Does This Paper Address?

Given a long data stream $D = \langle a_1, a_2, \ldots, a_m \rangle$ where each element comes from a universe $[n]$, how many distinct elements ($F_0$) did we observe?

In network monitoring, query optimization, and fraud detection, data streams are far too massive to store in memory or sort. Over forty years of theoretical research produced space-optimal algorithms for $(1 \pm \varepsilon)$-approximating $F_0$ with high probability ($1 - \delta$). However, existing state-of-the-art methods depend heavily on hash families with limited independence (e.g., pairwise or $k$-wise independent hash functions) and non-trivial sketching techniques. Consequently, teaching this foundational streaming problem in undergraduate or introductory graduate courses has remained an uphill battle.

The authors set out to find an algorithm simple enough to be presented in an undergraduate textbook right after introducing basic tail bounds, without sacrificing rigor.

Methods

The paper introduces $F_0$-Estimator, a dynamic sampling algorithm:

  1. State Tracking: The algorithm maintains a buffer $\mathcal{X}$ of sampled distinct elements and a sampling rate $p$ (initialized to $p = 1$). It sets a maximum capacity $\text{thresh} = \frac{12}{\varepsilon^2} \log\left(\frac{8m}{\delta}\right)$.
  2. Stream Processing: When an element $a_i$ arrives:
  3. It is first removed from $\mathcal{X}$ if it is already present.
  4. It is then re-added to $\mathcal{X}$ with probability $p$.
  5. Buffer Management (Subsampling): Whenever the buffer size $|\mathcal{X}|$ hits $\text{thresh}$:
  6. Every element in $\mathcal{X}$ is independently tossed out with probability $1/2$ (a fair coin flip).
  7. The sampling probability is halved: $p \leftarrow p / 2$.
  8. If by extreme misfortune no elements were tossed out, the algorithm aborts (fails).
  9. Final Estimate: After scanning the stream, the algorithm outputs $|\mathcal{X}| / p$.

Unlike standard distinct-element algorithms, there are no hash tables, no universal hashing, and no advanced pseudorandom generators—only Bernoulli coin flips.

Datasets

Not discussed in the paper. The paper is purely theoretical and does not perform empirical evaluations on benchmark datasets.

Key Insights and Ideas

  • Handling Duplicates Without Memory: In traditional streaming algorithms, a consistent hash function is used so repeated occurrences of an element always produce the same random decision. This algorithm solves the duplicate problem with an elegant trick: remove $a_i$ from $\mathcal{X}$ first, then re-insert it with probability $p$. This guarantees that whether an item appeared once or a million times, at the current step it exists in $\mathcal{X}$ with probability exactly $p$.
  • Maintaining the Invariant: At any step, every distinct element seen so far is in $\mathcal{X}$ independently with probability $p$. When $|\mathcal{X}|$ fills up, dropping each element with probability $1/2$ and setting $p \leftarrow p/2$ preserves this invariant precisely.
  • Elementary Proof of Correctness: Because sample inclusion decisions are genuinely independent, the accuracy proof boils down to a single application of Chernoff bounds, split neatly into:
  • Bounding the probability that $p$ decays too fast (via union bound over $m$ steps).
  • Bounding the error of $|\mathcal{X}|/p$ conditioned on $p$ being sufficiently large.

Key Results

  • Theoretical Guarantees: For any stream of length $m$ from universe $[n]$ and parameters $\varepsilon, \delta \in (0, 1)$, the algorithm returns an estimate $c$ satisfying: $$\Pr\big[(1 - \varepsilon) F_0 \le c \le (1 + \varepsilon) F_0\big] \ge 1 - \delta$$
  • Space Complexity: The algorithm stores at most $\text{thresh}$ elements, using: $$O\left(\frac{1}{\varepsilon^2} \log n \cdot (\log m + \log(1/\delta))\right) \text{ bits of memory.}$$
  • Simplicity: The pseudocode is only 10 lines long and the complete correctness proof spans less than two pages using standard first-principles probability.

Limitations and Future Work

  • Dependence on Stream Length $m$: The threshold and memory footprint depend logarithmically on the stream length $m$ ($\log m$). In contrast, optimal streaming algorithms achieve $O(\log n + \varepsilon^{-2} \log(1/\delta))$ bits, which is independent of $m$.
  • Known Stream Length Bound: The threshold requires knowing $m$ (or an upper bound on $m$) in advance.
  • Coin-Flip Cost: The algorithm requires generating physical random bits dynamically for every arrival and every subsampling phase, whereas hash-based algorithms generate random bits once to seed hash functions.

Summary updated

BibTeX
@inproceedings{chakraborty2022distinct,
  author = {Sourav Chakraborty and N. V. Vinodchandran and Kuldeep S. Meel},
  title = {{Distinct Elements in Streams: An Algorithm for the (Text) Book}},
  booktitle = {ESA},
  year = {2022},
  url = {https://openreview.net/forum?id=lfIafUZKfj},
}

Loading related content...

Figure