Skip to main content

Permutations

An important object in discrete mathematics is called permutation - a bijective mapping between set XX and itself. From Theorem 241, there are a total of n!n! permutations on a set XX of size nn. For instance, when X={1,2,3}X= \{1,2,3\}, a bijective mapping ff can be defined as f(1)=2f(1) = 2, f(2)=1f(2) = 1, and f(3)=3f(3) =3.

We can write ff in a two-row representation:

(123213)\begin{pmatrix} 1 & 2 & 3\\ 2 & 1 & 3 \end{pmatrix}

The first row lists all elements of XX and, under each element xXx \in X, we list f(x)f(x) right under it. In fact, we only need one line to represent such a permutation: Just take the second line (2,1,3)(2, 1, 3), which means that f(1)=2f(1) = 2, f(2)=1f(2)=1 and f(3)=3f(3)=3. Note that such a representation suffices since we know that the permuted items are listed under a fixed total order of XX (in this case the natural increasing order 1,2,31,2,3).

A graphical representation of (4, 8, 3, 5, 2, 9, 6, 1, 7).

Figure 11: A graphical representation of (4,8,3,5,2,9,6,1,7)(4, 8, 3, 5, 2, 9, 6, 1, 7).

Figure 11 shows a graphic representation of permutations where an arrow from ii to jj represents f(i)=jf(i) =j. It is easy to see (by picture) that each permutation has a representation that is a union of disjoint cycles. Again, no matter how you try to draw a permutation, you will see this fact. So it is obvious in drawing, but how do we really define these cycles formally?

Example 23 (Composition of permutations)

Let ff and gg be two permutations on XX. Define fgf \circ g as fg(i)=f(g(i))f\circ g(i) = f(g(i)). Notice that fgf\circ g is also a permutation on XX.

Proof:

We need to argue one-to-one and onto properties. For the one-to-one, assume that fg(i)=fg(j)f\circ g(i) = f\circ g(j); so f(g(i))=f(g(j))f(g(i)) = f(g(j)). Since ff is one-to-one, we have g(i)=g(j)g(i) = g(j); since gg is one-to-one, we have i=ji = j. To show the onto property, consider jXj \in X. Since ff is onto, we have that there exists xXx\in X for which f(x)=jf(x) = j; since gg is onto, there exists yXy \in X for which g(y)=xg(y) = x; therefore, fg(y)=f(g(y))=f(x)=jf\circ g(y) = f(g(y)) = f(x) = j.

For any permutation ff, we define fkf^k as the permutation that is a kk-fold composition of ff with itself, i.e., f1=ff^1 = f and fk=ffk1f^k = f\circ f^{k-1}. Define the relation \approx on the set XX where iji \approx j if and only if there exists k1k \geq 1 such that fk(i)=jf^k(i) = j.

Exercise 61

Prove that \approx is an equivalence relation and that its equivalence classes are the cycles of ff.

Define the identity permutation id{\sf id} on XX as id(i)=i{\sf id}(i) = i for all iXi \in X.

Exercise 62

Let π\pi be a permutation. Prove that there exists integer kk for which πk=id\pi^k = {\sf id}. As a bonus, describe an algorithm that, given a permutation π\pi, computes the minimum value of kk satisfying the former property.

Derangements

Now we will consider an interesting example of counting that requires non-trivial, creative thinking. Suppose there are nn students in class, and we (teachers) already graded the student's homework. If we shuffle the graded homework at random before returning them to students, how many students should we expect to receive their own homework? This is a non-trivial question, and we will see several ways to answer this question.

We say that a permutation π\pi on [n]={1,,n}[n] = \{1,\ldots, n\}2 is a derangement if π(i)i\pi(i) \neq i for all i[n]i \in [n]. In other words, π\pi does not have a fixed point.

The question here is, out of the n!n! permutations, how many of them are derangements?

Let Πn\Pi_n be the set of derangements on [n][n] and Dn=ΠnD_n= |\Pi_n| be the number of such derangements. We will prove the following recurrence:

Theorem 25

Dn=(n1)(Dn1+Dn2)D_n = (n-1)(D_{n-1} + D_{n-2})

Proof:

We want to count the number of derangements ff on [n][n]. First, notice that f(n)f(n) can map to (n1)(n-1) possibilities (anything but nn). We partition the derangements in Πn\Pi_n into (n1)(n-1) sets based on this value, i.e., Πn,i={fΠn:f(n)=i}\Pi_{n,i} = \{f \in \Pi_n: f(n) = i\}, so we have that Πn=i=1n1Πn,i|\Pi_n| = \sum_{i=1}^{n-1} |\Pi_{n,i}|. By a bijection argument, it is easy to see that the sets Πn,i\Pi_{n,i} are of equal size, so we have that Πn=(n1)Πn,n1|\Pi_n| = (n-1)|\Pi_{n, n-1}|.

For simplicity, denote Πn,n1\Pi_{n,n-1} by F\mathcal{F}. We further partition F\mathcal{F} into F1\mathcal{F}_1 and F2\mathcal{F}_2 based on the value of f(n1)f(n-1):

F1={fF:f(n1)n} and F2={fF:f(n1)=n}\mathcal{F}_1 = \{f \in \mathcal{F}: f(n-1) \neq n\} \text{ and } \mathcal{F}_2 = \{f \in \mathcal{F}: f(n-1) = n\}

The key observations are that: (i) there is a bijection between F1\mathcal{F}_1 and Πn1\Pi_{n-1} (this is very easy to see) and (ii) a bijection between F2\mathcal{F}_2 and Πn2\Pi_{n-2}. In the exercises, you will formally prove these.

