Discrete MathematicsCounting: Choices, Arrangements and Selections7 of 7
Counting: Choices, Arrangements and Selections · 7

Stars and Bars: Distributing Identical Objects

When we hand out distinct items, we choose a recipient for each item one by one, but when the items are identical, only the final count each person receives matters.

1

Distributing Identical Objects

Suppose we want to find how many ways we can distribute 7\starcount{7} identical cookies to 33 kids (Alice, Bob, and Charlie) if each kid can receive any number of cookies, including 00.

Distributing Identical Objects with Stars and Bars
Alice\text{Alice}
Bob\text{Bob}
Charlie\text{Charlie}
ways= ?\text{ways} = \,?
44
22
11
Alice\text{Alice}
Bob\text{Bob}
Charlie\text{Charlie}
00
44
33
Alice\text{Alice}
Bob\text{Bob}
Charlie\text{Charlie}
Text solution
Turn cookies and kids into stars and barsDraw the 7\starcount{7} identical cookies as 7\starcount{7} stars in a row, and use 2\barcount{2} vertical bars to divide the row into 33 sections for Alice, Bob, and Charlie.
⋆ ⋆ ⋆ ⋆⏟Alice: 4 ∣ ⋆ ⋆⏟Bob: 2 ∣ ⋆⏟Charlie: 1\displaystyle \underbrace{\starcount{\star \ \star \ \star \ \star}}_{\text{Alice: } 4} \ \barcount{\big|} \ \underbrace{\starcount{\star \ \star}}_{\text{Bob: } 2} \ \barcount{\big|} \ \underbrace{\starcount{\star}}_{\text{Charlie: } 1}
Choose spaces for the barsEvery arrangement of the 7\starcount{7} stars and 2\barcount{2} bars uses 7+2=9\starcount{7} + \barcount{2} = 9 total spaces, and choosing which 2\barcount{2} spaces get the bars automatically fills the other 7\starcount{7} spaces with stars.
C(9,2)\displaystyle C(9, \barcount{2}) =9×82\displaystyle = \frac{9 \times 8}{2} =36\displaystyle = \waycount{36}

So there are 36\waycount{36} ways to distribute the 7\starcount{7} identical cookies to the 33 kids.

Notice in the figure how placing a bar at the end or two bars right next to each other leaves a kid with 00 cookies.

Every choice of 2 spaces for the bars splits the remaining 7 stars into a unique cookie count for Alice, Bob, and Charlie.
2

Minimums and Integer Equations

What if nobody is allowed to get zero, or different groups have different required minimums? We want to count the ways to give 7\starcount{7} cookies to 33 kids when every kid must get at least 11, and then solve the same kind of question written as an algebra equation: finding the number of integer solutions to x1+x2+x3=12x_1 + x_2 + x_3 = \starcount{12} where x1≥2x_1 \ge 2, x2≥0x_2 \ge 0, and x3≥3x_3 \ge 3.

Minimums and Integer Equations
every kid gets at least 1\text{every kid gets at least 1}
Alice\text{Alice}
Bob\text{Bob}
Charlie\text{Charlie}
ways= ?\text{ways} = \,?
1\htmlClass{lc-mute}{1}
2\htmlClass{lc-mute}{2}
3\htmlClass{lc-mute}{3}
4\htmlClass{lc-mute}{4}
5\htmlClass{lc-mute}{5}
6\htmlClass{lc-mute}{6}
C(6,2)=15C(6, \htmlClass{lc-pink}{2}) = \htmlClass{lc-green}{15}
Alice\text{Alice}
Bob\text{Bob}
Charlie\text{Charlie}
C(6,2)=15C(6, \htmlClass{lc-pink}{2}) = \htmlClass{lc-green}{15}

When every group needs at least 11 star, placing the 2\barcount{2} bars into the 66 gaps between the 7\starcount{7} stars gives C(6,2)=15C(6, \barcount{2}) = \waycount{15} ways. Handing out the minimums upfront gets the same C(6,2)=15C(6, \barcount{2}) = \waycount{15} ways and also works when the minimums are uneven, as in x1+x2+x3=12x_1 + x_2 + x_3 = \starcount{12} with x1≥2x_1 \ge 2, x2≥0x_2 \ge 0, and x3≥3x_3 \ge 3:

Text solution
Hand out the minimums upfrontPut 22 ones into x1x_1 and 33 ones into x3x_3 right away so the minimum requirements are already satisfied, leaving 7\starcount{7} stars that can be distributed with no restrictions.
12−(2+0+3)\displaystyle \starcount{12} - (2 + 0 + 3) =7 stars left\displaystyle = \starcount{7} \text{ stars left}
Place the bars among the remaining starsSeparating the 33 variables still takes 2\barcount{2} bars, which gives 7+2=9\starcount{7} + \barcount{2} = 9 blank spaces to choose from.
C(9,2)\displaystyle C(9, \barcount{2}) =36\displaystyle = \waycount{36}

So there are 36\waycount{36} integer solutions to x1+x2+x3=12x_1 + x_2 + x_3 = \starcount{12} that satisfy the minimums.

4

Choosing with Repetition

Stars and Bars also shows up when we choose a collection of items from several categories with repeats allowed. Suppose you are buying a box of 7\starcount{7} donuts from a bakery that sells 33 flavors (Glazed, Chocolate, and Jelly) with plenty of each in stock, and we want to know how many different boxes of 7\starcount{7} donuts you can walk out with.

Choosing with Repetition
7\htmlClass{lc-blue}{7}
Glazed\text{Glazed}
Chocolate\text{Chocolate}
Jelly\text{Jelly}
boxes= ?\text{boxes} = \,?
Text solution
Write the fixed total as an equationWhatever combination of flavors you choose, the flavor counts must add up to the 7\starcount{7} donuts in the box, which makes the 7\starcount{7} donuts the stars and the 33 flavors the groups.
Glazed+Chocolate+Jelly\displaystyle \text{Glazed} + \text{Chocolate} + \text{Jelly} =7\displaystyle = \starcount{7}
Place the bars between the flavor columnsSplitting the 7\starcount{7} stars across 33 flavor categories takes 2\barcount{2} bars, giving 7+2=9\starcount{7} + \barcount{2} = 9 total spaces.
C(9,2)\displaystyle C(9, \barcount{2}) =36\displaystyle = \waycount{36}

So there are 36\waycount{36} different boxes of 7\starcount{7} donuts you can buy.

Each donut in the box adds one star tally to its flavor column on the order form, so the 7\starcount{7} donuts are the stars and the 33 flavors are the groups.

Every box of 7 donuts from 3 flavors matches a row of 7 stars and 2 bars on the order form.
5

Upper Limits by Complement

Giving stars upfront sets a minimum, because a group can still receive more stars afterward. What if a group has a maximum instead? We want to find the number of integer solutions to x1+x2+x3=7x_1 + x_2 + x_3 = \starcount{7} (with x1,x2,x3≥0x_1, x_2, x_3 \ge 0) when x1x_1 is limited to x1≤3x_1 \le 3.

Upper Limits by Complement
x1+x2+x3=7x_1 + x_2 + x_3 = \htmlClass{lc-blue}{7}
x1,x2,x3≥0,x1≤3x_1, x_2, x_3 \ge 0, \quad x_1 \le 3
≤3\htmlClass{lc-orange}{\le 3}
Alice\text{Alice}
Bob\text{Bob}
Charlie\text{Charlie}
solutions= ?\text{solutions} = \,?
Text solution
Count all distributions without the upper limitWith 7\starcount{7} stars and 2\barcount{2} bars for the 33 variables, find the total number of solutions where x1,x2,x3≥0x_1, x_2, x_3 \ge 0.
Total\displaystyle \text{Total} =C(9,2)\displaystyle = C(9, \barcount{2}) =36\displaystyle = 36
Turn the upper limit into a minimum to count the bad casesBreaking the rule x1≤3x_1 \le 3 means having x1≥4\badcount{x_1 \ge 4}, so give 44 stars to x1x_1 upfront and distribute the remaining 7−4=3\starcount{7} - 4 = \starcount{3} stars using 2\barcount{2} bars.
Bad\displaystyle \badcount{\text{Bad}} =C(3+2,2)\displaystyle = C(\starcount{3} + \barcount{2}, \barcount{2}) =C(5,2)\displaystyle = C(5, \barcount{2}) =10\displaystyle = \badcount{10}
Subtract the bad cases from the totalRemove the distributions that give x1x_1 too many stars.
36−10\displaystyle 36 - \badcount{10} =26\displaystyle = \waycount{26}

