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 is the same partition as , since it's the grouping that matters, not any labeling of the groups. The Stirling number of the second kind, written , counts the number of ways to partition distinct items into exactly non-empty groups.
These numbers satisfy a simple recurrence that mirrors how you'd build up the count by hand: to partition items into groups, either the -th item joins one of the existing groups formed from the first items ( ways to choose which group, times ways to have formed them), or the -th item starts a brand-new group on its own ( ways to have partitioned the rest into the remaining groups). This gives .
As a worked example, take items split into groups. Using the recurrence with known small values and : . Checking by hand for items confirms exactly 7 ways to split them into two non-empty groups. Summing over every possible from 1 to gives the Bell number, the total count of all ways to partition an -item set into any number of groups.
counts partitions into exactly 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