Counting Multisets: Stars and Bars
Choosing with repeats allowed, like 12 donuts from 5 flavors, turns into an ordinary combination once you write the order as 12 stars split into 5 groups by 4 bars. Every order is just a choice of where the 4 bars go among the 16 symbols.
What the symbols mean
- Items
How many identical items are chosen or distributed.
- Types
How many kinds or bins they go into.
Explanation
Stars and bars is simply a way to count selections where you're allowed to take the same thing more than once, like filling a box of donuts when you can pick three glazed. The name is literal: you draw each donut as a star and use bars as dividers between flavors. It's good for any problem about splitting identical things among groups, like cookies among children or servers among services. The answer looks odd, but its pieces are friendly: (how many donuts), (how many flavors), (the number of dividers you need to split a row into sections) and (donuts plus dividers, all the spots in the row). So it's just "choose where the dividers go." Once you see the picture, the formula writes itself.
Combinations count selections where order doesn't matter, but they assume you can't pick the same thing twice. A box of donuts breaks that: you can take three glazed if you want. A selection like that, with repeats allowed and order ignored, is called a multiset. So how do we count those?
The trick is to find a picture where the problem becomes one we already know. Write a choice of items from types as a row of stars and bars. The stars before the first bar are flavor 1, the stars between the first and second bars are flavor 2, and so on. For example, ★★|★|||★★★ is 2 of flavor 1, 1 of flavor 2, none of flavors 3 and 4, and 3 of flavor 5. You only need bars because bars are dividers: 4 walls make 5 sections.
Notice that every box of donuts gives exactly one row of symbols, and every row gives exactly one box. So counting boxes is the same as counting rows. A row has positions, and it's fixed once you choose which of them hold bars, so the count is . The repeats that broke combinations have disappeared into an ordinary choice of positions.
The same count gives the nonnegative integer solutions of . That's the same problem in different clothes: is how many donuts of flavor you took.
At least one of each. If every type has to show up, give each type one item first. That uses items and leaves to distribute freely, so there are positive solutions.
A common mistake. It's tempting to use bars for types, or to choose positions for the stars out of only spots. Neither works. You need one fewer bar than types, and the stars and bars share the positions, which is where the in the formula comes from.
See it
Worked examples
How many ways are there to choose 12 donuts from 5 flavors?
- 1
Each donut is a star and the 5 flavors need dividers, so a box is a row of stars and 4 bars: 16 symbols in all.
How many positive integer solutions does have?
- 1
Each variable has to be at least 1, so give each one a 1 up front. That leaves 7 to hand out freely among 3 variables.
Practice
Type your answer and check it. Solve them all to complete the lesson.
How many ways are there to choose 6 bagels from 3 kinds, repeats allowed?
How many nonnegative integer solutions does have?
How many positive integer solutions does have?
In how many ways can 10 identical cookies go to 4 children if each gets at least one?
A bakery sells 4 kinds of muffins. In how many ways can you buy a dozen, if only the number of each kind matters?
How many lattice paths go from to using only steps one unit right or one unit up?
The hardest one here. Get it, and you've got this lesson.
How many solutions does have in nonnegative integers with ?
Where it's used
Allocating identical resources, such as server instances among services or seats among parties, is a stars-and-bars count.