Massachusetts Institute of Technology Lecture ?? 6.042J/18.062J: Mathematics for Computer Science November 5, 1996 Professors Nancy Lynch and Tom Leighton

Notes 17

Reading: Section 4.6 Next: ???

So far, have studied permutations and combinations of sets of objects, where each object can only be used once.

Today consider a variety of more complicated permutation and combination problems.

1. Permutations and combinations where any number of repetitions of elements is allowed.

Related problems: Number of ways of throwing indistinguishable balls into baskets. Number of ways a number can be decomposed as a sum.

2. Permutations and combinations where a bounded amount of repetition is allowed.

Related: Indistinguishable balls in baskets, with constraints on number of balls in each basket. Distinguishable balls in baskets. Dealing bridge or poker hands.

1 Permutations and Combinations with Unbounded Repetition

1.1 Permutations

Recall that an r-permutation of a set of n elements is a sequence of length r of distinct elements.

Definition 1.1 An r-permutation of a set of n elements allowing repetitions is a sequence of r elements, each chosen from the set. (Repeats allowed.)

Recall that the number of r-permutations without repetitions is \frac{n!}{(n−r)!}.

The number of r-permutations with repetitions is n^{r}.

By Product Rule. Simple.

Example 1 How many strings of length 10, of letters A-M? Same as number of 10-permutations with repetitions allowed, of the set of letters A-M. 13^{10}.

Example 2 Given a bag containing 5 red balls and 5 blue balls. Experiment involves picking a sequence of 4 balls (without replacement). How many different color sequences are possible?

This is easy – just the number of 4-permutations of the set of 2 elements (red, blue), which is 2^{4}.

It doesn’t matter here how many red and blue balls there are – just matters that there are more than 4. Really the unlimited case.

1.2 Combinations

Similar to permutations, but the order doesn’t matter. Still any number of repetitions allowed.

Definition 1.2 An r-combination of a set of n elements allowing repetitions is a multiset of r elements, each chosen from the set.

Multiset is like a set, but it allows repeated elements. E.g., {R, R, B, B} is multiset consisting of 2 reds and 2 blues. Also written as {2 · R, 2 · B}.

Recall that the number of r-combinations without repetitions is \binom{n}{r} = \frac{n!}{r!(n−r)!}.

Theorem 1.3 The number of r-combinations of n elements, allowing repetitions, is:

\binom{n + r − 1}{r}.

Not so clear why – look at an example.

Example 3 Given 5 red balls, 5 blue balls, 5 green balls. How many different multisets of size 5 can be chosen from among these balls?

Again, this is essentially the unlimited case, since there are enough balls to allow any combination.

Possibilities here: 5R, 5B, 5G 4R − 1B, 4R − 1G, 4B − 1R, 4B − 1G, 4G − 1R, 4G − 1B 3R − 2B, 3R − 2G, 3R − 1B − 1G, (and six others like these) 2R − 2B − 1G (and two others like this)

Total number is: 21 Here, n = 3 since 3 kinds of elements. r = 5. \binom{n+r−1}{r} = \binom{3+5−1}{5} = \binom{7}{5} = 21.

Why is this calculation the right one? Understand it by a thought experiment:

Represent each combination concisely by a string of stars and bars. E.g., 3R − 2B is ***—**— 1R − 3B − 1G is *—***—* The two bars are like dividers between compartments, the stars represent how many of each color. In order, red, blue, green.

Every combination has a unique representation as a string of 5 stars and 2 bars, and every string of 5 stars and 2 bars represents a combination. Bijection. So enough to count the number of different strings of 5 stars and 2 bars. This is \binom{7}{2} = \binom{7}{5} = 21. Think of choosing the positions representing the stars (or bars).

The general proof follows this argument:

Proof. (Combinatorial) There is a bijection between the number of r-combinations of n elements with repetition and the number of strings of stars and bars having exactly r stars and n − 1 bars. The number of such strings is \binom{n+r−1}{r}.

