Counting Surjections
How many ways can you distribute distinct letters into distinct mailboxes? Every mailbox must receive at least one letter. This is the question of counting surjections. Surjections are functions from an -element set onto a -element set. It is one of the cleanest nontrivial applications of the Principle of Inclusion-Exclusion. Naively counting functions is trivial ( of them). But insisting that every element of the codomain be hit introduces dependencies. These dependencies make direct counting hard. Inclusion-exclusion resolves this by systematically correcting for the functions that miss one or more codomain elements. This problem matters far beyond its cute mailbox phrasing. Surjection counts are the combinatorial engine behind the Stirling Numbers of the Second Kind. These numbers count set partitions. Surjection counts also lie behind the general classification of counting problems known as the Twelvefold Way. This organizes every basic way of counting functions between finite sets. It does so by whether domain/codomain elements are distinguishable. It also depends on whether the function must be injective, surjective, or arbitrary. Surjection counts also appear whenever one counts multi-set orderings, exponential generating functions, or the number of ways a group of people can be split into labeled nonempty teams. The big picture is this: surjections onto a -set are exactly the functions from elements to elements. They avoid missing any of the targets. And "avoiding a bad event for each target" is precisely what inclusion-exclusion is built to compute. We phrase "target is missed" as a property. We count functions violating any chosen subset of these properties. This is easy, since they land in a smaller codomain. We then alternately add and subtract over all subsets. This article gives the precise definition of a surjection. It builds intuition through a small worked case. It states and completely proves the closed-form counting theorem. It works two nontrivial numerical examples. It shows exactly what breaks if a hypothesis of the theorem is dropped. It closes by connecting the surjection count formula to Stirling numbers. It also connects to real applications in combinatorics, probability, and computer science.
- A surjection (onto function) hits every element of the codomain at least once.
- Counting surjections directly is hard because "every element is hit" is a global condition coupling all targets.
- Inclusion-Exclusion turns this into a sum over subsets of "missed targets," each easy to count.
- The result is the explicit alternating sum .
- Dividing by gives , the Stirling Numbers of the Second Kind, counting partitions into unlabeled blocks.
- Key idea: count all functions, then alternately subtract and add back functions missing each possible subset of codomain elements.
Where this lives today
The surjection-counting formula and its Stirling-number cousin appear throughout discrete mathematics and its applications. In enumerative combinatorics, they are the seed computation for the Twelvefold Way, the table organizing all twelve basic ways of counting functions between finite sets by distinguishability and injectivity/surjectivity constraints. In probability and statistics, the same alternating sum computes the probability that a random function (e.g., a hash function, or a random assignment of balls to bins) covers every bin at least once — this is the classical coupon collector and balls-into-bins covering problem, central to the analysis of hashing schemes and load balancing in computer science. In algebraic combinatorics, surjection counts are the coefficients arising from the exponential generating function identity
a fact provable independently via the exponential formula for labeled structures, and this connects surjection counting to the broader machinery of generating functions used throughout combinatorial species theory. In cryptography and coding theory, surjectivity of key-derivation or scrambling functions onto their output space is a design desideratum, and counting/estimating the number of such surjective maps informs security-margin arguments. In statistical physics, the same alternating-sum technique (inclusion-exclusion over "missed states") appears in computing partition functions and occupation-number statistics for distinguishable particles filling distinguishable states with no state left empty.
Ask the tutor: How does the surjection-counting formula generalize when we require each of the target "boxes" to receive at least elements instead of just at least ?
Let be positive integers, let , and let . A function is a surjection (or is onto) if for every there exists with . Equivalently, , i.e. the image of is the entire codomain. The surjection number is the number of surjections from to :
By convention (the empty function is a surjection between empty sets) and for , and for .
Intuition
Think of as labeled balls and as labeled boxes. A function is any assignment of each ball to a box, with no restriction — there are such assignments since each of the balls independently picks one of boxes. A surjection is an assignment in which no box is left empty. The difficulty is that "box is nonempty" and "box is nonempty" are not independent events, and there is no simple product rule for "all boxes nonempty" the way there is for "all balls placed somewhere." Instead, define for each the "bad" property : the property that a function misses target , meaning . A function is a surjection exactly when it has none of the bad properties . Inclusion-exclusion computes the size of the complement of inside the set of all functions. The key computational fact is that the "bad" events are easy to handle in combination: the number of functions avoiding a fixed set of targets entirely (i.e. landing inside ) is simply , because each ball now has only legal boxes. Inclusion-exclusion assembles the surjection count from these easy quantities.
Central theorem
Let and be positive integers. The number of surjections from an -element set onto a -element set is
We use the Principle of Inclusion-Exclusion applied to the set of all functions , .
Setup. For each , define
the set of functions that fail to hit target . A function is a surjection if and only if it lies in none of ; equivalently,
Hence
Computing intersections. Fix a nonempty subset with . We claim
Indeed, means misses every target in simultaneously, i.e. . Since , such an is precisely an arbitrary function , and there are exactly of these (each of the domain elements independently chooses one of the remaining targets). This proves the claim. Note the value depends only on , not on which particular targets are in — a symmetry that will let us collapse the inclusion-exclusion sum into a sum over rather than over all subsets.
Applying inclusion-exclusion. The Principle of Inclusion-Exclusion states that for any finite sets ,
Substituting and grouping subsets by their size — there are subsets of size — gives
Conclusion. Substituting into :
The term in the sum equals , which matches the standalone term above exactly. Hence
as claimed.
∎For all ,
where denotes the number of ways to partition an -element set into exactly nonempty, unlabeled blocks (the Stirling Numbers of the Second Kind).
Fix a partition of into nonempty unlabeled blocks . Assigning distinct labels from to the blocks of turns it into a surjection by setting whenever : every block is nonempty so every label is used, giving a surjection. There are exactly ways to label the blocks of (a bijection from blocks to ), and distinct labelings of the same partition give distinct surjections since the underlying fiber structure (and hence the induced partition) differs only when a different bijection is used, while conversely any two labelings of different partitions give different surjections since the fibers of a surjection always form an (ordered) partition of into nonempty blocks. This exhibits a -to- correspondence: the map "surjection partition of into blocks , unlabeled" sends exactly surjections (one for each relabeling) to each partition into nonempty blocks, and every such partition arises this way. Hence the surjections are partitioned into fibers of size indexed by the partitions counted by , giving
Worked examples
Example 1: balls onto labeled boxes (asymmetric, nontrivial size). We compute .
Summing: . So . As a sanity check via the lemma, , which matches the standard tabulated value of the Stirling number . As a second sanity check, the total number of functions is ; the non-surjective ones (missing box 1, or box 2, or box 3) number , and indeed by inclusion-exclusion on the complement, consistent with the computation above.
Example 2: , — surjections onto a same-size codomain are exactly bijections. Compute :
Sum: . This matches the independent fact that a surjection from an -set to an -set must also be injective (a surjective self-map of a finite set is automatically a bijection, since equal finite cardinalities of domain and image force injectivity by the pigeonhole principle), so . Here , confirming the alternating sum computation exactly.
The theorem's proof relies essentially on and being finite — in particular, on the fact that is a finite number, and on the finite Principle of Inclusion-Exclusion, which is proved by induction on the (finite) number of sets . Consider dropping finiteness of the domain: let (a countably infinite domain) and . The formula would formally suggest computing with interpreted as a cardinal exponent, but for infinite is now a cardinal-arithmetic expression, and is not a meaningful subtraction in cardinal arithmetic — cardinal subtraction is not well-defined in general, since cardinal arithmetic has no additive inverses once we leave the finite realm. Concretely, there certainly do exist surjections (e.g. send evens to and odds to ), and in fact there are of them — but there is no way to recover this count by naively plugging into the alternating sum, because the derivation used , a subtraction that is only licensed when all quantities involved are finite (so that "" is ordinary integer subtraction, not ill-defined cardinal subtraction). This shows the finiteness hypothesis on (and ) is not a technicality: without it, the subtraction step in the proof of inclusion-exclusion has no meaning, and the closed form simply fails to apply.
Scope, edge cases & common traps
| Situation | What happens | Why |
|---|---|---|
| Cannot hit distinct targets using fewer than domain elements (pigeonhole) | ||
| Surjective self-map of a finite set is automatically bijective | ||
| The empty function has empty image, cannot hit any nonempty codomain | ||
| No functions at all exist into the empty set from a nonempty domain | ||
| very large relative to formula's summation bound | Sum still runs | The term is always for , contributing nothing but included for symmetry with inclusion-exclusion's derivation |
| Confusing with | Off by factor | Surjections are labeled-codomain counts; Stirling numbers count unlabeled partitions |
The mathematician behind it
Origins · 1 minJames Joseph Sylvester and how this was discovered
Sylvester was a prolific English mathematician who, alongside Arthur Cayley, developed much of nineteenth-century invariant theory, and who coined a striking number of mathematical terms still in daily use — "matrix," "discriminant," and "graph" (in the modern sense) among them. Sylvester's combinatorial work, including deep studies of partitions and of what we would now recognize as inclusion-exclusion-style counting arguments, fed directly into the systematic treatment of set partitions and surjective counting that later crystallized as the theory of Stirling numbers and inclusion-exclusion enumeration.
Why can't we just count surjections directly without inclusion-exclusion?
There is no simple product rule for "every box gets at least one ball" the way there is for "every ball goes in some box." If we try to place balls one at a time while tracking which boxes are still empty, the number of legal choices for each ball depends on the entire history of previous placements, so a naive product formula does not exist. Inclusion-exclusion sidesteps this by first overcounting (via all functions), then systematically correcting for the functions that miss one or more specific boxes, using only quantities — — that are simple products. This is precisely why the technique from Inclusion-Exclusion is the natural tool here rather than a direct combinatorial product argument.
Is ever negative, and should I worry the alternating sum could give a negative number?
No — despite the alternating signs, is always a nonnegative integer, since it counts an actual finite set of functions. The alternating sum is guaranteed by the proof (via inclusion-exclusion) to evaluate to this nonnegative count; the individual signed terms can be large, but they always combine to the correct nonnegative total. If a computation yields a negative or non-integer number, that is a sure sign of an arithmetic error, not a valid alternative answer.
How does this connect to the Twelvefold Way?
The Twelvefold Way organizes the twelve basic counting problems for functions by two binary choices: whether elements of are distinguishable or not, whether elements of are distinguishable or not, and whether the function is arbitrary, injective, or surjective. Counting surjections with both sets labeled (distinguishable) is exactly the entry computed by in this article. Dividing by moves to the entry where the codomain is treated as unlabeled — giving the Stirling number — and further collapsing the domain labels too changes the problem into counting partitions of the integer into parts, a different entry entirely.
What is the exponential generating function for surjections, and where does it come from?
The EGF is . This follows from the "exponential formula" in combinatorial species theory: a surjection onto corresponds to partitioning into labeled nonempty blocks, each block being an arbitrary nonempty subset (EGF per block, since counts all subsets including the empty one). Raising to the th power accounts for the distinguishable blocks. Extracting the coefficient of and multiplying by reproduces the same alternating sum as the inclusion-exclusion derivation, giving an independent check on the formula.
Does the formula work when ?
Yes. When , every function automatically sends every element to the unique target, so it is automatically surjective (for ), giving . Checking the formula: for , matching perfectly. This also confirms the formula correctly handles the boundary convention for .
Can this formula be extended to count surjections with prescribed minimum fiber sizes, e.g. onto k boxes each with at least 2 balls?
Yes, this is a natural generalization solved by the same inclusion-exclusion technique combined with generating functions: instead of requiring each fiber nonempty, require each fiber to have size in some prescribed set, and use the EGF of that constraint raised to the th power, then extract coefficients. The basic surjection formula is the special case where the constraint is simply "nonempty," i.e., fiber size . This generalization is a standard bridge topic connecting surjection-counting to the broader theory of set partitions with block-size restrictions.
Why is intuitively, beyond the algebraic verification?
A function between two finite sets of the same size is surjective if and only if it is injective if and only if it is bijective — these three conditions coincide exactly when domain and codomain have equal finite cardinality. This is a direct consequence of the pigeonhole principle: if is surjective but not injective, two domain elements would share an image, forcing some other target to be missed by counting, a contradiction with total images needed to cover targets using only domain elements with a repeat. Hence surjections from to are exactly the permutations of , of which there are .