Discrete Mathematics and Combinatorics

Stirling Numbers and Ways to Split a Set

Learn what the two kinds of Stirling numbers count, how to build their tables and how they connect to Bell numbers and powers.

Key takeaways

  • S(n, k) counts partitions of n distinct items into k nonempty, unlabeled blocks.
  • It obeys S(n, k) = k·S(n − 1, k) + S(n − 1, k − 1), which builds the whole table by hand.
  • Multiplying S(n, k) by k! counts the ways to fill k labeled boxes so that none is empty.
  • Stirling numbers of the first kind count permutations of n items that have exactly k cycles.

Stirling numbers of the second kind count the ways to split a set of n distinct things into k groups, where no group is empty and the groups carry no labels. They are written S(n, k). For example, 4 people can be split into 2 unnamed teams in 7 ways, so S(4, 2) = 7.

There is also a first kind, which counts arrangements by the number of cycles they contain. The two families look quite different but turn out to undo each other.

Splitting four people

A partition of a set is a division into nonempty blocks that do not overlap and together contain everything. Take the set {a, b, c, d} and ask for exactly 2 blocks.

a | bcd      b | acd      c | abd      d | abc
ab | cd      ac | bd      ad | bc

There are 4 splits into sizes 1 and 3 and 3 splits into sizes 2 and 2, for a total of 7. The order of the blocks and the order within a block mean nothing, so ab | cd and cd | ab are one partition.

Some values are immediate. S(n, 1) = 1 and S(n, n) = 1 for n ≥ 1. By convention S(0, 0) = 1, and S(n, 0) = 0 for n ≥ 1.

Building the table

Think about where item number n goes. Either it forms a block by itself, in which case the other n − 1 items make k − 1 blocks, or it joins one of the k blocks already made from the other n − 1 items.

S(n, k) = S(n − 1, k − 1) + k × S(n − 1, k)

S(5, 3) = S(4, 2) + 3 × S(4, 3) = 7 + 3 × 6 = 25

The factor k reflects the k different blocks the newcomer could join. This is a recurrence with two indices, much like Pascal's rule with an extra multiplier.

n k = 1 k = 2 k = 3 k = 4 k = 5 k = 6
1 1
2 1 1
3 1 3 1
4 1 7 6 1
5 1 15 25 10 1
6 1 31 90 65 15 1
Where does the last item go?
1 Set item n aside Partition questions start with n − 1 items 2 Alone Others form k − 1 blocks: S(n−1, k−1) 3 Joins a block Others form k blocks: k × S(n−1, k) 4 Add The cases never overlap Set item n aside Partition questions start with n − 1 items 1 Alone Others form k − 1 blocks: S(n−1, k−1) 2 Joins a block Others form k blocks: k × S(n−1, k) 3 Add The cases never overlap 4

The two cases give the recurrence for S(n, k).

Patterns in the table

Two columns have simple formulas. S(n, 2) = 2ⁿ⁻¹ − 1, since a split into two blocks is settled by choosing the block containing item 1: take item 1 plus any subset of the other n − 1 items, except all of them. For n = 6 that gives 31.

S(n, n − 1) = C(n, 2), where C(n, 2) is the number of pairs. With n items in n − 1 blocks, exactly one block holds two items, and choosing that pair determines everything. For n = 6 this is 15.

Labeled boxes and onto functions

Suppose the blocks get names, such as 3 distinct projects shared among 5 students, with every project receiving at least one student. First partition the students into 3 blocks, then assign the blocks to projects in 3! ways.

3! × S(5, 3) = 6 × 25 = 150

In general the number of onto functions from n inputs to k outputs is k!·S(n, k). Counting those functions by inclusion-exclusion and dividing by k! gives an explicit formula:

S(n, k) = (1/k!) × sum over j = 0..k of (−1)ʲ C(k, j) (k − j)ⁿ

S(4, 2) = (16 − 2 × 1 + 0)/2 = 7

Compare this with identical objects in labeled boxes, which is a stars and bars problem. What changes the count is whether the objects, and the boxes, can be told apart.

Distinct objects? Labeled boxes? No box empty: count
Yes Yes k!·S(n, k)
Yes No S(n, k)
No Yes C(n − 1, k − 1)
No No partitions of the integer n into k parts

Bell numbers

Add across a row of the table to count all partitions of an n-element set, with any number of blocks. These are the Bell numbers: 1, 1, 2, 5, 15, 52, 203, starting from n = 0.

They have their own recurrence. In a partition of n + 1 items, look at the block containing the new item. Choose the k items that are not in that block and partition them freely:

B(n + 1) = sum over k = 0..n of C(n, k) × B(k)

B(4) = 1×1 + 3×1 + 3×2 + 1×5 = 15

Bell numbers agree with the Catalan numbers for a few terms and then grow much faster.

Fixed block sizes

Sometimes the sizes of the blocks are given in advance. To split 6 people into 3 unnamed pairs, choose members as if the pairs were named, then divide by the 3! orders of the pairs:

C(6,2) × C(4,2) × C(2,2) / 3! = 15 × 6 × 1 / 6 = 15

