logo
Published on

Stars and Bars

Authors

Motivation

Stars and Bars Theorem is quite a cool mathematics technique, it's usually used in solving this problem:

Given a non-negative integer nn and a positive integer kk, find the number of solution of:

x1+x2++xk=nx_1 + x_2 + \cdots + x_k = n

where xix_i is a non-negative integer.

For an example, when n=4n=4 and k=3k=3 then some of the possible solutions for (x1,x2,x3)(x_1, x_2, x_3) are:

(1,1,2),(3,0,1),(0,4,0),(1, 1, 2), (3, 0, 1), (0, 4, 0), \ldots

Theorem

To solve the problem above, we can use the Stars and Bars Theorem which states that:

The number of ways to put nn identical objects into kk labeled boxes is:

(n+k1n)\binom{n + k - 1}{n}

The proof involves turning the objects into stars and separating the boxes using bars (therefore the name). E.g. we can represent with:

 \bigstar | \bigstar \bigstar |~| \bigstar \bigstar

The above situation: 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. This is one way of dividing 55 objects into 44 boxes.

It should be pretty obvious, that every partition can be represented using nn stars and k1k - 1 bars and every stars and bars permutation using nn stars and k1k - 1 bars represents one partition.

Therefore the number of ways to divide nn identical objects into kk labeled boxes is the same number as there are permutations of nn stars and k1k - 1 bars. The Binomial Coefficient gives us the desired formula.

Solving The Problem

Now back on solving the previous problem, for n=4n=4 and k=3k=3 we wanted to find the number of possible solutions of:

x1+x2+x3=4x_1 + x_2 + x_3 = 4

where xix_i is a non-negative integer for 1ik1 \leq i \leq k.

We can represent this with stars and bars, let's have nn stars and k1k-1 bars, then will have the following image:

 \bigstar \bigstar \bigstar \bigstar |~|

The above image states that x1=4x_1=4, x2=0x_2=0, and x3=0x_3=0.

Another example is when we have:

\bigstar |\bigstar \bigstar| \bigstar

Then we will have x1=1x_1=1, x2=2x_2=2, and x3=1x_3=1.

Therefore, we can just find every possible combinations of stars and bars to solve this solution.

So using the theorem, the number of solution is then:

(n+k1n)=(4+314)=(64)=6!4!2!=15\begin{aligned} \binom{n + k - 1}{n} &= \binom{4+3-1}{4} \\ &= \binom{6}{4} \\ &= \frac{6!}{4! \cdot 2!} \\ &= 15 \end{aligned}

Solving With Lower Bound Constraint

Let's have one more constraint, let's introduce new non-positive integers yiy_i such that 0yixi0 \leq y_i \leq x_i for each 1ik1 \leq i \leq k

Now the original problem remains the same, but we have to make sure that for each xix_i the constraint 0yixi0 \leq y_i \leq x_i is satisfied.

Formally, given a non-negative integer nn and a positive integer kk, find the number of solution of:

x1+x2++xk=nx_1 + x_2 + \cdots + x_k = n

where xix_i is a non-negative integer and 0yixi0 \leq y_i \leq x_i.

To solve this problem, let zi=xiyiz_i = x_i - y_i, now we have 0zi0 \leq z_i and now the equation becomes:

x1+x2++xk=n(z1+y1)+(z2+y2)++(zk+yk)=nz1+z2++zk=n(y1+y2++yk)\begin{aligned} x_1 + x_2 + \cdots + x_k &= n \\ (z_1 + y_1) + (z_2 + y_2) + \cdots + (z_k + y_k) &= n \\ z_1 + z_2 + \cdots + z_k &= n - (y_1 + y_2 + \cdots + y_k) \end{aligned}

This is the same as the original problem, but now the right equation becomes different, now let:

m=ni=1kykm = n - \sum_{i=1}^{k}y_k

Therefore the number of solution is now:

(m+k1m)\binom{m + k - 1}{m}