# Derangement

In combinatorial mathematics, a derangement is a permutation of the elements of a set in which no element appears in its original position. In other words, a derangement is a permutation that has no fixed points.
The number of derangements of a set of size n is known as the subfactorial of n or the n th derangement number or n th de Montmort number (after Pierre Remond de Montmort). Notations for subfactorials in common use include !n, D$_n$, d$_n$, or n¡ .
For n > 0 , the subfactorial !n equals the nearest integer to n!/e, where n! denotes the factorial of n and e ≈ 2.718281828... is Euler's number.
The problem of counting derangements was first considered by Pierre Raymond de Montmort in his Essay d'analyse sur les jeux de hazard in 1708; he solved it in 1713, as did Nicholas Bernoulli at about the same time.

## Example

Suppose that a professor gave a test to 4 students – A, B, C, and D – and wants to let them grade each other's tests. Of course, no student should grade their own test. How many ways could the professor hand the tests back to the students for grading, such that no student receives their own test back? Out of 24 possible permutations (4!) for handing back the tests,
    ABCD, ABDC, ACBD, ACDB, ADBC, ADCB, BACD, BADC, BCAD, BCDA, BDAC, BDCA, CABD, CADB, CBAD, CBDA, CDAB, CDBA, DABC, DACB, DBAC, DBCA, DCAB, DCBA.
there are only 9 derangements (shown in blue italics above). In every other permutation of this 4-member set, at least one student gets their own test back (shown in bold red).
Another version of the problem arises when we ask for the number of ways n letters, each addressed to a different person, can be placed in n pre-addressed envelopes so that no letter appears in the correctly addressed envelope.

## Counting derangements

Counting derangements of a set amounts to the hat-check problem, in which one considers the number of ways in which n hats (call them h$_1$ through h$_n$) can be returned to n people (P$_1$ through P$_n$) such that no hat makes it back to its owner.
Each person may receive any of the n − 1 hats that is not their own. Call the hat which the person P$_1$ receives h$_i$ and consider h$_i$'s owner: P$_i$ receives either P$_1$'s hat, h$_1$, or some other. Accordingly, the problem splits into two possible cases:
* P$_i$ receives a hat other than h$_1$. This case is equivalent to solving the problem with n − 1 people and n − 1 hats because for each of the n − 1 people besides P$_1$ there is exactly one hat from among the remaining n − 1 hats that they may not receive (for any P$_j$ besides P$_i$, the unreceivable hat is h$_j$, while for P$_i$ it is h$_1$). Another way to see this is to rename h$_1$ to h$_i$, where the derangement is more explicit: for any j from 2 to n, P$_j$ cannot receive h$_j$.
* P$_i$ receives h$_1$. In this case the problem reduces to n − 2 people and n − 2 hats, because P$_1$ received h$_i$'s hat and P$_i$ received h$_1$'s hat, effectively putting both out of further consideration.
For each of the n − 1 hats that P$_1$ may receive, the number of ways that P$_2$, ..., P$_n$ may all receive hats is the sum of the counts for the two cases.
This gives us the solution to the hat-check problem: Stated algebraically, the number !n of derangements of an n-element set is $!n=\left(n-1\right){\bigl{!\left(n-1\right)}+{!\left(n-2\right)}{\bigr )}$ for $n\geq 2$, where $!0=1$ and $!1=0.$
The number of derangements of small lengths is given in the table below.
The number of derangements of an n-element set (sequence A000166 in the OEIS) for small n :
| n | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 |
| !n | 1 | 0 | 1 | 2 | 9 | 44 | 265 | 1,854 | 14,833 | 133,496 | 1,334,961 | 14,684,570 | 176,214,841 | 2,290,792,932 |
There are various other expressions for !n, equivalent to the formula given above. These include $!n=n!\sum _{i=0}^{n}{\frac {(-1)^{i}}{i!}}$ for $n\geq 0$ and
    $!n=\left[{\frac {n!}{e}}\right]=\left\lfloor {\frac {n!}{e}}+{\frac {1}{2}}\right\rfloor$ for $n\geq 1,$
where $\left[x\right]$ is the nearest integer function and $\left\lfloor x\right\rfloor$ is the floor function.
Other related formulas include $!n=\left\lfloor {\frac {n!+1}{e}}\right\rfloor ,\quad \ n\geq 1,$ $!n=\left\lfloor \left(e+e^{-1}\right)n!\right\rfloor -\lfloor en!\rfloor ,\quad n\geq 2,$ and $!n=n!-\sum _{i=1}^{n}{n \choose i}\cdot {!(n-i)},\quad \ n\geq 1.$
The following recurrence also holds: $!n={\begin{cases}1&{\text{if }}n=0,\\n\cdot \left(!(n-1)\right)+(-1)^{n}&{\text{if }}n>0.\end{cases}}$

### Derivation by inclusion–exclusion principle

