# Stars and bars (combinatorics)

In combinatorics, stars and bars (also called "sticks and stones", "balls and bars", and "dots and dividers") is a graphical aid for deriving certain combinatorial theorems. It can be used to solve a variety of counting problems, such as how many ways there are to put n indistinguishable balls into k distinguishable bins. The solution to this particular problem is given by the binomial coefficient ${\tbinom {n+k-1}{k-1}}$, which is the number of subsets of size k − 1 that can be formed from a set of size n + k − 1.
If, for example, there are two balls and three bins, then the number of ways of placing the balls is ${\tbinom {2+3-1}{3-1}}={\tbinom {4}{2}}=6$. The table shows the six possible ways of distributing the two balls, the strings of stars and bars that represent them (with stars indicating balls and bars separating bins from one another), and the subsets that correspond to the strings. As two bars are needed to separate three bins and there are two balls, each string contains two bars and two stars. Each subset indicates which of the four symbols in the corresponding string is a bar.
Six configurations of two balls in three bins and their star and bar representations :
| Bin 1 | Bin 2 | Bin 3 | String | Subset of {1,2,3,4} |
| 2 | 0 | 0 | ★ ★ | | | {3,4} |
| 1 | 1 | 0 | ★ | ★ | | {2,4} |
| 1 | 0 | 1 | ★ | | ★ | {2,3} |
| 0 | 2 | 0 | | ★ ★ | | {1,4} |
| 0 | 1 | 1 | | ★ | ★ | {1,3} |
| 0 | 0 | 2 | | | ★ ★ | {1,2} |

## Statements of theorems

The stars and bars method is often introduced specifically to prove the following two theorems of elementary combinatorics concerning the number of solutions to an equation.

### Theorem one

For any pair of positive integers n and k, the number of k-tuples of positive integers whose sum is n is equal to the number of (k − 1)-element subsets of a set with n − 1 elements.
For example, if n = 10 and k = 4, the theorem gives the number of solutions to x$_1$ + x$_2$ + x$_3$ + x$_4$ = 10 (with x$_1$, x$_2$, x$_3$, x$_4$ > 0) as the binomial coefficient
    ${\binom {n-1}{k-1}}={\binom {10-1}{4-1}}={\binom {9}{3}}=84,$
where ${\tbinom {n-1}{k-1}}$ is the number of combinations of n − 1 elements taken k − 1 at a time.
This corresponds to compositions of an integer.

### Theorem two

For any pair of positive integers n and k, the number of k-tuples of non-negative integers whose sum is n is equal to the number of multisets of size k − 1 taken from a set of size n + 1, or equivalently, the number of multisets of size n taken from a set of size k, and is given by
    ${\binom {n+k-1}{k-1}}.$
For example, if n = 10 and k = 4, the theorem gives the number of solutions to x$_1$ + x$_2$ + x$_3$ + x$_4$ = 10 (with x$_1$, x$_2$, x$_3$, x$_4$ $\geq 0$ ) as
    $\left(\!\!{n+1 \choose k-1}\!\!\right)=\left(\!\!{k \choose n}\!\!\right)={\binom {n+k-1}{k-1}}={\binom {10+4-1}{4-1}}={\binom {13}{3}}=286,$
where the multiset coefficient $\left(\!\!{\binom {k}{n}}\!\!\right)$ is the number of multisets of size n, with elements taken from a set of size k.
This corresponds to weak compositions of an integer. With k fixed, the numbers for n = 0, 1, 2, 3, ... are those in the (k − 1)st diagonal of Pascal's triangle. For example, when k = 3 the nth number is the (n + 1)st triangular number, which falls on the second diagonal, 1, 3, 6, 10, ....

## Proofs via the method of stars and bars
### Theorem one proof

