Random Permutation Generation
Given an array A of items, come up with an algorithm that produces a random permutation of A on every run
Last updated
Given an array A of items, come up with an algorithm that produces a random permutation of A on every run
Last updated
Our main objective is here is to generate permutations with good randomness. For an array with items, we need to ensure every one of the permutations will be producible by our algorithm with probability exactly .
Does the following algorithm work?
No. The above algorithm does not ensure that each permutation of A
has equal probability of being generated. This can be proved as follows:
For each iteration, there are possible outcomes of . So, there are a total of outcomes that can be generated (obviously many of them will give rise to the same permutation). But the crucial point to note is that it is (in general) not possible to divide each of the outcomes equally among the permutations. In particular, is not necessarily an integer (eg. ). So, there is no way that each permutation has equal chance of being chosen).
What about the algorithm below? (Note that once we mark an element as picked, we donāt select it again)
To show that an algorithm does not in fact generate a truly random permutation, it suffices to:
find a permutation that can never be generated
Assume that we use a truly random permutation algorithm for peer-checking of homework. That is, given an array of distinct student names A
, the algorithm generates a permutation of A
(say B
). Then, student A[i]
is told to grade student B[i]
's homework. If the size of the class is 600, what is the expected number of students who have to grade their own homework?
Answer: Only 1 !!! (Moreover, this does not depend on the size of the class)
The question is identical to asking: āAfter generating a permutation, what is the probability that a specific element remains in its exact spot?ā
It is interesting that for any student, the probability that they get to grade their own homework is inversely proportional to the total class size but the expected number of such students is independent of the class size.
By linearity (recall that this applies even though the variables are not independent!), we have:
and so, we get the same answer as before.
The above algorithm is in fact "random" in the sense that each of the permutations have equal chance of being generated. But there are some problems with this algorithm too:
Firstly, we are using an additional space.
More importantly, the probability of randomly selecting a previously picked item increases as we progress, leading to more and more time spent on re-picking the random index. As gets very large, the probability of having to keep re-picking a random index for the last slot approaches 100%. (Think about it this way, if and you are at the iteration. You have only 1 acceptable value of that has not been picked yet. But you donāt know what that is and you are trying to find it randomly (trial and error). So, the expected number of iterations to find the correct would be: , where is the number of times you need to call to find the correct (there is a 1/1000 chance that you get it in the first try, if you donāt get it, then you are at your initial stage again and you have to try again - you havenāt even eliminated anything lol)
Solving the above would give you . In other words, it would take you iterations to find the correct value of for the last slot. As gets larger, this expected number also increases!
It can be shown that each permutation has exactly probability of being generated.
Time: , Space:
show that the number of possible choices/decisions made by the algorithms is not a multiple of and so each permutation cannot be equally likely
Each element has probability of being assigned to any position. In particular, it has probability of being assigned to its original spot. (In other words, the probability that a student named John gets to grade his own homework is ā> which depends on the class size)
Alternatively, number of permutations of the remaining elements, if we keep one of them fixed in its place: . So, the probability that the element stays in its spot is .
Since there are students, the expected number of elements which are at their original spot =
Alternatively, we can solve it without thinking of permutations at all (and simply using linearity of expectation). Suppose we have students. Then, for any student , the probability that she has to grade her own homework is , and this probability is the same for any student.
Let be an indicator variable that is 1 iff student grades her own homework. Then, we want to calculate .