Example 4 How many ways to choose 12 doughnuts from 21 varieties?

This is a combination problem – order doesn’t matter. Allows repetitions. So it’s asking for the number of 12-combinations of a 21-set, with repetitions allowed.

Formula says \binom{21+12−1}{12} =???. Intuitively, see in terms of 20 bars and 12 stars.

Now an equivalent problem – counting the number of ways of putting r indistinguishable balls into n (distinguishable) baskets. To see the equivalence, think of the doughnut problem as putting 12 indistinguishable “doughnut requests” into the 21 doughnut bins.

Example 5 Given n baskets in an amusement park arcade game, r balls (all same), how many different results can you get by throwing the balls into the baskets?

Again, the number of r-combinations of n elements. Can see this by corresponding each result with a pattern of r stars and n − 1 bars.

\binom{n+r−1}{r}

Another equivalent problem: Counting the number of ways to build up a number by addition:

Example 6 In how many ways can a positive integer r be written as the sum of exactly n nonnegative integers, where the order matters?

E.g., write 7 as sum of at most 3 positive integers: 7 + 0 + 0, 0 + 7 + 0, 0 + 0 + 7, 6 + 1 + 0, etc. 5 + 2 + 0, etc.

Again, the number of r-combinations of n elements. Can see this by corresponding each sum with a pattern of r stars and n − 1 bars. The bars are the + signs, the stars are the units being counted.

Here, \binom{7+3−1}{7} = \binom{9}{7} = 36.

Note the preceding is different from the number of ways where the order of terms doesn’t matter.

For 7 and 3, that gives 8: 7 + 0 + 0, 6 + 1 + 0, 5 + 2 + 0, 5 + 1 + 1, 4 + 3 + 0, 4 + 2 + 1, 3 + 3 + 1, 3 + 2 + 2. Q: What is that number, in general?

Example 7 How many increasing 7-sequences are there, of the numbers 1, 2, 3, 4, 5, 6, 7, 8, 9, 10?

There’s one for every way of choosing 7 distinct elements from these 10, i.e., \binom{10}{7}.

What if they are only required to be nondecreasing?

Then there is one for every 7-combination of these elements, allowing repeats. That’s \binom{10+7−1}{7} = \binom{16}{7}.

2 Perms and Combs with Bounded Repetition

2.1 Combinations

So far, these r-combination problems all allowed arbitrary numbers of occurrences of each element of the set. (Arbitrary number of balls in each basket, arbitrary number for each summation term.) Can also consider variations in which there are constraints on the number of occurrences of each element. Start with some lower bounds:

Example 8 In how many ways can 7 be written as the sum of exactly 3 positive integers (that is, no zeros)?

In terms of stars and bars, there are 7 stars and 2 bars, as before, only now there must be at least one star in each compartment. We can just eliminate consideration of 3 stars – their position is determined. What’s left is distributing the remaining 4 stars into the 3 compartments, which is \binom{4+3−1}{4} =

\binom{6}{4} = 15.

In general, to write r as sum of n positive integers (where order matters), where r ≥ n, have

\binom{(r − n) + (n − 1)}{r − n} = \binom{r − 1}{r − n} = \binom{r − 1}{n − 1}.

Express this as another theorem:

Theorem 2.1 The number of r-combinations of n elements, allowing repetitions, and where each element occurs at least once, is

\binom{r − 1}{n − 1}.

Proof. As in the example above. Each such combination can be represented by a sequence of r − n stars and n − 1 bars. The number of these is

\binom{(r − n) + (n − 1)}{r − n} = \binom{r − 1}{r − n} = \binom{r − 1}{n − 1}.

Same thing here:

Example 9 Given n baskets in arcade, r balls, how many different results in which each basket has at least one ball?

This is number of r-combinations of n elements in which each element is represented at least once, which is

\binom{r − 1}{n − 1}.

Generalize this to other constraints:

Example 10 Arcade, n baskets, r balls, but now basket i has to have at least b_{i} balls.

Just subtract off the number that are already assigned to baskets, that is, b = Σ_{i}b_{i}, and allocate the rest to the n baskets.

So, the number of (r − b)-combinations of n elements, which is \binom{r−b+n−1}{r−b} = \binom{r−b+n−1}{n−1}.

Theorem 2.2 The number of r-combinations of n elements, with repetitions, and where each element i occurs at least b_{i} times, is

\binom{r − b + n − 1}{r − b} = \binom{r − b + n − 1}{n − 1},

where b = Σ_{i}b_{i}.

The following examples use these ideas:

Example 11 In how many ways can the 2n + 1 seats in a congress be divided among three parties so that any two parties can form a majority? (Not distinguishing the individuals here – just the combinations of numbers.)

To compute this number, we first compute the number of all assignments of the seats to the parties, without any restrictions. This is the number of 2n + 1-combinations of 3 objects (parties) allowing repetitions:

\binom{3 + (2n + 1) − 1}{2n + 1} = \binom{2n + 3}{2n + 1} = \binom{2n + 3}{2}.

Second, we compute the number of wrong distributions, i.e., when there is a party with at least n + 1 seats. We have three choices for this unique party, then we can give it n + 1 seats and distribute the remaining n seats in

\binom{3 + n − 1}{2} = \binom{n + 2}{2}

ways. Thus the number of wrong distributions is 3\binom{n+2}{2}, so the number of good distributions is

\binom{2n + 3}{2} − 3\binom{n + 2}{2} = \frac{n(n + 1)}{2} = \binom{n + 1}{2}.

Q: Combinatorial interpretation? Well, suppose that we consider distributing negative seats to the three parties instead of positive seats. If everyone starts with presumption of the full 2n + 1, we need to distribute 4n + 2 negative seats. Everyone has to have at least n + 1 negative seats, in order not to have n + 1 positive seats. So that leaves 4n + 2 − 3(n + 1) = n − 1 to be allocated to 3 bins, or

\binom{(n − 1) + 3 − 1}{2} = \binom{n + 1}{2}.

(Get by applying Theorem 2.2, where b = 3(n + 1), r = 4n + 2, n = 3, and \binom{r−b+n−1}{n−1} =

\binom{n−1+2}{2}).

Example 12 What if the total number of seats is even, 2n? Majority is still n + 1. This is more complicated. We can again compute the number of all distributions, this time getting \binom{2n+2}{2}. What about the number of wrong permutations? If the parties B and C fail to have absolute majority, then it means that A has at least n seats. Thus we can give to A n seats in advance and then distribute the n remaining seats to among the three parties in \binom{n+2}{2} ways. The same can be said if B or C has at least n seats. However, we cannot say that “therefore, the number of wrong distributions is 3\binom{n+2}{2}” as we did before, because three distributions, namely (n, n, 0), (n, 0, n) and (0, n, n), were counted twice.

(This couldn’t happen in the previous example since there couldn’t be more than one party that held alone at least the half of the seats.) Therefore, the number of wrong distributions is 3\binom{n+2}{2} − 3, thus the number of good distributions is

\binom{2n + 2}{2} − 3\binom{n + 2}{2} + 3 = \frac{(n − 1)(n − 2)}{2} = \binom{n − 1}{2}.

Q: Combinatorial interpretation? Negative votes, need 4n, each needs at least n + 1, so leaves 4n − 3(n + 1) = n − 3 to be allocated to 3 bins. That’s

\binom{n − 3 + 3 − 1}{2} = \binom{n − 1}{2}.

Can also apply inclusion-exclusion to understand the last example. The wrong combinations are those in which some party has ≥ n seats. This is the union of three sets, one for each party. To count the union, use the sum of the three, minus the sum of the sizes of the pairwise intersections, plus the size of the 3-way intersection. Each set has size \binom{n+2}{2}, for the number of ways of allocating the remaining n seats to the 3 parties. Each 2-way intersection has exactly one possibility. There is no 3-way intersection.