The problem of enumerating k-tuples whose sum is n is equivalent to the problem of counting configurations of the following kind: let there be n objects to be placed into k bins, so that all bins contain at least one object. The bins are distinguished (say they are numbered 1 to k) but the n objects are not (so configurations are only distinguished by the number of objects present in each bin). A configuration is thus represented by a k-tuple of positive integers.
The n objects are now represented as a row of n stars; adjacent bins are separated by bars. The configuration will be specified by indicating the boundary between the first and second bin, the boundary between the second and third bin, and so on. Hence k − 1 bars need to be placed between stars. Because no bin is allowed to be empty, there is at most one bar between any pair of stars. There are n − 1 gaps between stars and hence n − 1 positions in which a bar may be placed. A configuration is obtained by choosing k − 1 of these gaps to contain a bar; therefore there are ${\tbinom {n-1}{k-1}}$ configurations.

### Example

With n = 7 and k = 3, start by placing seven stars in a line:
Now indicate the boundaries between the bins:
In general two of the six possible bar positions must be chosen. Therefore there are ${\tbinom {6}{2}}=15$ such configurations.

### Theorem two proof

In this case, the weakened restriction of non-negativity instead of positivity means that we can place multiple bars between stars and that one or more bars also be placed before the first star and after the last star. In terms of configurations involving objects and bins, bins are now allowed to be empty.
Rather than a (k − 1)-set of bar positions taken from a set of size n − 1 as in the proof of Theorem one, we now have a (k − 1)-multiset of bar positions taken from a set of size n + 1 (since bar positions may repeat and since the ends are now allowed bar positions). An alternative interpretation in terms of multisets is the following: there is a set of k bin labels from which a multiset of size n is to be chosen, the multiplicity of a bin label in this multiset indicating the number of objects placed in that bin. The equality $\left(\!\!{n+1 \choose k-1}\!\!\right)=\left(\!\!{k \choose n}\!\!\right)$ can also be understood as an equivalence of different counting problems: the number of k-tuples of non-negative integers whose sum is n equals the number of (n + 1)-tuples of non-negative integers whose sum is k − 1, which follows by interchanging the roles of bars and stars in the diagrams representing configurations.
To see the expression ${\tbinom {n+k-1}{k-1}}$ directly, observe that any arrangement of stars and bars consists of a total of n + k − 1 symbols, n of which are stars and k − 1 of which are bars. Thus, we may lay out n + k − 1 slots and choose k − 1 of these to contain bars (or, equivalently, choose n of the slots to contain stars).

### Example

When n = 7 and k = 5, the tuple (4, 0, 1, 2, 0) may be represented by the following diagram:
If possible bar positions are labeled 1, 2, 3, 4, 5, 6, 7, 8 with label i ≤ 7 corresponding to a bar preceding the ith star and following any previous star and 8 to a bar following the last star, then this configuration corresponds to the (k − 1)-multiset {5,5,6,8}, as described in the proof of Theorem two. If bins are labeled 1, 2, 3, 4, 5, then it also corresponds to the n-multiset {1,1,1,1,3,4,4}, also as described in the proof of Theorem two.

## Relation between Theorems one and two

Theorem one can be restated in terms of Theorem two, because the requirement that each variable be positive can be imposed by shifting each variable by −1, and then requiring only that each variable be non-negative.
For example:
    $x_{1}+x_{2}+x_{3}+x_{4}=10$
with $x_{1},x_{2},x_{3},x_{4}>0$
is equivalent to:
    $x'_{1}+x'_{2}+x'_{3}+x'_{4}=6$
with $x'_{1},x'_{2},x'_{3},x'_{4}\geq 0,$
where $x'_{i}=x_{i}-1$ for each $i\in \{1,2,3,4\}$.

## Further examples
### Example 1

If one wishes to count the number of ways to distribute seven indistinguishable one dollar coins among Amber, Ben, and Curtis so that each of them receives at least one dollar, one may observe that distributions are essentially equivalent to tuples of three positive integers whose sum is 7. (Here the first entry in the tuple is the number of coins given to Amber, and so on.) Thus Theorem 1 applies, with n = 7 and k = 3, and there are ${\tbinom {7-1}{3-1}}=15$ ways to distribute the coins.

### Example 2