Exercise 63

Formally complete the proof of the above theorem. In particular,

  • describe a bijection between Πn,n1\Pi_{n,n-1} and Πn,i\Pi_{n,i} for all i[n1]i\in[n-1],
  • describe a bijection between F1\mathcal{F}_1 and Πn1\Pi_{n-1}, and
  • describe a bijection between F2\mathcal{F}_2 and Πn2\Pi_{n-2}.

We want to remark that the above theorem immediately implies a computational solution for the number of derangements. With the recurrence relation, it is relatively straightforward to code a dynamic programming (DP).

Exercise 64

Derive the following explicit formula for DnD_n:

Dn=n!k=0n(1)kk!D_n = n! \sum_{k=0}^n \frac{(-1)^k}{k!}

Hint: Define Cn=DnnDn1C_n = D_n - n \cdot D_{n-1}. Write the recurrence for CnC_n and solve it.

Mathematical theory of sorting

In computer science, we encounter permutations very often, most notably in the context of sorting. In this section, we will treat sorting as a purely mathematical topic. A sorting algorithm AA can be seen as a mapping that takes a permutation π:[n][n]\pi: [n] \rightarrow [n] as input and performs a sequence of exchange operations until π\pi becomes an identity permutation id{\sf id}. A good sorting algorithm is one that uses as small number of exchange operations as possible.

Composition of permutation \pi= (2,1,5,3,4) with g={\sf swap}_{3,5} = (1,2,5,4,3). The arrows on the left and right show the mapping of g and \pi respectively. The outcome of this composition is \pi \circ g= (2,1,4,3,5) (obtained by swapping numbers in \pi in the 3rd and 5th positions).

Figure 12: Composition of permutation π=(2,1,5,3,4)\pi= (2,1,5,3,4) with g=swap3,5=(1,2,5,4,3)g={\sf swap}_{3,5} = (1,2,5,4,3). The arrows on the left and right show the mapping of gg and π\pi respectively. The outcome of this composition is πg=(2,1,4,3,5)\pi \circ g= (2,1,4,3,5) (obtained by swapping numbers in π\pi in the 3rd and 5th positions).

An exchange operation can be seen as composing the input permutation (the unsorted elements) with a swap permutation: swapi,j(i)=j,swapi,j(j)=i{\sf swap}_{i,j}(i) = j, {\sf swap}_{i,j}(j) = i, and swapi,j(k)=k{\sf swap}_{i,j}(k) = k for k∉{i,j}k \not\in \{i,j\}. Intuitively, if we represent a permutation π\pi in a one-line notation, applying the composition swapi,j{\sf swap}_{i,j} would exchange the numbers in the ii-th and jj-th position of the permutation. See Figure 12 for illustration. The following claim makes this discussion formal.

Proposition 2

If π=(p1,,pn)\pi = (p_1,\ldots, p_n) and 1i<jn1 \leq i<j\leq n, then

πswapi,j=(p1,,pi1,pj,pi+1,pj1,pi,pj+1,,pn)\pi \circ {\sf swap}_{i,j} = (p_1,\ldots, p_{i-1}, p_j, p_{i+1} \ldots, p_{j-1}, p_i, p_{j+1}, \ldots, p_n)

Therefore, a sorting algorithm AA can be seen as, on input permutation π\pi, output a collection of {(i1,j1),,(iq,jq)}\{(i_1,j_1),\ldots, (i_q, j_q)\} so that πswapi1,j1swapi2,j2swapiq,jq=id\pi \circ {\sf swap}_{i_1, j_1} \circ {\sf swap}_{i_2,j_2} \cdots \circ {\sf swap}_{i_q, j_q} = {\sf id}. For instance, to sort a permutation π=(1,2,4,3)\pi = (1,2,4,3), we can compose π\pi with g=swap3,4g = {\sf swap}_{3,4}, and πg\pi \circ g gives us an identity permutation.

Let π\pi be a permutation. We say that (i,j)(i,j) is an inversion of π\pi if i<ji <j and π(i)>π(j)\pi(i) > \pi(j). Denote by I(π)I(\pi) the set of all inversions of π\pi. This notion of inversion captures exactly the locations of π\pi that are currently in an "swapped" relative order and need to be "inverted" somehow. Notice that the identity id{\sf id} has I(id)=I({\sf id}) = \emptyset, and the reverse permutation (n,n1,,2,1)(n,n-1,\ldots, 2,1) has n(n1)/2n(n-1)/2 inversions. In this way, I(π)I(\pi) measures how far π\pi is from an identity permutation.

Exercise 65

Prove that I(π)I(\pi) is transitive (i.e., prove that if (i,j)(i,j) and (j,k)(j,k) are inversions, then so is (i,k)(i,k))

The concept of inversion can be used to analyze the performance of some natural sorting algorithms. In particular, we learn from algorithms classes that the best sorting algorithms take O(nlogn)O(n \log n) steps. However, if sorting algorithms are allowed to only exchange consecutive numbers, it is known to be unable to achieve the optimal performance.

Exercise 66

Consider a sorting algorithm AA that only exchanges two neighboring locations (i.e., performing swapi,i+1{\sf swap}_{i,i+1} for some ii). Prove that there exists a permutation π\pi for which A(π)A(\pi) must perform at least n210\frac{n^2}{10} exchange operations.

Footnotes

  1. Note that a one-to-one function is also "onto" if it maps objects from a finite set to itself.

  2. Note that here we redefined [n][n] to start from one. This is usually more convenient when counting whereas starting from 00 is usually more convenient when working with modulo operations as in Number and Algorithms.