Equal probability always - PrismoSkills



Equal probability always - PrismoSkills

Puzzle: Given a stream of infinite numbers, write an algorithm such that
the probability of choosing a random number at any given time is 1/N, where
N is the count of numbers received so far.

Constraint: Since the stream of numbers is infinite, it is not possible to
store all the numbers either in memory or in secondary storage.


Example: If 1 million numbers have been received by 1 pm, then every number (chosen randomly) should have a probability of being chosen as 1/million
If at 2 pm, 10 million numbers have been received, then every number should have a probability of being chosen as 1/(10 million).


Solution: Since it is not possible to store all the numbers, we should think of storing just one number.
If we save just the last number always, then probability of choosing any other number is 0.
If we save just the first number (or number at N/2 position), then also the situation is no different.

Probability of choosing any number is 1/N if choose a random number between 0 and N-1 and
pick the stream number corresponding to that index. But that would require to store all the numbers.

Another try: We choose a random number between 0 and N and save the current
number (i.e. the Nth number) if that random number equals N.

With this algorithm, the probability of Nth number being choses in clearly 1/N.
Let us check the probability of being chosen for other numbers.

Probability that (N-1)th number was chosen is 1/(N-1).
For (N-1)th number to remain being-chosen when Nth number arrives, the
random number should not equal N and the probability for that is (N-1)/N.
Thus total probability for (N-1)th number to remain chosen when Nth number arrives is (1 / (N-1)) * ((N-1) / N) = 1/N

This can be extended to N-2, N-3 and so on for all the numbers to make sure that every
number has a probability of being chosen as 1/N


Extension to selection of 'k' numbers with k/N probability


If the requirement is to select 'k' numbers instead of just 1 number, then also
the approach remains mostly the same. Analysis of the probabilities changes a little bit though.

If 'k' numbers are to be selected, then required probability is k/N.
To do this, initially we select the first k numbers and put them in an array 'chosenSet'.
For every number that is received in the stream after these 'k' numbers, a
random number is generated between 0 and N.
If this random number is between 0 and k, then chosenSet[k] is replaced
with the current number (i.e. Nth number).

Analysis of probability: For Nth number, the probability of being placed into 'chosenSet' is clearly k/N.
For the numbers chosen in the first slot of 'k' numbers, the probability of remaining in the final selection
(i.e. when the Nth number arrives) is the probability of not being selected when (k+1)th number
arrived, and then not being selected when (k+2)th number arrived and so on.

This probability is:
    (k / (k+1)) * ((k+1) / (k+2)) * ... ((N-1) / (N))
    which is equal to (k / N)

So, with the above algorithm, 'k' elements can be selected from a stream of infinite numbers with a probability of k/N



Note: This algorithm for selection of k random elements from a set of N is also called Reservoir Sampling
It can be useful for situations like the following:
    Given an iterator that can only move forwards, implement a function that will return 'k' random elements

Read full article from Equal probability always - PrismoSkills


No comments:

Post a Comment

Labels

Algorithm (219) Lucene (130) LeetCode (97) Database (36) Data Structure (33) text mining (28) Solr (27) java (27) Mathematical Algorithm (26) Difficult Algorithm (25) Logic Thinking (23) Puzzles (23) Bit Algorithms (22) Math (21) List (20) Dynamic Programming (19) Linux (19) Tree (18) Machine Learning (15) EPI (11) Queue (11) Smart Algorithm (11) Operating System (9) Java Basic (8) Recursive Algorithm (8) Stack (8) Eclipse (7) Scala (7) Tika (7) J2EE (6) Monitoring (6) Trie (6) Concurrency (5) Geometry Algorithm (5) Greedy Algorithm (5) Mahout (5) MySQL (5) xpost (5) C (4) Interview (4) Vi (4) regular expression (4) to-do (4) C++ (3) Chrome (3) Divide and Conquer (3) Graph Algorithm (3) Permutation (3) Powershell (3) Random (3) Segment Tree (3) UIMA (3) Union-Find (3) Video (3) Virtualization (3) Windows (3) XML (3) Advanced Data Structure (2) Android (2) Bash (2) Classic Algorithm (2) Debugging (2) Design Pattern (2) Google (2) Hadoop (2) Java Collections (2) Markov Chains (2) Probabilities (2) Shell (2) Site (2) Web Development (2) Workplace (2) angularjs (2) .Net (1) Amazon Interview (1) Android Studio (1) Array (1) Boilerpipe (1) Book Notes (1) ChromeOS (1) Chromebook (1) Codility (1) Desgin (1) Design (1) Divide and Conqure (1) GAE (1) Google Interview (1) Great Stuff (1) Hash (1) High Tech Companies (1) Improving (1) LifeTips (1) Maven (1) Network (1) Performance (1) Programming (1) Resources (1) Sampling (1) Sed (1) Smart Thinking (1) Sort (1) Spark (1) Stanford NLP (1) System Design (1) Trove (1) VIP (1) tools (1)

Popular Posts