One may derive a non-recursive formula for the number of derangements of an n-set, as well. For $1\leq k\leq n$ we define $S_{k}$ to be the set of permutations of n objects that fix the k th object. Any intersection of a collection of i of these sets fixes a particular set of i objects and therefore contains $(n-i)!$ permutations. There are (n/i){\textstyle {n \choose i}} such collections, so the inclusion–exclusion principle yields ${\begin{aligned}|S_{1}\cup \dotsm \cup S_{n}|&=\sum _{i}\left|S_{i}\right|-\sum _{i<j}\left|S_{i}\cap S_{j}\right|+\sum _{i<j<k}\left|S_{i}\cap S_{j}\cap S_{k}\right|+\cdots +(-1)^{n+1}\left|S_{1}\cap \dotsm \cap S_{n}\right|\\&={n \choose 1}(n-1)!-{n \choose 2}(n-2)!+{n \choose 3}(n-3)!-\cdots +(-1)^{n+1}{n \choose n}0!\\&=\sum _{i=1}^{n}(-1)^{i+1}{n \choose i}(n-i)!\\&=n!\ \sum _{i=1}^{n}{(-1)^{i+1} \over i!},\end{aligned}}$ and since a derangement is a permutation that leaves none of the n objects fixed, this implies $!n=n!-\left|S_{1}\cup \dotsm \cup S_{n}\right|=n!\sum _{i=0}^{n}{\frac {(-1)^{i}}{i!}}~.$
On the other hand, $n!=\sum _{i=0}^{n}{\binom {n}{i}}\ !i$ since we can choose n - i elements to be in their own place and derange the other i elements in just !i ways, by definition.

## Growth of number of derangements as n approaches ∞

From $!n=n!\sum _{i=0}^{n}{\frac {(-1)^{i}}{i!}}$ and $e^{x}=\sum _{i=0}^{\infty }{x^{i} \over i!}$ by substituting x=−1{\textstyle x=-1} one immediately obtains that $\lim _{n\to \infty }{!n \over n!}=\lim _{n\to \infty }\sum _{i=0}^{n}{\frac {(-1)^{i}}{i!}}=e^{-1}\approx 0.367879\ldots .$ This is the limit of the probability that a randomly selected permutation of a large number of objects is a derangement. The probability converges to this limit extremely quickly as n increases, which is why !n is the nearest integer to n!/e. The above semi-log graph shows that the derangement graph lags the permutation graph by an almost constant value.
More information about this calculation and the above limit may be found in the article on the statistics of random permutations.

### Asymptotic expansion in terms of Bell numbers

An asymptotic expansion for the number of derangements in terms of Bell numbers is as follows: $!n={\frac {n!}{e}}+\sum _{k=1}^{m}\left(-1\right)^{n+k-1}{\frac {B_{k}}{n^{k}}}+O\left({\frac {1}{n^{m+1}}}\right),$ where $m$ is any fixed positive integer, and $B_{k}$ denotes the $k$-th Bell number. Moreover, the constant implied by the big O-term does not exceed $B_{m+1}$.

## Generalizations

The problème des rencontres asks how many permutations of a size-n set have exactly k fixed points.
Derangements are an example of the wider field of constrained permutations. For example, the ménage problem asks if n opposite-sex couples are seated man-woman-man-woman-... around a table, how many ways can they be seated so that nobody is seated next to his or her partner?
More formally, given sets A and S, and some sets U and V of surjections A → S, we often wish to know the number of pairs of functions (f, g) such that f is in U and g is in V, and for all a in A, f(a) ≠ g(a); in other words, where for each f and g, there exists a derangement φ of S such that f(a) = φ(g(a)).
Another generalization is the following problem:
    How many anagrams with no fixed letters of a given word are there?
For instance, for a word made of only two different letters, say n letters A and m letters B, the answer is, of course, 1 or 0 according to whether n = m or not, for the only way to form an anagram without fixed letters is to exchange all the A with B, which is possible if and only if n = m. In the general case, for a word with n$_1$ letters X$_1$, n$_2$ letters X$_2$, ..., n$_r$ letters X$_r$, it turns out (after a proper use of the inclusion-exclusion formula) that the answer has the form $\int _{0}^{\infty }P_{n_{1}}(x)P_{n_{2}}(x)\cdots P_{n_{r}}(x)\ e^{-x}dx,$ for a certain sequence of polynomials P$_n$, where P$_n$ has degree n. But the above answer for the case r = 2 gives an orthogonality relation, whence the P$_n$'s are the Laguerre polynomials (up to a sign that is easily decided).
In particular, for the classical derangements, one has that $!n={\frac {\Gamma (n+1,-1)}{e}}=\int _{0}^{\infty }(x-1)^{n}e^{-x}dx$ where $\Gamma (s,x)$ is the upper incomplete gamma function.

## Computational complexity

