Coutig Methods CSE 191, Class Note 05: Coutig Methods Computer Sci & Eg Dept SUNY Buffalo c Xi He (Uiversity at Buffalo CSE 191 Discrete Structures 1 / 48 Need for Coutig The problem of coutig the umber of differet objects appears i may computer sciece applicatios. Some examples: All Java programs use idetifiers. Say, each idetifier is limited to 8 characters log, the first character must be a letter. How may possible idetifiers are there? I GPS map routig applicatio, we kow the startig locatio, the edig locatio, ad the itermediate locatios o the map, how may differet routes are there? (The routig program must select the best route from these routes. I Facebook applicatio, we are give three users A, B, C. We kow the set of the frieds of A, B, C. How do we cout the umber of users who are the fried of at least oe of A, B, C? We will discuss some commoly used coutig methods. c Xi He (Uiversity at Buffalo CSE 191 Discrete Structures 2 / 48
Product Rule Product Rule Suppose that a procedure ca be broke ito a sequece of two tasks. If there are 1 ways to do the first task ad 2 ways to do the secod task, the there are 1 2 ways to do the procedure. Example 1: The chairs i a auditorium are labeled by upper case Eglish letters followed by a iteger betwee 0 ad 99. What s the largest umbers of chairs that ca be labeled this way? Solutio: There are 26 ways to pick letters. There are 100 ways to pick itegers. So, by Product Rule, there are 26 100 = 2600 ways to label chairs. c Xi He (Uiversity at Buffalo CSE 191 Discrete Structures 4 / 48 Product Rule The Product Rule ca be easily geeralized from 2 tasks to k tasks for ay iteger k. Example 2: How may differet bit strigs of legth 8 are there? Solutio: 2 8 = 256. Example 3: Suppose that Java program idetifiers cosist of oly upper ad lower cases Eglish letters ad digits. The first character must be a upper case letter. The last character must be a digit. (They are ot real requiremets, just a example. Cout the umber of possible idetifiers of legth exactly 6. Solutio: There are 26 choices for the 1st character. There are 26+26+10=62 choices for each of the 2d - 5th character. There are 10 choices for the last character. So the aswer is 26 62 62 62 62 10. c Xi He (Uiversity at Buffalo CSE 191 Discrete Structures 5 / 48
Product Rule Notatio: A deotes the umber of elemets i the set A. Product Rule: A 1 A 2... A m = A 1 A 2... A m. Example 4: Show that the umber of differet subsets of a fiite set S is 2 S. Let S = {s 1, s 2,... s } be a fiite set cotaiig S = elemets. To describe a subset A S, we eed to make the followig choices: whether to iclude s 1 or ot; (2 choices; whether to iclude s 2 or ot; (2 choices;... whether to iclude s or ot; (2 choices; The differet choices i each step ( steps i total will result i differet subsets. So the umber of differet subsets of S is 2 = 2 S. c Xi He (Uiversity at Buffalo CSE 191 Discrete Structures 6 / 48 Sum Rule Sum Rule Let A 1, A 2,..., A m be differet sets such that A i A j = for all i j. The: A 1 A 2... A m = A 1 + A 2 + + A m Example 5: I a high school, 32 studets take Frech class, 45 studets take Spaish. Assumig o studet take both Frech ad Spaish, how may studets take either Frech or Spaish? Solutio: Simple eough: 32+45= 77. It is simple because we assumed A i A j =. What if we drop this assumptio? c Xi He (Uiversity at Buffalo CSE 191 Discrete Structures 8 / 48
Iclusio-Exclusio Priciple Iclusio-Exclusio Priciple (with two sets Let A 1, A 2 be two sets. The: A 1 A 2 = A 1 + A 2 A 1 A 2 Example 5a: I a high school; 32 studets take Frech class, 45 studets take Spaish. 15 studet take both Frech ad Spaish. How may studets take either Frech or Spaish? Solutio: 32 + 45 15 = 62. c Xi He (Uiversity at Buffalo CSE 191 Discrete Structures 9 / 48 Iclusio-Exclusio Priciple Example 7: Determie the umber of 8-bits biary strigs such that: Either the first bit is 1; Or the last two bits are 00. Let A be the set of 8-bits biary strigs whose first bit is 1. Let B be the set of 8-bits biary strigs whose last two bits are 00. We wat to determie A B. A = 2 7 (because the first bit is fixed. B = 2 6 (because the last two bits are fixed. A B = 2 5 (because the first ad the last two bits are fixed. So A B = 2 7 + 2 6 2 5 = 160. This priciple ca be geeralized to more tha two sets. c Xi He (Uiversity at Buffalo CSE 191 Discrete Structures 10 / 48
Iclusio-Exclusio Priciple Iclusio-Exclusio Priciple (with three sets Let A 1, A 2, A 3 be three sets. The: A 1 A 2 A 3 = A 1 + A 2 + A 3 ( A 1 A 2 + A 1 A 3 + A 2 A 3 + A 1 A 2 A 3 Example 6: I a high school, 32 studets take Frech class, 45 studets take Spaish, 20 take Germa. 15 studets take both Frech ad Spaish; 12 studets take both Germa ad Spaish; 10 studets take both Frech ad Germa; 6 studets take all three foreig laguages. How may studets take at least oe foreig laguage? Solutio: 32 + 45 + 20 (15 + 12 + 10 + 6 = 66. c Xi He (Uiversity at Buffalo CSE 191 Discrete Structures 11 / 48 Iclusio-Exclusio Priciple Iclusio-Exclusio Priciple (geeral case: Let A 1, A 2,... A m be m sets. The: A 1... A m = A 1 + A 2 +... + A m (sum of A i A j for all possible i j +(sum of A i A j A k for all possible i j k (sum of A i A j A k A l for all possible i j k l... +( 1 m 1 A 1 A 2... A m Exercise: Write the geeral formula for the case m = 4. Note: For m > 4, this formula is too complicated to be useful. c Xi He (Uiversity at Buffalo CSE 191 Discrete Structures 12 / 48
Permutatios Defiitio A permutatio of a set of distict objects is a ordered arragemet of these objects. A ordered arragemet of r elemets of a set is called a r-permutatio. The umber of r-permutatios of a set with elemets is deoted by P(, r. c Xi He (Uiversity at Buffalo CSE 191 Discrete Structures 14 / 48 Permutatios Example 1: I how may ways ca we select 3 studets from a group of 5 studets to stad i lie for a picture? Solutio: We ca select ay of the 5 studets for the first positio. After the first positio is selected, we ca choose ay of the 4 remaiig studets for the 2d positio. After the 1st ad the 2d positios are selected, we ca choose ay of the 3 remaiig studets for the 3rd positio. So, the aswer is: 5 4 3 = 60. I this example, we are coutig the umber of distict 3-permutatios i a set of 5 elemets. What we have show is: P(5, 3 = 5 4 3 = 60 c Xi He (Uiversity at Buffalo CSE 191 Discrete Structures 15 / 48
Permutatios Defiitio: The factorial of is deoted by! ad defied by:! = ( 1 ( 2 2 1 Note: For coveiece, we defie: 0!=1. Theorem 1: P(, r = ( 1 ( r + 1 = }{{} r terms P(, = ( 1 2 1 =!! ( r! c Xi He (Uiversity at Buffalo CSE 191 Discrete Structures 16 / 48 Permutatios Example 2: How may ways are there to select a first-prize wier; a secod-prize wier; ad a third-prize wier from 100 differet people? Solutio: P(100, 3 = 100 99 98 = 970200. Example 3: A salesma must visit eight differet cities. He must begi his trip from a specific city (his home, but he ca visit other seve cities i ay order, the he must retur to his home city. How may possible orders ca he use? Solutio: 7! = 5040. c Xi He (Uiversity at Buffalo CSE 191 Discrete Structures 17 / 48
Permutatios Example 3: How may permutatios of the letters i {A, B, C, D, E, F, G} cotai the strig ABC. Solutio: There are 7 letters. Is 7! the solutio? No. Sice the strig ABC must appear as a block, it is like a sigle symbol. So we are really askig the umber of permutatios from 5 symbols. So the solutio is 5! = 120. c Xi He (Uiversity at Buffalo CSE 191 Discrete Structures 18 / 48 Combiatios Defiitio A r-combiatio of elemets of a set is a uordered selectio of r elemets from the set. So a r-combiatio is simply a subset with r elemets. The umber ( of r-combiatios of a set with elemets is deoted by C(, r or. r Example 1: How may differet ways to select a committee of three members from 10 studets? Solutio: Each committee is just a subset of three elemets. So the aswer is simply C(10, 3. c Xi He (Uiversity at Buffalo CSE 191 Discrete Structures 20 / 48
Combiatios Theorem 2: For ay o-egative iteger ad r such that 0 r : C(, r =! r!( r! = ( 1 ( r + 1 r (r 1 2 1 Proof: Cosider ay r-combiatio, which is just a r elemet set A. There are r! differet orderigs of the elemets i A. Each of which is a r-permutatio of the set. Thus: Sice P(, r =! ( r!, we have: C(, r = P(, r r! = P(, r = C(, r r!! r!( r! = r terms {}}{ ( 1 ( r + 1 r! c Xi He (Uiversity at Buffalo CSE 191 Discrete Structures 21 / 48 Combiatios Example 1: How may differet ways to select a committee of three members from 10 studets? Solutio: C(10, 3 = 10 9 8 3 2 1 = 120. Example 2: (a How may poker hads of 5 cards ca be selected from a stadard 52 cards? (b How may hads of 47 cards ca be selected from a stadard 52 cards? Solutio: (a C(52, 5 = 52 51 50 49 48 5 4 3 2 1 = 2, 598, 960. (b C(52, 47 = 52 51 50 7 6 47 46 3 2 1. Is there a better way? c Xi He (Uiversity at Buffalo CSE 191 Discrete Structures 22 / 48
Combiatios Corollary Proof: Both of them equal to! r!( r!. C(, r = C(, r c Xi He (Uiversity at Buffalo CSE 191 Discrete Structures 23 / 48 Combiatios Example 3: I CSE departmet, there are 8 full professors, 7 associate professors, ad 11 assistat professors. We wat to form a committee to evaluate BS degree program. The committee must cosist of 4 full professors, 3 associate professors ad 3 assistat professors. How may differet ways to select the committee? The umber of ways to select full professors: C(8, 4 The umber of ways to select associate professors: C(7, 3 The umber of ways to select assistat professors: C(11, 3 By product rule, the solutio is: C(8, 4 C(7, 3 C(11, 3 = 8! 4!4! 7! 3!4! 11! 3!8! = 404250 c Xi He (Uiversity at Buffalo CSE 191 Discrete Structures 24 / 48
Biomial Coefficiets ad Idetities ( r is also called a biomial coefficiet. Why? Example: ( 0 (x + y 0 = 1 = 0 ( ( 1 1 (x + y 1 = x + y = x + y 0 1 ( ( ( 2 2 2 (x + y 2 = x 2 + 2xy + y 2 = x 0 2 + xy + y 1 2 2 ( ( ( ( 3 3 3 3 (x + y 3 = x 3 + 3x 2 y + 3xy 2 + y 3 = x 0 3 + x 1 2 y + xy 2 2 + y 3 3... ( (x + y is called a biomial. is a coefficiet i the expasio of the r biomial. c Xi He (Uiversity at Buffalo CSE 191 Discrete Structures 26 / 48 Biomial Coefficiets ad Idetities The Biomial Theorem: Let x ad y be variables ad be a o-egative iteger. The: (x + y = ( j=0 x j j y j = ( ( ( ( ( x 0 + x 1 1 y + x 2 2 y 2 + + xy 1 1 + y Proof: The geeral terms i the product are of the forms x j y j for 0 j. We eed to cout the umber of the terms of the form x j y j. To get such a term, we must pick j x s (so that the remaiig j terms i the product are y s. ( ( So the umber of the terms x j y j is, which is also. j j c Xi He (Uiversity at Buffalo CSE 191 Discrete Structures 27 / 48
Biomial Idetities Corollary 1: j=0 ( j = 2. Proof: Let x = 1 ad y = 1 i the Biomial Theorem, we have: 2 = (1 + 1 = ( j j=0 1 j 1 j = ( j j=0 Corollary 2: j=0 ( 1j ( j = 0. Proof: Let x = 1 ad y = 1 i the Biomial Theorem, we have: 0 = (1 1 = j=0 ( j 1 j ( 1 j = ( ( 1 j j j=0 c Xi He (Uiversity at Buffalo CSE 191 Discrete Structures 28 / 48 Pascal Idetity Pascal Idetity: Let ad k be positive itegers ad k. The ( ( ( + 1 = + k k 1 k ( + 1 Proof: Recall that is the # of k-elemet subsets of a set S with k + 1 elemets. Fix a elemet a S. These k-elemet subsets ca be divided ito two groups: 1. The subsets that cotai ( a. So we are choosig other k 1 elemets from S {a}. So there are such subsets. k 1 2. The subsets that do ot cotai ( a. So we are choosig other k elemets from S {a}. So there are such subsets. k So the sum of the two groups equals to LHS. c Xi He (Uiversity at Buffalo CSE 191 Discrete Structures 29 / 48
Pascal Triagle Pascal s Triagle ( 0 0 ( 1 0 ( 1 1 ( 2 0 ( 2 1 ( 2 2 ( 3 0 ( 3 1 ( 3 2 ( 3 3 ( 4 0 ( 4 1 ( 4 2 ( 4 3 ( 4 4 It s values: 1 1 1 1 2 1 1 3 3 1 1 4 6 4 1 Note: The left- ad the right-most umbers are always 1. Ay other umber is the sum of the two umbers o its shoulder. c Xi He (Uiversity at Buffalo CSE 191 Discrete Structures 30 / 48 Pigeohole Priciple The idea of Pigeohole Priciple is very simple. But whe used properly, we ca get very iterestig results. Pigeohole Priciple (Versio 1: If you put pigeos ito m pigeoholes with > m, the at least oe pigeohole cotais at least two pigeos. Pigeohole Priciple (Versio 1, equivalet statemet: If k is a positive iteger ad k + 1 or more objects are put ito k boxes, the at least oe box cotais two or more objects. Proof: This statemet is so obvious that really eeds o formal proof. But for completeess, here is a proof by cotrapositio. Suppose that oe of k boxes cotais more tha oe object. The the total umber of objects i all boxes is at most k. This is a cotradictio, because there are k + 1 objects. c Xi He (Uiversity at Buffalo CSE 191 Discrete Structures 32 / 48
Pigeohole Priciple Pigeohole Priciple (Versio 2: If you put N pigeos ito k pigeoholes, the at least oe pigeohole cotais at least N/k pigeos. x deotes the smallest iteger that is x. For example: 0.6 = 1; 3 = 3; 4.0000001 = 5. c Xi He (Uiversity at Buffalo CSE 191 Discrete Structures 33 / 48 Pigeohole Priciple examples Example 1: At lease two people i NYC have the same umber of hairs. Example 2: At lease 16 people i NYC have the same umber of hairs. There are 8 millio people i NYC. It is kow that the maximum umber of hairs a perso ca have is 500,000. Each perso is a pigeo. The ith pigeohole holds the people with i hairs. So there are 500,001 pigeoholes. So at least oe pigeohole cotais at least 8000000/500001 = 15.999968 = 16 people. c Xi He (Uiversity at Buffalo CSE 191 Discrete Structures 34 / 48
Pigeohole Priciple examples Example 3: At ay momet, at least 1574 ATT cell phoes are coected to a ATT cell phoe tower i US. There are 40,032 ATT towers, ad 63,000,000 ATT cell phoes (the umbers are ot curret, just example So at least 63000000/40032 = 1574 cell phoes are coected to a tower. c Xi He (Uiversity at Buffalo CSE 191 Discrete Structures 35 / 48 Pigeohole Priciple examples Example 4: How may cards must be selected from a stadard deck of 52 cards to esure that at least three cards of the same suit are chose? Example 5: How may cards must be selected from a stadard deck of 52 cards to esure that at least three hearts are selected? Example 6: If we pick 5 umbers from the itegers 1,2,..., 8, the two of them must add up to 9. We will discuss these examples i class. c Xi He (Uiversity at Buffalo CSE 191 Discrete Structures 36 / 48
More difficult examples Example: At least two people i this room have the same umber of frieds i this room. (Of course we must assume there are at least two people i this room. Proof: Let k be the umber of people who has at least oe fried i this room. Case 1 k = 0: The all people have 0 frieds. Case 2 k > 0: Amog these k people: the umbers of frieds they ca have are 1, 2,..., (k 1. These choices are k 1 pigeoholes. But there are k people, each is a pigeo. So at least oe pigeohole cotais at least two pigeos. I other words, at least two people have the same umber of frieds. c Xi He (Uiversity at Buffalo CSE 191 Discrete Structures 37 / 48 More difficult examples Example: Durig a moth of 30 days, a baseball team plays at least oe game per day. They played exactly 45 games durig these 30 days. Show that there must be a period of cosecutive days durig which the team must play exactly 14 games. A equivalet descriptio: We have arbitrary 30 itegers a 1,..., a 30. Each iteger a i (1 i 30 satisfies 1 a i. The sum of the 30 itegers is 45. Show that there is always a cotiuous sectio of itegers that add up to exactly 14. We will discuss this problem i class. c Xi He (Uiversity at Buffalo CSE 191 Discrete Structures 38 / 48
More difficult examples Theorem: Approximate irratioal umber by ratioal umbers with small errors: For ay real umber a, there exist ifiitely may ratioal umbers p/q (p ad q are itegers such that a p/q < 1 q 2. What we are tryig to do? Computer ca oly store fiite precisio umbers. Whe we store π = 3.1416, we are sayig: π 31416/10000 with error at most 1/10000. I geeral, we wat to approximate a irratioal umber a by a ratioal umber p/q with error as small as possible. If we isist o that the deomiator q must be a power of 10, we ca oly guaratee the error 1 q. This theorem shows that if we allow arbitrary iteger q as the deomiator, we ca double the accuracy: error < 1 q 2. c Xi He (Uiversity at Buffalo CSE 191 Discrete Structures 39 / 48 More difficult examples Example: π 3/1 = 3 22/7 = 3.1428571... 223/71 = 3.1408450... 333/106 = 3.1415094...... Proof: For ay umber x, let {x} deote the fractioal part of x, (i.e. the part after the decimal poit. Example: {0.34} = 0.34. {100.28} = 0.28. {π} = 0.1415926.... [c, d deotes the iterval of real umbers x such that c x < d c Xi He (Uiversity at Buffalo CSE 191 Discrete Structures 40 / 48
More difficult examples Let a be the real umber we try to approximate by ratioal umbers. Cosider ay positive iteger Q. Cosider the fractioal parts of the umbers: {0}, {a 1}, {a 2},... {a Q} (these are Q + 1 pigeos. Cosider the Q itervals: [0, 1 Q, [ 1 Q, 2 Q 1 Q,..., [ Q, 1. (These are the Q pigeoholes. By pigeohole priciple, at least oe pigeohole cotais at least two pigeos. I other words, there are q 1 ad q 2 (0 q 1, q 2 Q such that {a q 1 } ad {a q 2 } are i the same iterval. The {a q 1 } {a q 2 } < 1 Q. Suppose: a q 1 = x 1.{a q 1 } ad a q 2 = x 2.{a q 2 }. (Here x 1 is the iteger part of a q 1 ad x 2 is the iteger part of a q 2. Let q = q 1 q 2 ad p = x 1 x 2. c Xi He (Uiversity at Buffalo CSE 191 Discrete Structures 41 / 48 More difficult examples q a p = (q 1 q 2 a (x 1 x 2 = q 1 a q 2 a (x 1 x 2 = x 1.{q 1 a} x 2.{q 2 a} (x 1 x 2 = (x 1 x 2 + {q 1 a} {q 2 a} (x 1 x 2 = {q 1 a} {q 2 a} < 1 Q This implies: a p/q < 1 Q q 1 q 2 This completes the proof. c Xi He (Uiversity at Buffalo CSE 191 Discrete Structures 42 / 48
More difficult examples Example: Let a = π ad Q = 8. 0 π = 0. 1 π = 3.14159265. 2 π = 6.28318531. 3 π = 9.42477961. 4 π = 12.56637061. 5 π = 15.70796327. 6 π = 18.84955592. 7 π = 21.99114858. 8 π = 25.13274112. Note that {1 π} = 0.14159265 ad {8 π} = 0.13274112 are i the same iterval [1/8, 2/8. So we pick q 1 = 8, q 2 = 1. The p = 25 3 = 22 ad q = 8 1 = 7. We have π 22/7. c Xi He (Uiversity at Buffalo CSE 191 Discrete Structures 43 / 48 Proofs based o parity argumet. Defiitio The parity of a iteger specifies whether it is eve or odd. Example: the parity of 8 ad 0 are eve. the parity of 1 ad 7 are odd. The parity of itegers is a simple cocept. But based o parity, sometimes we ca prove iterestig or eve surprisig results. c Xi He (Uiversity at Buffalo CSE 191 Discrete Structures 45 / 48
Proofs based o parity argumet. Example: We are give a 8 8 checkerboard (each square is paited white ad black alterately. If we remove the top-left ad the bottom-right white squares, ca we cover the remaiig 62 squares by usig 1 2 domioes? No. Because: Each domio covers 1 white ad 1 black square. So the portio of the checkerboard covered by domioes must have the same umber of white ad black squares. If we remove two white squares, there are 32 black ad 30 white squares left. So it is impossible to cover them all. Example: If we remove the top-left ad the bottom-left squares, ca we cover the remaiig 62 squares by usig 1 2 domioes? c Xi He (Uiversity at Buffalo CSE 191 Discrete Structures 46 / 48 Proofs based o parity argumet. A umber game: Ask a fried do the followig: (He does these steps while you are NOT watchig. Pick ay odd iteger. Write the umbers 1, 2, 3,..., 2 o a piece of paper. Pick ay two umbers i ad j o the paper. Erase i ad j. Write i j o the paper. Repeat these steps, util oly oe umber remais. At this poit, you will guess the parity of the remaiig umber. Hit: You always say odd. Why you are always right? Try to make this game more iterestig. c Xi He (Uiversity at Buffalo CSE 191 Discrete Structures 47 / 48
Proofs based o parity argumet. Fact: The umber of people (i this room who has a odd umber of frieds (i this room must be eve. We will proof this fact by usig parity argumets. Fact: Amog ay 6 people, there must be: Either three mutual frieds. Or three mutual stragers. Try to prove this fact. (Not by parity argumet. c Xi He (Uiversity at Buffalo CSE 191 Discrete Structures 48 / 48