Problem Statement
N elements are going to appear on an input stream. The elements are all comparable, the comparisons are transitive, and the elements are distinct. Hence, the elements have a unique sorted order.
We would like to select the K smallest of these elements.
We are going to do this using a randomized algorithm. We will take K^2 buckets. Each time we process an element, we will assign it a bucket uniformly at random. If the bucket is empty, the element is placed into the bucket. If the bucket already contains another item, we compare the two and keep the smaller one. After processing the entire stream, we pool the items from all buckets and select the K smallest among them.
You do not know anything about the order in which the N items will appear in the input. In particular, it is not guaranteed that they will be in a random order.
Given N and K, calculate the worst-case probability of the above algorithm returning the correct answer.
(That is, imagine that for each possible order of the N items in the input stream we have calculated the probability of the algorithm giving the right answer. You should return the minimum of all those probabilities.)
Definition
- Class:
- ProbabilisticStreamMinimum
- Method:
- calculate
- Parameters:
- int, int
- Returns:
- double
- Method signature:
- double calculate(int N, int K)
- (be sure your method is public)
Notes
- As usual, assume that all random choices made by the algorithm are mutually independent.
- Your answer will be accepted if it has an absolute error not exceeding 1e-9.
Constraints
- N will be between 1 and 10^6, inclusive.
- K will be between 1 and min( N, 100 ), inclusive.
Examples
47
1
Returns: 1.0
The algorithm is guaranteed to work. The minimum element will end in one of the buckets.
3
3
Returns: 0.691358024691358
20
4
Returns: 0.66650390625
One possible execution of the algorithm starts as follows: An item of size 14576 arrives and it is placed into an empty bucket #13. An item of size 54245 arrives and it is placed into an empty bucket #7. An item of size 81234 arrives and again bucket #7 is chosen. As this item is bigger than the one already in bucket #7, we discard it. Bucket #7 still contains the item of size 54245. An item of size 9127 arrives and it is placed into an empty bucket #2. An item of size 23 arrives and again bucket #13 is chosen. As this item is smaller than the one in the bucket, we discard the item of size 14576 and instead we place the item of size 23 into bucket #13. ... followed by another 15 steps similar to these. The probability we seek is almost, but not exactly, equal to 2/3.
368939
2
Returns: 0.75
65566
4
Returns: 0.66650390625
451087
5
Returns: 0.65286144
580539
6
Returns: 0.6442583701671493
475715
7
Returns: 0.6383429887475824
598525
8
Returns: 0.6340278973402746
360163
9
Returns: 0.6307419125380342
269942
10
Returns: 0.6281565095552946
604958
13
Returns: 0.622907441456657
7157
20
Returns: 0.6169838376712793
7913
24
Returns: 0.6151929479534703
103225
27
Returns: 0.6142065611141312
350175
27
Returns: 0.6142065611141312
3323
28
Returns: 0.6139258512939367
573595
32
Returns: 0.6129820976571815
492646
33
Returns: 0.6127826277606554
876404
33
Returns: 0.6127826277606554
3052
38
Returns: 0.6119455085986218
5831
41
Returns: 0.6115428303592291
5889
41
Returns: 0.6115428303592291
826957
41
Returns: 0.6115428303592291
465529
45
Returns: 0.6110906788433947
1857
53
Returns: 0.6103936916833074
8385
57
Returns: 0.6101194214060622
963153
57
Returns: 0.6101194214060622
741
58
Returns: 0.6100568326946942
9947
59
Returns: 0.609996389585233
645523
61
Returns: 0.6098815134970482
4755
64
Returns: 0.6097228014700677
690684
64
Returns: 0.6097228014700677
241286
65
Returns: 0.6096731864509009
4335
72
Returns: 0.6093648275916982
9308
74
Returns: 0.609287536656543
7322
77
Returns: 0.6091791937118656
838533
77
Returns: 0.6091791937118656
592926
79
Returns: 0.6091115750589324
1670
80
Returns: 0.6090790441400206
9379
80
Returns: 0.6090790441400206
354857
80
Returns: 0.6090790441400206
330
82
Returns: 0.6090163819512175
1853
83
Returns: 0.6089861923957114
10555
83
Returns: 0.6089861923957114
358609
87
Returns: 0.6088724274387354
5248
88
Returns: 0.6088456144125352
119558
88
Returns: 0.6088456144125352
245982
90
Returns: 0.6087937891274775
537426
90
Returns: 0.6087937891274775
1194
94
Returns: 0.6086968014368741
914394
94
Returns: 0.6086968014368741
405434
100
Returns: 0.6085659649572782
1000000
100
Returns: 0.6085659649572782