random permutation algorithm and "Introduction to Algorithms" section 5.3 Exercise Solutions
The introduction of algorithms introduces two algorithms for randomly arranged arrays.
The first algorithm is to assign a random priority p[i] to each element of the array a[i], and then sort the elements in array a by priority. For example, if the initial array a= (1,2,3,4), the randomly selected priority p= (36,3,62,19), an array of b= (2,4,1,3) is generated, because the 2nd priority is the smallest, followed by the 4th, then 1th, and the last 3rd. We call this process a permute-by-sorting:
1 2 3 4 5 6 |
Permute-by-sorting (A) n = a.length let P[1..N] bes a new array for i = 1 to n p[i] = RANDOM (1,n^3) sort A, using P as sort Keys |
The 5th Row selects a random number between 1~n3. We use the scope 1~n3 to make all the priorities in p as unique as possible. We assume that all priority levels are unique.
The second algorithm is the original arrangement of the given array. The process randomize-in-place is completed in O (n) time. When I iterate, the element A[i] is randomly selected from element A[i] to a[n]. A[i] no longer changes after the first iteration.
1 2 3 4 |
Randomize-in-place (A) n = a.length for i = 1 to n swap a[i] with A[random (i,n)] |
Both of the above algorithms can calculate a uniform random permutation of array A, and the second algorithm is more beautiful.
Exercise 5.3 has several variants of the randomize-in-place algorithm, but the permutations they calculate are not uniformly random permutations of a, see below.
Professor 5.3-2 Kelp decided to write a process to randomly produce arbitrary permutations except for the identity permutation. He proposed the following process:
1 2 3 4 |
Permute-without-identity (A) n = a.length for i = 1 to n-1 swap a[i] with A[random (i+1,n)] |
Does this code implement the intentions of Professor Kelp?
No. Although this process does not produce an identity arrangement, it does not produce any permutations other than the identity arrangement.
There are n!-1 in any arrangement except for an identity arrangement, but this process can only produce (n-1) (n-2) ... 1 = (n-1)!, and (N!-1)-(n-1)! A permutation cannot be produced. For example: a= (A1,a2,..., an), A1 must and A2,a3,..., an exchange, assuming and AJ Exchange, then A1 can never go back to the first position, because when the loop to position J, AJ (the original A1) can only be exchanged to a (j+1),..., an. Any arrangement except the identity arrangement clearly contains the arrangement of the A1 at the first position.
Therefore, the process permute-without-identity cannot produce any arrangement other than the identity arrangement.
5.3-3 assumes that we are not adding elements a[i] to a sub-array a[i: N], instead of exchanging it with a random element at any position in the array:
1 2 3 4 |
Permute-with-all (A) n = a.length for i = 1 to n swap a[i] with A[random (1,n)] |
Does this code produce a uniform random arrangement? Why or why not.
This code does not produce a uniform random arrangement, using contradiction as follows.
Assuming this code produces a uniform random arrangement, and assuming n=3, the uniform random arrangement of three elements is 3!=6, and the probability of each permutation appearing is 1/6. But the process Permute-with-all altogether produces n3=27 (n=3,3 for loop, each random (1,3) has 3 possible) permutations, assuming that there is a total of M-XOR arrangement, and the arrangement is evenly random, then the probability of each heterogeneous arrangement is M/27, and satisfies M/27 =1/6. But M does not have integer solution, so the original hypothesis is not established, that is, the process permute-with-all can not produce a uniform random arrangement.
When actually n=3, the resulting permutations and probabilities are as follows: (−−−−)