TITLE: Friends pairing problem
QUESTION [6 upvotes]: 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!

REPLY [4 votes]: I guess, the main problem which I also faced was $(n-1)$*$f(n-2)$ part of the equation.
So instead consider the order $f(n-2)$*$(n-1)$.
This can be rewritten as $f(n-2)$ + $f(n-2)$ + $f(n-2)$ + $...$ $(n-1)$ times. This will be helpful afterwards
Now let's derive the recursive formula with following cases:

$n^{th}$ guy is single
$n^{th}$ guy gets paired up.

So the recursion will look something like this
$f(n)$ = $f(n-1)$ + $f(n-2)$
This says if the guy is single, the question remains the same for $(n-1)$ people or if the guy is paired up with anyone, question remain same for $(n-2)$ people (because 2 people are now let's say gone).
But heres a thing which I think is better understood with an example.
for $n = 4$ and $\{A,B,C,D\}$ as there names : 

guy D chose to be single, people left : ${A,B,C}$
guy D gets paired up.

Pairing options are : 
$(A)$, $(B,C)$ 
$(A,B)$,$(C)$
$(A,C)$, $(B)$
So we can say:
$f(n)$ = $f(n-1)$(if guy stays single) + $f(n-2)$(if first pair is formed) + $f(n-2)$(if second pair is formed) + $f(n-2)$(if third pair is formed).
So in essence, each pair contributes to the total, finally:
$f(n)$ = $f(n-1)$ + $f(n-2)$*$(n-1)$ 
$(n-1)$ = no. of ways $n$ can form a pair with $n-1$ people.