Derangements

Derangements

Definition

A derangement is a permutation in which no element appears in its original position. In other words, for a permutation of 1∼n1\sim n denoted by PP, if Pi≠iP_i\neq i, then PP is called a derangement of nn.

For example, the ternary derangements are {2,3,1}\{2,3,1\} and {3,1,2}\{3,1,2\}. The quaternary derangements are {2,1,4,3}\{2,1,4,3\}, {2,3,4,1}\{2,3,4,1\}, {2,4,1,3}\{2,4,1,3\}, {3,1,4,2}\{3,1,4,2\}, {3,4,1,2}\{3,4,1,2\}, {3,4,2,1}\{3,4,2,1\}, {4,1,2,3}\{4,1,2,3\}, {4,3,1,2}\{4,3,1,2\}, and {4,3,2,1}\{4,3,2,1\}. A derangement is a permutation with no fixed point, that is, with no cycle of length 1.

Computing with the Inclusion–Exclusion Principle

The universal set UU is the set of permutations of 1∼n1\sim n, so ∣U∣=n!|U|=n!; let SiS_i be the permutations in which Pi≠iP_i\neq i. Using complements and the inclusion–exclusion principle, the problem becomes finding:

∣⋂i=1nSi∣=∣U∣−∣⋃i=1nSi‾∣=n!−∑k=1n(−1)k−1∑ai<ai+1∣⋂i=1kSai‾∣\begin{aligned} \left|\bigcap_{i=1}^n S_i\right| &=|U|-\left|\bigcup_{i=1}^n\overline{S_i}\right|\\ &=n!-\sum_{k=1}^n(-1)^{k-1}\sum_{a_i<a_{i+1}}\left|\bigcap_{i=1}^{k}\overline{S_{a_i}}\right| \end{aligned}

Here the summation means choosing from 1,2,⋯ ,n1, 2, \cdots, n the numbers a1,a2,⋯ ,aka_1, a_2, \cdots, a_k such that ai<ai+1a_i<a_{i+1}. Therefore

∣⋂i=1kSai‾∣\left|\bigcap_{i=1}^{k}\overline{S_{a_i}}\right|

This is the number of permutations in which the kk numbers a1,a2,⋯ ,aka_1,a_2,\cdots,a_k satisfy Pai=aiP_{a_i}=a_i, while the remaining n−kn-k positions can contain any permutation. Hence:

∣⋂i=1kSai‾∣=(n−k)!\left|\bigcap_{i=1}^{k}\overline{S_{a_i}}\right|=(n-k)!

There are kk numbers, with (nk)\dbinom{n}{k} possible choices, so summing gives:

∑k=1n(−1)k−1∑ai<ai+1∣⋂i=1kSai‾∣=∑k=1n(−1)k−1(nk)(n−k)!=∑k=1n(−1)k−1n!k!=n!∑k=1n(−1)k−1k!\begin{aligned} &\sum_{k=1}^n(-1)^{k-1}\sum_{a_i<a_{i+1}}\left|\bigcap_{i=1}^{k}\overline{S_{a_i}}\right|\\ =&\sum_{k=1}^n(-1)^{k-1}\dbinom{n}{k}(n-k)!\\ =&\sum_{k=1}^n(-1)^{k-1}\frac{n!}{k!}\\ =&n!\sum_{k=1}^n\frac{(-1)^{k-1} }{k!} \end{aligned}

Therefore, the number of derangements of nn elements is:

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

The first few terms of the derangement sequence are 0,1,2,9,44,2650,1,2,9,44,265 (OEIS A000166).

Computing by Recurrence

Let us make the derangement problem concrete by considering the following question:

nn distinct letters, numbered 1,2,3,4,51,2,3,4,5, are to be placed into envelopes numbered 1,2,3,4,51,2,3,4,5, with no envelope number matching the letter number. How many different placements are possible?

Suppose we consider the nn-th envelope and initially place the nn-th letter into the nn-th envelope temporarily. We then derive a recurrence from two cases:

  • All of the first n−1n-1 envelopes contain the wrong letters;
  • Exactly one of the first n−1n-1 envelopes contains the correct letter, and all the others contain the wrong letters.

In the first case, all of the first n−1n-1 envelopes contain the wrong letters. Since the first n−1n-1 envelopes are already all wrong, the nn-th letter only needs to be swapped with any one of the preceding positions, giving Dn−1×(n−1)D_{n-1}\times (n-1) possibilities.

In the second case, exactly one of the first n−1n-1 envelopes contains the correct letter, and all the others contain the wrong letters. We consider this case because if one of the first n−1n-1 envelopes is correct, swapping that letter with the nn-th letter produces a derangement.

In all other cases, a single operation cannot turn the arrangement into a derangement of length nn.

Thus, the number of derangements satisfies the recurrence:

Dn=(n−1)(Dn−1+Dn−2)D_n=(n-1)(D_{n-1}+D_{n-2})

Here is another recurrence:

Dn=nDn−1+(−1)nD_n=nD_{n-1}+{(-1)}^n

Other Relations

The number of derangements has a simple rounding formula, and its growth differs from the factorial by only a constant factor:

Dn=⌊n!e+12⌋D_n=\left\lfloor\frac{n!}{\mathrm{e}} + \frac{1}{2}\right\rfloor

As the number of elements increases, the probability P of forming a derangement approaches:

P=lim⁡n→∞Dnn!=1eP=\lim_{n\to\infty}\frac{D_n}{n!}=\frac{1}{\mathrm{e}}

This content comes from OI Wiki: Derangements. See the original for authors and revision history. AIOJ has adapted the formatting, in-site links, and resource paths.

Content is shared under CC BY-SA 4.0 and SATA license; additional terms are governed by the original notice. Thanks to the OI Wiki authors and contributors; per the SATA terms, please visit the OI Wiki repository and star it to show your support.