Chapter 11: Problem 21
Consider the following algorithm for generating a random permutation of the elements \(1,2, \ldots, n .\) In this algorithm, \(P(i)\) can be interpreted as the element in position \(i\) Step 1: \(\quad\) Set \(k=1\). Step 2: \(\quad\) Set \(P(1)=1\). Step 3: If \(k=n\), stop. Otherwise, let \(k=k+1\). Step 4: Generate a random number \(U\), and let $$ \begin{aligned} P(k) &=P([k U]+1), \\ P([k U]+1) &=k . \end{aligned} $$ Go to step 3 . (a) Explain in words what the algorithm is doing. (b) Show that at iteration \(k\) -that is, when the value of \(P(k)\) is initially set-that \(P(1), P(2), \ldots, P(k)\) is a random permutation of \(1,2, \ldots, k\). Hint: Use induction and argue that $$ \begin{aligned} &P_{k}\left\\{i_{1}, i_{2}, \ldots, i_{j-1}, k, i_{j}, \ldots, i_{k-2}, i\right\\} \\ &\quad=P_{k-1}\left\\{i_{1}, i_{2}, \ldots, i_{j-1}, i, i_{j}, \ldots, i_{k-2}\right\\} \frac{1}{k} \end{aligned} $$ \(=\frac{1}{k !}\) by the induction hypothesis The preceding algorithm can be used even if \(n\) is not initially known.