Counting III: 1.1 20 Mile Walks
Counting III: 1.1 20 Mile Walks
062J Mathematics for Computer Science Srini Devadas and Eric Lehman
Counting III
Today well briey review some facts you dervied in recitation on Friday and then turn to some applications of counting.
10! 1! 2! 2! 3! 1! 1!
Bs Os Ks Es Ps Rs
This is a special case of an exceptionally useful counting principle. Rule 1 (Bookkeeper Rule). The number of sequences with n1 copies of l1 , n2 copies of l2 , . . . , and nk copies of lk is (n1 + n2 + . . . + nk )! n1 ! n2 ! . . . nk ! provided l1 , . . . , lk are distinct. Lets review some applications and implications of the Bookkeeper Rule.
Counting III
1.3
How many kelements subsets of an nelement set are there? This question arises all the time in various guises: In how many ways can I select 5 books from my collection of 100 to bring on vaca tion? How many different 13card Bridge hands can be dealt from a 52card deck? In how many ways can I select 5 toppings for my pizza if there are 14 available? There is a natural bijection between kelement subsets of an nelement set and nbit sequences with exactly k ones. For example, here is a 3element subset of {x1 , x2 , . . . , x8 } and the associated 8bit sequence with exactly 3 ones: { x1 , x4 , x 5 } ( 1, 0, 0, 1, 1, 0, 0, 0 ) Therefore, the answer to this problem is the same as the answer to the earlier question about bit sequences. Rule 2 (Subset Rule). The number of kelement subsets of an nelement set is: n! n = k! (n k)! k The factorial expression in the Subset Rule comes up so often that there is a shorthand, n . This is read n choose k since it denotes the number of ways to choose k items from k among n. We can immediately knock off all three questions above using the Sum Rule: I can select 5 books from 100 in 100 ways. 5 There are 52 different Bridge hands. 13
The kelement subsets of an nelement set are sometimes called kcombinations. There are a great many similarsounding terms: permutations, rpermutations, permutations with repetition, combinations with repetition, permutations with indistinguishable ob jects, and so on. For example, the Bookkeeper Rule is sometimes called the formula for permutations with indistinguishable objects. Broadly speaking, permutations concern se quences and combinations concern subsets. However, the counting rules weve taught you are sufcient to solve all these sorts of problems without knowing this jargon, so well skip it.
Binomial Theorem
Counting gives insight into one of the basic theorems of algebra. A binomial is a sum of two terms, such as a + b. Now lets consider a postive, integral power of a binomial: (a + b)n Suppose we multiply out this expression completely for, say, n = 4: (a + b)4 = aaaa + aaab + aaba + aabb + abaa + abab + abba + abbb + baaa + baab + baba + babb + bbaa + bbab + bbba + bbbb Notice that there is one term for every sequence of as and bs. Therefore, the number of terms with k copies of b and n k copies of a is: n! n = k! (n k)! k by the Bookkeeper Rule. Now lets group equivalent terms, such as aaab = aaba = abaa = baaa. Then the coefcient of ank bk is n . So for n = 4, this means: k 4 4 4 4 4 4 0 3 1 2 2 1 3 4 a b + a b + a b + a b + a0 b4 (a + b) = 0 1 2 3 4 In general, this reasoning gives the Binomial Theorem:
Counting III
The expression
n
k
This reasoning about binomials extends nicely to multinomials, which are sums of two or more terms. For example, suppose we wanted the coefcient of bo2 k 2 e3 pr in the expansion of (b + o + k + e + p + r)10 . Each term in this expansion is a product of 10 variables where each variable is one of b, o, k, e, p, or r. Now, the coefcient of bo2 k 2 e3 pr is the number of those terms with exactly 1 b, 2 os, 2 ks, 3 es, 1 p, and 1 r. And the number of such terms is precisely the number of rearrangments of the word BOOKKEEPER: 10! 10 = 1! 2! 2! 3! 1! 1! 1, 2, 2, 3, 1, 1 The expression on the left is an example of a multinomial coefcient and the notation on the right is a shorthand. This reasoning extends to a general theorem: Theorem 2 (Multinomial Theorem). For all n N and z1 , . . . zm R: n km n (z1 + z2 + . . . + zm ) = z k1 z k2 . . . zm k1 , k2 , . . . , km 1 2 k ,...,k N
1 m
k1 +...+km =n
Youll be far better off if your remember the reasoning behind the Multinomial Theo rem rather than this monstrous equation.
3 Poker Hands
There are 52 cards in a deck. Each card has a suit and a value. There are four suits:
spades
hearts
clubs
diamonds
jack queen
2, 3, 4, 5, 6, 7, 8, 9, J , Q , K, A
king ace
Thus, for example, 8 is the 8 of hearts and A is the ace of spades. Values farther to the right in this list are considered higher and values to the left are lower.
Counting III
FiveCard Draw is a card game in which each player is initially dealt a hand, a subset of 5 cards. (Then the game gets complicated, but lets not worry about that.) The number of different hands in FiveCard Draw is the number of 5element subsets of a 52element set, which is 52 choose 5: 52 total # of hands = = 2, 598, 960 5 Lets get some counting practice by working out the number of hands with various special properties.
Counting III
Counting III 5. The value of the extra card, which can be chosen in 11 ways. 6. The suit of the extra card, which can be selected in 4 = 4 ways. 1 Thus, it might appear that the number of hands with Two Pairs is: 4 4 12 11 4 13 2 2
Wrong answer! The problem is that there is not a bijection from such sequences to hands with Two Pairs. This is actually a 2to1 mapping. For example, here are the pairs of sequences that map to the hands given above: (3, {, } , Q, {, } , A, ) { 3, 3, Q, Q, A } (Q, {, } , 3, {, } , A, ) (9, {, } , 5, {, } , K, )
{ 9, 9, 5,
5, K } (5, {, } , 9, {, } , K, ) The problem is that nothing distinguishes the rst pair from the second. A pair of 5s and a pair of 9s is the same as a pair of 9s and a pair of 5s. We avoided this difculty in counting Full Houses because, for example, a pair of 6s and a triple of kings is different from a pair of kings and a triple of 6s. We ran into precisely this difculty last time, when we went from counting arrange ments of different pieces on a chessboard to counting arrangements of two identical rooks. The solution then was to apply the Division Rule, and we can do the same here. In this case, the Division rule says there are twice as many sequences and hands, so the number of hands with Two Pairs is actually: 13 4
12
4
11 4 2 2 2 Another Approach The preceding example was disturbing! One could easily overlook the fact that the map ping was 2to1 on an exam, fail the course, and turn to a life of crime. You can make the world a safer place in two ways: 1. Whenever you use a mapping f : A B to translate one counting problem to another, check the number elements in A that are mapped to each element in B. This determines the size of A relative to B. You can then apply the Division Rule with the appropriate correction factor.
Counting III 2. As an extra check, try solving the same problem in a different way. Multiple ap proaches are often available and all had better give the same answer! (Sometimes different approaches give answers that look different, but turn out to be the same after some algebra.)
We already used the rst method; lets try the second. There is a bijection between hands with two pairs and sequences that specify: ways. 2. The suits of the lowervalue pair, which can be selected in 4 ways. 2 3. The suits of the highervalue pair, which can be selected in 4 ways. 2 1. The values of the two pairs, which can be chosen in
2
13
4. The value of the extra card, which can be chosen in 11 ways. 5. The suit of the extra card, which can be selected in 4 = 4 ways. 1 For example, the following sequences and hands correspond: ({3, Q} , {, } , {, } , A, ) { 3, 3, Q, Q, A } ({9, 5} , {, } , {, } , K, ) { 9, 9, 5, 5, K } Thus, the number of hands with two pairs is: 4 4 13 11 4 2 2 2 This is the same answer we got before, though in a slightly different form.
Counting III For example, the hand above is described by the sequence: (7, K, A, 2, , 3) { 7, K, A, 2, 3 }
Are there other sequences that correspond to the same hand? There is one more! We could equally well regard either the 3 or the 7 as the extra card, so this is actually a 2to1 mapping. Here are the two sequences corresponding to the example hand: (7, K, A, 2, , 3) { 7, K, A, 2, 3 } (3, K, A, 2, , 7) Therefore, the number of hands with every suit is: 134 4 12 2
4 Magic Trick
There is a Magician and an Assistant. The Assistant goes into the audience with a deck of 52 cards while the Magician looks away. Five audience members each select one card from the deck. The Assistant then gathers up the ve cards and reveals four of them to the Magician, one at a time. The Magician concentrates for a short time and then correctly names the secret, fth card!
Counting III
Y = all sequences of 4 distinct cards
{8, K, Q, 9, 6}
For example, {8, K, Q, 2, 6} is an element of X on the left. If the audience selects this set of 5 cards, then there are many different 4card sequences on the right in set Y that the Assistant could choose to reveal, including (8, K, Q, 2), (K, 8, Q, 2), and (K, 8, 6, Q). Lets think about this problem in terms of graphs. Regard the elements of X and Y as the vertices of a bipartite graph. Put an edge between a set of 5 cards and a sequence of 4 if every card in the sequence is also in the set. In other words, if the audience selects a set of cards, then the Assistant must reveal a sequence of cards that is adjacent in the bipartite graph. Some edges are shown in the diagram above. What we need to perform the trick is a matching for the X vertices; that is, we need a subset of edges that join every vertex on the left to exactly one, distinct vertex on the right. If such a matching exists, then the Assistant and Magician can agree one in advance. Then, when the audience selects a set of 5 cards, the Assistant reveals the corresponding sequence of 4 cards. The Magician translates back to the correspoding set of 5 cards and names the one not already revealed. For example, suppose the Assistant and Magician agree on a matching containing the two bold edges in the diagram above. If the audience selects the set {8, K, Q, 9, 6}, then the Assistant reveals the corresponding sequence (K, 8, 6, Q). The Magician names the one card in the corresponding set not already revealed, the 9. Notice that the sets must be matched with distinct sequences; if the Assistant revealed the same sequence when the audience picked the set {8, K, Q, 2, 6}, then the Magician would be unable to determine whether the remaining card was the 9 or 2! The only remaining question is whether a matching for the X vertices exists. This is precisely the subject of Halls Theorem. Regard the X vertices as girls, the Y vertices as boys, and each edge as an indication that a girl likes a boy. Then a matching for the girls exists if and only if the marriage condition is satised: Every subset of girls likes at least as large a set of boys. Lets prove that the marriage condition holds for the magic trick graph. Well need a couple preliminary facts:
Counting III
11
Each vertex on the left has degree 5 4! = 120, since there are ve ways to select the card kept secret and there are 4! permutations of the remaining 4 cards. In terms of the marriage metaphor, every girl like 120 boys. Each vertex on the right has degree 48, since there are 48 possibilities for the fth card. Thus, every boy is liked by 48 girls. Now let S be an arbitrary set of vertices on the left, which were regarding as girls. There are 120 |S| edges incident to vertices in this set. Since each boy is liked by at most 48 girls, this set of girls likes at least 120 | | /48 |S | different boys. Thus, the marriage condition S is satised, a matching exists by Halls Theorem, and the trick can be done without magic!
For any two distinct values on this cycle, one is always between 1 and 6 hops clock wise from the other. For example, the 3 is 6 hops clockwise from the 10. The more counterclockwise of these two cards is revealed rst, and the other be comes the secret card. Thus, in our example, the 10 would be revealed, and the 3 would be the secret card. Therefore: The suit of the secret card is the same as the suit of the rst card revealed.
12
Counting III The value of the secret card is between 1 and 6 hops clockwise from the value of the rst card revealed. All that remains is to communicate a number between 1 and 6. The Magician and Assistant agree beforehand on an ordering of all the cards in the deck from smallest to largest such as: A 2 . . . K A 2 . . . Q A 2 . . . Q A 2 . . . Q The order in which the last three cards are revealed communicates the number ac cording to the following scheme: ( small, medium, large ) =1 ( small, large, medium ) = 2 ( medium, small, large ) =3 ( medium, large, small ) =4 ( large, small, medium ) = 5 ( large, medium, small ) =6 In the example, the Assistant wants to send 6 and so reveals the remaining three cards in large, medium, small order. Here is the complete sequence that the Magi cian sees: 10 Q J 9 The Magician starts with the rst card, 10, and hops 6 values clockwise to reach 3, which is the secret card!
Counting III
13
5 Combinatorial Proof
Suppose you have n different Tshirts only want to keep k. You could equally well select the k shirts you want to keep or select the complementary set of n k shirts you want to throw out. Thus, the number of ways to select k shirts from among n must be equal to the number of ways to select n k shirts from among n. Therefore: n n = k nk This is easy to prove algebraically, since both sides are equal to: n! k! (n k)! But we didnt really have to resort to algebra; we just used counting principles. Hmm.
5.1 Boxing
Ishan, famed 6.042 TA, has decided to try out for the US Olympic boxing team. After all, hes watched all of the Rocky movies and spent hours in front of a mirror sneering, Yo, you wanna piece a me?! Ishan gures that n people (including himself) are competing for spots on the team and only k will be selected. As part of maneuvering for a spot on the team, he need to work out how many different teams are possible. There are two cases to consider: Ishan is selected for the team, and his k 1 teammates are selected from among the other n 1 competitors. The number of different teams that be formed in this way is: n1 k1 Ishan is not selected for the team, and all k team members are selected from among the other n 1 competitors. The number of teams that can be formed this way is: n1 k All teams of the rst type contain Ishan, and no team of the second type does; therefore, the two sets of teams are disjoint. Thus, by the Sum Rule, the total number of possible Olympic boxing teams is: n1 n1 + k1 k
14
Counting III
Christos, equallyfamed 6.042 TA, thinks Ishan isnt so tough and so he might as well try out also. He reasons that n people (including himself) are trying out for k spots. Thus, the number of ways to select the team is simply: n k Christos and Ishan each correctly counted the number of possible boxing teams; thus, their answers must be equal. So we know: n1 n1 n + = k1 k k This is called Pascals Identity. And we proved it without any algebra! Instead, we relied purely on counting techniques.
Proof. We give a combinatorial proof. Let S be all ncard hands that can be dealt from a deck containing n red cards (numbered 1, . . . , n) and 2n black cards (numbered 1, . . . , 2n). First, note that every 3nelement set has 3n |S | = n
15
From another perspective, the number of hands with exactly r red cards is n 2n r nr 2n since there are n ways to choose the r red cards and nr ways to choose the n r black r cards. Since the number of red cards can be anywhere from 0 to n, the total number of ncard hands is: n n 2n |S | = r nr r=0 Equating these two expressions for |S| proves the theorem. Combinatorial proofs are almost magical. Theorem 3 looks pretty scary, but we proved it without any algebraic manipulations at all. The key to constructing a combinatorial proof is choosing the set S properly, which can be tricky. Generally, the simpler side of theequation should provide some guidance. For example, the right side of Theorem 3 is 3n , which suggests choosing S to be all nelement subsets of some 3nelement set. n