Re:Vitter's reservoir sampling algorithm D: randomly selecti...
by ridiculous_fish on Monday April 04, 2016 @06:11AM
(#11420637)
Nice save! FWIW, here's the fix I wrote up before I saw yours. Yours is much better ;)
We only need to consider values in the range 0 < k <= N/2. If k > N/2, we "pick the losers instead of winners" and that reduces k to that range.
The expected time E to pick k out of N, is N times the last k terms of H(N):
E(k) = N * (H(N) - H(N-k))
Applying the asymptote: E(k) ~ N * (γ + ln(N) - γ - ln(N-k))
= N * (ln(N) - ln(N-k))
We wish to determine its behavior as a function of k. Taking the derivative: dE/dk ~ N/(N - k)
which is positive and bounded above by 2 for 0 < k <= N/2: dE/dk <= 2
Using the fact that E(1) = 1, we have: E(k) <= 2k
So E cannot be worse than linear in k.
For the cases where k > n/2, the "full table scan" portion is of course also linear in k.