Number of surjections between finite sets (what is wrong with my solution?)
I'm trying to find the number of surjective functions from $[n]:=\{1,\dots,n\}$ onto $[m]$ and I thought I had it solved correctly as $n!\cdot m^{n-m}$, but the other questions on MSE seem to suggest the actual answer involves partitions and Stirling numbers and so on. What exactly is wrong with my solution?
If $n<m$ there are no surjective functions. Let $n\geq m$. Each function $f:[n]\to[m]$ corresponds to a tuple $(y_1,\dots,y_n)$, where $y_i=f(i)$. Consider the set of tuples $S=\{(1,\dots,m,y_1,\dots,y_{n-m}):y_i\in[m]\}$. In order for $f$ to be surjective, its corresponding tuple $t$ must contain every element of $[m]$ at least once, therefore $t$ must be a permutation of some element of $S$. There are $m^{n-m}$ elements in $S$, therefore there are $n!\cdot m^{n-m}$ surjective functions from $[n]$ onto $[m]$.
The problem is that your method has a lot of double counting.
Let's look at $n=3,m=2$. You are saying there should be $2\cdot 3!=12$ sujective functions, each determined by starting with a list $(1,2,*)$, choosing $*$ to be either $1$ or $2$, and then permuting this list. Let us look at the functions this generates:
$\,$ | $* = 1$ | $* = 2$ |
---|---|---|
$(1,2,*)$ | $(1,2,1)$ | $\color{red}{(1,2,2)}$ |
$(2,1,*)$ | $(2,1,1)$ | $(2,1,2)$ |
$(1,*,2)$ | $(1,1,2)$ | $\color{red}{(1,2,2)}$ |
$(2,*,1)$ | $(2,1,1)$ | $(2,2,1)$ |
$(*,1,2)$ | $(1,1,2)$ | $(1,1,2)$ |
$(*,2,1)$ | $(1,2,1)$ | $(2,2,1)$ |
I have highlighted one instance of a function which is counted twice by your method, but there are many more.