Counting problems arise throughout mathematics and computer science. Motivating Example: A password for an e-mail account should consist of six to eight characters. Each of these characters must be a digit or a letter of the alphabet (lower case or upper case). Each password must contain at least one digit and at least one character. How many passwords are there? Example: There are 20 students in our class. Each student has a pair of hands. How many hands do we have in our class? 1
5.1 Tree diagrams Counting problems can be solved using tree diagrams. tree root branches tree nodes leaves We represent the possible outcomes by the leaves. Example 8: Bit strings of length three that don't have two consecutive 0s. 1 st bit 1 0 2 nd bit 1 0 1 3 rd bit 1 1 1 0 0 111 110 101 011 010 2
Example 2: how many difference license plates are available if each plate contains a sequence of three letters followed by two digits? Solution: _ letters digits Answer = 1 757 600 3
The product rule: Suppose that a procedure can be broken down into sequence of two tasks. Suppose there are n 1 ways to do the first task. Suppose there are n 2 ways to do the second task (for each of those n 1 ways). Then there are n 1 n 2 ways to do the procedure. Also applies for more than 2 tasks. 4
Example: A student can choose a computer project from one of two groups. The first group contains 12 projects. The second group contains 19 possible projects. All projects are different. How many possible projects are there to choose from? The sum rule: If a task can be done either in one of n 1 ways or in one of n 2 ways (no overlap), then there are n 1 + n 2 ways to do the task. 5
Example: There are 20 students in our class. Each student has a pair of hands. How many hands do we have in our class if we do not count the tallest students' hands nor do we count the left hand of the shortest student? Basic subtraction rule: If a task can be done in n 1 ways, except that there are n 2 forbidden ways, then there are n 1 - n 2 ways to do the task. 6
Example: Let's go back to the question about the password. A password for an e-mail account should consist of six to eight characters. Each of these characters must be a digit or a letter of the alphabet (lower case or upper case). Each password must contain at least one digit and at least one character. How many passwords are there? Solution: Let P be the total number of possible passwords. Let P 6 denote the number of possible passwords of length 6, P 7 denote the number of possible passwords of length 7, and P 8 denote the number of possible passwords of length 8. By the sum rule P = P 6 + P 7 + P 8. Let's find P 6 : (lower case letters, upper case letters, digits: 26+26+10 = 62) 62 62 62 62 62 62 = 62 6 Recall restrictions: at least one digits and at least one character. We need to exclude the forbidden cases... 7
P 6 = 62 6-10 6-52 6 Similarly, P 7 =? and P 8 =? Hence, P = P 6 + P 7 + P 8 =... = 167 410 838 583 040 8
5.1 Inclusion - Exclusion principle Example: How many bit strings of length eight that either start with a 1 bit or end with the two bits 11? Solution: A. Let's count the ones that start with 1: 1 _ 1 2 2 2 2 2 2 2 = 2 7 B. Let's count the ones that end with 11: 1 1 2 2 2 2 2 2 1 1 = 2 6 Is the answer = 2 7 + 2 6? (by the sum rule?) 9
5.1 Inclusion - Exclusion principle Example: How many bit strings of length eight that either start with a 1 bit or end with the two bits 11? Solution: A. Let's count the ones that start with 1: 1 _ 1 2 2 2 2 2 2 2 = 2 7 B. Let's count the ones that end with 11: 1 1 2 2 2 2 2 2 1 1 = 2 6 C. Let's count the ones that start with 1 and end with 11: 1 _ 1 1 1 2 2 2 2 2 1 1 = 2 5 - these combinations are already included in the above two cases (therefore, we need to subtract them from the sum of the first two) Total: 2 7 + 2 6-2 5 = 128 + 64 32 = 160 10
The Inclusion Exclusion Principle (less basic subtraction principle) - Suppose a task can be done in n 1 or in n 2 ways (but some of the n 1 ways to do the task are the same as some of the n 2 ways to do the task). - To count: Add them up: n 1 +n 2 (but this overcounts ) Subtract: n 1 +n 2 - (# of ways to do the task in a way that is both among the set of n 1 ways and n 2 ways) 11
5.1 Inclusion - Exclusion principle The Inclusion Exclusion Principle n 1 +n 2 -(# of ways to do the task in a way that is both among the set of n 1 ways and n 2 ways) We can rephrase this counting principle in terms of sets. Let A 1 and A 2 be sets. There are A 1 ways to select an element from A 1, and A 2 ways to select an element from A 2. A 1 U A 2 = A 1 + A 2 - A 1 A 2 Example: Every student in a discrete mathematics class is either a computer science or a mathematics major or is a joint major in these two subjects. How many students are in the class if there are 38 computer science majors (including joint majors), 23 mathematics majors (including joint majors), and 7 joint majors? Answer = 54 12
5.1 Practice Problems Good Practice Problems (6.1) A: 1, 3, 7 B: 9, 11, C: 13, 15 D: 17, 25 13