The Twelvefold Way
How many ways are there to distribute balls into boxes? The honest answer is: it depends. Four separate questions must be answered first. Are the balls distinguishable from one another, or interchangeable? Are the boxes distinguishable, or interchangeable? Must every box receive at least one ball? Must every box receive at most one ball? Once these questions are answered, the counting problem becomes one of exactly twelve canonical types. Each type has a clean closed-form or generating-function answer. These answers are built from tools you already know. The tools are binomial coefficients, Stirling Numbers of the Second Kind, and Integer Partitions. This taxonomy matters for an important reason. It reorganizes a sprawling zoo of counting problems into a single table. The problems include functions, multisets, set partitions, partitions of integers, injections, and surjections. The table is , indexed by two binary choices and four constraint types. The binary choices are ball type and box type. The constraint types are none, injective, surjective, and bijective. Once you see the table, you stop re-deriving the same argument from scratch. This applies every time a new "how many ways" question appears in a different costume. The unifying language is that of functions . These map an -element domain to a -element codomain. Distinguishability of balls/boxes corresponds to a specific choice. We either count functions themselves or only their orbits. These orbits fall under a relevant symmetric group action. The name "Twelvefold Way" comes from Gian-Carlo Rota. He popularized this organizational scheme as a pedagogical device. The twelve cases are not twelve unrelated tricks. Instead, they are twelve consistent applications of one idea. That idea is functions from a set of size to a set of size . These functions are quotiented by the appropriate symmetries. This article states each of the twelve cases precisely. It proves the two hardest ones in full. These are surjections via inclusion–exclusion, and indistinguishable balls into indistinguishable boxes via partitions. The article also shows how the whole table serves as a launching pad. It leads toward Polya Enumeration. This generalizes "distinguishable vs. indistinguishable" to counting orbits. Such orbits form under an arbitrary group of symmetries. By the end, you will have a single mental table. You can consult it for any balls-into-boxes question. You will also have full proofs of the two entries requiring real combinatorial machinery. Worked examples will show you exactly how to translate a word problem into a cell of the table.
- Every balls-into-boxes counting problem is determined by two choices — are balls distinguishable? are boxes distinguishable? — crossed with a constraint: none, injective ( needed), surjective ( needed), or both (bijective, ).
- This gives a grid of exactly twelve cases (three constraint columns are collapsed into "none/injective/surjective" plus the bijective case shares columns), each with a known closed form.
- Distinguishable balls, distinguishable boxes, no constraint counts functions : answer .
- Indistinguishable balls, distinguishable boxes, no constraint counts weak compositions: answer , the Combinations with Repetition formula.
- The two genuinely hard cells — indistinguishable balls/distinguishable boxes with surjectivity, and indistinguishable balls/indistinguishable boxes — are governed respectively by Stirling Numbers of the Second Kind machinery and by Integer Partitions.
- Key idea: distinguishability of balls or boxes is exactly the statement that we count functions up to the action of a symmetric group on the domain or codomain — the seed of Polya Enumeration.
Where this lives today
The twelve-cell table is not merely pedagogical bookkeeping; each cell is the answer to recurring questions across mathematics and its applications. Counting functions between finite sets () underlies the size of function spaces in computer science, such as the number of possible hash outcomes or truth assignments. Counting injections () is the seed computation for the inclusion–exclusion method itself and appears whenever "no collisions allowed" matters, as in birthday-paradox-style analyses and in coding theory when assigning distinct codewords. Counting surjections and their Stirling-number cousin appear in the analysis of hash functions with guaranteed full coverage, in the theory of set partitions used in statistical physics (partitioning particles into indistinguishable energy levels, closely related to Bose–Einstein and Fermi–Dirac statistics, which correspond to different cells of exactly this table under the balls-are-particles, boxes-are-states dictionary), and in the combinatorics of restriction maps in algebra. Integer partitions, arising in the fully indistinguishable cells, are central to the representation theory of the symmetric group, to number-theoretic partition congruences, and to statistical mechanics partition functions (the name is not a coincidence: the same objects that count multisets of box-occupancies also index the irreducible representations of ). Finally, the entire table is the case, in a precise sense, of Polya Enumeration: instead of quotienting only by the full symmetric group or acting on one side, Pólya's theorem quotients by an arbitrary subgroup acting on colorings, recovering the Twelvefold Way's "indistinguishable" cases exactly when is the full symmetric group and recovering the "distinguishable" cases exactly when is trivial — the Twelvefold Way is the boundary case of a much richer group-theoretic enumeration theory.
Ask the tutor: How does the free group action argument in this proof generalize to Burnside's lemma and Pólya enumeration for non-symmetric groups?
Fix finite sets (the balls, ) and (the boxes, ). A distribution of balls into boxes is a function , where means ball is placed in box . The Twelvefold Way classifies the problem "count distributions of balls into boxes" along two independent axes:
Axis 1 (balls): balls are distinguishable (we count functions literally) or indistinguishable (we count functions up to precomposition by , i.e. we count only the multiset of box-occupancies, equivalently the orbit of under relabeling of ).
Axis 2 (boxes): boxes are distinguishable (functions counted literally) or indistinguishable (we count functions up to postcomposition by , i.e. up to relabeling of ).
Axis 3 (constraint): the function is unconstrained, or required to be injective (each box holds at most one ball; forces ), or required to be surjective (each box holds at least one ball; forces ), or required to be bijective (forces ). Crossing the two binary axes with three basic constraint types (none, injective, surjective — bijective is the intersection of injective and surjective and only nontrivial when ) yields twelve enumeration problems, tabulated below with the Stirling Numbers of the Second Kind and the number of partitions of into exactly positive parts.
| Balls | Boxes | Any | Injective | Surjective |
|---|---|---|---|---|
| distinguishable | distinguishable | |||
| indistinguishable | distinguishable | |||
| distinguishable | indistinguishable | (0 or 1) | ||
| indistinguishable | indistinguishable | (0 or 1) |
Here denotes the indicator (value if , else ); an injective function's image has size , and up to relabeling boxes there is only one way to have occupied indistinguishable boxes out of , provided .
Intuition
The table is best understood as a single idea applied four times. Start from the most refined object: an actual function , drawn as a bipartite diagram with an arrow from each ball to its box. Distinguishable balls and distinguishable boxes means we count these diagrams literally — two diagrams differing by which ball goes where are different, full stop. Making the boxes indistinguishable means we glue together all diagrams that agree after relabeling the boxes; formally we quotient by the action of on the codomain. What survives is the data of which balls end up together, i.e. a set partition of (if surjective) or a partial set partition (if not every box is used). Making the balls indistinguishable as well collapses even the identity of which ball is which, and what survives is only the sequence of box sizes — a partition of the integer .
The two "hard" cells of the table — surjections onto indistinguishable boxes, and the fully indistinguishable case — are exactly the cells that require Integer Partitions and Stirling Numbers of the Second Kind, because those are precisely the combinatorial objects that classify orbits of surjections under relabeling.
Let be positive integers, a set with , a set with . Let denote the number of partitions of the set into exactly nonempty, unordered, unlabeled blocks (the Stirling number of the second kind). Then: 1. The number of surjective functions (distinguishable balls, distinguishable boxes) equals . 2. Consequently, the number of surjections up to relabeling the codomain (distinguishable balls, indistinguishable boxes, surjective) equals . 3. Consequently, the total number of functions up to relabeling (distinguishable balls, indistinguishable boxes, unconstrained) equals . Moreover satisfies the closed form
Technique: inclusion–exclusion, then orbit-counting. Part 1, closed form for surjections. Let be the set of surjective functions and let be the set of all functions, so since each of the elements of independently has choices of image. For each box let be the set of functions that miss , i.e. . A function is surjective exactly when it lies in none of the , so
By inclusion–exclusion, for any subset with , the functions avoiding all boxes in are exactly the functions , of which there are (this includes , giving for ). Since for ranging over are indexed by a -element set, inclusion–exclusion gives
Therefore
where the term is , matching the definition consistently. Call this quantity ; we have shown
Bijection between surjections and (ordered set partitions). We now show directly by a counting bijection, which simultaneously proves the displayed formula for . Given a surjection , its fibers for form a partition of into exactly nonempty blocks (nonempty because is surjective; they partition because every element of has exactly one image; there are exactly of them since distinct give disjoint fibers, and each is nonempty by surjectivity, so none collapse). Conversely, given an unordered set partition of into nonempty blocks , choosing a bijection from the block index set to — equivalently, choosing which block maps to which element of — produces a surjection , and every surjection arises this way from exactly one set partition together with exactly one such labeling. Formally: define a map
by where is the partition into fibers of and sends the block to . This is well defined since surjective guarantees has exactly blocks and is a well-defined bijection (fibers of a function are pairwise disjoint by definition of function, and distinct boxes give distinct fibers since a block determines as the common value of on it). is injective: if then have the same fibers assigned to the same boxes, so for every (since lies in a unique fiber, and that fiber is sent to the same box by both). Hence . is surjective onto the target set: given with a partition of into nonempty blocks and a bijection from blocks to , define where . This is well defined (every lies in exactly one block, since is a partition), and by construction, and is surjective since is a bijection onto so every is for some , hence is hit. Thus is a bijection, so
since there are partitions of into exactly nonempty blocks by definition of the Stirling number, and for each such there are exactly bijections from its (exactly ) blocks to . Combining with the inclusion–exclusion computation, , which after dividing by is exactly the claimed closed form for . This proves the closed form and Part 1. Part 2. An unordered set partition of into nonempty blocks is precisely an equivalence class of surjections under the right action of (postcomposition), because two surjections have the same fibers if and only if one is obtained from the other by relabeling the codomain via some permutation of , and this action is free on : if satisfies for a surjective , then for every we get , so fixes every element of (using surjectivity), forcing . A free action of a group partitions a set into orbits each of size , so the number of orbits is . Each orbit is exactly the set of surjections sharing a common fiber-partition, i.e. each orbit corresponds to exactly one set partition into blocks; this recovers Part 2, that counting surjections with indistinguishable boxes gives . Part 3. A function (not necessarily surjective), up to relabeling , is determined exactly by its set of nonempty fibers, which is a partition of into some number of nonempty blocks with (as long as , ; if then trivially, contributing ). By the orbit-counting argument of Part 2 applied at each fixed number of used boxes (choosing which of the indistinguishable boxes are used is not even a separate choice, since the boxes are indistinguishable — only the count of nonempty fibers matters, not which labels), the number of such orbits with exactly nonempty fibers is . Summing over the possible number of nonempty fibers gives
as the total count of functions up to relabeling .
∎The number of surjections up to simultaneously relabeling both and (indistinguishable balls, indistinguishable boxes, surjective) equals , the number of partitions of the integer into exactly positive parts. Consequently the unconstrained count (indistinguishable balls, indistinguishable boxes, no constraint) is , the number of partitions of into at most parts.
Technique: further quotient of the Stirling result by relabeling the domain. By the Theorem above (Part 2), surjections up to relabeling alone correspond bijectively to set partitions of into exactly nonempty blocks. Now further quotient by relabeling as well, i.e. by the action of on such set partitions by relabeling elements. Two set partitions of into blocks lie in the same -orbit if and only if they have the same multiset of block sizes: given two partitions with the same sorted list of block sizes , one can biject blocks of matching sizes and extend to a permutation of carrying one partition to the other (concretely, list the elements of each block in an arbitrary fixed order and match position-by-position across blocks of equal size in decreasing size order); conversely any permutation of applied to a partition preserves the multiset of its block sizes, since permuting elements does not change how many elements land in each block, only which labels do. Hence the orbits of acting on the set of partitions of into exactly nonempty blocks are in bijection with multisets satisfying — that is, with partitions of the integer into exactly positive parts. By definition, the number of these is . This proves the surjective case. For the unconstrained case, as in Part 3 of the theorem above, a function up to relabeling both and is determined by the sizes of its nonempty fibers only (not even which underlying elements are involved), giving a partition of into at most positive parts, since the number of nonempty fibers can be any with . Summing over gives the stated total, where .
∎Worked examples
Example 1 (asymmetric, all three lower rows in play). A caterer has distinct desserts (a cheesecake, a tiramisu, a flan, a brownie, a sorbet, a macaron, a pavlova) and must place them onto indistinguishable dessert trays for a buffet, where any tray may hold any number of desserts including zero, but every tray used must be visually distinct in content only insofar as which desserts sit on it — i.e. we only care which desserts end up together, not tray labels, and empty trays are allowed to sit unused on the table. This is the cell: distinguishable balls, indistinguishable boxes, unconstrained (, ). By Part 3 of the theorem,
We compute the needed Stirling numbers of the second kind via the recurrence , with , for , for . Building up to : . . . . . . . . . . So
There are 715 ways to distribute the distinct desserts onto indistinguishable trays (allowing empty trays). As a sanity check, note ... rather, we simply trust the direct recurrence computation; a second sanity check is that this must be less than the distinguishable-boxes unconstrained count divided roughly by symmetries, which is plausible in order of magnitude (, consistent with substantial but partial symmetrization since boxes are frequently unevenly filled, so the naive division by is not exact but close).
Example 2 (surjective, indistinguishable balls, distinguishable boxes — a genuinely different cell). A vending-machine warehouse must ship identical bags of rice to distinguishable stores (Store A, B, C, D), and every store must receive at least one bag (no store may end up empty), but bags within a shipment are not tracked individually. This is the cell: indistinguishable balls, distinguishable boxes, surjective. By the classical stars-and-bars-with-positivity argument (itself the base case underlying Combinations with Repetition reasoning), the count of positive integer solutions to with each equals
We verify this by the standard substitution with , turning the equation into with , whose nonnegative solution count is . Computing, . So there are 84 ways to ship the bags so that every store gets at least one, matching the table entry directly with . To cross-check against the general table, note that this cell's formula is derived independently of Stirling numbers (it is pure stars-and-bars with a positivity shift), which is exactly why the "indistinguishable balls" row is combinatorially easier than the "distinguishable balls" row — occupancy vectors are the entire story once ball identity is erased, with no further symmetrization needed since the boxes remain distinguishable.
Consider balls and boxes, and the naive claim: "the number of surjections with indistinguishable boxes can be computed by first counting surjections with distinguishable boxes, and then always dividing by ." This is false in general, and already breaks it in a way worth seeing explicitly, though for a cleaner break take . With distinguishable balls and distinguishable boxes, the number of surjections is . If one incorrectly assumed dividing by always gives the indistinguishable-box count, one would predict — and here it happens to be correct, since exactly. Now push further to see where the naive "always divide by " heuristic actually fails: take . Distinguishable-box surjections: (the two functions and ). Dividing naively by gives , which does match . The division-by- trick works precisely because acts freely on the set of surjections — this was proved as a specific step inside the Theorem's proof (Part 2), not assumed. It is exactly this freeness that fails once we drop surjectivity: consider but unconstrained functions. There are total functions : the two constant functions (both balls to box A; both to box B) and the two surjections above. Dividing by gives — but the true indistinguishable-box unconstrained count is , which happens to agree numerically here purely by coincidence of small numbers, so let us use to expose the real failure. For : total functions . Naive division by predicts indistinguishable-box classes. But the true count is — still matching! The reason these small cases keep "accidentally" matching is that has order , and the only non-free orbits are the two constant functions, each fixed pointwise by the nontrivial swap, contributing functions collapsing to classes instead of the "generic" functions collapsing to class — and for this off-by-one-per-constant-function exactly cancels in the arithmetic by luck of small . Take instead, still unconstrained, with : total functions ; naive division by gives a non-integer, — an immediate, undeniable failure. The correct count from Part 3 of the theorem is (using since you cannot partition a 2-element set into 3 nonempty blocks). The moral: the "divide by " shortcut is only valid on the locus where acts freely, which is exactly the surjections — dropping the surjectivity hypothesis (one hypothesis of Part 2 of the Theorem) breaks the free-action argument used in its proof, and the correct replacement is the sum-over- formula of Part 3, not naive division.
Scope, edge cases & common traps
| Situation | Correct handling |
|---|---|
| and injectivity required | Fine: count is (dist./dist.) or (indist. balls) — nonzero. |
| and surjectivity required | Impossible: by pigeonhole a function from an -set cannot hit targets, so every surjective-column entry is whenever ; formulas like automatically evaluate to since for . |
| The empty function exists uniquely when ; it is vacuously injective, and surjective iff too. Boundary values , for must be used, not "undefined." | |
| , | No function exists when ; every entry is . |
| Confusing "indistinguishable boxes, injective" with a nontrivial count | It is always or : if there is exactly one way (up to relabeling boxes, an injection just picks which of the box-slots are used, and since boxes are indistinguishable this "which slots" datum vanishes) — a frequent source of error is trying to apply a binomial coefficient here. |
| Reading "distinguishable" as a property of the count rather than of the objects | Distinguishability is a modeling choice about the balls/boxes themselves, made before any constraint is applied — see the Counterexample above for what breaks if this order is not respected. |
The mathematician behind it
Origins · 1 minGian-Carlo Rota and how this was discovered
Rota was an Italian-American mathematician and philosopher who spent most of his career at MIT, where he transformed enumerative and algebraic combinatorics from a collection of clever tricks into a subject organized around structural principles: incidence algebras, Möbius functions on posets, and umbral calculus. He was equally serious about phenomenology and the philosophy of mind, publishing in both mathematics and philosophy journals, and insisted that mathematical taxonomy — naming and organizing — was itself a creative act worthy of care. The "Twelvefold Way," presented in his lecture notes and later in the textbook Enumerative Combinatorics he co-authored pedagogical material around, is a signature example of his style: take a question every student already sort of knows the answer to, and reveal that the true content is a small, complete, and previously invisible table.
Why is it called the "Twelvefold Way" if the table above only shows a grid, which is twelve cells — isn't that just the same thing?
Yes — the name refers exactly to the fact that crossing two binary choices (balls distinguishable or not; boxes distinguishable or not) with three basic constraint types (no constraint, injective, surjective) yields genuinely distinct enumeration problems. The table's four rows in this article group two axes into one column of four labeled row-pairs for readability, but the count of essentially different problems is twelve; the fourth "constraint," bijective, is not independently counted because it only makes sense when , at which point it coincides with either the injective or surjective column (both equal in the distinguishable/distinguishable case when ), so it does not add a thirteenth genuinely new case.
Is there a slicker way to remember which cells use Stirling numbers versus integer partitions?
Yes: Stirling numbers of the second kind appear whenever balls are distinguishable but boxes are not (they count set partitions, which remember which specific balls are grouped together), while integer partitions appear whenever both balls and boxes are indistinguishable (only the sizes of the groups survive, not which elements form them). If boxes are distinguishable, you never need Stirling numbers or partitions at all — those cells reduce to binomial coefficients (Combinations with Repetition and its variants), because distinguishable boxes let you track an occupancy vector directly.
Does the injective column ever need Stirling numbers or partitions?
No. Injective functions have no repeated outputs, so there is no nontrivial "grouping" data to remember — an injection is already the simplest possible structure, an embedding of into . This is why the injective column of the table is uniformly simple: , , and two trivial indicator cases. Injectivity and the need for Stirling numbers/partitions are in tension, since those objects exist precisely to encode non-trivial groupings that injectivity forbids.
How does this table relate to occupancy problems in statistical mechanics, like Bose–Einstein statistics?
Directly: modeling particles among energy states as "balls into boxes," classical (Maxwell–Boltzmann) statistics treats particles as distinguishable, giving the or related distinguishable-ball cells; Bose–Einstein statistics treats particles as indistinguishable bosons with distinguishable states, landing in the indistinguishable-balls/distinguishable-boxes row governed by -type counts; and further constraints (like the Pauli exclusion principle for fermions, forcing at most one particle per state) correspond to the injective column, giving . The Twelvefold Way is thus a combinatorial skeleton underlying the three classical quantum statistics.
What goes wrong if I try to compute the indistinguishable-balls, indistinguishable-boxes, unconstrained cell by summing binomial coefficients instead of partition numbers?
You get the wrong answer because binomial coefficients count occupancy vectors distinguishing which box got how many balls, which is exactly the information erased once boxes become indistinguishable. Once boxes are indistinguishable, occupancy vectors that are rearrangements of each other (like and for boxes) must be identified, and the object that correctly accounts for this identification is an integer partition (an unordered list of positive part sizes), not a composition (an ordered list) counted by binomial coefficients.
Is the formula always guaranteed to be an integer, even though it has a in the denominator?
Yes, and this is a nontrivial fact guaranteed by the theorem's own proof: the sum was shown to equal , an actual count of functions, and separately shown to equal via the explicit bijection with (partition, bijection-to-) pairs. Since is by definition a count of set partitions (an integer), the division by is guaranteed to land on an integer — the apparent fraction is an artifact of the inclusion–exclusion derivation, not evidence of non-integrality.
Where does this table lead next, and why should I care about Pólya enumeration afterward?
The Twelvefold Way handles only two extreme symmetry regimes on each side: the trivial group (distinguishable) and the full symmetric group (indistinguishable). Many real problems — coloring the faces of a cube, counting distinct necklaces under rotation, counting molecules with a specific symmetry group — need to quotient by some intermediate group of symmetries, like a rotation group, which is neither trivial nor the full symmetric group. Polya Enumeration generalizes exactly this, replacing "distinguishable/indistinguishable" with an arbitrary permutation group acting on the balls or boxes, and the Twelvefold Way's easy cells become the two extreme special cases of Pólya's much more general cycle-index formula.