So there are 26\waycount{26} solutions where x1≤3x_1 \le 3.

Notice that this single subtraction handles a single upper limit; if two or more groups have upper limits at the same time, subtracting each group's bad cases separately would subtract overlapping violations more than once, which requires Inclusion-Exclusion.

Key concepts

Before using Stars and Bars, check that the items being distributed are actually identical: if 77 different cookies go to 33 kids, each cookie has 33 choices for a total of 373^7 ways, whereas Stars and Bars applies when only the count in each group matters.

Whether a problem distributes identical items (7\starcount{7} cookies to 33 kids) or selects items with repetition allowed (7\starcount{7} donuts chosen from 33 flavors), you can always write it as an addition equation like x1+x2+x3=7x_1 + x_2 + x_3 = \starcount{7}. The fixed total on the right side is the number of stars (7\starcount{7}), and the kk distinct groups or categories on the left are separated by k−1\barcount{k - 1} bars (2\barcount{2} bars), giving 7+2=9\starcount{7} + \barcount{2} = 9 spaces and C(9,2)C(9, \barcount{2}) ways to place the bars.

From that starting setup, three adjustments handle extra conditions on the equation:

Minimums (≥\ge)

Hand out the required minimums upfront so every variable can start at 00. For x1+x2+x3=12x_1 + x_2 + x_3 = \starcount{12} with x1≥2,x2≥0,x3≥3x_1 \ge 2, x_2 \ge 0, x_3 \ge 3, give away 2+3=52 + 3 = 5 stars first, leaving 7\starcount{7} stars and 2\barcount{2} bars for C(9,2)C(9, \barcount{2}) ways.

At most a total (≤\le on the sum)

Add a cookie-jar variable to hold the leftovers and turn the inequality into an equation. For x1+x2+x3≤7x_1 + x_2 + x_3 \le \starcount{7}, write x1+x2+x3+jar=7x_1 + x_2 + x_3 + \jar{\text{jar}} = \starcount{7}, which uses 7\starcount{7} stars and 3\barcount{3} bars for C(10,3)C(10, \barcount{3}) ways.

A single maximum (≤\le on one variable)

Subtract the complement to turn the maximum into a minimum. For x1≤3x_1 \le 3 in x1+x2+x3=7x_1 + x_2 + x_3 = \starcount{7}, breaking the rule means x1≥4\badcount{x_1 \ge 4}, which leaves C(9,2)−C(5,2)C(9, \barcount{2}) - \badcount{C(5, 2)} ways.

6

Practice

1

In how many ways can 1010 identical coins be distributed among 44 children if each child can receive any number of coins, including 00?

How many bars does it take to split a row of 1010 stars into 44 sections?

We have 10\starcount{10} stars for the coins, and splitting them among 44 children takes 4−1=3\barcount{4 - 1} = \barcount{3} bars (be careful not to use 44 bars for 44 children, which would give C(14,4)=1001C(14, 4) = \wrongcount{1001}, or forget to add the 3\barcount{3} bars to the total spaces and compute C(10,3)=120C(10, 3) = \wrongcount{120}). With 10+3=13\starcount{10} + \barcount{3} = 13 total spaces, choosing 3\barcount{3} spaces for the bars gives
C(13,3)\displaystyle C(13, \barcount{3}) =13×12×113×2×1\displaystyle = \frac{13 \times 12 \times 11}{3 \times 2 \times 1} =286\displaystyle = 286
2

