Notes 29 · Full lecture · 75 minutes
CS 574 · University of Illinois, Urbana-Champaign · Fall 2026
First: the minimum of ideal random hash values. Then: bounded samples using pairwise-independent randomness.
| Part | Minutes |
|---|---|
| Model and target | 5 |
| Minimum estimator and its moments | 20 |
| Pairwise sampling at one scale | 20 |
| Amplifying a mixed output | 10 |
| Unknown scale and finite seeds | 15 |
| Guarantees and discussion | 5 |
\[ \sigma=(s_1,\ldots,s_m),\quad s_t\in[n],\qquad S=\{s_t:1\leq t\leq m\}. \] \[ u=|S|=F_0(\sigma). \] Repeated occurrences contribute only once to \(S\).
Given \(0<\varepsilon,\delta<1\), return \(\widehat u\) such that \[ \Pr[(1-\varepsilon)u\leq\widehat u\leq(1+\varepsilon)u]\geq1-\delta. \]
One pass; insertion-only stream; space measured in words. Return \(0\) on an empty stream. For \(n=1\), count exactly with a nonempty flag.
A hash function \(h\) assigns one random value to each key. Every occurrence of key \(i\) reuses \(h(i)\).
New randomness at every occurrence would reflect the stream length \(m\). The target here is the number \(u\) of distinct values.
Assume \(h:[n]\to[0,1]\) assigns independent uniform real values to distinct keys. Store \[ Y=\min_{1\leq t\leq m}h(s_t)=\min_{i\in S}h(i). \]
The repeated values disappear automatically under the minimum operation.
For \(u\geq1\), let \[ X_1,\ldots,X_u\overset{\mathrm{iid}}\sim\operatorname{Unif}[0,1], \qquad Y=\min(X_1,\ldots,X_u). \]
For \(0\leq t\leq1\), \[ \begin{aligned} \Pr[Y>t] &=\Pr[X_1>t,\ldots,X_u>t]\\ &=\prod_{i=1}^u\Pr[X_i>t]=(1-t)^u. \end{aligned} \]
Independence is essential to the product.
\[ F_Y(t)=\Pr[Y\leq t]=1-(1-t)^u. \] Differentiate on \((0,1)\): \[ f_Y(t)=u(1-t)^{u-1}. \]
A density is not a point probability. For \(0\leq a\leq b\leq1\), \(\Pr[a\leq Y\leq b]=\int_a^b f_Y(t)\,dt\).
Since \(0\leq Y\leq1\), \[ Y=\int_0^1\mathbf1[Y>t]\,dt. \] Taking expectations, \[ \mathbb E Y=\int_0^1\Pr[Y>t]\,dt =\int_0^1(1-t)^u\,dt=\frac1{u+1}. \]
\[ Y^2=\int_0^1 2t\,\mathbf1[Y>t]\,dt. \] \[ \begin{aligned} \mathbb E Y^2 &=2\int_0^1 t(1-t)^u\,dt\\ &=2\int_0^1(1-z)z^u\,dz\\ &=2\left(\frac1{u+1}-\frac1{u+2}\right) =\frac2{(u+1)(u+2)}. \end{aligned} \]
\[ \begin{aligned} \operatorname{Var}(Y) &=\frac2{(u+1)(u+2)}-\frac1{(u+1)^2}\\ &=\frac{u}{(u+1)^2(u+2)}. \end{aligned} \]
\[ \boxed{\frac{\operatorname{Var}(Y)}{(\mathbb E Y)^2} =\frac{u}{u+2}<1.} \]
\[ \mathbb E Y=\frac1{u+1},\qquad \mathbb E Y\neq u. \] First estimate \(\mu=1/(u+1)\) accurately; then invert and subtract \(1\).
The variance-to-mean-square ratio is bounded independently of \(u\).
For independent copies \(Y_1,\ldots,Y_\alpha\), set \[ A=\frac1\alpha\sum_jY_j. \] Then \[ \mathbb E A=\mu,\qquad \operatorname{Var}(A)\leq\frac{\mu^2}{\alpha}. \]
Set \(\eta=\varepsilon/4\) and \(\alpha=\lceil64/\varepsilon^2\rceil\). \[ \Pr[|A-\mu|>\eta\mu] \leq\frac1{\alpha\eta^2}\leq\frac14. \]
Each block needs \(O(\varepsilon^{-2})\) independent minimum sketches.
Build \(\beta=2\lceil12\ln(1/\delta)\rceil+1\) independent block averages. Let \(Z=\operatorname{median}(A_1,\ldots,A_\beta)\).
If \(G\) counts good blocks, then \(\mathbb E G\geq3\beta/4\) and \[ \Pr[|Z-\mu|>\eta\mu] \leq\Pr[G<\beta/2]\leq e^{-\beta/24}\leq\delta. \]
Choose independent ideal functions \(h_{i,j}\). On item \(s_t\), update \[ Y_{i,j}\gets\min\{Y_{i,j},h_{i,j}(s_t)\}. \] At the end, average within blocks and take the median across blocks.
\[ M=\alpha\beta=O\left(\varepsilon^{-2}\log\frac2\delta\right). \]
On the success event, write \[ Z=\frac{1+\theta}{u+1},\qquad |\theta|\leq\varepsilon/4. \] Output \(\widehat u=1/Z-1\). Then \[ \widehat u-u=-\frac{(u+1)\theta}{1+\theta}. \]
For \(u\geq1\) and \(0<\varepsilon<1\), \[ \begin{aligned} \frac{|\widehat u-u|}{u} &=\frac{u+1}{u}\frac{|\theta|}{|1+\theta|}\\ &\leq\frac{2|\theta|}{1-|\theta|} \leq\frac{2\varepsilon}{4-\varepsilon}<\varepsilon. \end{aligned} \]
Assuming perfectly random real-valued hashes and their stored values cost \(O(1)\) words, \[ \Pr[|\widehat u-u|>\varepsilon u]\leq\delta, \] using \(O(\varepsilon^{-2}\log(2/\delta))\) words in one pass.
The constant-space hash assumption is idealized. We next give an explicit finite-seed construction.
For each \(i\in[n]\), let \(B_i\in\{0,1\}\), with \[ \Pr[B_i=1]=p. \] Assume the bits are pairwise independent and reproducible from a small seed.
Retain the set \(R=\{i\in S:B_i=1\}\).
On arrival of \(s_t\), evaluate \(B_{s_t}\). If it is \(1\), insert \(s_t\) into a set \(R\); duplicates do nothing.
\[ X=|R|=\sum_{i\in S}B_i. \] By linearity, \[ \mathbb E X=\sum_{i\in S}\mathbb E B_i=pu. \]
The sum runs over distinct values, not stream positions.
For \(i\neq j\), \[ \operatorname{Cov}(B_i,B_j)=0. \] Therefore \[ \operatorname{Var}(X)=\sum_{i\in S}\operatorname{Var}(B_i) =up(1-p)\leq up. \]
For \(p>0\), let \(\rho=X/p\). Then \[ \mathbb E\rho=u,\qquad \operatorname{Var}(\rho)=\frac{u(1-p)}p. \]
\[ \Pr[|\rho-u|>\varepsilon u] \leq\frac{1-p}{\varepsilon^2pu} \leq\frac1{\varepsilon^2pu}. \]
Accurate sampling needs \(pu\) on the order of \(\varepsilon^{-2}\). But \(u\) is the unknown target.
First solve a promised problem: given a scale \(N\) with \(u>N/4\), return an estimate or a valid declaration that \(u>2N\).
Given \(0<\varepsilon<1/4\) and \(u>N/4\), output one of:
The output is correct with probability at least \(7/8\).
Both output types can be correct when \(u>2N\).
In the ideal sampling calculation, choose \[ p=\min\left\{1,\frac{32}{N\varepsilon^2}\right\}, \qquad T=pN. \] Keep distinct sampled keys until \(|R|>8T\).
The minimum with \(1\) is necessary at small scales.
The maximum stored size is \(\lfloor8T\rfloor+1=O(\varepsilon^{-2})\).
If \(p=1\), then \(N\leq32/\varepsilon^2\) and \(R\) contains every distinct key seen.
No overflow: return \(|R|=u\) exactly.
Overflow: more than \(8N\) distinct keys appeared, so \(u>8N>2N\).
Both outputs are correct deterministically.
Suppose \(p=32/(N\varepsilon^2)<1\) and \(u>N/4\). Then \[ pu>\frac{32}{N\varepsilon^2}\frac N4=\frac8{\varepsilon^2}. \]
\[ \Pr[|X-pu|>\varepsilon pu] \leq\frac{\operatorname{Var}(X)}{\varepsilon^2p^2u^2} \leq\frac1{\varepsilon^2pu}<\frac18. \]
Let \(X\) be the size of the final conceptual sample, even if storage stopped at overflow. Define \[ \mathcal G=\{|X-pu|\leq\varepsilon pu\}. \] Then \(\Pr[\mathcal G]\geq7/8\).
On \(\mathcal G\), prove correctness separately for a number and for large.
If there was no overflow, the stored set is the full conceptual sample. On \(\mathcal G\), \[ (1-\varepsilon)pu\leq X\leq(1+\varepsilon)pu. \] Divide by \(p\): \[ (1-\varepsilon)u\leq\rho\leq(1+\varepsilon)u. \]
Samples grow monotonically, so overflow implies \(X>8pN\). On \(\mathcal G\), \(X\leq(1+\varepsilon)pu\). Thus \[ u\geq\frac X{(1+\varepsilon)p} >\frac{8N}{1+\varepsilon}>2N. \]
This is an unconditional success proof. We do not condition the probability bound on observing overflow.
Under \(u>N/4\), the capped sampler uses \(O(\varepsilon^{-2})\) words and returns a correct output with probability at least \(7/8\).
Small scale: exact counting until overflow.
Larger scale: pairwise sampling, rescaling, and Chebyshev.
Run \(r\) independent copies, each with its own seed and bounded sampled set. Each output is either a number or large.
The amplification rule must handle both types of output.
For the proof, we will ensure fewer than \(r/4\) copies are incorrect.
A copy is bad if it gives a false declaration or an inaccurate number. \[ B=\sum_{j=1}^r\mathbf1[\text{copy }j\text{ is bad}], \qquad \Pr[\text{copy }j\text{ bad}]\leq1/8. \] The indicators are independent across copies.
Using Markov on \(2^B\), \[ \Pr[B\geq r/4]\leq2^{-r/4}\mathbb E[2^B] \leq\left(\frac{9/8}{2^{1/4}}\right)^r. \] Since \(\ln(9/8)-(\ln2)/4<-1/24\), \[ \Pr[B\geq r/4]\leq e^{-r/24}. \]
Suppose \(B<r/4\) and at least \(r/2\) copies say large. At least one of those declarations is correct. \[ \text{Correct declaration}\ \Longrightarrow\ u>2N. \] So the aggregate declaration is correct.
Otherwise, the number \(q\) of numerical outputs satisfies \(q>r/2\). At most \(B<r/4<q/2\) of them are bad.
More than half of the numerical outputs lie in \([(1-\varepsilon)u,(1+\varepsilon)u]\). Their median lies there too.
For a failure budget \(0<\eta<1\), choose \[ r=\lceil24\ln(2/\eta)\rceil. \] Then the aggregate output is correct with probability at least \(1-\eta\), using \[ O\left(\varepsilon^{-2}\log\frac2\eta\right)\text{ words}. \]
Choose a prime \(n\leq Q<2n\) for \(n\geq2\). Choose independent uniform \(a,b\in\mathbb F_Q\) and define \[ h(i)=a(i-1)+b\pmod Q. \]
Two field elements specify a pairwise-independent hash function.
For distinct keys \(i,j\) and prescribed outputs \(x,y\), \[ a(i-1)+b=x,\qquad a(j-1)+b=y\pmod Q. \] Subtracting gives a unique \(a\) because \(i-j\neq0\) in the field; then \(b\) is unique. \[ \Pr[h(i)=x,h(j)=y]=Q^{-2}. \]
\[ p_0=\min\{1,32/(N\varepsilon^2)\},\qquad K=\lceil Qp_0\rceil. \] \[ B_i=\mathbf1[h(i)<K],\qquad p'=K/Q\geq p_0. \]
Use the actual probability \(p'\) in both \(T=p'N\) and \(\rho=|R|/p'\).
If \(p_0<1\) and \(u>N/4\), \[ p'u\geq p_0u>8/\varepsilon^2. \] Therefore the same Chebyshev bound applies.
If \(p'=1\), the sample is exact. Its size is still capped, so the exact/overflow proof applies.
For \(p_0<1\), \[ T=p'N\leq\frac{32}{\varepsilon^2}+\frac NQ. \] At the dyadic scales used below, \(N<2n\) and \(Q\geq n\), so \[ T<\frac{32}{\varepsilon^2}+2. \]
The sample cap is \(O(\varepsilon^{-2})\); the seed and threshold use \(O(1)\) words.
For \(n\geq2\), define \[ L=\lceil\log_2 n\rceil,\qquad N_i=2^i\quad(1\leq i\leq L). \] Run an amplified sampler at every scale in parallel.
Give each level failure budget \(\eta=\delta/L\). Its space is \[ O\left(\varepsilon^{-2}\log\frac{2L}{\delta}\right). \]
Across all \(L\) levels: \[ O\left(\varepsilon^{-2}L\log\frac{2L}{\delta}\right)\text{ words}. \]
After the stream ends, inspect levels in increasing order. Skip large declarations and return the first number.
We must prove that a numerical output exists before leaving the range where the one-scale promise holds.
For \(u\geq1\), let \[ i_* = \min\{i:N_i\geq u\}. \] This exists since \(N_L\geq n\geq u\). \[ u\leq N_{i_*}\leq2u. \]
For every \(j\leq i_*\), \[ N_j\leq N_{i_*}\leq2u, \qquad N_j/4\leq u/2<u. \] Thus the one-scale guarantee applies to every level we need.
Levels beyond \(i_*\) need not satisfy the promise, and their outputs are irrelevant.
Each level through \(i_*\) is wrong with probability at most \(\delta/L\). \[ \Pr[\text{some level }j\leq i_*\text{ is wrong}] \leq i_*\delta/L\leq\delta. \]
Cross-level independence is unnecessary for this union bound. Independence is required among the repetitions within each level.
On the event that all levels through \(i_*\) are correct, level \(i_*\) cannot return large: \[ u\leq N_{i_*}\quad\Longrightarrow\quad u>2N_{i_*}\text{ is false}. \] It must return an accurate number. Any earlier number is also accurate.
The first numerical output occurs at some \(j\leq i_*\). On the simultaneous success event, \[ (1-\varepsilon)u\leq\widehat u\leq(1+\varepsilon)u. \] Hence \[ \Pr[|\widehat u-u|>\varepsilon u]\leq\delta. \]
For \(n\geq2\), \(0<\varepsilon<1/4\), and \(0<\delta<1\), a one-pass algorithm uses \[ O\left(\varepsilon^{-2}\log n\, \log\frac{2\lceil\log_2 n\rceil}{\delta}\right)\text{ words} \] and returns a \((1\pm\varepsilon)\) estimate of \(F_0\) with probability at least \(1-\delta\).
Use exact handling for the empty stream and for \(n=1\). For larger requested \(\varepsilon\), run with \(\min\{\varepsilon,1/8\}\).
Fix any constant \(a>0\) and take \(\delta=n^{-a}\). Then \[ L=O(\log n),\qquad \log(2L/\delta)=O(\log n). \]
\[ \boxed{O(\varepsilon^{-2}\log^2n)\text{ words}, \qquad\Pr[\text{success}]\geq1-n^{-a}.} \]
| Method | Randomness | Space |
|---|---|---|
| Minimum + average + median | Ideal fully random real hashes | \(O(\varepsilon^{-2}\log(2/\delta))\) ideal words |
| Capped sampling at dyadic scales | Finite pairwise hash seeds | \(O(\varepsilon^{-2}L\log(2L/\delta))\) words |
Both ignore repeated keys consistently; the second has an explicit small seed.
Based on Notes 29, Approximating the Number of Distinct Elements in a Stream.
Prerequisites: median estimation from Notes 28, Chebyshev’s inequality, Chernoff amplification, and polynomial hashing. Background cited in the notes: (Motwani & Raghavan, 1995).
CS 574 · Distinct counting