core30 min studyLesson 226 in Mathematics

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

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 S(n,k)S(n,k) 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.

S(n,k)=kS(n−1,k)+S(n−1,k−1),S(0,0)=1,S(n,0)=0 (n>0)S(n,k)=kS(n-1,k)+S(n-1,k-1),\qquad S(0,0)=1,\quad S(n,0)=0\ (n>0)
For n≥1 and k≥1, the first case inserts the new label into an existing block and the second creates a singleton. Set S(n,k)=0 when k>n.↔ Scroll wide equations horizontally.

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.

k!S(n,k)=∑j=0k(−1)j(kj)(k−j)n(n≥1, k≥1)k!S(n,k)=\sum_{j=0}^{k}(-1)^j\binom kj(k-j)^n\qquad(n\ge1,\ k\ge1)
The right side counts functions that hit every named box. Dividing by k! then forgets the names of the nonempty fibers.↔ Scroll wide equations horizontally.

Fixing the number of components

A single nonempty block on a prescribed label set has EGF ex−1e^x-1. Exactly k unordered blocks therefore have EGF (ex−1)k/k!(e^x-1)^k/k!. 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.

∑n≥0S(n,k)xnn!=(ex−1)kk!,mn=∑k=0nS(n,k)m(m−1)⋯(m−k+1)\sum_{n\ge0}S(n,k)\frac{x^n}{n!}=\frac{(e^x-1)^k}{k!},\qquad m^n=\sum_{k=0}^{n}S(n,k)m(m-1)\cdots(m-k+1)
The second identity counts functions to an m-element set by the number k of nonempty fibers, with the empty product equal to one.↔ Scroll wide equations horizontally.

Five labels in three workstations

Compute S(5,3) using insertion, and check it using named boxes.

  1. 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.

  2. 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.

  3. 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.

S(5,3)=35−3⋅25+33!=25S(5,3)=\frac{3^5-3\cdot2^5+3}{3!}=25
Every named box must be used before division by the number of block-name assignments is valid.↔ Scroll wide equations horizontally.
The result

There are twenty-five partitions. Named-box assignments give six representations of each unordered grouping, rather than a new partition.

Explore the model

Interactive lab

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.

Two nonempty blocks from assignmentsThese are exact binomial probabilities for independent trials with one fixed success probability. Playback tours the distribution; it does not resample outcomes. Current readings: Mean · np: 5; Variance · np(1−p): 2.5; Total probability: 1. All parameters are adjustable with the labeled controls below.03581000.070.150.220.3Successes kP(X = k)k = 0 · P = 9.77e-4
Exact probabilityHighlighted outcomeMean np
Mean · np
5
Variance · np(1−p)
2.5
Total probability
1
t = 0 s

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.

Check your understanding

A surjection from six labels to three named boxes determines an unordered partition into three blocks. How many such surjections determine each partition?

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.

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.