A smoothie shop offers 66 different fruit add-ins. You order a smoothie with 44 scoops of add-ins, and repeats are allowed. How many different combinations of 44 scoops can you choose?

Write an equation for the total number of scoops in your cup: which number is the fixed total of stars, and which is the number of categories?

The counts of the 66 fruit add-ins must add up to the fixed total of 4\starcount{4} scoops in your cup, so there are 4\starcount{4} stars and 66 categories separated by 5\barcount{5} bars (don't swap the stars and groups by treating the 66 fruits as stars and the 44 scoops as groups, which gives C(6+3,3)=84C(6 + 3, 3) = \wrongcount{84}). With 4+5=9\starcount{4} + \barcount{5} = 9 spaces, choosing where the 5\barcount{5} bars (or 4\starcount{4} stars) go gives
C(9,5)\displaystyle C(9, \barcount{5}) =C(9,4)\displaystyle = C(9, \starcount{4}) =9×8×7×64×3×2×1\displaystyle = \frac{9 \times 8 \times 7 \times 6}{4 \times 3 \times 2 \times 1} =126\displaystyle = 126
3

A teacher has 1111 identical pencils to place into 44 distinct supply bins so that every bin gets at least 11 pencil. In how many ways can she do this?

Take care of the "at least 11" rule first: how many pencils are left to distribute freely once every bin has 11?

Put 11 pencil into each of the 44 bins upfront, leaving 11−4=7\starcount{11} - 4 = \starcount{7} stars to distribute freely among the 44 bins using 3\barcount{3} bars (or place 3\barcount{3} bars directly into the 1010 gaps between the 11\starcount{11} stars). Be careful not to forget to add the 3\barcount{3} bars to the 7\starcount{7} remaining stars when counting spaces (which would give C(7,3)=35C(7, 3) = \wrongcount{35}) or use 44 bars instead of 3\barcount{3} (which would give C(11,4)=330C(11, 4) = \wrongcount{330}). Choosing 3\barcount{3} spaces out of 7+3=10\starcount{7} + \barcount{3} = 10 gives
C(10,3)\displaystyle C(10, \barcount{3}) =10×9×83×2×1\displaystyle = \frac{10 \times 9 \times 8}{3 \times 2 \times 1} =120\displaystyle = 120
4

How many integer solutions are there to x1+x2+x3+x4=15x_1 + x_2 + x_3 + x_4 = 15 where x1≥3x_1 \ge 3, x2≥1x_2 \ge 1, x3≥0x_3 \ge 0, and x4≥2x_4 \ge 2?

Hand out the required minimums to x1,x2,x_1, x_2, and x4x_4 first so that all four variables can start at 00.

Hand out the minimums 3+1+0+2=63 + 1 + 0 + 2 = 6 upfront, leaving 15−6=9\starcount{15} - 6 = \starcount{9} stars to distribute freely (watch that x3≥0x_3 \ge 0 needs 00 stars upfront; giving x3x_3 a star out of habit leaves 88 stars and gives C(11,3)=165C(11, 3) = \wrongcount{165}). Splitting the 9\starcount{9} remaining stars among 44 variables takes 3\barcount{3} bars, so with 9+3=12\starcount{9} + \barcount{3} = 12 spaces we get
C(12,3)\displaystyle C(12, \barcount{3}) =12×11×103×2×1\displaystyle = \frac{12 \times 11 \times 10}{3 \times 2 \times 1} =220\displaystyle = 220
5

How many integer solutions are there to x1+x2+x3≤8x_1 + x_2 + x_3 \le 8 where x1≥0x_1 \ge 0, x2≥0x_2 \ge 0, and x3≥0x_3 \ge 0?

Where does the unused part of the 88 go when x1+x2+x3x_1 + x_2 + x_3 is less than 88?

Add a fourth variable for the leftovers so that x1+x2+x3+slack=8x_1 + x_2 + x_3 + \jar{\text{slack}} = \starcount{8}. That gives 8\starcount{8} stars split among 44 groups using 3\barcount{3} bars (don't use just 22 bars for the 33 original variables, which only counts the exact sum x1+x2+x3=8x_1 + x_2 + x_3 = 8 and gives C(10,2)=45C(10, 2) = \wrongcount{45}). Choosing 3\barcount{3} spaces out of 8+3=11\starcount{8} + \barcount{3} = 11 gives
C(11,3)\displaystyle C(11, \barcount{3}) =11×10×93×2×1\displaystyle = \frac{11 \times 10 \times 9}{3 \times 2 \times 1} =165\displaystyle = 165
6

A baker is packing a box of 99 pastries chosen from 44 types: croissants, scones, muffins, and danishes. There are plenty of scones, muffins, and danishes, but only 44 croissants left. How many different boxes of 99 pastries can the baker pack?

Having only 44 croissants sets an upper limit (croissants≤4\text{croissants} \le 4): how many croissants would a box need to break that rule?

With no limit on croissants, 9\starcount{9} pastries (stars) chosen from 44 types (3\barcount{3} bars) gives C(9+3,3)=C(12,3)=220C(\starcount{9} + \barcount{3}, \barcount{3}) = C(12, \barcount{3}) = 220 total boxes. A box breaks the rule croissants≤4\text{croissants} \le 4 when it has at least 5\badcount{5} croissants (be careful not to use 44 croissants as the bad case, which leaves 55 stars, C(8,3)=56C(8, 3) = \wrongcount{56}, and gives 220−56=164220 - \wrongcount{56} = \wrongcount{164}). Putting 55 croissants in the box upfront leaves 9−5=4\starcount{9} - 5 = \starcount{4} stars and 3\barcount{3} bars, or C(4+3,3)=C(7,3)=35C(\starcount{4} + \barcount{3}, \barcount{3}) = C(7, \barcount{3}) = \badcount{35} bad boxes. Subtracting the bad boxes from the total gives
220−35\displaystyle 220 - \badcount{35} =185\displaystyle = 185
7

How many integer solutions are there to x1+x2+x3=11x_1 + x_2 + x_3 = 11 where 1≤x1≤41 \le x_1 \le 4, x2≥2x_2 \ge 2, and x3≥0x_3 \ge 0?

First count all solutions that satisfy the minimums x1≥1x_1 \ge 1 and x2≥2x_2 \ge 2, then subtract the ones that break the upper limit x1≤4x_1 \le 4 while still keeping x2≥2x_2 \ge 2.

First satisfy the minimums x1≥1x_1 \ge 1 and x2≥2x_2 \ge 2 by handing out 1+2=31 + 2 = 3 stars upfront, leaving 11−3=8\starcount{11} - 3 = \starcount{8} stars and 2\barcount{2} bars for C(8+2,2)=C(10,2)=45C(\starcount{8} + \barcount{2}, \barcount{2}) = C(10, \barcount{2}) = 45 solutions. Among these, a solution breaks the upper limit x1≤4x_1 \le 4 when x1≥5\badcount{x_1 \ge 5} and x2≥2x_2 \ge 2 (don't forget that x2≥2x_2 \ge 2 still holds in the bad cases: subtracting only 55 from 1111 gives C(8,2)=28C(8, 2) = \wrongcount{28} and 45−28=1745 - \wrongcount{28} = \wrongcount{17}; and don't add 55 on top of the 11 already in x1x_1 to get x1≥6x_1 \ge 6, which gives C(5,2)=10C(5, 2) = \wrongcount{10} and 45−10=3545 - \wrongcount{10} = \wrongcount{35}). Handing out 55 stars to x1x_1 and 22 stars to x2x_2 leaves 11−7=4\starcount{11} - 7 = \starcount{4} stars and 2\barcount{2} bars, giving C(4+2,2)=C(6,2)=15C(\starcount{4} + \barcount{2}, \barcount{2}) = C(6, \barcount{2}) = \badcount{15} bad solutions. Subtracting the bad solutions leaves
45−15\displaystyle 45 - \badcount{15} =30\displaystyle = 30