If n = 5, k = 4, and the k bin labels are a, b, c, d, then ★|★★★||★ could represent either the 4-tuple (1, 3, 0, 1), or the multiset of bar positions {2, 5, 5}, or the multiset of bin labels {a, b, b, b, d}. The solution of this problem should use Theorem 2 with n = 5 stars and k – 1 = 3 bars to give ${\tbinom {5+4-1}{4-1}}={\tbinom {8}{3}}=56$ configurations.

### Example 3

In the proof of Theorem two there can be more bars than stars, which cannot happen in the proof of Theorem one.
So, for example, 10 balls into 7 bins gives ${\tbinom {16}{6}}$ configurations, while 7 balls into 10 bins gives ${\tbinom {16}{9}}$ configurations, and 6 balls into 11 bins gives ${\tbinom {16}{10}}={\tbinom {16}{6}}$ configurations.

### Example 4

The graphical method was used by Paul Ehrenfest and Heike Kamerlingh Onnes—with symbol ε (quantum energy element) in place of a star and the symbol 0 in place of a bar—as a simple derivation of Max Planck's expression for the number of "complexions" for a system of "resonators" of a single frequency.
By complexions (microstates) Planck meant distributions of P energy elements ε over N resonators. The number R of complexions is
    $R={\frac {(N+P-1)!}{P!(N-1)!}}.\$
The graphical representation of each possible distribution would contain P copies of the symbol ε and N – 1 copies of the symbol 0. In their demonstration, Ehrenfest and Kamerlingh Onnes took N = 4 and P = 7 (i.e., R = 120 combinations). They chose the 4-tuple (4, 2, 0, 1) as the illustrative example for this symbolic representation: εεεε0εε00ε.

## Relation to generating functions

The enumerations of Theorems one and two can also be found using generating functions involving simple rational expressions. The two cases are very similar; we will look at the case when $x_{i}\geq 0$, that is, Theorem two first. There is only one configuration for a single bin and any given number of objects (because the objects are not distinguished). This is represented by the generating function
    $1+1x+1x^{2}+1x^{3}+\ldots =1+x+x^{2}+x^{3}+\ldots ={\frac {1}{1-x}}.$
The series is a geometric series, and the last equality holds analytically for |x| < 1, but is better understood in this context as a manipulation of formal power series. The exponent of x indicates how many objects are placed in the bin.
Each additional bin is represented by another factor of ${\frac {1}{1-x}}$; the generating function for k bins is
    $\underbrace {{\frac {1}{1-x}}{\frac {1}{1-x}}\dots {\frac {1}{1-x}}} _{k{\text{ factors}}}={\frac {1}{(1-x)^{k}}}$,
where the multiplication is the Cauchy product of formal power series.
To find the number of configurations with n objects, we want the coefficient of $x^{n}$ (denoted by prefixing the expression for the generating function with $[x^{n}]$), that is,
    $[x^{n}]{\frac {1}{(1-x)^{k}}}=[x^{n}](1-x)^{-k}$.
This coefficient can be found using binomial series and agrees with the result of Theorem two, namely ${\tbinom {n+k-1}{k-1}}$.
This Cauchy product expression is justified via stars and bars: the coefficient of $x^{n}$ in the expansion of the product
    $\underbrace {(1+x+x^{2}+\ldots )(1+x+x^{2}+\ldots )\ldots (1+x+x^{2}+\ldots )} _{k{\text{ factors}}}$
is the number of ways of obtaining the nth power of x by multiplying one power of x from each of the k factors. So the stars represent xs and a bar separates the xs coming from one factor from those coming from the next factor.
For the case when $x_{i}>0$, that is, Theorem one, no configuration has an empty bin, and so the generating function for a single bin is
    $x+x^{2}+x^{3}+\ldots ={\frac {x}{1-x}}$.
The Cauchy product is therefore ${\frac {x^{k}}{(1-x)^{k}}}$, and the coefficient of $x^{n}$ is found using binomial series to be ${\tbinom {n-1}{k-1}}$.