S(6, 3) = 90 is the total over all the possible size patterns: sizes 4, 1, 1 give 15 partitions, sizes 3, 2, 1 give 60, and sizes 2, 2, 2 give these 15. The sum is 90, which matches the table. Divide only among blocks of equal size, since blocks of different sizes can already be told apart.

Where Bell numbers appear

Any rule for sorting things into classes is a partition in disguise. An equivalence relation is a relation that is reflexive (each thing is related to itself), symmetric (it works both ways) and transitive (it passes along chains). Its classes form a partition, and every partition arises this way. So a 3-element set carries exactly B(3) = 5 equivalence relations.

Rhyme schemes are another example. A three-line verse can rhyme as aaa, aab, aba, abb or abc, where lines with the same letter rhyme. These are the 5 ways to partition the set of lines.

Factoring supplies a third. The number 30 = 2 × 3 × 5 can be written as a product of whole numbers above 1, ignoring order, in these ways: 30, 2 × 15, 3 × 10, 5 × 6 and 2 × 3 × 5. Each corresponds to grouping the three distinct primes, giving 5 again. The shortcut works only when no prime is repeated.

Turning powers into falling products

Define the falling factorial (x)ₖ = x(x − 1)(x − 2)...(x − k + 1), a product of k factors. Then every power can be rewritten as

xⁿ = sum over k of S(n, k) × (x)ₖ

x³ = x + 3x(x − 1) + x(x − 1)(x − 2)
check at x = 3:  3 + 18 + 6 = 27

The reason is a double count. For a whole number x, the left side counts all ways to paint n distinct items with x colors. On the right, first group the items that share a color into k blocks, then give the blocks different colors in (x)ₖ ways. Since both sides are polynomials that agree at every positive whole number, they are identical.

This rewriting is useful for sums, because falling factorials add up as neatly as powers integrate.

A sum of squares from Stirling numbers

Here is the idea at work. Falling factorials obey a clean summing rule: adding (x)ₖ for x = 0, 1, ..., N gives (N + 1)ₖ₊₁ / (k + 1).

Row 2 of the table says x² = (x)₂ + (x)₁. Apply the rule to each part:

0² + 1² + ... + N² = (N+1)(N)(N−1)/3 + (N+1)(N)/2

N = 4:  5×4×3/3 + 5×4/2 = 20 + 10 = 30
direct: 0 + 1 + 4 + 9 + 16 = 30

The same recipe handles cubes, fourth powers and beyond. Read the row of S(n, k), apply the summing rule term by term, and simplify if you wish. Related techniques appear in telescoping sums.

The first kind: counting cycles

Any rearrangement of n items breaks into cycles. If person 1 passes to 2, 2 to 3 and 3 back to 1, that is one cycle of length 3. The unsigned Stirling number of the first kind, often written c(n, k), counts rearrangements with exactly k cycles.

Its recurrence is c(n, k) = c(n − 1, k − 1) + (n − 1)·c(n − 1, k). Item n either forms its own cycle or is inserted just after one of the n − 1 earlier items.

n k = 1 k = 2 k = 3 k = 4 k = 5
3 2 3 1
4 6 11 6 1
5 24 50 35 10 1

Each row adds to n!, since every rearrangement has some number of cycles. The first column is (n − 1)!.

These numbers are the coefficients in the rising product x(x + 1)(x + 2) = x³ + 3x² + 2x. Reading the row for n = 3 backwards gives 1, 3, 2. Attaching alternating signs turns this into the expansion of (x)ₖ in powers of x, the exact reverse of the identity above.

How many cycles to expect

Pick a random rearrangement of n items, all equally likely. How many cycles will it have on average? For n = 3, weigh each cycle count by its frequency:

(1×2 + 2×3 + 3×1) / 6 = 11/6 = 1 + 1/2 + 1/3

The pattern holds in general: the average number of cycles is the harmonic sum 1 + 1/2 + ... + 1/n. It grows about like the natural logarithm of n, so even a thorough shuffle of a thousand items typically has only a handful of cycles.

The recurrence explains it. When item k is inserted, it starts a new cycle in exactly 1 of the k equally likely choices, so it adds 1/k to the average.

The first kind also counts something that sounds unrelated. Scan a rearrangement from left to right and note each time a new largest value appears. The number of rearrangements with exactly k such records is again c(n, k). For 3 items, only 321 and 312 have a single record, matching c(3, 1) = 2.

Names and notation

James Stirling, a Scottish mathematician, described these numbers in work published in 1730, while studying how to convert between kinds of expansions. His name is also on the famous approximation for factorials.

Notation varies from book to book. Many write S(n, k) with curly braces and the first kind with square brackets, and some include the signs in the first kind. Check the definition before copying a value.

Common mistakes include treating blocks as labeled when they are not, allowing empty blocks, and using the multiplier k from one recurrence in place of n − 1 from the other. If in doubt, list the n = 3 case: three items form 2 blocks in 3 ways, and 6 rearrangements split into 2 three-cycles, 3 swaps and 1 identity.

Written for 4Mathematics with AI writing tools and not reviewed line by line by a person. Check anything you rely on against a textbook or primary source. Report an error.

Keep reading

All 45 articles in Discrete Mathematics and Combinatorics →

Latest news on this topic