FMM22: Difference between revisions
From charlesreid1
(Created FMM22: The Cautious Alchemist's Vial (Reservoir Sampling / Algorithm R) (via create-page on MediaWiki MCP Server)) |
(Replaced em dashes with regular dashes (via update-page on MediaWiki MCP Server)) |
||
| Line 3: | Line 3: | ||
|problem= | |problem= | ||
An alchemist encounters a stream of vials produced one by one from a magical spring. He may carry exactly one vial home, and once he passes on a vial, it is gone forever. He has no idea how many vials the spring will produce in total | An alchemist encounters a stream of vials produced one by one from a magical spring. He may carry exactly one vial home, and once he passes on a vial, it is gone forever. He has no idea how many vials the spring will produce in total - the stream could stop at any moment. | ||
What strategy should the alchemist follow so that, no matter how many vials appear, '''every vial has an equal probability of being the one he carries home'''? | What strategy should the alchemist follow so that, no matter how many vials appear, '''every vial has an equal probability of being the one he carries home'''? | ||
(Source: Donald Knuth, ''The Art of Computer Programming, Volume 2: Seminumerical Algorithms'' | (Source: Donald Knuth, ''The Art of Computer Programming, Volume 2: Seminumerical Algorithms'' - Algorithm R, Reservoir Sampling) | ||
|solution= | |solution= | ||
| Line 42: | Line 42: | ||
</math> | </math> | ||
Since this holds for every <math>k</math>, all <math>N</math> vials have an equal probability <math>\tfrac{1}{N}</math> of being selected | Since this holds for every <math>k</math>, all <math>N</math> vials have an equal probability <math>\tfrac{1}{N}</math> of being selected - regardless of the unknown total <math>N</math>. | ||
=== Why this is remarkable === | === Why this is remarkable === | ||
Latest revision as of 17:42, 24 June 2026
Friday Morning Math Problem
The Cautious Alchemist's Vial
An alchemist encounters a stream of vials produced one by one from a magical spring. He may carry exactly one vial home, and once he passes on a vial, it is gone forever. He has no idea how many vials the spring will produce in total - the stream could stop at any moment.
What strategy should the alchemist follow so that, no matter how many vials appear, every vial has an equal probability of being the one he carries home?
(Source: Donald Knuth, The Art of Computer Programming, Volume 2: Seminumerical Algorithms - Algorithm R, Reservoir Sampling)
| Solution |
|---|
|
Let the total number of vials be $ N $ (unknown to the alchemist ahead of time). Label the vials $ 1, 2, \dots, N $ in the order they appear.
Strategy (Algorithm R for $ K=1 $)When the $ k $-th vial appears, the alchemist keeps it with probability $ \tfrac{1}{k} $, discarding whatever vial he was holding previously. Otherwise (with probability $ 1 - \tfrac{1}{k} $), he ignores it and keeps whatever he is already holding. Proof that every vial is equally likelyWe claim that each vial $ k $ (where $ 1 \leq k \leq N $) ends up as the final chosen vial with probability exactly $ \tfrac{1}{N} $. For vial $ k $ to be the final choice, two things must happen:
The probability of not keeping vial $ k+1 $ is $ 1 - \tfrac{1}{k+1} = \tfrac{k}{k+1} $. The probability of not keeping vial $ k+2 $ is $ \tfrac{k+1}{k+2} $, and so on. Thus, the probability that vial $ k $ is the final choice is: $ P(k) = \frac{1}{k} \cdot \frac{k}{k+1} \cdot \frac{k+1}{k+2} \cdot \dots \cdot \frac{N-1}{N} $ Every numerator cancels with the denominator of the following fraction (a telescoping product), leaving: $ P(k) = \frac{1}{N} $ Since this holds for every $ k $, all $ N $ vials have an equal probability $ \tfrac{1}{N} $ of being selected - regardless of the unknown total $ N $. Why this is remarkableThe alchemist achieves a perfectly uniform random sample from a stream of unknown length using only $ O(1) $ memory (a single vial slot). This is the $ K=1 $ case of Reservoir Sampling, which generalizes to selecting $ K $ items uniformly from a stream of unknown size. |
Flags
| Friday Morning Math Problems weekly math problems
Spider Socks and Shoes FMM1 Sums of Powers of 2 FMM2 Fifty Coins FMM3 The Zeta Monogram FMM4 The Cthulhus Monogram FMM4B Multiplication Logic FMM5 The Termite and the Cube FMM6 Sharing Dump Trucks FMM7 The Flippant Juror FMM8 Bus Routes FMM9 A Robust Bus System FMM9B Square-Free Sequence FMM10 Inferring Rule from Sequence FMM11 Checkerboard Color Schemes FMM12 One-Handed Chords FMM13 First Ace FMM14 Which Color Cab FMM15 Petersburg Paradox Revisited FMM16 A Binomial Challenge FMM17 A Radical Sum FMM18 Memorable Phone Numbers FMM19 Arrange in Order FMM20 A Pair of Dice Games: FMM21 The Cautious Alchemist's Vial FMM22
Category:Puzzles · Category:Math Flags · Template:FMMFlag · e |