Number of surjective functions from a set with $m$ elements onto a set with $n$ elements

In general computing the number of surjections between finite sets is difficult.

Your procedure for obtaining the figure of $n! \cdot n^{m-n}$ is overcounting, and also erroneous for another reason.

  • It is overcounting beacuse you are specifying an ordered pair of functions (one bijective, one arbitrary) which piece together to form a surjection $A \to B$, but in general there are many ways of breaking up a surjection into a bijection and an arbitrary function.
  • It is additionally erroneous because part of your procedure involves 'the first $n$ elements' of $A$, which means you've picked a distinguished subset of $A$ of size $n$. There are $\binom{m}{n}$ ways of doing this, so your procedure should in fact yield $\binom{m}{n} \cdot n! \cdot n^{m-n}$. But it's still overcounting: it counts the number of ordered triples $(A',f,g)$, where $A' \subseteq A$ is a subset with $n$ elements, $f : A' \to B$ is a bijection and $g : A \setminus A' \to B$ is an arbitrary function.

Even computing the number of surjections $A \to B$ when $n(A)=m$ and $n(B)=3$ is pretty tricky. There are $3^m - 3 \cdot 2^m + 3$ of them (see here, for instance).

If I recall correctly, there is no known closed form expression for the number of surjections from a set of size $m$ to a set of size $n$.


You can write an expression using inclusion-exclusion. There are $n^m$ total functions from $A$ to $B$. Subtract off the ones that do not cover one element. There are $(n-1)^m$ that skip one particular element, so you would subtract $n(n-1)^m$ to remove the ones that skip some element. You have removed all the ones that skip two elements twice, so we need to add them back in. There are ${n \choose 2}(n-2)^m$ that skip two elements. Now we have removed the ones that skip three elements three times and added them back three times, so we need to subtract ${n \choose 3}(n-3)m$. The final expression is $$n^m+\sum_{i=1}^{n-1}(-1)^i{n \choose i}(n-i)^m$$


The number of surjections from a set of $m$ elements to a set of $n$ elements is $$n! \;S(m,n)$$ where $S(m,n)$ is a Stirling number of the second kind.