Foudatios of Computer Sciece Lecture 3 Coutig Coutig Sequeces Build-Up Coutig Coutig Oe Set by Coutig Aother: Bijectio Permutatios ad Combiatios
Last Time Be careful of what you read i the media: sex i America. 2 Bipartite Graphs ad Matchig (Hall s Theorem). 3 Stable Marriage. 4 Coflict Graphs ad Colorig. Every tree is 2-colorable. 5 Other Graph Problems Coected compoets, spaig tree, Euler paths, etwork flow. ([easy]) Hamiltoia paths, K-ceter, vertex cover, domiatig set. ([hard]) Creator: Malik Magdo-Ismail Coutig: 2 / 7 Today
Today: Coutig Coutig sequeces. 2 Build-up coutig. 3 Coutig oe set by coutig aother: bijectio. 4 Permutatios ad combiatios. Creator: Malik Magdo-Ismail Coutig: 3 / 7 Objects We Ca Cout
Discrete Math Is About Objects We Ca Cout Three colors of cady:,,. A goody-bag has 3 cadies. How may goody-bags? (Oly the umber of each color matters: { } ad { } are the same goody-bag.) { } { } { } { } { } { } { } { } { } { } Challege Problems. What if there are 5 cadies per goody-bag ad 0 colors of cady? 2 Goody-bags come i bulk packs of 5. How may differet bulk packs are there? There are too may to list out. We eed tools! Creator: Malik Magdo-Ismail Coutig: 4 / 7 Sum Rule
Sum Rule How may biary sequeces of legth 3: {000, 00, 00, 0, 00, 0, 0, }. There are two types: those edig i 0 ad those edig i, {b b 2 b 3 } = {b b 2 0} {b b 2 } Sum Rule. N objects of two types: N of type- ad N 2 of type-2. The, N = N + N 2. {b b 2 b 3 } = {b b 2 0} + {b b 2 } (sum rule) = {b b 2 } 2 = ( {b 0} + {b } ) 2 (sum rule) = {b } 2 2 = 2 2 2 Creator: Malik Magdo-Ismail Coutig: 5 / 7 Product Rule
Product Rule {b b 2 b 3 } 2 2 2 Let N be the umber of choices for a sequece x x 2 x 3 x r x r. Let N be the umber of choices for x ; Let N 2 be the umber of choices for x 2 after you choose x ; Let N 3 be the umber of choices for x 3 after you choose x x 2 ; Let N 4 be the umber of choices for x 4 after you choose x x 2 x 3 ;. Let N r be the umber of choices for x r after you choose x x 2 x 3 x r. N = N N 2 N 3 N 4 N r. Example. There are 2 biary sequeces of legth : N = N 2 = = N = 2. The sum ad product rules are the oly basic tools we eed... plus TINKERING. Creator: Malik Magdo-Ismail Coutig: 6 / 7 Examples
Examples Meus. breakfast {pacake, waffle, Doritos} luch {burger, Doritos} dier {salad, steak, Doritos} {BLD} = 3 2 3 = 8. This works because: every complex object (meu) is a sequece BLD ad every sequece BLD is a uique complex object (meu). 2 NY plates. {ABC-234} = 26 26 26 0 0 0 0 = 26 3 0 4 76 millio. 3 Races. With 0 ruers, how may top-3 fiishes? {FST} = 0 9 8 = 720. 4 Passwords. Letter (a,...,z,a,...,z) followed by 5,6 or 7 letters or digits (0,...,9). 3 types: FS S 2 S 3 S 4 S 5 (or FS 5 for short); FS 6 ; ad, FS 7. (52 choices for F ad 62 for S.) {passwords} = {FS 5 } + {FS 6 } + {FS 7 } (sum rule) = 52 62 5 + 52 62 6 + 52 62 7 2 0 4 (product rule) ( millisecod to test a password 3 moths to break i usig 32,000 cores.) 5 Committees. 0 studets. How may ways to form a party plaig committee? Each studet ca be i or out of the committee: 2 2 2 = 2 0 = 024. {committees} = {0-bit biary strigs} e.g. {s s 2 s 3 s 4 s 5 s 6 s 7 s 8 s 9 s 0 } 0 0 0 0 0 0 {s, s 2, s 4, s 9 } 0 0 0 0 0 0 0 0 0 0 Creator: Malik Magdo-Ismail Coutig: 7 / 7 Examples from CS
Examples from CS Challege Exercises: A problem from the area of atural laguage processig (NLP). Wikipedia has 40 millio articles (6 millio i Eglish). You compute a edit distace betwee every pair of articles ad store this data i a 40millio 40millio array of 64-bit double precisio umbers. Estimate how much RAM you will eed to load this matrix ito memory. Ay suggestios o feasibly performig tasks usig this edit-distace data? (aswer: 3TB) A problem i schedulig exams. RPI has about 400 courses ad 5000 studets who iduce coflicts amog these courses for exam schedulig. Are 5 exam slots eough to schedule all the exams? You realize that you eed to color a 400-ode coflict graph usiig 5 colors. You ca geerate ad test validity of a 5-colorig at a rate of 3 millio per secod. Estimate how log it would take to determie that 5 exam slots is ot eough. (aswer: 0 456 years) Ay suggestios o what to do? Creator: Malik Magdo-Ismail Coutig: 8 / 7 Build-Up Coutig
Build-Up Coutig Number of biary sequeces of legth : 2. = umber biary sequeces of legth with exactly k s 0 k. k Legth-3 sequeces: 000 00 00 0 00 0 0 Legth-4 sequeces: 0000 000 000 00 000 00 00 0 000 00 00 0 00 0 0 Legth-5 sequeces: 00000 0000 0000 000 0000 000 000 00 0000 000 000 00 000 00 00 0 0000 000 000 00 000 00 00 0 000 00 00 0 00 0 0 ( ) k 0 2 3 4 5 6 7 8 k 0 2 3 4 5 6 7 8 Pascal s Triagle 2 3 3 4 6 4 5 0 0 5 6 5 20 5 6 7 2 35 35 2 7 8 28 56 70 56 28 8 {-sequece with k s} = 0 {( )-sequece with k s} }{{} ( k ) {( )-sequece with (k ) s} }{{} ( k ) k = + k (sum rule) base cases: = ; =. k 0 Creator: Malik Magdo-Ismail Coutig: 9 / 7 Goody Bags
Build-up Coutig for Goody Bags Q(, k) = umber of goody-bags of cadies with k colors Build-up coutig: there are ( + ) types of goody-bag. 0 2 3 Q(, k ) Q(, k ) Q( 2, k ) Q( 3, k ) Q(0, k ) Q(, k) = Q(0, k ) + Q(, k ) + + Q(, k ) (sum rule) Q(, k) 0 2 3 4 5 2 3 4 5 k 6 2 3 4 5 6 7 8 9 0 3 6 0 5 2 28 36 45 55 66 4 0 20 35 56 84 20 65 220 286 5 5 35 70 26 20 330 495 75 00 6 2 56 26 252 462 792 287 2002 3003 7 8 9 0 Q(, ) = ; Q(0, k) = Challege problems we had earlier. (5 cadies, 0 colors) 2002 goody-bags. 2 How may 5 goody-bag bulk packs (goody-bags have 3 cadies of 3 colors)? There are 0 types of goody-bag; 5 i a bulk pack. So we eed Q(5, 0) = 2002. Creator: Malik Magdo-Ismail Coutig: 0 / 7 Bijectio
Coutig Oe Set By Coutig Aother: Bijectio 0 goody-bags with 3 cadies of 3 colors. Ca label those goody bags usig {, 2,..., 0}. { } { } { } { } { } { } { } { } { } { } 2 3 4 5 6 7 8 9 0 -to- correspodece betwee goody-bags ad the set {, 2,..., 0}, a bijectio. A B A B A B A B ot a fuctio -to-; ot oto (ijectio, A ij B) A B oto; ot -to- (surjectio, A sur B) A B -to- ad oto (bijectio, A bij B) A = B A bij B implies A = B. Ca cout A by coutig B. Cout meus by coutig sequeces {BLD}. Works because Every sequece specifies a distict meu (-to- mappig). Every meu correspods to a sequece (the mappig is oto). Creator: Malik Magdo-Ismail Coutig: / 7 Goody Bags
Goody Bags Usig Bijectio to Biary Sequeces 3 colors,,,. Cosider the goody-bag {2, 3, 2 }: red cadies delimiter blue cadies delimiter gree cadies ifer color from positio goody-bags bij 9-bit biary sequeces with two s. Examples. 0000000000 {2, 3,,, 3 } 000000000 {0, 4, 0,, 4 } cadies ad k colors (k ) delimiters. umber of goody-bags with cadies of k colors = umber of ( + k )-bit sequeces with (k ) s Q(, k) = + k. k ( k ) keeps poppig up but we do t have a formula for it. Creator: Malik Magdo-Ismail Coutig: 2 / 7 Kig-Quee Positios
Kig-Quee Positios o a Chessboard complex object kig-quee positio umber of positios sequece to recostruct it c3g4 umber of sequeces c K r K c Q r Q Every positio gives a sequece with r k r Q ad c K c Q Every such sequece is a uique valid positio. 0Z0Z0Z0Z 8 Z0Z0Z0Z0 7 0Z0Z0Z0Z 6 Z0Z0Z0Z0 5 0Z0Z0ZqZ 4 Z0J0Z0Z0 3 0Z0Z0Z0Z 2 Z0Z0Z0Z0 a b c d e f g h Cout sequeces with r k r Q ad c K c Q. 8 choices for c K ad r K ; after choosig c K, r K, there are oly 7 choices for c Q ad r Q. By the product rule, the umber of sequeces is 8 8 7 7 = 336. By bijectio coutig, umber of Kig-Quee positios is 336. Creator: Malik Magdo-Ismail Coutig: 3 / 7 Castle-Castle Positios
Castle-Castle Positios o a Chessboard complex object castle-castle positio sequece to recostruct it c3g4 umber of positios umber of sequeces c r c 2 r 2 Problem. c3g4 ad g4c3 are the same positio. positio two sequeces with r r 2 ad c c 2 Twice as may sequeces as positios! 0Z0Z0Z0Z 8 Z0Z0Z0Z0 7 0Z0Z0Z0Z 6 Z0Z0Z0Z0 5 0Z0Z0ZrZ 4 Z0s0Z0Z0 3 0Z0Z0Z0Z 2 Z0Z0Z0Z0 a b c d e f g h Multiplicity Rule. If each object i A correspods to k objects i B, the B = k A. 8 choices for c ad r ; after choosig c, r, there are oly 7 choices for c 2 ad r 2. By the product rule, the umber of sequeces is 8 8 7 7 = 336. By the multiplicity rule, umber of Castle-Castle positios is 336 = 568. 2 Creator: Malik Magdo-Ismail Coutig: 4 / 7 Permutatios ad Combiatios
Permutatios ad Combiatios S = {, 2, 3, 4}, the 2-orderigs are: { 2, 3, 4, 2, 2 3, 2 4, 3, 3 2, 3 4, 4, 4 2, 4 3} (permutatios) the 2-subsets are: { 2, 3, 4, 2 3, 2 4, 3 4} (combiatios) With elemets, by the product rule, the umber of k-orderigs is umber of k-orderigs = ( ) ( 2) ( (k )) = e.g. umber of top-3 fiishes i 0-perso race is 0 9 8 = 0!/7!.! ( k)!. Pick a k-subset ( ( ) k ways) ad reorder it i k! ways to get a k-orderig. umber of k-orderigs = umber of k-subsets k! product rule = ( k ) k! bijectio to sequeces with k s umber of k-subsets =! = k k!( k)! Exercise. How may 0-bit biary sequeces with four s? Creator: Malik Magdo-Ismail Coutig: 5 / 7 Biomial Theorem
Biomial Theorem: (x + y) = i= i x i y i (x + y) 3 = (x + y)(x + y)(x + y) = xxx + xxy + xyx + xyy + yxx + yxy + yyx + yyy = x 3 + 3x 2 y + 3xy 2 + y 3. (All legth-3 biary sequeces b b 2 b 3 where each b i {x, y}) (x + y) = x + (?)x y+ (?)x 2 y 2 + (?)x 3 y 3 + + (?)xy + y strigs with ( ) x s ( ) ( 2 strigs with strigs with strigs with ( 2) x s ( 3) x s x ) ( ( ) 3) = ( ) x + ( ) x y + ( ) 2 x 2 y 2 + ( 3 ) x 3 y 3 + + ( ) xy + ( ) 0 y Example. What is the coefficiet of x 7 i the expasio of ( x + 2x) 0 Need ( x) i (2) 0 i x 7, which implies i = 6. The x 7 term is ( ) 0 6 ( x) 6 (2x) 4 Coefficiet of x 7 is ( ) 0 6 2 4 = 3360. Creator: Malik Magdo-Ismail Coutig: 6 / 7 Geeral Approach to Coutig
Geeral Approach to Coutig Complex Objects To cout complex objects, give a sequece of istructios that ca be used to costruct a complex object. Every sequece of istructios gives a uique complex object. There is a sequece of istructios for every complex object. Cout the umber of possible sequeces of istructios, which equals the umber of complex objects. Creator: Malik Magdo-Ismail Coutig: 7 / 7