Recurrence Relation for Number of Derangements on Finite Set

Theorem
The number of derangements on a finite set $$S$$ where $$|S|=n+1$$ is $$D_{n+1} = n(D_n+D_{n-1})$$, where $$D_1 = 0$$, $$D_2 = 1$$.

Proof
First note that $$D_1=0$$ because if $$S=\{s\}$$ (i.e., $$|S|=1$$), $$f(s)=s$$ is the only permutation on $$S$$, and is not a derangement.

If $$S=\{s,t\}$$, then there are two permutations on $$S$$: $$f=\{(s,s),(t,t)\}$$ and $$g=\{(s,t),(t,s)\}$$, and only the latter is a derangement, so $$D_2=1$$.

Now, let $$f:S\to S$$ be a derangement. We aim to count the total number of such $$f$$.

Without loss of generality, we can take $$S = \{1,2,\ldots, n+1\}$$. Now, consider an arbitrary $$s \in S$$ such that $$s \neq 1$$, and set $$f(s)=1$$.

By the sum rule for counting, the total number of $$f$$ will be:

( the number of $$f$$ where $$f(1) \neq s$$ ) + ( the number of $$f$$ where $$f(1)=s$$ ).

Case 1 $$f(1) \neq s$$
Take $$T_1=\{1,2,\ldots ,s-1,s+1, \ldots, n+1\}=S\backslash\{s\}$$ and define the derangement $$g_1:T_1 \to T_1$$ by $$g_1(t) = f(t)$$ $$\forall t \in T_1$$.

Then $$g_1$$ is a derangement on a set of order $$n,$$ and so there are $$D_n$$ such $$g_1$$.

Note that $$f=g_1\cup f(s)$$.

Since $$s$$ can be chosen in $$n$$ ways, by the product rule for counting there are in total $$nD_n$$ such $$f$$ where $$f(s) \neq 1$$.

Case 2 $$f(1) = s$$
Take $$T_2=\{2,3,\ldots ,s-1,s+1, \ldots, n+1\}=S\backslash\{1,s\}$$ and define the derangement $$g_2:T_2 \to T_2$$ by $$g_2(t) = f(t)$$ $$\forall t \in T_2$$.

Then $$g_2$$ is a derangement on a set of order $$n-1$$, and so there are $$D_{n-1}$$ such $$g_2$$.

Note that $$f=g_2\cup \{s\} \cup f(s)$$. Since $$s$$ can be chosen in $$n$$ ways, by the product rule for counting there are in total $$nD_{n-1}$$ such $$f$$ where $$f(s) = 1$$.

Summing the results from both cases, we get the total number of derangements $$f$$ on a set of order $$n+1$$ is$$D_{n+1}=nD_n + nD_{n-1} = n(D_n+D_{n-1})$$ as desired.

QED