skip to main content

Stars and Bars

A short and useful theorem in combinatorics.

How many ways are there to distribute nn undistinguishable items into kk bins? Consider the following illustration for the situation where we distribute 77 items (stars) into 4 bins (intervals between bars) indicated by separators:

  |   | |    \star~\star~\text{\textbar}~\star~\star~\text{\textbar}~\text{\textbar}~\star~\star~~\star

We see that the problem is equivalent to choosing k1k-1 out of the overall n+k1n + k - 1 positions for separators (bars) before placing nn items (stars) in the remaining slots.

This leads to the following conclusion:

Lemma (Stars and Bars)

Let n,kNn, k \in \mathbb{N} with k1k \geq 1. The number of ways to place nn undistinguishable items into kk bins is given by

(n+k1k1)(1)\tag{1} \binom{n + k - 1}{k - 1}

Equivalently, eq. (1) gives the number of non-negative integer solutions to the following equation where xiNx_i \in \mathbb{N} for 1ik1 \leq i \leq k:

x1++xk=nx_1 + \dots + x_k = n

Example

Let’s consider the following example problem, where n,kNn, k \in \mathbb{N} with nkn \geq k:

How many different ways are there to select exactly kk elements from {1,,n}\{1, \dots, n\} such that the sum of these elements is at most n?

This problem can be reformulated as follows:

How many solutions does the inequality x1++xknx_1 + \dots + x_k \leq n permit for natural numbers xi1x_i \geq 1?

We can rewrite the inequality to use non-negative integer variables yiy_i by adding 11 to every variable, as well as adding a dummy term yk+1y_{k+1} representing the (non-negative) difference between the right- and left-hand side to obtain an equality:

(y1+1)++(yk+1)n    y1++yk+yk+1=nk\begin{aligned} (y_1 + 1) &+ \dots + (y_k + 1) &&\leq n \\ \iff y_1 &+ \dots + y_k + y_{k+1} &&= n - k \end{aligned}

By the Stars and Bars lemma (1), this problem has

((nk)+(k+1)1(k+1)1)=(nk)\binom{(n - k) + (k + 1) - 1}{(k + 1) - 1} = \binom{n}{k}

solutions.