book-sectionChapter 7

Exercise 7.1

All algorithms run in Θ(N)\Theta(N) time without trying to reconstruct the associated permutation.

  • left-to-right maxima: Traverse the inversion table from left to right and register indices where qk=0q_k=0.

  • right-to-left minima: Record the index of the last entry in the list of left-to-right minima (see the book); this is the index of an absolute minimum. Store this index and set the counter variable c=1c=1. Traverse the inversion table from this index from left to right and seek for an element where kcqk=1k-c-q_k=1. Prepend kk to the current list, increase the counter by one, and repeat the process until reaching the last element. Prepend the index NN, since the last element from the right is the first seen minima.

  • right-to-left maxima: Record the index of the last entry in the list of left-to-right maxima; this is the index of an absolute maximum (see the first item in this list). Store this index mm and set the counter variable c=1c=1. Traverse the inversion table from m+1m+1 to N1N-1 and store the maximal index of every encountered value of qkq_k along the way. We need this lookup table in the next stage. Repeat the following 3 steps until c=qNc=q_N: Seek for an index kk where qk=cq_k=c using our lookup table. If there is a match and kk is greater than the last recorded index, then prepend kk to the current list. Increase cc by one. Prepend the index NN, since the last element from the right is the first seen maxima.

Exercise 7.2

Let mm be the number of cycles in the permutation and denote by lkl_k the length of the kkth cycle. The number of ways to write the sample permutation in cycle notation is m!k=1mlkm! \prod_{k=1}^m l_k.

Exercise 7.3

There are (2NN)((N1)!)2/2\binom{2N}{N}((N-1)!)^2/2 permutations of 2N2N elements having exactly two cycles, each of length NN.

There are (2NN)N!/2N=(2N1)!!\binom{2N}{N}N!/2^N=(2N-1)!! permutations of 2N2N elements having exactly NN cycles, each of length 2. Notice that we must divide by 2N2^N, since for a given representation all other possible ways of "flipping" elements in those 2 cycles are duplicates.

Exercise 7.4

We must maximize the product from Exercise 2 subject to the constraint k=1mlk=N\sum_{k=1}^m l_k = N. The formula depends on the number of cycles and the product of their lengths. If we increase mm then we reduce the lengths and vice versa. Nonetheless, the contribution of mm is more important, as it grows factorially.

Let’s assume that we start with an identity permutation with m=Nm=N. This results in N!N! equivalent representations. Can we maximize this number? If we set only one cycle to length 2 and keep the others at 1, then we get (N1)!2<N!(N-1)!2<N!. It turns out that we cannot attain a better result.

Therefore, the identity permutation has the greatest number of different representations with cycles.

Exercise 7.5

The Python 3 program below implements the task in Θ(N2)\Theta(N^2) time.

It outputs [18, 2, 40, 86, 4, 41, 6, 2, 14, 7, 2, 5, 2, 1, 1].

triangle-exclamation

Exercise 7.6

Last updated