Quant Memo
Advanced

Stirling Numbers and Set Partitions

Counting the number of ways to split a set of n distinct items into exactly k non-empty groups, and the closely related count of all possible groupings regardless of how many groups there are.

Prerequisites: Binomial Coefficients and Pascal's Triangle

A set partition splits a collection of distinct items into non-empty, unordered groups — unordered meaning {A,B},{C}\{A,B\}, \{C\} is the same partition as {C},{B,A}\{C\}, \{B,A\}, since it's the grouping that matters, not any labeling of the groups. The Stirling number of the second kind, written S(n,k)S(n,k), counts the number of ways to partition nn distinct items into exactly kk non-empty groups.

These numbers satisfy a simple recurrence that mirrors how you'd build up the count by hand: to partition nn items into kk groups, either the nn-th item joins one of the existing kk groups formed from the first n1n-1 items (kk ways to choose which group, times S(n1,k)S(n-1,k) ways to have formed them), or the nn-th item starts a brand-new group on its own (S(n1,k1)S(n-1,k-1) ways to have partitioned the rest into the remaining k1k-1 groups). This gives S(n,k)=kS(n1,k)+S(n1,k1)S(n,k) = k \cdot S(n-1,k) + S(n-1,k-1).

As a worked example, take n=4n=4 items split into k=2k=2 groups. Using the recurrence with known small values S(3,1)=1S(3,1)=1 and S(3,2)=3S(3,2)=3: S(4,2)=2S(3,2)+S(3,1)=23+1=7S(4,2) = 2 \cdot S(3,2) + S(3,1) = 2 \cdot 3 + 1 = 7. Checking by hand for items {1,2,3,4}\{1,2,3,4\} confirms exactly 7 ways to split them into two non-empty groups. Summing S(n,k)S(n,k) over every possible kk from 1 to nn gives the Bell number, the total count of all ways to partition an nn-item set into any number of groups.

S(n,k)S(n,k) counts partitions into exactly kk unordered groups and builds from a simple recurrence: the last item either joins an existing group or starts a new one.

Related concepts

Practice in interviews

Further reading

  • Graham, Knuth, Patashnik, Concrete Mathematics, ch. 6
ShareTwitterLinkedIn