# Principle of inclusion exclusion and counting surjective functions

Let $$f$$ be a function from $$A \rightarrow B$$.

$$|A| = 4, |B| = 3$$

The number of surjective functions by applying the principle of inclusion exclusion is given by: $$3^{4} - \binom{3}{1} 2^{4} + \binom{3}{2}1^{4}$$.

The rationale is that we begin with the set of all possible functions, and subtract off the functions with one element in the codomain that is not in the range, and add back the functions with two elements in the codomain not in the range.

However, I don't understand why we are adding back $$\binom{3}{2}1^{4}$$.

If $$\binom{3}{1}2^{4}$$ already include functions with two elements from the codomain not in the range, then wouldn't it be done already, as that's all the non-surjective functions?

Let the elements of $$B$$ be $$a$$, $$b$$, and $$c$$. For a set $$S\subseteq B$$, let $$f_S$$ be the number of functions whose range is a subset of $$S$$. The formula that you're suggesting can be expressed like this:
$$f_B-f_{\{a,b\}}-f_{\{a,c\}}-f_{\{b,c\}}.$$
To see the problem with this equation, consider the function that sends everything to $$a$$. That function is not surjective so we want it to make zero contribution to our counting. It is counted by the terms $$f_B, f_{\{a,b\}}$$, and $$f_{\{a,c\}}$$, so its total contribution to this formula is $$1-1-1+0=-1$$. We counted this function twice when we were getting rid of bad functions, so we need to add it back once to cancel out the negative contribution. The same holds for the function that sends everything to $$b$$ and the function that sends everything to $$c$$. Hence, the formula we want looks like
$$f_B-f_{\{a,b\}}-f_{\{a,c\}}-f_{\{b,c\}}+f_{\{a\}}+f_{\{b\}}+f_{\{c\}}.$$