I came across this question:
Given $n$ friends, each one can remain single or can be paired up with some other friend. Each friend can be paired only once. Find out the total number of ways in which friends can remain single or can be paired up.
The recursive solution for this problem is:
Let $f(n)$ be the number of ways $n$ people can remain single or pair up. Then, $$f(n) = f(n - 1) + (n - 1) * f(n - 2)$$
For the $n$-th person there are two choices:
- the $n$-th person remains single, we recur for $f(n - 1)$
- the $n$-th person pairs up with any of the remaining $n - 1$ persons. We get $(n - 1) * f(n - 2)$
Can anyone please elaborate this solution. Thanks!
Label the people as $1,...,n$.
If person $n$ pairs with someone, there are $n-1$ possibilities for the person who pairs with person $n$, and once that pair is specified, we have $n-2$ people left, with the same counting problem, but with $2$ less people, so for this case, the number of ways is $(n-1)f(n-2)$.
If person $n$ stays single, then we have $n-1$ people left, with the same counting problem, but with $1$ less person, so for this case, the number of ways is $f(n-1)$.
Hence we have the recursion $$f(n)=(n-1)f(n-2)+f(n-1)$$ for $n\ge 2$, together with the initial conditions $f(0)=f(1)=1$.
Notes: