Compute derangements. A permutation p of the numbers {1, 2, . . . , n} is called a…
- User
Compute derangements. A permutation p of the numbers {1, 2, . . . , n} is called a derangement if p[i] ̸= i, for all i. For ex- ample, p = [2, 3, 1] is a derangement but q = [2, 1, 3] isn’t since q[3] = 3. (Note: Here permutations are arrays indexed as p[1...n] and q[1...n].) (a) GiveanO(n)expected-timealgorithmtocomputeauniformrandomderangementof{1,2,...,n}. Do not give code. Hint: The number of derangements of the set {1, 2, . . . , n} is the integer closest to n! , where e e = 2.718... is Euler’s constant. (b) Prove your algorithm is correct. (c) Prove your algorithm has expected run-time O(n).
- Assistant
- User
try different way
- Assistant