It is NP-complete to determine whether a given permutation group (described by a given set of permutations that generate it) contains any derangements.
    Table of factorial and derangement values $n$ Permutations, $n!$ Derangements, $!n$ ${\frac {!n}{n!}}$ 0 1 =1×10$^0$ 1 =1×10$^0$  = 1 1 1 =1×10$^0$ 0  = 0 2 2 =2×10$^0$ 1 =1×10$^0$  = 0.5 3 6 =6×10$^0$ 2 =2×10$^0$ ≈0.33333 33333 4 24 =2.4×10$^1$ 9 =9×10$^0$  = 0.375 5 120 =1.20×10$^2$ 44 =4.4×10$^1$ ≈0.36666 66667 6 720 =7.20×10$^2$ 265 =2.65×10$^2$ ≈0.36805 55556 7 5,040 =5.04×10$^3$ 1,854 ≈1.85×10$^3$ ≈0.36785,71429 8 40,320 ≈4.03×10$^4$ 14,833 ≈1.48×10$^4$ ≈0.36788 19444 9 362,880 ≈3.63×10$^5$ 133,496 ≈1.33×10$^5$ ≈0.36787 91887 10 3,628,800 ≈3.63×10$^6$ 1,334,961 ≈1.33×10$^6$ ≈0.36787 94643 11 39,916,800 ≈3.99×10$^7$ 14,684,570 ≈1.47×10$^7$ ≈0.36787 94392 12 479,001,600 ≈4.79×10$^8$ 176,214,841 ≈1.76×10$^8$ ≈0.36787 94413 13 6,227,020,800 ≈6.23×10$^9$ 2,290,792,932 ≈2.29×10$^9$ ≈0.36787 94412 14 87,178,291,200 ≈8.72×10$^{10}$ 32,071,101,049 ≈3.21×10$^{10}$ ≈0.36787 94412 15 1,307,674,368,000 ≈1.31×10$^{12}$ 481,066,515,734 ≈4.81×10$^{11}$ ≈0.36787 94412 16 20,922,789,888,000 ≈2.09×10$^{13}$ 7,697,064,251,745 ≈7.70×10$^{12}$ ≈0.36787 94412 17 355,687,428,096,000 ≈3.56×10$^{14}$ 130,850,092,279,664 ≈1.31×10$^{14}$ ≈0.36787 94412 18 6,402,373,705,728,000 ≈6.40×10$^{15}$ 2,355,301,661,033,953 ≈2.36×10$^{15}$ ≈0.36787 94412 19 121,645,100,408,832,000 ≈1.22×10$^{17}$ 44,750,731,559,645,106 ≈4.48×10$^{16}$ ≈0.36787 94412 20 2,432,902,008,176,640,000 ≈2.43×10$^{18}$ 895,014,631,192,902,121 ≈8.95×10$^{17}$ ≈0.36787 94412 21 51,090,942,171,709,440,000 ≈5.11×10$^{19}$ 18,795,307,255,050,944,540 ≈1.88×10$^{19}$ ≈0.36787 94412 22 1,124,000,727,777,607,680,000 ≈1.12×10$^{21}$ 413,496,759,611,120,779,881 ≈4.13×10$^{20}$ ≈0.36787 94412 23 25,852,016,738,884,976,640,000 ≈2.59×10$^{22}$ 9,510,425,471,055,777,937,262 ≈9.51×10$^{21}$ ≈0.36787 94412 24 620,448,401,733,239,439,360,000 ≈6.20×10$^{23}$ 228,250,211,305,338,670,494,289 ≈2.28×10$^{23}$ ≈0.36787 94412 25 15,511,210,043,330,985,984,000,000 ≈1.55×10$^{25}$ 5,706,255,282,633,466,762,357,224 ≈5.71×10$^{24}$ ≈0.36787 94412 26 403,291,461,126,605,635,584,000,000 ≈4.03×10$^{26}$ 148,362,637,348,470,135,821,287,825 ≈1.48×10$^{26}$ ≈0.36787 94412 27 10,888,869,450,418,352,160,768,000,000 ≈1.09×10$^{28}$ 4,005,791,208,408,693,667,174,771,274 ≈4.01×10$^{27}$ ≈0.36787 94412 28 304,888,344,611,713,860,501,504,000,000 ≈3.05×10$^{29}$ 112,162,153,835,443,422,680,893,595,673 ≈1.12×10$^{29}$ ≈0.36787 94412 29 8,841,761,993,739,701,954,543,616,000,000 ≈8.84×10$^{30}$ 3,252,702,461,227,859,257,745,914,274,516 ≈3.25×10$^{30}$ ≈0.36787 94412 30 265,252,859,812,191,058,636,308,480,000,000 ≈2.65×10$^{32}$ 97,581,073,836,835,777,732,377,428,235,481 ≈9.76×10$^{31}$ ≈0.36787 94412

## Footnotes

* ↑ The name "subfactorial" originates with William Allen Whitworth.