Stirling numbers and set partitions
Count partitions of distinct labels by block number, using a structural recurrence and an exponential generating function.
On this page
The question for today
What you’ll understand
- Derive the recurrence for Stirling numbers of the second kind.
- Relate surjections to partitions without ordering the blocks.
- Extract fixed-block counts from their exponential generating function.
Five named instruments can be grouped into shared workstations in several ways, even when the workstations themselves have no names. Their group sizes do not determine who shares a station. Stirling numbers of the second kind count precisely this membership information. They connect a simple insertion recurrence to surjections and the exponential formula. The first kind of Stirling number counts a different object, permutations by cycles; specifying the kind prevents a name from concealing a change of problem.
Insert the largest label
Write for the number of ways to partition a specified n-element set into k nonempty unordered blocks. The empty set has one partition into zero blocks, while a nonempty set has none into zero blocks. More blocks than elements are impossible. To insert the largest label into a partition, distinguish whether it forms a singleton. If it does, delete that block and count a partition of the remaining labels into k minus one blocks.
If the largest label is not a singleton, remove it from its block. A partition into k nonempty blocks remains, and there are exactly k choices of the block to which the label can be restored. The blocks are not ordered, but they are distinct subsets of labels, so choosing one is meaningful. The two insertion cases are disjoint and reversible. This reasoning supplies a recurrence and its boundary conditions together, avoiding an unjustified division by a block-ordering factor at every stage.
Labeling the blocks produces a surjection
A surjection from an n-element label set to k named boxes produces a set partition by its nonempty fibers. Conversely, a partition into k blocks produces k factorial surjections by assigning the k box names bijectively to its blocks. Every fiber of this forgetting map has the same size because the blocks are nonempty, disjoint, and distinguishable as subsets. The constant-fiber argument explains exactly why dividing the number of surjections by k factorial works here.
Inclusion-exclusion counts surjections by removing functions that miss one or more named boxes. Start from all k-to-the-n functions and alternate over the sets of forbidden boxes. With positive n, the resulting expression also handles cases where too many boxes are requested. The formula is useful for a fixed small block count, but alternating large terms may be a poor floating-point computation. The positive-term recurrence is usually safer for building a complete table with exact integer arithmetic.
Fixing the number of components
A single nonempty block on a prescribed label set has EGF . Exactly k unordered blocks therefore have EGF . This is a formal coefficient identity over the rationals: the zero constant term makes every coefficient calculation finite. Summing over k gives the Bell-number EGF, since the block count of a partition of n cannot exceed n. Ordinary integer partitions do not obey this formula because they record sizes without distributing distinct labels.
One can also use set partitions to sort all functions into cases according to the number of distinct image values. First partition the domain into nonempty fibers, then assign distinct codomain values to those blocks. The number of injective assignments is a falling factorial. This gives an identity expressing ordinary powers in the falling-factorial basis. It is another counting proof, not merely a change of polynomial notation, and it makes the distinction between unlabeled blocks and labeled image values visible.
Work it through
Five labels in three workstations
Compute S(5,3) using insertion, and check it using named boxes.
The boundary values give S(3,1)=1, S(3,2)=3, and S(3,3)=1. Inserting the fourth label gives S(4,2)=2×3+1=7 and S(4,3)=3×1+3=6.
Insert the fifth label: S(5,3)=3S(4,3)+S(4,2)=18+7=25. The terms distinguish a nonsingleton fifth label from a singleton fifth label.
For three named boxes, inclusion-exclusion gives 3⁵−3·2⁵+3·1⁵=243−96+3=150 surjections. Each partition has 3!=6 assignments of names, so 150/6=25.
There are twenty-five partitions. Named-box assignments give six representations of each unordered grouping, rather than a new partition.
The living notebook
Explore the model
Interactive lab · probability
Two nonempty blocks from assignments
Pause and Reset, then set p=0.5 and n=6. Predict the total mass away from zero and six successes. Multiplying by 64 counts assignments that use both named boxes; divide this count by two to forget their names.
- Mean · np
- 5
- Variance · np(1−p)
- 2.5
- Total probability
- 1
These are exact binomial probabilities for independent trials with one fixed success probability. Playback tours the distribution; it does not resample outcomes. Parameter changes pause playback and recompute the model at the current elapsed time. Reset restores both parameters and returns time to zero. Playback starts paused. Adjust either parameter, then choose Play or Step.
Look forThe interior mass is 62/64, giving sixty-two surjections and S(6,2)=31. The lab shows only the two-box assignment distribution; the partition count requires the separate constant-fiber argument.
Pause & reason
Check your understanding
Your turn
Try it yourself
Count partitions of a six-element set into two nonempty blocks, and give a reason the result is not 32.
Take a moment to make a prediction or work it out on paper. Open a hint when you need one.
Give me a hint
Begin with all assignments to two named boxes, then remove the assignments with an empty box.
Compare with the solution
There are 2⁶=64 assignments. The all-first and all-second assignments each leave one box empty, so sixty-two assignments are surjective. Each unordered two-block partition occurs twice after box names are assigned. Hence S(6,2)=62/2=31. Dividing all sixty-four assignments by two would include one extra object corresponding to an empty block, which the definition excludes.
Keep the idea
Take it with you
- Stirling numbers of the second kind count membership partitions of distinct labels.
- The insertion recurrence separates a new singleton from insertion into an existing block.
- Surjections count named blocks; division by k! forgets those names only after all blocks are nonempty.
Read further
Original sources and deeper explanations to follow at your own pace.
Optional progress, saved in this browser when available. Revisit any lesson at any time.
Lesson not marked complete.