The statistics of random permutations, such as the cycle structure of a random permutation are of fundamental importance in the analysis of algorithms, especially of sorting algorithms, which operate on random permutations. Suppose, for example, that we are using quickselect (a cousin of quicksort) to select a random element of a random permutation. Quickselect will perform a partial sort on the array, as it partitions the array according to the pivot. Hence a permutation will be less disordered after quickselect has been performed. The amount of disorder that remains may be analysed with generating functions. These generating functions depend in a fundamental way on the generating functions of random permutation statistics. Hence it is of vital importance to compute these generating functions.
Contents
- The fundamental relation
- Number of permutations that are involutions
- Number of permutations that are mth roots of unity
- Number of permutations of order exactly k
- Number of permutations that are derangements
- Derangements containing an even and an odd number of cycles
- One hundred prisoners
- A variation on the 100 prisoners problem keys and boxes
- Number of permutations containing m cycles
- Expected number of cycles of a given size m
- Moments of fixed points
- Expected number of fixed points in random permutation raised to some power k
- Expected number of cycles of any length of a random permutation
- Number of permutations with a cycle of length larger than n2
- Expected number of transpositions of a random permutation
- Expected cycle size of a random element
- Probability that a random element lies on a cycle of size m
- Probability that a random subset of n lies on the same cycle
- Number of permutations containing an even number of even cycles
- Permutations that are squares
- Odd cycle invariants
- Permutations where the sum of the lengths of the even cycles is six
- Permutations where all even cycles have the same length
- Permutations where the maximum length of an even cycle is four
- The recurrence
- A problem from the 2005 Putnam competition
- The difference between the number of cycles in even and odd permutations
- References
The article on random permutations contains an introduction to random permutations.
The fundamental relation
Permutations are sets of labelled cycles. Using the labelled case of the Flajolet–Sedgewick fundamental theorem and writing
Translating into exponential generating functions (EGFs), we have
where we have used the fact that the EGF of the set of permutations (there are n! permutations of n elements) is
This one equation will allow us to derive a surprising number of permutation statistics. Firstly, by dropping terms from
because there are k! / k labelled cycles.
This means that by dropping terms from this generating function, we may constrain the size of the cycles that occur in a permutation and obtain an EGF of the permutations containing only cycles of a given size.
Now instead of removing and selecting cycles, let's put different weights on different size cycles. If
the value of b for a permutation
This is a mixed generating function which is exponential in the permutation size and ordinary in the secondary parameter u. Differentiating and evaluating at u = 1, we have
i.e. the EGF of the sum of b over all permutations, or alternatively, the OGF, or more precisely, PGF (probability generating function) of the expectation of b.
This article uses the coefficient extraction operator [zn], documented on the page for formal power series.
Number of permutations that are involutions
An involution is a permutation σ so that σ2 = 1 under permutation composition. It follows that σ may only contain cycles of length one or two, i.e. the EGF g(z) of these permutations is
This gives the explicit formula for the total number
Dividing by n! yields the probability that a random permutation is an involution. These numbers are known as Telephone numbers.
Number of permutations that are mth roots of unity
This generalizes the concept of an involution. An mth root of unity is a permutation σ so that σm = 1 under permutation composition. Now every time we apply σ we move one step in parallel along all of its cycles. A cycle of length d applied d times produces the identity permutation on d elements (d fixed points) and d is the smallest value to do so. Hence m must be a multiple of all cycle sizes d, i.e. the only possible cycles are those whose length d is a divisor of m. It follows that the EGF g(x) of these permutations is
When m = p, where p is prime, this simplifies to
Number of permutations of order exactly k
This one can be done by Möbius inversion. Working with the same concept as in the previous entry we note that the combinatorial species
Translation to exponential generating functions we obtain the EGF of permutations whose order divides k, which is
Now we can use this generating function to count permutations of order exactly k. Let
It follows by Möbius inversion that
Therefore we have the EGF
The desired count is then given by
This formula produces e.g. for k = 6 the EGF
with the sequence of values starting at n = 5
which is OEIS A061121.
For k = 8 we get the EGF
with the sequence of values starting at n = 8
which is OEIS A061122.
Finally for k = 12 we get the EGF
with the sequence of values starting at n = 7
which is OEIS A061125.
Number of permutations that are derangements
Suppose there are n people at a party, each of whom brought an umbrella. At the end of the party everyone picks an umbrella out of the stack of umbrellas and leaves. What is the probability that no one left with his/her own umbrella? This problem is equivalent to counting permutations with no fixed points, and hence the EGF (subtract out fixed points by removing z) g(x) is
Now multiplication by
Hence there are about
This result may also be proved by inclusion–exclusion. Using the sets
This formula counts the number of permutations that have at least one fixed point. The cardinalities are as follows:
Hence the number of permutations with no fixed point is
or
and we have the claim.
There is a generalization of these numbers, which is known as rencontres numbers, i.e. the number
It follows that
and hence
This immediately implies that
for n large, m fixed.
Derangements containing an even and an odd number of cycles
We can use the same construction as in the previous section to compute the number of derangements
Now some very basic reasoning shows that the EGF
We thus have
which is
Subtracting
The difference of these two (
One hundred prisoners
A prison warden wants to make room in his prison and is considering liberating one hundred prisoners, thereby freeing one hundred cells. He therefore assembles one hundred prisoners and asks them to play the following game: he lines up one hundred urns in a row, each containing the name of one prisoner, where every prisoner's name occurs exactly once. The game is played as follows: every prisoner is allowed to look inside fifty urns. If he or she does not find his or her name in one of the fifty urns, all prisoners will immediately be executed, otherwise the game continues. The prisoners have a few moments to decide on a strategy, knowing that once the game has begun, they will not be able to communicate with each other, mark the urns in any way or move the urns or the names inside them. Choosing urns at random, their chances of survival are almost zero, but there is a strategy giving them a 30% chance of survival, assuming that the names are assigned to urns randomly – what is it?
First of all, the survival probability using random choices is
so this is definitely not a practical strategy.
The 30% survival strategy is to consider the contents of the urns to be a permutation of the prisoners, and traverse cycles. To keep the notation simple, assign a number to each prisoner, for example by sorting their names alphabetically. The urns may thereafter be considered to contain numbers rather than names. Now clearly the contents of the urns define a permutation. The first prisoner opens the first urn. If he finds his name, he has finished and survives. Otherwise he opens the urn with the number he found in the first urn. The process repeats: the prisoner opens an urn and survives if he finds his name, otherwise he opens the urn with the number just retrieved, up to a limit of fifty urns. The second prisoner starts with urn number two, the third with urn number three, and so on. This strategy is precisely equivalent to a traversal of the cycles of the permutation represented by the urns. Every prisoner starts with the urn bearing his number and keeps on traversing his cycle up to a limit of fifty urns. The number of the urn that contains his number is the pre-image of that number under the permutation. Hence the prisoners survive if all cycles of the permutation contain at most fifty elements. We have to show that this probability is at least 30%.
Note that this assumes that the warden chooses the permutation randomly; if the warden anticipates this strategy, he can simply choose a permutation with a cycle of length 51. To overcome this, the prisoners may agree in advance on a random permutation of their names.
We consider the general case of
or
so that the desired probability is
because the cycle of more than
which yields
Finally, using an integral estimate such as Euler–Maclaurin summation, or the asymptotic expansion of the nth harmonic number, we obtain
so that
or at least 30%, as claimed.
A related result is that asymptotically, the expected length of the longest cycle is λn, where λ is the Golomb–Dickman constant, approximately 0.62.
This example is due to Anna Gál and Peter Bro Miltersen; consult the paper by Peter Winkler for more information, and see the discussion on Les-Mathematiques.net. Consult the references on 100 prisoners for links to these references.
The above computation may be performed in a more simple and direct way, as follows: first note that a permutation of
then
For
Explanation:
We conclude that
A variation on the 100 prisoners problem (keys and boxes)
There is a closely related problem that fits the method presented here quite nicely. Say you have n ordered boxes. Every box contains a key to some other box or possibly itself giving a permutation of the keys. You are allowed to select k of these n boxes all at once and break them open simultaneously, gaining access to k keys. What is the probability that using these keys you can open all n boxes, where you use a found key to open the box it belongs to and repeat.
The mathematical statement of this problem is as follows: pick a random permutation on n elements and k values from the range 1 to n, also at random, call these marks. What is the probability that there is at least one mark on every cycle of the permutation? The claim is this probability is k/n.
The species
The index in the inner sum starts at one because we must have at least one mark on every cycle.
Translating the specification to generating functions we obtain the bivariate generating function
This simplifies to
or
In order to extract coefficients from this re-write like so
It now follows that
and hence
Divide by
We do not need to divide by n! because
Number of permutations containing m cycles
Applying the Flajolet–Sedgewick fundamental theorem, i.e. the labelled enumeration theorem with
we obtain the generating function
The term
yields the signed Stirling numbers of the first kind, and
We can compute the OGF of the signed Stirling numbers for n fixed, i.e.
Start with
which yields
Summing this, we obtain
Using the formula involving the logarithm for
Comparing the coefficients of
a falling factorial. The computation of the OGF of the unsigned Stirling numbers of the first kind works in a similar way.
Expected number of cycles of a given size m
In this problem we use a bivariate generating function g(z, u) as described in the introduction. The value of b for a cycle not of size m is zero, and one for a cycle of size m. We have
or
This means that the expected number of cycles of size m in a permutation of length n less than m is zero (obviously). A random permutation of length at least m contains on average 1/m cycles of length m. In particular, a random permutation contains about one fixed point.
The OGF of the expected number of cycles of length less than or equal to m is therefore
where Hm is the mth harmonic number. Hence the expected number of cycles of length at most m in a random permutation is about ln m.
Moments of fixed points
The mixed GF
Let the random variable X be the number of fixed points of a random permutation. Using Stirling numbers of the second kind, we have the following formula for the mth moment of X:
where
which is zero when
Expected number of fixed points in random permutation raised to some power k
Suppose you pick a random permutation
For every divisor
We get
which is
Once more continuing as described in the introduction, we find
which is
The conclusion is that
The general procedure is
Once more continuing as before, we find
We have shown that the value of
Expected number of cycles of any length of a random permutation
We construct the bivariate generating function
Note that
and generates the unsigned Stirling numbers of the first kind.
We have
Hence the expected number of cycles is the harmonic number
Number of permutations with a cycle of length larger than n/2
(Note that Section One hundred prisoners contains exactly the same problem with a very similar calculation, plus also a simpler elementary proof.)
Once more, start with the exponential generating function
There can only be one cycle of length more than
or
which is
The exponent of
It follows that the answer is
The sum has an alternate representation that one encounters e.g. in the OEIS (A024167).
finally giving
Expected number of transpositions of a random permutation
We can use the disjoint cycle decomposition of a permutation to factorize it as a product of transpositions by replacing a cycle of length k by k − 1 transpositions. E.g. the cycle
and
Hence the expected number of transpositions
We could also have obtained this formula by noting that the number of transpositions is obtained by adding the lengths of all cycles (which gives n) and subtracting one for every cycle (which gives
Note that
To see this, note that the above is equivalent to
and that
which we saw to be the EGF of the unsigned Stirling numbers of the first kind in the section on permutations consisting of precisely m cycles.
Expected cycle size of a random element
We select a random element q of a random permutation
Hence the expected length of the cycle that contains q is
Probability that a random element lies on a cycle of size m
This average parameter represents the probability that if we again select a random element of
It follows that the probability that a random element lies on a cycle of length m is
Probability that a random subset of [n] lies on the same cycle
Select a random subset Q of [n] containing m elements and a random permutation, and ask about the probability that all elements of Q lie on the same cycle. This is another average parameter. The function b(k) is equal to
Averaging out we obtain that the probability of the elements of Q being on the same cycle is
or
In particular, the probability that two elements p < q are on the same cycle is 1/2.
Number of permutations containing an even number of even cycles
We may use the Flajolet–Sedgewick fundamental theorem directly and compute more advanced permutation statistics. (Check that page for an explanation of how the operators we will use are computed.) For example, the set of permutations containing an even number of even cycles is given by
Translating to exponential generating functions (EGFs), we obtain
or
This simplifies to
or
This says that there is one permutation of size zero containing an even number of even cycles (the empty permutation, which contains zero cycles of even length), one such permutation of size one (the fixed point, which also contains zero cycles of even length), and that for
Permutations that are squares
Consider what happens when we square a permutation. Fixed points are mapped to fixed points. Odd cycles are mapped to odd cycles in a one-to-one correspondence, e.g.
which yields the EGF
Odd cycle invariants
The types of permutations presented in the preceding two sections, i.e. permutations containing an even number of even cycles and permutations that are squares, are examples of so-called odd cycle invariants, studied by Sung and Zhang (see external links). The term odd cycle invariant simply means that membership in the respective combinatorial class is independent of the size and number of odd cycles occurring in the permutation. In fact we can prove that all odd cycle invariants obey a simple recurrence, which we will derive. First, here are some more examples of odd cycle invariants.
Permutations where the sum of the lengths of the even cycles is six
This class has the specification
and the generating function
The first few values are
Permutations where all even cycles have the same length
This class has the specification
and the generating function
There is a semantic nuance here. We could consider permutations containing no even cycles as belonging to this class, since zero is even. The first few values are
Permutations where the maximum length of an even cycle is four
This class has the specification
and the generating function
The first few values are
The recurrence
Observe carefully how the specifications of the even cycle component are constructed. It is best to think of them in terms of parse trees. These trees have three levels. The nodes at the lowest level represent sums of products of even-length cycles of the singleton
where
is even, too, and hence
Letting
which yields the recurrence
A problem from the 2005 Putnam competition
A link to the Putnam competition website appears in the section External links. The problem asks for a proof that
where the sum is over all
Now the sign of
where the product is over all cycles c of
Hence we consider the combinatorial class
where
or
Now we have
and hence the desired quantity is given by
Doing the computation, we obtain
or
Extracting coefficients, we find that the coefficient of
or
which is the desired result.
As an interesting aside, we observe that
where
Now the value of the product on the right for a permutation
which yields
and finally
The difference between the number of cycles in even and odd permutations
Here we seek to show that this difference is given by
Recall that the sign
where the product ranges over the cycles c from the disjoint cycle composition of
It follows that the combinatorial species
where we have used
Translating to generating functions we have
This simplifies to
which is
Now the two generating functions
and
We require the quantity
which is
Finally, extracting coefficients from this generating function, we obtain
which is
which is in turn
This concludes the proof.