Reservoir Sampling | GeeksforGeeks It can be solved in O(n) time. The solution also suits well for input...

Please Visit: http://ift.tt/1ajReyV



Reservoir Sampling | GeeksforGeeks

It can be solved in O(n) time. The solution also suits well for input in the form of stream. The idea is similar to this (Link->http://ift.tt/Uyxg2Z) post. Following are the steps.

1) Create an array reservoir[0..k-1] and copy first k items of stream[] to it.

2) Now one by one consider all items from (k+1)th item to nth item.

…a) Generate a random number from 0 to i where i is index of current item in stream[]. Let the generated random number is j.

…b) If j is in range 0 to k-1, replace reservoir[j] with arr[i]

http://ift.tt/MxNpkB

http://ift.tt/Uyxg35



Reservoir Sampling | GeeksforGeeks







from Public RSS-Feed of Jeffery yuan. Created with the PIXELMECHANICS 'GPlusRSS-Webtool' at http://gplusrss.com http://ift.tt/1roNpmw

via LifeLong Community

No comments:

Post a Comment