Sub topic 10
Algorithm L: Skipping Ahead with Geometric Jumps
This chapter assumes you know Algorithm R, can follow its correctness argument, and are comfortable with logarithms and the geometric distribution. In the cost analysis you saw that Algorithm R calls the random number generator once for every item after the first k. Algorithm L, published by Kim-Hung Li in 1994, produces the same uniform sample with far fewer random calls.
Why Algorithm R Wastes Work
Algorithm R accepts item i (0-based) with probability k/(i+1). Late in a long stream that probability is tiny. Suppose k = 100 and you are a million items in. You draw a random number per item, and about 9,999 of every 10,000 draws reject the item. Almost all of those draws do nothing.
Algorithm L asks a different question. Instead of "should I keep this item?", it asks "how many items will I reject before the next acceptance?" It answers that with a single random draw and then jumps straight to the next accepted item.
The Working Model
Picture every item getting an independent uniform key in (0, 1), with the reservoir holding the k items with the smallest keys. Let W be the largest key currently in the reservoir. A new item gets in only if its key is below W, which happens with probability W.
Algorithm L gets two facts from this picture:
- Skip length. Each later item enters with probability W, independently of the others, so the number of rejected items before the next acceptance is geometric. If U is uniform in (0, 1), then
floor(log U / log(1 − W))has exactly that distribution. - Updating W. The new item's key is uniform below W. After it replaces the old maximum, the k keys in the reservoir are k uniforms on (0, W). Their maximum is distributed as
W · U^(1/k). The starting value of W is the maximum of k uniforms on (0, 1), which isU^(1/k).
The algorithm never stores the keys. It only tracks W. Which slot gets overwritten is chosen uniformly, because by symmetry every reservoir member is equally likely to hold the maximum key.
Implementation
import math
import random
from itertools import islice
_END = object()
def _open_unit(rng):
u = 0.0
while u == 0.0:
u = rng.random() # value in (0, 1)
return u
def algorithm_l(stream, k, rng=random):
it = iter(stream)
reservoir = list(islice(it, k))
if len(reservoir) < k:
return reservoir
w = math.exp(math.log(_open_unit(rng)) / k)
while True:
skip = math.floor(math.log(_open_unit(rng)) / math.log(1 - w))
nxt = next(islice(it, skip, skip + 1), _END)
if nxt is _END:
return reservoir
reservoir[rng.randrange(k)] = nxt
w *= math.exp(math.log(_open_unit(rng)) / k)
if __name__ == "__main__":
random.seed(1)
print(algorithm_l(range(1_000_000), 5))_open_unit rejects 0.0 because log(0) raises an error. random.random() returns values in [0, 1), so 0.0 can occur, though rarely. islice(it, skip, skip + 1) consumes the skipped items and returns the one after them. It returns _END if the stream runs out first.
Cost Compared with Algorithm R
Each acceptance uses three random numbers: one for the skip, one for the slot, and one for the W update. The expected number of acceptances is about k(1 + ln(n/k)).
| Property | Algorithm R | Algorithm L |
|---|---|---|
| Random numbers | n − k | O(k(1 + log(n/k))) expected |
| Memory | O(k) | O(k) |
| Items touched | n | n for an iterator; fewer if the source can seek |
| Output distribution | Uniform | Uniform |
With n = 10⁹ and k = 100, Algorithm R draws about a billion random numbers. Algorithm L draws roughly 3 × 100 × 17 ≈ 5,000.
Exercise
Partway through a stream, W = 0.01. What is the expected number of items Algorithm L skips before the next replacement, and how many random numbers does Algorithm R spend over the same stretch?
Show answer
The skip is geometric with success probability 0.01, so the expected number of rejected items is (1 − W)/W = 99. Algorithm L uses one draw for that skip, plus two more at the acceptance. Algorithm R spends one draw per item, about 100 draws, to reach the same acceptance.
Exercise
Why can Algorithm L overwrite a uniformly random reservoir slot instead of the slot holding the largest key?
Show answer
The algorithm never stores keys, and the k keys are exchangeable. Every reservoir member is equally likely to hold the current maximum key, so picking a slot uniformly at random gives the same distribution over reservoir contents.