Providing reservoir sampling algorithm and proof
I'm presenting the reservoir sampling algorithm to select k items uniformly from n, along with a proof that each item has probability k/n of being chosen.
Reservoir Sampling (k items)
Problem: You have a stream of items, one by one, total length n is unknown in advance. You want to maintain a “reservoir” of size k such that, when the stream ends, each of the n items has been selected with equal probability k/n.
Algorithm (One Pass, O(1) Memory)
- Initialize an empty array
Reservoir[1..k].
- Fill it with the first
k items of the stream.
- For each subsequent item number
i (1-indexed) in the stream, where i > k:
- Draw a uniform random integer
r in {1, 2, ..., i}.
- If
r ≤ k, replace Reservoir[r] with the new item; otherwise do nothing.
- At the end,
Reservoir is your sample.
This is the standard “Reservoir Sampling” algorithm.
Proof of Correctness
We want to show that, when the stream has length n, each item among the n has probability k/n of being in the final reservoir.
Intuition: For an item at position i, the chance it enters the reservoir is k/i; once in, for every later item position j > i, it survives replacement with probability (1 - k/j). Multiply these probabilities across all later steps to get exactly k/n.
Formal Inductive Proof:
We prove by induction on the stream length n that each item is present in the reservoir with probability k/n.
-
Base Cases:
- If
n < k, the reservoir simply holds all n items, so each item’s probability is 1 = k/n only when k=n; otherwise it’s trivial to see each item is in the reservoir with probability 1. We typically assume n ≥ k, else we can pad with fewer samples.
- If
n = k, the first k items are chosen and never replaced, so each is in the reservoir with probability 1 = k/n.
-
Inductive Step: Assume the statement holds for a stream of length n - 1. We now consider a stream of length n.
-
Item #n (the nth arrival): On arrival, we draw a random r uniformly in {1, 2, ..., n}. If r ≤ k, we put item n in. So probability of inclusion for item n is:
[
\Pr(\text{item n included}) = \frac{k}{n}.
]
-
Any prior item #i (i < n): By the inductive hypothesis, in a length n-1 stream, item #i survives in the reservoir with probability \frac{k}{n-1}. At step n, we might replace it with the new item #n. The probability that item #i is replaced at step n is:
[
\Pr(\text{replaced}) = \Pr(\text{choose item #i} \mid \text{pick some reservoir slot}) \times \Pr(\text{pick a reservoir slot})
= \frac{1}{k} \times \frac{k}{n} = \frac{1}{n}.
]
Therefore, probability it remains in the reservoir is:
[
\Pr(\text{item #i survives step n}) = \frac{k}{n - 1} \times \left(1 - \frac{1}{n}\right) = \frac{k}{n - 1} \times \frac{n - 1}{n} = \frac{k}{n}.
]
Hence each of the prior items also ends up with probability k/n of being in the reservoir after processing item #n.
This completes the inductive step.
Thus, by induction, for a stream of length n each item has probability k/n of being in the final reservoir.
Summary
- Algorithm: Keep the first
k items. For each item i > k, pick an integer r uniformly from 1..i. If r ≤ k, replace Reservoir[r] with the item.
- Correctness: Induction shows each item is kept with probability
k/n.