Stars and Bars

A stars and bars problem (also known as a balls and bins problem) is a combinatorial technique used to count the number of ways to form unordered selections with repetition allowed from a set.

It is equivalent to counting the number of non-negative integer solutions to

is the number of distinct categories (bins), and is the number of items (stars) to be distributed among the categories. Each is simply the count of items for that category.

The number of such combinations is given by

Intuition

Although stars and bars problems may be initially difficult to understand since you cannot derive it from any of the previous concepts, the explanation is not difficult. To begin, consider , which represents the number of ways to “take out” elements from a set without replacing them, or considering order. In stars and bars we have replacement, so given a set , is a valid length-7 sequence.

Think for a second about what and alternatively represent:

  • We have identical elements to assign.
  • We need to divide them into buckets (or equivalently, dividers).

For example, the length-7 sequence from the set above can equivalently be represented as
Therefore, we are counting all the ways to combine the identical elements (stars) and the identical dividers (bars), which together form a sequence of symbols. Since we are selecting positions to place the elements (or equivalently, positions to place the dividers), the total number of such arrangements is

Examples

Oranges and Apples

We want to put 6 out of an infinite number of oranges and apples in 4 baskets. The order which we put the fruit in the basket does not matter. We can transform this example into a binomial string, where 1s represent separations between boxes and 0s represent the fruit. An example string is:

Where (from left to right) there is 1 fruit in first basket, no fruit in the 2nd basket, 3 fruits in the third basket, and 2 fruits in the fourth basket.

Knowing this, we can say that the total number of choices is equal to the number of 0s (fruits) plus the number of 1s (baskets) - 1. Then, we want to count all the possibilities of placing fruit in each basket where ordering does not matter. We use the SRC (since order doesn’t matter) to come up with:

Farmer’s Market

Suppose you want items from the farmer’s market. Count how many ways you can do this, assuming:

Jelly Bean Factory

A candy factory has an endless supply of red, orange and yellow jelly beans. The factory packages the jelly beans into jars of 100 jelly beans each, with each possible combination of colors in the jar being equally likely.