Recurrence Relation for Number of Derangements on Finite Set

Theorem
The number of derangements $D_n$ on a finite set $S$ of cardinality $n$ is:
 * $D_n = \left({n - 1}\right) \left({D_{n-1} + D_{n-2}}\right)$

where $D_1 = 0$, $D_2 = 1$.

Proof
Let $\left|{S}\right| = 1$ such that $S = \left\{{s}\right\}$, say.

Then $f \left({s}\right) = s$ is the only permutation on $S$.

This by definition is not a derangement.

Thus:
 * $D_1 = 0$

Let $\left|{S}\right| = 2$.

Then $S = \left\{{s, t}\right\}$, say.

There are two permutations on $S$:
 * $f = \left\{{\left({s, s}\right), \left({t, t}\right)}\right\}$

and:
 * $g = \left\{{\left({s, t}\right), \left({t, s}\right)}\right\}$

and only the latter is a derangement.

So:
 * $D_2 = 1$

Let $\left|{S}\right| > 2$.

Let $f: S \to S$ be a derangement.

We aim to count the total number of such $f$.

, we can take:
 * $S = \left\{{1, 2, \ldots, n + 1}\right\}$

Now, consider an arbitrary $s \in S$ such that $s \ne 1$.

Let $f \left({s}\right) = 1$.

By the Sum Rule for Counting, the total number of $f$ will be:


 * $\left({\text{the number of $f$ where } f \left({1}\right) \ne s}\right) + \left({\text{the number of $f$ where} f \left({1}\right) = s}\right)$

Case 1

 * $f \left({1}\right) \ne s$

Take:
 * $T_1 = \left\{ {1, 2, \dotsc, s-1, s+1, \dotsc, n + 1}\right\} = S \setminus \left\{ {s}\right\}$

Define the derangement $g_1: T_1 \to T_1$ by:
 * $\forall t \in T_1: g_1 \left({t}\right) = f \left({t}\right)$

Then $g_1$ is a derangement on a set of cardinality $n$.

Thus there are $D_n$ such $g_1$.

Note that:
 * $f = g_1 \cup f \left({s}\right)$

We have that $s$ can be chosen in $n$ ways.

By the Product Rule for Counting there are in total $n D_n$ such $f$ where $f \left({s}\right) \ne 1$.

Case 2

 * $f \left({1}\right) = s$

Take:
 * $T_2 = \left\{{2, 3, \dotsc, s-1, s+1, \dotsc, n+1}\right\} = S \setminus \left\{ {1, s}\right\}$

Define the derangement $g_2: T_2 \to T_2$ by:
 * $\forall t \in T_2: g_2 \left({t}\right) = f \left({t}\right)$

Then $g_2$ is a derangement on a set of cardinality $n-1$.

So there are $D_{n-1}$ such $g_2$.

Note that:
 * $f = g_2 \cup \left\{ {s}\right\} \cup f \left({s}\right)$

We have that $s$ can be chosen in $n$ ways

By the Product Rule for Counting there are in total $n D_{n-1}$ such $f$ where $f \left({s}\right) = 1$.

Summing the results from both cases, we get the total number of derangements $f$ on a set of cardinality $n+1$ is:
 * $D_{n+1} = n D_n + n D_{n-1} = n \left({D_n + D_{n-1} }\right)$

as was to be proved.