Skip to content

Commit b40b297

Browse files
committed
Add stars and bars article
1 parent cbf891d commit b40b297

2 files changed

Lines changed: 47 additions & 0 deletions

File tree

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
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.

src/index.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ especially popular in field of competitive programming.*
6060
- [Binomial Coefficients](./combinatorics/binomial-coefficients.html)
6161
- [Catalan Numbers](./combinatorics/catalan-numbers.html)
6262
- [Placing Bishops on a Chessboard](./combinatorics/bishops-on-chessboard.html)
63+
- [Stars and bars](./combinatorics/stars_and_bars.html)
6364

6465
### Numerical Methods
6566
- [Ternary Search](./num_methods/ternary_search.html)

0 commit comments

Comments
 (0)