Stars and Bars
Published on August 25, 2024A short and useful theorem in combinatorics.
How many ways are there to distribute undistinguishable items into bins? Consider the following illustration for the situation where we distribute items (stars) into 4 bins (intervals between bars) indicated by separators:
We see that the problem is equivalent to choosing out of the overall positions for separators (bars) before placing items (stars) in the remaining slots.
This leads to the following conclusion:
Let with . The number of ways to place undistinguishable items into bins is given by
Equivalently, eq. (1) gives the number of non-negative integer solutions to the following equation where for :
Example
Let’s consider the following example problem, where with :
How many different ways are there to select exactly elements from such that the sum of these elements is at most n?
This problem can be reformulated as follows:
How many solutions does the inequality permit for natural numbers ?
We can rewrite the inequality to use non-negative integer variables by adding to every variable, as well as adding a dummy term representing the (non-negative) difference between the right- and left-hand side to obtain an equality:
By the Stars and Bars lemma (1), this problem has
solutions.