Possible Duplicate:
How would you pick a uniform random element in linked list with unknown length?
Suppose we want to randomized choose an element from linked list,
but we don’t know the length of the linked list.
Design an algorithm which takes as least as possible running time to randomized choose the element.
There’s a simple algorithm with
O(N), get the length, then pick an element.But you can do better, with an online algorithm, accessing every element only once:
You select the first element as the answer, then on the
kth element you replace your answer with that element with a probability of1/k. The fact that this method is not biased can be proved with mathematical induction.The a generalized version (pick k elements), is the reservoir sampling.