3\binom{n + 2}{2} − 3 · 1

Now consider the r balls in n baskets problem again, only now with upper bound constraints. That is, suppose that we have to have < b_{i} balls in basket i, for all i.

Example 13 10 balls in 4 baskets, with fewer than 7 in each basket. Can calculate by all 10-combinations of 4 elements minus the wrong ones.

All: \binom{4+10−1}{10} = \binom{13}{10}.

Wrong: Some basket has at least 7. Do as union of 4 sets, based on which has at least 7. Disjoint, so don’t need inclusion-exclusion. Each reduces to number of 3-combinations of 4 elements (allowing repetitions), or \binom{3+4−1}{3} = \binom{6}{3}. So, total is \binom{13}{10} − 4\binom{6}{3}.

Example 14 Same, but for 10 balls in 9 baskets, with fewer than 4 in each basket Now will need inclusion-exclusion. All: \binom{9+10−1}{8} = \binom{18}{8}. Wrong: Let A_{i} be the set of combinations in which basket i has ≥ 4 balls. Want size of ∪A_{i}, do by inclusion-exclusion. For each i, |A_{i}| is the number of 6-combinations of 9, which is \binom{9+6−1}{8} = \binom{14}{8}. There are \binom{9}{1} such i. For each i, j, |A_{i} ∩ A_{j}| is the number of 2-combinations of 9, which is \binom{9+2−1}{8} = \binom{10}{8}. There are \binom{9}{2} such pairs. All the 3-way or more-way intersections are empty. So, we have \binom{18}{8} − \binom{9}{1}\binom{14}{8} + \binom{9}{2}\binom{10}{8}.

Extending this idea, we get:

Theorem 2.3 The number of r-combinations of n elements, with repetitions, and where each element i occurs fewer than b times, is: \binom{n+r−1}{n−1} − \binom{n}{1}\binom{n+r−b−1}{n−1} + \binom{n}{2}\binom{n+r−2b−1}{n−1} − … + (−1)^{n}\binom{n}{n}\binom{n+r−nb−1}{n−1}.

In the previous sum the n + r − kb − 1 expressions might become 0, but that’s OK – the binomial coefficients containing them will then reduce to 0, which is what we want. Of course, could also extend to a different bound for each element, LTTR.

2.2 Permutations

One last topic – permutations of elements allowing repeats, with bounds.

The general problem is: Given n elements, with upper and/or lower bounds on number of times to use each, how many different r-permutations are there?

Simplest case: Where each of the n elements has exact bounds – we’re told exactly how many to use of each. This only makes sense when r is equal to the sum of the bounds.

This is the only case we will work out in this course.

Example 15 n = 3; the elements are the letters P, E, R. The bounds are: P − 3, E − 2, R − 1. r = 6.

How many permutations? Same as asking how many permutations of the word PEPPER.

To analyze, subscript each letter to make them distinct, yielding P_{1}, P_{2}, P_{3}, E_{1}, E_{2}, R_{1}. This set has 6! permutations. However, by mapping all the P_{i} to P and all the E_{i} to E, we get a 3!2! : 1 mapping. So by rule of division, we see that the correct number of distinct permutations of PEPPER is \frac{6!}{3!2!}.

The question is the same as asking for the number of permutations of a particular multiset.

Theorem 2.4 Let S be a multiset with finite repetition counts b_{1}, b_{2}, … b_{n}, where b_{1} + b_{2} + … + b_{n} = r = |S|. Then the number of permutations of S is

\frac{r!}{b_{1}!b_{2}! ⋯ b_{n}!}.

We can do a proof using the division rule, as in the example for PEPPER. Give an alternative proof below:

Proof. (Combinatorial) Consider r slots in which we put the elements (exactly one element to each slot) for each permutation. Then there are \binom{r}{b_{1}} ways to choose slots for the first type of element,

