Module 7 Complexity Module 15 Combinatorics Rosen 5
Module #7 - Complexity Module #15: Combinatorics Rosen 5 th ed. , §§ 4. 1 -4. 3 ~19 slides, ~1 lecture 2020/12/2 (c)2001 -2003, Michael P. Frank 1
Module #7 - Complexity Combinatorics • The study of the number of ways to put things together into various combinations. • E. g. In a contest entered by 100 people, how many different top-10 outcomes could occur? • E. g. If a password is 6 -8 letters and/or digits, how many passwords can there be? 2020/12/2 (c)2001 -2003, Michael P. Frank 2
Module #7 - Complexity Sum and Product Rules • Let m be the number of ways to do task 1 and n the number of ways to do task 2 (with each number independent of how the other task is done), and assume that no way to do task 1 simultaneously also accomplishes task 2. • The sum rule: The task “do either task 1 or task 2, but not both” can be done in m+n ways. • The product rule: The task “do both task 1 and task 2” can be done in mn ways. 2020/12/2 (c)2001 -2003, Michael P. Frank 3
Module #7 - Complexity Set Theoretic Version • If A is the set of ways to do task 1, and B the set of ways to do task 2, and if A and B are disjoint, then: • The ways to do either task 1 or 2 are A B, and |A B|=|A|+|B| • The ways to do both task 1 and 2 can be represented as A B, and |A B|=|A|·|B| 2020/12/2 (c)2001 -2003, Michael P. Frank 4
Module #7 - Complexity IP Address Example • Some facts about Internet Protocol vers. 4: – Valid computer addresses are in one of 3 types: • A class A IP address contains a 7 -bit “netid” ≠ 17, and a 24 -bit “hostid” • A class B address has a 14 -bit netid and a 16 -bit hostid. • A class C addr. Has 21 -bit netid an 8 -bit hostid. – The 3 classes have distinct headers (0, 110) – Hostids that are all 0 s or all 1 s are not allowed. • How many valid computer addresses are there? e. g. , ufl. edu is 128. 227. 74. 58 2020/12/2 (c)2001 -2003, Michael P. Frank 5
Module #7 - Complexity IP address solution • (# addrs) = (# class A) + (# class B) + (# class C) (by sum rule) • # class A = (# valid netids)·(# valid hostids) (by product rule) • • • 2020/12/2 (# valid class A netids) = 27 − 1 = 127. (# valid class A hostids) = 224 − 2 = 16, 777, 214. Continuing in this fashion we find the answer is: 3, 737, 091, 842 (3. 7 billion IP addresses) (c)2001 -2003, Michael P. Frank 6
Module #7 - Complexity Inclusion-Exclusion Principle • Suppose that k m of the ways of doing task 1 also simultanously accomplish task 2. (And thus are also ways of doing task 2. ) • Then the number of ways to accomplish “Do either task 1 or task 2” is m n k. • Set theory: If A and B are not disjoint, then |A B|=|A| |B| |A B|. 2020/12/2 (c)2001 -2003, Michael P. Frank 7
Module #7 - Complexity Inclusion/Exclusion Example • Hypothetical rules for passwords: – Passwords must be 2 characters long. – Each password must be a letter a-z, a digit 0 -9, or one of the 10 punctuation characters !@#$%^&*(). – Each password must contain at least 1 digit or punctuation character. 2020/12/2 (c)2001 -2003, Michael P. Frank 8
Module #7 - Complexity Setup of Problem • A legal password has a digit or puctuation character in position 1 or position 2. – These cases overlap, so the principle applies. • (# of passwords w. OK symbol in position #1) = (10+10)·(10+10+26) • (# w. OK sym. in pos. #2): also 20· 46 • (# w. OK sym both places): 20· 20 • Answer: 920+920− 400 = 1, 440 2020/12/2 (c)2001 -2003, Michael P. Frank 9
Module #7 - Complexity Pigeonhole Principle • A. k. a. Dirichlet drawer principle • If ≥k+1 objects are assigned to k places, then at least 1 place must be assigned ≥ 2 objects. • In terms of the assignment function: If f: A→B and |A|≥|B|+1, then some element of B has ≥ 2 preimages under f. I. e. , f is not one-to-one. 2020/12/2 (c)2001 -2003, Michael P. Frank 10
Module #7 - Complexity Example of Pigeonhole Principle • There are 101 possible numeric grades (0%100%) rounded to the nearest integer. • There are >101 students in this class. • Therefore, there must be at least one (rounded) grade that will be shared by at least 2 students at the end of the semester. – I. e. , the function from students to rounded grades is not a one-to-one function. 2020/12/2 (c)2001 -2003, Michael P. Frank 11
Module #7 - Complexity Fun Pigeonhole Proof (Ex. 4, p. 314) • Theorem: n N, a multiple m>0 of n m has only 0’s and 1’s in its decimal expansion! • Proof: Consider the n+1 decimal integers 1, 111, …, 1 1. They have only n possible residues mod n. n+1 So, take the difference of two that have the same residue. The result is the answer! □ 2020/12/2 (c)2001 -2003, Michael P. Frank 12
Module #7 - Complexity A Specific Example • Let n=3. Consider 1, 111, 1111. – 1 mod 3 = 1 Note same residue. – 11 mod 3 = 2 – 111 mod 3 = 0 Lucky extra solution. – 1, 111 mod 3 = 1 • 1, 111 − 1 = 1, 110 = 3· 370. – It has only 0’s and 1’s in its expansion. – Its residue mod 3 = 0, so it’s a multiple of 3. 2020/12/2 (c)2001 -2003, Michael P. Frank 13
Module #7 - Complexity Generalized Pigeonhole Principle • If N objects are assigned to k places, then at least one place must be assigned at least N/k objects. • E. g. , there are N=280 students in this class. There are k=52 weeks in the year. – Therefore, there must be at least 1 week during which at least 280/52 = 5. 38 =6 students in the class have a birthday. 2020/12/2 (c)2001 -2003, Michael P. Frank 14
Module #7 - Complexity Proof of G. P. P. • By contradiction. Suppose every place has < N/k objects, thus ≤ N/k − 1. • Then the total number of objects is at most • So, there are less than N objects, which contradicts our assumption of N objects! □ 2020/12/2 (c)2001 -2003, Michael P. Frank 15
Module #7 - Complexity G. P. P. Example • Given: There are 280 students in the class. Without knowing anybody’s birthday, what is the largest value of n for which we can prove that at least n students must have been born in the same month? • Answer: 280/12 = 23. 3 = 24 2020/12/2 (c)2001 -2003, Michael P. Frank 16
Module #7 - Complexity Permutations • A permutation of a set S of objects is a sequence containing each object once. • An ordered arrangement of r distinct elements of S is called an r-permutation. • The number of r-permutations of a set with n=|S| elements is P(n, r) = n(n− 1)…(n−r+1) = n!/(n−r)! 2020/12/2 (c)2001 -2003, Michael P. Frank 17
Module #7 - Complexity Permutation Example • A terrorist has planted an armed nuclear bomb in your city, and it is your job to disable it by cutting wires to the trigger device. There are 10 wires to the device. If you cut exactly the right three wires, in exactly the right order, you will disable the bomb, otherwise it will explode! If the wires all look the same, what are your chances of survival? P(10, 3) = 10· 9· 8 = 720, 2020/12/2 so there is a 1 in 720 chance (c)2001 -2003, Michael P. Frank that you’ll survive! 18
Module #7 - Complexity Combinations • An r-combination of elements of a set S is simply a subset T S with r members, |T|=r. • The number of r-combinations of a set with n=|S| elements is • Note that C(n, r) = C(n, n−r) – Because choosing the r members of T is the same thing as choosing the n−r non-members of T. 2020/12/2 (c)2001 -2003, Michael P. Frank 19
Module #7 - Complexity Combination Example • How many distinct 7 -card hands can be drawn from a standard 52 -card deck? – The order of cards in a hand doesn’t matter. • Answer C(52, 7) = P(52, 7)/P(7, 7) = 52· 51· 50· 49· 48· 47· 46 / 7· 6· 5· 4· 3· 2· 1 17 10 7 8 2 52· 17· 10· 7· 46 = 133, 784, 560 2020/12/2 (c)2001 -2003, Michael P. Frank 20
- Slides: 20