|
| 1 | +<!--?title Stars and bars --> |
| 2 | +# Stars and bars |
| 3 | + |
| 4 | +Stars and bars is a mathematical technique for solving certain combinatorial problems. |
| 5 | +It occurs whenever you want to count the number of ways to group identical objects. |
| 6 | + |
| 7 | +## Theorem |
| 8 | + |
| 9 | +The number of ways to put $n$ identical objects into $k$ labeled boxes is |
| 10 | +$$\binom{n + k - 1}{n}.$$ |
| 11 | + |
| 12 | +The proof involves turning the objects into stars and separating the boxes using bars (therefore the name). |
| 13 | +E.g. we can represent with $\bigstar | \bigstar \bigstar |~| \bigstar \bigstar$ the following situation: |
| 14 | +in the first box is one object, in the second box are two objects, the third one is empty and in the last box are two objects. |
| 15 | +This is one way of dividing 5 objects into 4 boxes. |
| 16 | + |
| 17 | +It should be pretty obvious, that every partition can be represented using $n$ stars and $k - 1$ bars and every stars and bars permutation using $n$ stars and $k - 1$ bars represents one partition. |
| 18 | +Therefore the number of ways to divide $n$ identical objects into $k$ labeled boxes is the same number as there are permutations of $n$ stars and $k - 1$ bars. |
| 19 | +The [Binomial Coefficient](./combinatorics/binomial-coefficients.html) gives us the desired formula. |
| 20 | + |
| 21 | +## Number of non-negative integer sums |
| 22 | + |
| 23 | +This problem is a direct application of the theorem. |
| 24 | + |
| 25 | +You want to count the number of solution of the equation |
| 26 | +$$x_1 + x_2 + \dots + x_k = n$$ |
| 27 | +with $x_i \ge 0$. |
| 28 | + |
| 29 | +Again we can represent a solution using stars and bars. |
| 30 | +E.g. the solution $1 + 3 + 0 = 4$ for $n = 4$, $k = 3$ can be represented using $\bigstar | \bigstar \bigstar \bigstar |$. |
| 31 | + |
| 32 | +It is easy to see, that this is exactly the stars an bars theorem. |
| 33 | +Therefore the solution is $\binom{n + k - 1}{n}$. |
| 34 | + |
| 35 | +## Number of lower-bound integer sums |
| 36 | + |
| 37 | +This can easily be extended to integer sums with different lower bounds. |
| 38 | +I.e. we want to count the number of solutions for the equation |
| 39 | +$$x_1 + x_2 + \dots + x_k = n$$ |
| 40 | +with $x_i \ge a_i$. |
| 41 | + |
| 42 | +After substituting $x_i' := x_i - a_i$ we receive the modified equation |
| 43 | +$$(x_1' + a_i) + (x_2' + a_i) + \dots + (x_k' + a_k) = n$$ |
| 44 | +$$\Leftrightarrow ~ ~ x_1' + x_2' + \dots + x_k' = n - a_1 - a_2 - \dots - a_k$$ |
| 45 | +with $x_i' \ge 0$. |
| 46 | +So we have reduced the problem to the simpler case with $x_i' \ge 0$ and again can apply the stars and bars theorem. |
0 commit comments