TITLE: Finding number of functions from a set to itself such that $f(f(x)) = x$
QUESTION [6 upvotes]: The questions states that $f: A\rightarrow A$ is a function which satisfies $f(f(x)) = x.$
We have to find the number of such functions with $A = \left\{1,2,3,4\right\}$.
The given condition clearly states that the function is the inverse of itself. Since the set $A$ was given to be so small, I first tried to think of some familiar functions that could fit. I came up with $f(x) = x$ and $f(x) = 5-x$. But the answer was nowhere near $2$.
I think there has to be a general method to find the no. of functions that satisfy a given condition instead of hit and trial and counting on fingers. But I'm not able to figure out what it is.

REPLY [2 votes]: This can me asked as a graph theory problem: how many graphs with vertices $\{a,b,c,d\}$ exist such that each vertex lies in exactly one cycle of length at most $2$? 
Case 1: All cycles are of length 1. This is equivilant to the identity function, which we know is unique. However from a graph theoretic point of view this is equal to $\binom{4}{4}$ because we are choosing 4 vertices to have cycle length one (notice that order does not matter because cycles start and end in the same place, and if two functions have the same cycles regardless of order they are equal). $\binom{4}{4} = 1$, consistent with what we know about the identity. 
Case 2: One cycle is of length 2, and 2 are of length one. We know that the two of length one are chosen by $\binom{4}{2}$ and the reaming two vertices are forces to be in the cycles of length two (or are 'chosen' by $\binom{2}{2}$). 
Case 3: 2 cycles of length 2, which is the same amount of choices as above, since the two remaining ones are forced to be the cycle. We have to divide by the number of ways there are to arrange 2 $2$-cycles, which is $2!$
Summing the amounts of functions from the three cases we get $10$.