Imagine you work at a news aggregator. Every second, thousands of articles arrive — each one stamped with a popularity score. Your boss wants a random sample of 10 articles to display, but articles with higher scores should appear more often than obscure ones. The catch: you have no disk space to store the whole stream, and you must always have a ready answer.
This is the weighted reservoir sampling problem. It generalises the classic (uniform) reservoir sampling algorithm — where each item has an equal chance of making the sample — to the case where items carry weights that bias the draw.
The elegant solution, algorithm A-Res (short for Algorithm with a Reservoir), was published by Pavlos Efraimidis and Paul Spirakis in 2006. It processes each item exactly once, keeps a fixed-size reservoir of candidates, and guarantees that after seeing any prefix of the stream each item's probability of being in the reservoir equals its weight divided by the total weight seen so far.
No second pass. No random-access memory of old items. Just a single priority queue that shrinks naturally as stronger candidates arrive.
The idea connects deeply to randomized algorithms and to the kind of one-pass streaming that makes modern large-scale analytics possible.
Comments
Loading comments...