Problem Statement
Suppose that we have an array with arrayLength distinct elements. A quite common task in programming is to randomly permute this array. Novices who encounter this situation often implement the following algorithm:
- Choose a positive integer swapCount.
- swapCount times randomly choose two distinct indices and swap the corresponding elements.
This method of permuting an array is bad, because some permutations of the array will be more likely than others. In this problem, you shall compute how bad this method is for a given situation.
You will be given four
Definition
- Class:
- RandomSwaps
- Method:
- getProbability
- Parameters:
- int, int, int, int
- Returns:
- double
- Method signature:
- double getProbability(int arrayLength, int swapCount, int a, int b)
- (be sure your method is public)
Notes
- The indices of elements that are going to be swapped are generated with a uniform probability distribution, i.e., each pair of indices has got the same probability of being chosen.
- The indices are zero-based, i.e., the array contains elements with indices 0 to arrayLength-1, inclusive.
- The returned value must have an absolute or relative error less than 1e-9.
Constraints
- arrayLength will be between 2 and 1,000, inclusive.
- swapCount will be between 1 and 100,000, inclusive.
- a and b will be between 0 and arrayLength-1, inclusive.
Examples
5
1
0
0
Returns: 0.6
There are ten possible pairs of indices to swap: (0,1), (0,2), (0,3), (0,4), (1,2), (1,3), (1,4), (2,3), (2,4), and (3,4). Out of these ten, the last six leave the element 0 untouched. Thus the probability is 6/10.
5
1
0
3
Returns: 0.1
Only the swap (0,3) will move the element from position 0 to position 3. The probability of choosing this swap is 1/10.
5
2
0
0
Returns: 0.4
Now there are two possibilities: Either the 0-th element stays in its place for the whole time, or it is swapped away and back again. The probability of the first possibility is (6/10)^2, for the second possibility it is (4/10)*(1/10).
100
500
3
3
Returns: 0.010036635745123007
For 100 elements, even after 500 swaps, the permutation won't be random enough.
2
1
0
0
Returns: 0.0
2
1
0
1
Returns: 1.0
2
1
1
0
Returns: 1.0
2
1
1
1
Returns: 0.0
1000
100000
1
3
Returns: 0.0010
1000
10000
47
997
Returns: 9.999999980198375E-4
996
8976
23
23
Returns: 0.001004030387635356
979
9432
11
11
Returns: 0.0010214545726463526
1000
10000
134
134
Returns: 0.0010000019781824516
2
100000
0
0
Returns: 1.0
2
100000
0
1
Returns: 0.0
2
98765
0
1
Returns: 1.0
3
333
2
2
Returns: 0.3333333333333333
3
1
1
2
Returns: 0.3333333333333333
3
1
2
2
Returns: 0.3333333333333333
3
2
1
2
Returns: 0.3333333333333333
3
2
2
2
Returns: 0.3333333333333333
3
3
1
2
Returns: 0.3333333333333333
3
3
2
2
Returns: 0.3333333333333333
3
4
0
1
Returns: 0.3333333333333333
3
4
1
1
Returns: 0.3333333333333333
3
5
0
2
Returns: 0.3333333333333333
3
5
0
0
Returns: 0.3333333333333333
1000
47
47
47
Returns: 0.9102011623170692
1000
213
24
254
Returns: 3.474410834317587E-4
1000
999
999
999
Returns: 0.13592918707313337
1000
4700
0
999
Returns: 9.999188199335141E-4
976
7547
362
557
Returns: 0.0010245899732474594
764
5366
424
424
Returns: 0.0013096640581993224
13
98765
1
1
Returns: 0.07692307692307693
987
7654
23
534
Returns: 0.0010131710455277341
746
3522
533
234
Returns: 0.001340378947277188
436
3256
333
424
Returns: 0.002293577283284885
1000
1
34
34
Returns: 0.998
1000
1
44
676
Returns: 2.002002002002002E-6
1000
9
21
45
Returns: 1.7874401585442643E-5
976
32
23
656
Returns: 6.515999944043824E-5
997
9800
46
355
Returns: 0.0010030090242864138
986
9765
53
53
Returns: 0.0010142011810771873
943
9965
535
542
Returns: 0.0010604453863906687
857
47433
465
532
Returns: 0.0011668611435239206
1000
100000
1
2
Returns: 0.0010
1000
100000
0
0
Returns: 0.0010
1000
100000
3
4
Returns: 0.0010
1000
100000
12
13
Returns: 0.0010
1000
100000
2
3
Returns: 0.0010
1000
100000
3
9
Returns: 0.0010
1000
100000
0
997
Returns: 0.0010
1000
100000
0
1
Returns: 0.0010
2
100000
0
1
Returns: 0.0
1000
100000
2
2
Returns: 0.0010
1000
100000
500
500
Returns: 0.0010
100
100000
1
2
Returns: 0.01
1000
65535
23
45
Returns: 0.0010
1000
100000
100
100
Returns: 0.0010
1000
100000
200
500
Returns: 0.0010
999
99999
42
666
Returns: 0.001001001001001001
999
99999
9
99
Returns: 0.001001001001001001
2
1
0
1
Returns: 1.0
1000
2000
1
5
Returns: 9.818306173489915E-4
1000
100000
5
6
Returns: 0.0010
1000
100000
1
998
Returns: 0.0010