Cycles in permutations of a given size How would I find the number of permutations of $[n]$ in which all cycles have length $1$ or $2$?
I know how to do this if I have, say, $8$ numbers from ${{1,2,3,...,8}}$ and I want them to be broken into Count$(4, 3, 1)$. But not when I don't know how many cycles of $2$ and $1$ I want.
Note:
I'm looking for a way to derive a formula or something. I need to use it to prove by induction, I'm assuming, $r(n + 1) = r(n) + n · r(n − 1)$.
 A: These permutations are involutions, meaning $\sigma \in S_n$ with $\sigma^2=1$.
Online Encyclopedia of Integer Sequences A000085.  This was one of the first sequences in that library.

1, 1, 2, 4, 10, 26, 76, 232, 764, 2620, 9496



*

*a(n) is also the number of matchings in the complete graph K(n).

*a(n) is the number of partitions of a set of n distinguishable elements into sets of size 1 and 2. 

*a(n) = number of nonnegative lattice paths of upsteps U = (1,1) and downsteps D = (1,-1) that start at the origin and end on the vertical line x = n in which each downstep (if any) is marked with an integer between 1 and the height of its initial vertex above the x-axis. For example, with the required integer immediately preceding each downstep, a(3) = 4 counts UUU, UU1D, UU2D, U1DU.

*a(n) = a(n-1) + (n-1)*a(n-2)

A: I will show why indeed $a_n = (n-1)a_{n-2}+a_{n-1}$ is the right reccurence relation. Clearly $a_1 = 1$. Let $S_n$ be a set of all permutations $\pi$ of length $n$. The number of permutations of length $n$ with cycles only of lengths $1$ and $2$ we will denote $a_n$.
Next, all permutations from $S_{n+1}$ can be constructed by following procedure from $\pi\in S_n$, where
$$\pi = \begin{pmatrix}
1 & 2 & 3 & \cdots & n \\ 
\pi(1) & \pi(2) & \pi(3) & \cdots & \pi(n)
\end{pmatrix}.$$
By adding 1-cycle $\pi(n+1)=n+1$ one gets $\pi^*\in S_{n+1}$
$$\pi^* = \begin{pmatrix}
1 & 2 & 3 & \cdots & n & n+1 \\ 
\pi(1) & \pi(2) & \pi(3) & \cdots & \pi(n) & n+1
\end{pmatrix}$$
and then by $n$ inversions to get another $S_{n+1}$ permutations of the form
$$\pi^*_i = \begin{pmatrix}
1 & 2 & 3 & \cdots & i & \cdots & n & n+1 \\ 
\pi(1) & \pi(2) & n+1 & \cdots & n & \cdots & \pi(n) & \pi(i)
\end{pmatrix}$$
In total $n!+nn!=(n+1)!$ distinct permutations. Now, it is clear that all permutations $\pi^*$ constructed by just adding $\pi(n+1)=n+1$ satisfy the original statement. The number of them is $a_n$. Next, since the inversion will increase the cycle length which has been swapped with by one (see this answer of mine), only those permutations $\pi^*_i$ with swap 1-cycle by 1-cycle are allowed. To know the number of these, we first define $\Pi_n$ a set of allowed permutations of length $n$, i.e. those with only 1-cycles and 2-cycles (there are $a_n$ of elements of this set), $j(\pi)$ as a number of 1-cycles in a given permutation. With this one can easily obtain the desired reccurence (will be added later in this proof)

GENERATING FUNCTION
Consider the sum
$$S_n(x,y) = \sum_{\pi\in\Pi_n}x^{j(\pi)}y^{n-j(\pi)}$$
This may look strange and meaningless, but you will see it is indeed fruitful. The reason is that
$$S_n(1,1)=a_n.$$
Trivially, we have $S_1(x,y)=x$. Let us express $S_{n+1}(x,y)$, since we can decompose it as permutations $\pi\in\Pi_n$ with added $\pi(n+1)=n+1$ (i.e. $\pi^*$, clearly $j(\pi^*)=j(\pi)+1$) and one with inversions such that they invert $\pi(n+1)=n+1$ with only 1-cycles (there are exactly $j(\pi)$ disting inversion to make an allowed permutation, similarly $j(\pi^*_i)=j(\pi)$), therefore
$$S_{n+1}(x,y) = \sum_{\pi\in\Pi_{n+1}}x^{j(\pi)}y^{n+1-j(\pi)}= \sum_{\pi\in\Pi_n}x^{j(\pi^*)}y^{n+1-j(\pi^*)} + \sum_{\pi\in\Pi_n}j(\pi)x^{j(\pi^*_i)}y^{n+1-j(\pi^*_i)}=
\sum_{\pi\in\Pi_n}x^{j(\pi)+1}y^{n-j(\pi)} + \sum_{\pi\in\Pi_n}j(\pi)x^{j(\pi)}y^{n+1-j(\pi)}= \left(x+y\frac{\partial}{\partial x}\right)S_n(x,y)
$$
By $S_1(x,y)=x$ we can therefore in the spirit of the previous relation define $S_0(x,y)=1$. We define a generation function
$$G(x,y,t)=\sum_{n=0}^\infty t^n S_n(x,y),$$
therefore
$$\frac{1}{t}(G(x,y,t)-1) = \left(x+y\frac{\partial}{\partial x}\right)G(x,y,t)
$$
According to wolphram alpha and by imposing $G(0,y,t)=1$ we get
$$G(x,y,t) = \exp\left(\frac{2x-tx^2}{2t y}\right) + \frac{1}{t}\sqrt{\frac{\pi y}{2}}\exp\left(-\frac{(tx-1)^2}{2t^2 y}\right)\left[\operatorname{erfi}\left(\frac{1-tx}{t\sqrt{2y}}\right)-\operatorname{erfi}\left(\frac{1}{t\sqrt{2y}}\right)\right]$$
Since $G(1,1,t) = \sum_{n=1}^\infty a_n t^n$ with $a_0=1$ we get
$$\sum_{n=0}^\infty a_n t^n = \exp\left(\frac{2-t}{2t}\right) + \frac{1}{t}\sqrt{\frac{\pi}{2}}\exp\left(-\frac{(t-1)^2}{2t^2 }\right)\left[\operatorname{erfi}\left(\frac{1-t}{t\sqrt{2}}\right)-\operatorname{erfi}\left(\frac{1}{t\sqrt{2}}\right)\right]$$
