Lecture notes on counting, used in CS 70 Fall 2001 (Paraphrased from Prof. Demmel's Math 55 Lecture notes #15, #17) Review Counting Principles 1) The Sum Rule: If S1 and S2 are disjoint sets, then the number of members of S1 U S2 is |S1 U S2| = |S1| + |S2| 2) Inclusion-Exclusion Principle: If S1 and S2 are arbitrary sets, then |S1 U S2| = |S1| + |S2| - |S1 inter S2| 3) The Product Rule: If S1 and S2 are sets, and S1 x S2 ={(s1,s2) : s1 in S1 and s2 in S2} is the Cartesian product of S1 and S2, then |S1 x S2| = |S1| x |S2| If S1, S2, ... , Sk are sets, S1 x S2 x ... Sk the Cartesian product, then |S1 x ... x Sk| = |S1| x ... x |Sk| 4) Tree diagrams EX: How many bit strings of length 4 without two consecutive zeros? Ans: 8; Enumerate all possibilities in a tree: Legal bit strings 0 - 1 0101 / / 0 0110 0 - 1 - 1 - 1 0111 / / 0 1010 / 0 - 1 - 1 1011 / / 0 - 1 1101 / / / / 0 1110 x -----1 - 1 - 1 - 1 1111 Bit # 1 2 3 4 Tree Diagrams (formally) Draw a tree where the children of each node represent all the possible values of the next entry EX: How many 2-out-of-3 game playoffs are there? Ans: 6 Winner sequence Winner b bb b / / b bab b b - a - a baa a / / b abb b / / b - a aba a x -- a - a aa a (5) Pigeonhole Principle: If k+1 or more objects (pigeons) are placed in k boxes (holes), then at least one box contains 2 or more objects. (proof by contradition: if each box had at most one object, there would only be k or fewer objects, a contradiction) EX: In any group of 27 English words, at 2 begin with the same letter, since there are only 26 letters. 7) Permuations DEF: a permutation of a set S of n distinct objects is an ordered list of these objects DEF: an r-permutation is an ordered list of r elements of S EX: S={1,2,3}, all permutations={(1,2,3),(2,1,3),(1,3,2),(2,3,1),(3,1,2),(3,2,1)} all 2-permutations={(1,2),(2,1),(1,3),(3,1),(2,3),(3,2)} DEF: the number of r-permutations of a set S with n elements is P(n,r) Theorem: P(n,r) = n*(n-1)*(n-2)*...*(n-r+1) = n!/(n-r)! Proof: (product rule): there are n ways to choose the first in list, n-1 ways to choose second, ... , n-r+1 ways to choose rth EX: P(3,3)=3*2*1=6, P(3,2)=3*2=6 EX: how many different ways can a salesman visit 8 cities? P(8,8)=8!=40320 EX: How many different ways can 10 horses in a race win, place and show (come in first, second, third)? P(10,3) = 10*9*8 = 720 8) Combinations DEF: an r-combination from a set S is simply an unordered subset of r elements from S EX: S={1,2,3}, all 2-combinations={{1,2},{1,3},{2,3}} Comparing to all 2-permutations, we see we ignore order, DEF: C(n,r) = number of r-combinations from a set with n-elements Theorem: C(n,r) = n! / [ (n-r)! r! ] Proof: the set of all r-permutations can be formed from the set of all r-combinations by taking all r! orderings of each r-combination, so P(n,r)=r! * C(n,r), and C(n,r)=P(n,r)/r!= n! / [ (n-r)! r! ]= n*(n-1)*(n-2)*...*(n-r+1)/r! EX: C(3,2)=P(3,2)/2!=6/2=3 DEF C(n,r) also called binomial coefficient, written (n \\ r), pronounced "n choose r" Note that C(0,0)= 0!/0!*0! = 1; C(n,0)=C(n,n)=1 Corollary: C(n,r)=C(n,n-r) Proof: C(n,r)=n!/[(n-r)! r!] = n!/[ r! (n-r)!] = n!/[(n-(n-r))! (n-r)!] = C(n,n-r) EX C(3,1)=C(3,2)=1 DEF Pascal triangle: (0) (0) (1) (1) (0) (1) (2) (2) (2) (0) (1) (2) (3) (3) (3) (3) (0) (1) (2) (3) (4) (4) (4) (4) (4) (0) (1) (2) (3) (4) (5) (5) (5) (5) (5) (5) (0) (1) (2) (3) (4) (5) (6) (6) (6) (6) (6) (6) (6) (0) (1) (2) (3) (4) (5) (6) ... row sum 1 1 1 1 2 1 2 1 4 1 3 3 1 8 1 4 6 4 1 16 1 5 10 10 5 1 32 1 6 15 20 15 6 1 64 Note that to get any entry, you sum its neighbors to left above, right above EX: How many different desserts can you make out of 4 scoops of ice cream, each of which may be chocolate (C), vanilla (V) or strawberry (S)? Here are the 15 possibilities: CCCC VVVV SSSS CCCV VVVC SSSC CCCS VVVS SSSV CCVS VVCS SSCV CCVV VVSS CCSS Here is a more systematic way to get the answers: we will represent each dessert by a sequence of 4 stars (representing the 4 scoops) and 2 bars (dividing the starts into 3 groups: C, V and S). Here are some examples: **|*|* represents 2 Cs, 1 V and 1 S *|**|* represents 1 C , 2 V's and 1 S *|***| represents 1 C , 3 V's and 0 S's |****| represents 0 C , 4 V's and 0 S's ||**** represents 0 C , 0 V's and 4 S's etc The idea is that every sequence of 4 stars and 2 bars represents exactly one dessert. How many such sequences are there? The idea is that we take 6 possible possible positions (for 4 stars and 2 bars) and choose 2 of them for bars. There are C(6,2) = 6!/(2! 4!) = 15 ways to do this. Here is the general result: Theorem: Suppose I have n types of objects ("flavors"). How many differnt sets ("desserts") consisting of r objects ("scoops") are there? The answer is C(n+r-1,n-1). Proof: The idea is the same as before: each sequence of r stars ("scoops") and (n-1) bars represents a possible set. There are C(n+r-1,n-1) ways to pick n-1 places out of r+n-1 locations to put the bars. Ex: If I have n=3 flavors of ice cream, and make desserrs of r=4 scoops, there are C(n+r-1,n-1)=C(3+4-1,3-1)=C(6,2)=16 difference desserts. EX: How many anagrams are there of the word "mammal"? Recall that an anagram is a distint ordering of the letters. Here are some smaller examples: the word "the": The 6 anagrams are the, teh, eth, eht, het, hte the word "see": The 3 anagrams are see, ese, ees Here are different ways to try to solve this problem for the word "mammal", followed by the general result: Solution 1: Pick 3 locations for the m's Pick 2 of the remaining locations for the 2 a's Pick the remaining location for l By the product rule, the number of ways to pick locations is C(6,3) ... for the m's * C(3,2) ... for the a's * C(1,1) ... for the l = 20*3*1 = 60 Solution 2: Pick 1 location for the l Pick 3 of the remaining locations for the m's Pick the remaining 2 locations for the a's By the product rule, the number of ways to pick locations is C(6,1) ... for the l * C(5,3) ... for the m's * C(2,2) ... for the a's = 6*10*1 = 60, the same answer (whew!) Solution 3: Let us start by labeling the m's as m1,m2 and m3, and and the a's as a1 and a2, so we can distinguish them. So now we have 6 distinct symbols, m1,a1,m2,m3,a2,l, and the number of ways to order them is 6!. But clearly we have counted some ordering as distinct that we should not, so let's try to divide out by the number of multiple copies. For example, consider all the orderings where the first 3 characters are m's, and the last three are a1,a2,l. The are clearly 3! = 6 such orderings, since m1,m2,m3 can appear in the first three positions in any order, but yield the same anagram. This argument that we are counting each anagram 3! times works no matter where the 3 m's appear, so we should divide the number of orderings by 3! to account for the 3 m's. Similary, we should divide by 2! to account for the two a's. This yields 6!/ (3! 2!) = 60, the same answer (whew!)