then \binom{r−b_{1}}{b_{2}} ways to choose slots for the second type of element from the remaining slots, and so on. This shows that the total number of permutations is:

\binom{r}{b_{1}}\binom{r − b_{1}}{b_{2}} ⋯ \binom{r − b_{1} − ⋯ − b_{n−1}}{b_{n}} = \frac{r!}{b_{1}!b_{2}! ⋯ b_{n}!}

The quantity above is the multinomial coefficient

\binom{r}{b_{1}, b_{2}, …, b_{n}},

which generalizes binomial coefficients, which corresponds to the case n = 2.

2.3 Distinguishable Balls in Baskets

Here is another problem that looks quite different from the permutation problem we just did, but whose solution turns out to look very similar, using multinomial coefficients.

After presenting the problem and its solution, I’ll try to explain how it corresponds to the permutation problem, which will show why the solutions look so much the same.

The problem is another balls-in-baskets problem, with r balls in n baskets, only this time, the balls are distinguishable, e.g., they have numbers written on them. (They are all one color.) We assume that each basket i can hold n_{i} balls, where Σ_{i}n_{i} = r. What we are counting is the number of different patterns that are possible, where a pattern describes the sets of balls in all the baskets.

Example 16 Bridge hands. Have n = 4 for the 4 players. Want to deal out all 52 cards into 4 hands of 13 each. How many possible ways to do this? (Count as different if it’s the same hands but different players.)

There are \binom{52}{13} ways to choose the first player’s hand, then \binom{39}{13} ways to choose the sec-

ond player’s hand from the remaining cards, then \binom{26}{13} ways to choose the third player’s hand from the remaining cards, then there is only one way for the last player.

So (using trees), the total number of ways is: \binom{52}{13}\binom{39}{13}\binom{26}{13}, which is

\frac{52!}{13!39!} \frac{39!}{13!26!} \frac{26!}{13!13!} = \frac{52!}{13!13!13!13!}.

This latter is the multinomial coefficient \binom{52}{13,13,13,13}.

Example 17 Poker hands, dealing 5 to each of 4 players. Corresponds to 5 bins, where one bit represents all the cards that aren’t dealt out. Then get \binom{52}{5}\binom{47}{5}\binom{42}{5}\binom{37}{5}, which reduces to the multinomial coefficient \binom{52}{5,5,5,5,32}.

In general, for n bins with capacities b_{1}, …, b_{n} with sum r, we get the multinomial coefficient \binom{r}{b_{1},b_{2},...,b_{n}}.

Same answer as for number of permutations of r elements, where groups of b_{1}, b_{2}, …, b_{n} are indistinguishable.

Why are these the same problem? Show a bijection between the two sets. Let B be the set of patterns of balls in baskets (dealt hands), and P the set of permutations of the “capacity multiset” b_{1} · 1, b_{2} · 2, …, b_{n} · n

Arrange all the r balls in a sequence R, and number them according to their order in this sequence. So we can talk about ball 1, ... ball r.

Consider any element of B, a pattern of r balls in the n baskets, with b_{i} in basket i. Represent it by a permutation of the capacity multiset as follows: In the sequence R, replace each ball that goes in basket i with i, the name of the basket itself.

This clearly yields a unique permutation of the capacity multiset. Two different patterns don’t yield the same permutation (one-to-one). Every permutation of the capacity multiset arises from some pattern.

Inverse: Given a permutation of the capacity multiset, assign the balls whose positions are labelled with i to basket i.

Example 18 Bridge hands, enumerate the cards 1, …, 52 in the usual order, ace to king, spades, hearts, diamonds, clubs. The two sets are the deals, vs. the set of permutations of the multiset {13·1, 13·2, 13·3, 13·4}.

Here, 1 − 4 represent the four players. Represent a deal by putting 1 in the positions in the sequence indicating the cards that are dealt to player 1, and similarly for 2, 3 and 4.