CS 103 Discrete Structures Lecture 16 Counting (1) Chapter 1 section 1.1 by Dr. Mosaad Hassan
Counting Chapter 6 With Question/Answer Animations
Chapter Summary The Basics of Counting The Pigeonhole Principle Permutations and Combinations Binomial Coefficients and Identities
The Basics of Counting Section 6.1
Section Summary The Product Rule The Sum Rule The Subtraction Rule The Division Rule Examples, Examples, and Examples Tree Diagrams
The Basics of Counting Suppose that a password on a computer system consists of 6 to 8 characters Each of these characters must be a digit (0-9: 10 possibilities) or a capital letter (A-Z: 26 possibilities) of the alphabet Each password must have at least one digit How many such passwords are there? The techniques needed to answer this question and a wide variety of similar problems will be introduced in this chapter
Basic Counting Principles We will discuss the following basic counting principles: Product Rule Sum Rule Subtraction Rule Product rule is applied when a procedure is made up of a number of separate tasks Suppose that a procedure can be broken down into a sequence of two tasks If there are n1 ways to do the 1st task For each of these ways of doing the 1st task, there are n2 ways to do the 2nd task Then there are n1 ∙ n2 ways to do the procedure
The Product Rule: Example 1 How many bit strings of length 8 are there? b b b b b b b b Solution: There are a total of 8 tasks. Each task involves assigning one of two values ( i.e. 0 or 1) to a bit. By the product rule, there are 2 ∙ 2 ∙ 2 ∙ 2 ∙ 2 ∙ 2 ∙ 2 ∙ 2 = 28 = 256 different possible strings
The Product Rule: Example 2 How many different license plates can be made if each plate contains a sequence of three Arabic letters (from the list ا ب ح د ر س ص ط ع ق ك ل م ن ه و ى ) followed by four digits? L L L d d d d Solution: By the product rule, there are 17 ∙ 17 ∙ 17 ∙ 10 ∙ 10 ∙ 10 ∙ 10 = 49,130,000 different possible license plates Note: The actual number of different license plates is lower as some of the letter combinations are not allowed
Product Rule: Mobile Numbers Operator Format Possibilities STC 050-xxx-xxxx 053-xxx-xxxx 055-xxx-xxxx 107 30,000,000 Mobily 054-xxx-xxxx 056-xxx-xxxx 20,000,000 Zain 058-0xx-xxxx 058-1xx-xxxx 059-xxx-xxxx 106 12,000,000 Virgin 057-0xx-xxxx 057-1xx-xxxx 057-2xx-xxxx 3,000,000 Lebara 057-6xx-xxxx 057-7xx-xxxx 057-8xx-xxxx 68,000,000
Product Rule: Example 4 What is the value of k after the this code has been executed? Solution: Initially, k is 0 Each time the nested loop is traversed, 1 is added to k By the product rule, it follows that the nested loop is traversed n1 ∙ n2 ∙ n3 ... ∙ nm times the final value of k = n1 ∙ n2 ∙ n3 ... ∙ nm
Product Rule: Example 5 A new company with just 2 employees, Sami and Ali, rents a floor of a building with 12 offices. How many ways are there to assign different offices to these 2 employees? Solution: Assigning offices will be a 2-step process: Assign an office to Sami. This can be done in 12 ways And then assign an office to Ali different from the office assigned to Sami. This can be done in 11 ways By the product rule, there are 12 ∙ 11 = 132 ways to assign offices to the 2 employees
Product Rule: Example 6 How many strings of 4 decimal digits, that do not contain the same digit twice? Solution: We want to chose a digit, then another that is not the same, then another… and so forth 1st digit: 10 possibilities 2nd digit: 9 possibilities (all but the 1st digit) 3rd digit: 8 possibilities 4th digit: 7 possibilities Total = 10 ∙ 9 ∙ 8 ∙ 7 = 5,040 strings
Counting Principles: Sum Rule If a task can be done either in one of n1 ways or in one of n2 ways, where none of the set of n1 ways is the same as any of the set of n2 ways, then there are a total of n1 + n2 ways to do the task
Sum Rule: Example 1 Suppose that a teacher or student is chosen as a representative. How many different choices are there for this representative if there are 37 teachers and 83 students and no one is both a teacher and a student? Solution: There are 37 ways to choose a teacher and 83 ways to choose a student Choosing a teacher is never the same as choosing a student because no one is both By the sum rule, there are 37 + 83 = 120 possible ways to pick this representative
Sum Rule: Example 2 A student can choose a computer project from one of 3 lists. The 3 lists contain 23, 15, and 19 possible projects, respectively. No project is on more than one list. How many possible projects are there to choose from? Solution: The student can choose a project from the 1st, 2nd, or 3rd list as no project is on more than one list By the sum rule there are 23 + 15 + 19 = 57 ways to choose a project
Sum Rule: Example 3 What is the value of k after this code has been executed? Solution: Initially, k = 0 This block of code is made up of m different loops Each time a loop is traversed, 1 is added to k Because we only traverse one loop at a time The sum rule shows that the final value of k is n1 + n2 + n3 + ... + nm
Sum Rule: Set Terminology The sum rule can be phrased in terms of sets as: If A1, A2 , … , Am are disjoint finite sets, then the number of elements in the union of these sets is the sum of the numbers of elements in the sets The number of ways to choose an element from one of the sets, which is the number of elements in the union, is: |A1 A2 … Am| = |A1| + |A2| + … + |Am| This equality applies only when the sets are disjoint
Combining the Sum and Product Rules Example 1: Suppose statement labels in a programming language can be either a single letter or a letter followed by a digit. Find the number of possible labels Solution: Use the product and sum rules 26 + 26 ∙ 10 = 286
Example 2: Counting Passwords How many possible passwords are there if each password is 6 to 8 characters long, where each character is an uppercase letter or a digit. Each password must contain at least one digit Solution: Let P be the total number of passwords, and let P6, P7, and P8 be the passwords of length 6, 7, and 8 By the sum rule, P = P6 + P7 + P8 To find each of P6, P7, and P8, we find the number of passwords of the specified length composed of letters and digits and subtract the number composed only of letters. We find that: P6 = 366 − 266 = 2,176,782,336 − 308,915,776 = 1,867,866,560 P7 = 367 − 267 = 78,364,164,096 − 8,031,810,176 = 70,332,353,920 P8 = 368 − 268 = 2,821,109,907,456 − 208,827,064,576 = 2,612,282,842,880 P = P6 + P7 + P8 = 2,684,483,063,360
Counting Principles: Subtraction Rule The subtraction rule generalizes the sum rule to non- disjoint sets If a task can be done either in one of n1 ways or in one of n2 ways, then the total number of ways to do the task is n1 + n2 minus the number of ways to do the task that are common to the two different ways In set notation, for arbitrary (but finite) sets A, B: |A B| = |A| + |B| - |A B| Example: Let |A| = 5, |B| = 3, and |A B| = 1 Different elements in the union are 5 + 3 - 1 = 7 not 8 This rule is also known as the inclusion-exclusion principle
Subtraction Rule: Example 1 How many bit strings of length 8 either start with a 1 or end with 00? We can construct a bit string of length 8 as shown shown in the figure Consequently, the number of bit strings of length eight that begin with 1 or end with a 00, is 128 + 64 - 32 = 160
Subtraction Rule: Example 2 In a university has 1000 students in total 200 students are taking a course in mathematics 300 are taking a course in physics 50 are taking both How many students are taking at least one of those courses? Solution: If M = set of students taking Mathematics P = set of students taking Physics then |M P| = |M| + |P| - |M P| = 300 + 200 - 50 = 450
Subtraction Rule: Example 3 A company received 350 applications for a job: 220 of the applicants majored in CS 147 majored in business 51 majored in CS as well as in business How many of the applicants majored neither in CS nor in business?
Example 3 (contd.) Let ACS and AB be the set of students who majored in CS and business, respectively then ACS AB is the set of students who majored in CS or business (or both), and ACS AB is the set of students who majored both in CS and in business By the subtraction rule, the number of students who majored either in CS or in business (or both) is |ACS AB| = |ACS| + |AB| - |ACS AB| = 220 + 147 - 51 = 316 Therefore, 350 - 316 = 34 applicants majored neither in CS nor in business
Tree Diagrams for Counting Counting problems can be solved using tree diagrams A tree consists of: A root A number of branches leaving the root Possible additional branches leaving the endpoints of other branches To use trees in counting: We use a branch to represent each possible choice We represent the possible outcomes by the leaves
Tree Diagrams: Example 1 How many bit strings of length 4 do not have 2 consecutive 1’s? This tree diagram displays all bit strings of length 4 without 2 consecutive 1’s
Tree Diagrams: Example 2 T-shirts come in 5 sizes: S, M, L, XL, and XXL S, M, and L comes in white, red, green, and black XL comes only in red, green, and black XXL comes only in green and black How many different t-shirts does a shop have to stock to have at least 1 of each available size and color? 17
Tree Diagrams: Example 3 A series between 2 teams consists of at most 5 games. The 1st team that wins 3 games wins the series. In how many different ways can the series occur? 20
The Pigeonhole Principle Section 6.2
Section Summary The Pigeonhole Principle The Generalized Pigeonhole Principle
The Pigeonhole Principle Suppose that a flock of 13 pigeons flies into a set of 12 pigeonholes to roost. At least one of these 12 pigeonholes must have at least 2 pigeons in it If each pigeonhole had at most one pigeon in it, at most 12 pigeons, one per pigeonhole, could be accommodated Pigeonhole Principle: If there are more pigeons than pigeonholes, then there must be at least one pigeonhole with at least 2 pigeons in it
The Pigeonhole Principle
The Pigeonhole Principle This principle applies to objects other than pigeons and pigeonholes Pigeonhole Principle: If k + 1 or more objects are placed into k boxes, then there is at least 1 box containing 2 or more of the objects Proof by contraposition: Suppose none of the k boxes has more than 1 object. Then the total objects would be at most k. This contradicts the statement that we have k + 1 objects Example 1: Among any group of 367 people, there must be at least 2 with the same birthday, because there are only 366 possible birthdays Example 2: In any group of 27 English words, there must be at least 2 that begin with the same letter, because there are 26 letters in the English alphabet
Pigeonhole Principle: Example How many students must be in a class to guarantee that at least 2 students receive the same score on the final exam if the exam is graded on a scale from 0 to 100? There are 101 possible scores on the final According to the pigeonhole principle, among any 102 students there must be at least 2 students with the same score
Generalized Pigeonhole Principle If N objects are placed into k boxes, then there is at least one box containing N/k objects Proof by contraposition: Suppose that none of the boxes contains more than ⌈N/k⌉ − 1 objects. Then the total number of objects is at most where the inequality ⌈N/k⌉ < ⌈N/k⌉ + 1 has been used. This is a contradiction because there are a total of N objects
Generalized Pigeonhole Principle If N objects are placed into k boxes, then there is at least one box containing N/k objects Example 1: Among 100 people, there are at least 100/12 = 9 born in the same month Example 2: How many students, N, in a class must there be to ensure that 6 students get the same grade (one of A, B, C, D, or F)? The smallest integer that satisfy N/5 = 6 is N is 26 Objects Boxes Objects Boxes
CS 103 Discrete Structures Lecture 17 Counting (2) Chapter 1 section 1.1 by Dr. Mosaad Hassan
Permutations and Combinations Section 6.3
Section Summary Permutations Combinations
Permutations A permutation of a set of distinct objects is an ordered arrangement of all objects in that set An ordered arrangement of r elements of a set is called an r-permutation Example: For S = {1, 2, 3}, The ordered arrangement 3, 1, 2 is a permutation of S The ordered arrangement 3, 2 is a 2-permutation of S Chapter 5 by Dr. Mosaad Wageh Hassan
r-Permutations The number of r-permutations of a set with n elements is denoted by P(n, r) For example, the 2-permutations of S = {1, 2, 3} are 1,2; 1,3; 2,1; 2,3; 3,1; and 3,2. Hence, P(3, 2) = 6 Theorem: If n is a positive integer and r is an integer with 1 ≤ r ≤ n, then there are P(n, r) = n(n − 1)(n − 2) ∙∙∙ (n − r + 1) r-permutations of a set with n distinct elements Proof: Use the product rule. The first element can be chosen in n ways. The second in n − 1 ways, and so on until there are n − (r − 1) ways to choose the last element Note: P(n, 0) = 1, i.e. there is only 1 way to order 0 elements
r-Permutations We can find P(n, r) from the following corollary Corollary: If n and r are integers with 0 r n, then:
Permutations: Examples 1 & 2 In how many ways can we select 3 students from a group of 5 to stand in line? There are 5 ways to select the 1st student 4 ways to select the 2nd 3 ways to select the 3rd By the product rule, there are 5 · 4 · 3 = 60 = 5! / (5 - 3)! ways to select 3 students out of a group of 5 In how many ways can we arrange all 5 of these students in a line? There are 5 ways to select the 1st student 4 for 2nd 3 for 3rd 2 for 4th 1 for 5th Consequently, there are 5 · 4 · 3 · 2 · 1 = 5! / (5 - 5)! = 120 ways
Permutations: Example 3 Suppose that there are 8 runners in a race The winner receives a gold medal The 2nd-place finisher receives silver The 3rd-place finisher receives bronze How many different ways are there to award these medals, if all possible outcomes of the race can occur and there are no ties? Solution: The number of different ways to award the medals is the number of 3-permutations of a set of 8 P(8, 3) = 8!/(8 - 3)! = 8!/5! = 8 · 7 · 6 = 336 possible ways
Permutations: Example 4 Suppose that a salesman has to visit 8 different cities He must begin his trip in a specified city, but he can visit the other 7 cities in any order he wishes How many possible ways can the salesman travel when visiting these cities? Solution: The number of possible paths between the cities is the number of permutations of 7 elements The 1st city is determined, but the remaining seven can be ordered arbitrarily There are 7! / (7 - 7)! = 5,040 ways
Permutations: Example 5 How many permutations of the letters ABCDEFGH contain the string ABC? Solution: We find the answer by finding the number of permutations of 6 objects, with the restriction that the letters ABC must occur as a block Theses objects to be selected are block ABC and the individual letters D, E, F, G, and H Because these 6 objects can occur in any order, there are 6! / (6 – 6)! = 720 permutations of the letters ABCDEFGH in which ABC occurs as a block
Combinations Combinations are used to count unordered selections of objects This is in contrast to permutations, where the order is important and we count ABC as different from BCA In combinations, the order is not issue. When we need to select from {A, B, C}, ABC and BCA are the same combination An r-combination of elements of a set is an unordered selection of r elements from the set. Thus, an r-combination is simply a subset of the set with r elements
r-Combinations Theorem: The number of r-combinations of a set with n elements, denoted by C(n, r), where n and r are integers, n 0 and 0 r n, is: C(n, r) = 𝑛! 𝑟! 𝑛 − 𝑟 ! = 𝑃(𝑛, 𝑟) 𝑟! Example 1: If S = {1, 2, 3, 4}, then {1, 2, 3}; {1, 2, 4}; {1, 3, 4}; and {2, 3, 4}; are the 3-combination from S. Here, C(n, r) = C(4, 3) = 4!/(3! 1!) = 4 Corollary: Let n and r be non-negative integers with r n then C(n, r) = C(n, n - r)
Combinations: Example 2 How many ways are there to select 5 players from a 10-member tennis team to make a trip to a match at another school? Solution: The answer is given by the number of 5-combinations of a set with 10 elements, since the order is not important The number of such combinations is: C(10, 5) = 10! / (5! (10 - 5)!) = 252
Combinations: Example 3 A group of 30 people have been trained as astronauts to go on the first mission to Mars How many ways are there to select a crew of 6 people to go on this mission, assuming that all crew members have the same job? Solution: The number of ways to select a crew of 6 from the pool of 30 people is the number of 6-combinations of a set with 30 elements, because the order in which these people are chosen does not matter The number of such combinations is: C(30, 6) = 30! / (6! (30 - 6)!) = 593,775
Combinations: Example 4 How many bit strings of length n contain exactly r 1’s? Example bit-string: 0101 1100 0000 0011 n = 16 r = 6 Solution: The positions of r 1’s in a bit string of length n form an r-combination of the set {1, 2, 3, …, n} Hence, there are C(n, r) bit strings of length n that contain exactly r 1s
Combinations: Example 5 Suppose that there are 9 professors in the math department and 11 in the CS department How many ways are there to select a committee to develop a discrete structures course if the committee is to consist of 3 professors from the math department and 4 from the CS department? Solution: The answer is the product of the number of 3-combinations of a set with 9 elements and the number of 4-combinations of a set with 11 elements C(9, 3) · C(11, 4) = [9! / (3! (9 - 3)!)] · [11! / (4! (11 - 4)!)] = 84 · 330 = 27,720
Binomial Coefficients and Identities Section 6.4
Section Summary The Binomial Theorem Pascal’s Identity and Triangle
Binomial Coefficients A binomial expression is simply the sum of 2 terms, such as x + y Binomial coefficient are the coefficients in the expansion of powers of binomial expressions such as (x + y)n The binomial theorem gives the coefficients of the expansion of powers of binomial expressions Binomial coefficients are combinations C(n, r), which are also written as 𝑛 𝑟 Example: 4 0 x4 + 4 1 x3y + 4 2 x2y2 + 4 3 xy3 + 4 4 y4 = 𝑗=0 4 4 𝑗 𝑥 4−𝑗 𝑦 𝑗
Powers of Binomial Expressions We can use counting principles to find the coefficients in the expansion of (x + y)n where n is a positive integer To illustrate this idea, we first look at the process of expanding (x + y)3 (x + y)(x + y)(x + y) expands into a sum of terms that are the product of a term from each of the three sums Terms of the form x3, x2y, xy2, y3 arise. The question is what are the coefficients? To obtain x3 , an x must be chosen from each of the sums. There is only one way to do this. So, the coefficient of x3 is 1 To obtain x2y, an x must be chosen from 2 of the sums and a y from the other. There are 3 2 ways to do this - so the coefficient of x2y is 3 To obtain xy2, an x must be chosen from of the sums & a y from the other 2. There are 3 1 ways to do this - so the coefficient of xy2 is 3 To obtain y3 , a y must be chosen from each of the sums. There is only one way to do this. So, the coefficient of y3 is 1 We used a counting argument to show that (x + y)3 = x3 + 3x2y + 3xy2 + y3
The Binomial Theorem Theorem: Let x & y be variables, and n Z+ Proof: We use combinatorial reasoning. The terms in the expansion of (x + y)n are of the form xn – j yj for j = 0, 1, 2, …, n. To form the term xn – j yj, it is necessary to choose n – j x’s from the n sums. the coefficient of xn – j yj is 𝑛 𝑛 −𝑗 , which equals 𝑛 𝑗 Example
Binomial Coefficients: Examples Example 1: What is the coefficient of x12 y13 in the expansion of (x + y)25? (x + y)25 = 𝑗=0 25 25 𝑗 𝑥 25−𝑗 𝑦 𝑗 Coefficient of x12 y13 is 25 13 = 25! 13! 12! = 5,200,300 Example 2: What is the coefficient of x12 y13 in the expansion of (2x - 3y)25? (2x - 3y)25 = [2x + (-3y)]25 By the Binomial Theorem:
Binomial Theorem: Identities Corollary 1: Let n be a non-negative integer, then: Corollary 2: Let n be a positive integer, then: Corollary 3: Let n be a non-negative integer, then:
Pascal's Identity and Triangle Pascal’s identity: where n, k Z+, n k This identity is the basis for a geometric arrangement of the binomial coefficients in a triangle form That triangle is known as Pascal's triangle
Pascal Triangle of Binomial Coefficients The nth row in the triangle consists of the binomial coefficients, k = 0,1,….,n By Pascal’s identity, adding two adjacent bionomial coefficients results is the binomial coefficient in the next row between these two coefficients
6.1 Exercises 7. How many different 3-letter initials can people have? 10. How many bit strings are there of length 8? 15. How many strings are there of lowercase letters of length 4 or less, not counting the empty string? 25. How many strings of 3 decimal digits do not contain the same digit three times? begin with an odd digit? have exactly two digits that are 4s? 29. How many license plates can be made using either 2 uppercase English letters followed by 4 digits or 2 digits followed by 4 uppercase English letters?
6.1 Exercises 55. Suppose that a password for a computer must have between 8 and 12, characters, where each character in the password is a lowercase letter, an uppercase letter, a digit, or one of the 6 special characters: *, >, <, !, +, and =. How many different passwords are available for this computer? How many of these passwords contain at least 1 occurrence of at least 1 of the 6 special characters? Using your answer to part (a), determine how long it takes a hacker to try every possible password, assuming that it takes 1 nanosecond to check each possible password
6.1 Exercises 57. The name of a variable in the JAVA programming language is a string of between 1 and 65,535 characters, inclusive, where each character can be an uppercase or a lowercase letter, a dollar sign, an underscore, or a digit, except that the first character must not be a digit. Determine the number of different variable names in JAVA. 63. Use the principle of inclusion-exclusion to find the number of positive integers less than 1,000,000 that are not divisible by either 4 or by 6. 64. Use a tree diagram to find the number of bit strings of length four with no three consecutive 0s.
6.2 Exercises 2. Show that if there are 30 students in a class, then at least 2 have last names that begin with the same letter 3. A drawer contains a dozen brown socks and a dozen black socks, all unmatched. A man takes socks out at random in the dark. How many socks must he take out to be sure that he has at least 2 socks of the same color? How many socks must he take out to be sure that he has at least 2 black socks? 13. Show that if 5 integers are selected from the 1st 8 +ive integers, there must be a pair of these integers with a sum equal to 9
6.2 Exercises 31. Show that there are at least six people in California (population: 37 million) with the same three initials who were born on the same day of the year (but not necessarily in the same year). Assume that everyone has three initials. 35. There are 38 different time periods during which classes at a university can be scheduled. If there are 677 different classes, how many different rooms will be needed?
6.2 Exercises 36. A computer network consists of 6 computers. Each computer is directly connected to at least 1 of the other computers. Show that there are at least 2 computers in the network that are directly connected to the same number of other computers. 44. There are 51 houses on a street. Each house has an address between 1000 and 1099, inclusive. Show that at I east two houses have addresses that are consecutive integers.
6.3 Exercises 2. How many different permutations are there of the set {a, b, c, d, e, f, g} 4. Let S = {1, 2, 3, 4, 5}. List all the 3-permutations of S List all the 3-combinations of S 5. A coin is flipped 10 times, where each flip comes up either heads or tails. How any possible outcomes are there in total? contain exactly two heads? contain at most three tails? contain the same number of heads and tails?
6.3 Exercises 8. In how many different orders can five runners finish a race if no ties are allowed? 11. How many bit strings of length 10 contain exactly four 1’s? at most four 1’s? at least four 1’s? an equal number of 0’s and 1’s? 13. How many permutations of the letters ABCDEFG contain the string BCD the string CFGA the strings BA and GF
6.3 Exercises 21. The English alphabet contains 21 consonants and 5 vowels. How many strings of 6 lowercase letters of the English alphabet contain exactly one vowel? exactly two vowels? at least one vowel? at least two vowels? 27. How many bit strings of length 10 contain at least three 1’s and at least three 0’s?
6.4 Exercises 3. Find the expansion of (x + y)6 4. Find the coefficient of x5y8 in (x + y)13 9. What is the coefficient of x101y99 in the expansion of (2x - 3y)200? 12. The row of Pascal's triangle containing the binomial coefficients 10 𝑘 , 0 ≤ k ≤ 10, is: 1 10 45 120 210 252 210 120 45 10 1 Use Pascal's identity to produce the row immediately following this row in Pascal's triangle
6.4 Exercises 13. Prove Corollary1: Let n be a non-negative integer, then: 14. Prove Corollary2: Let n be a positive integer, then: 15. Prove Corollary3: Let n be a non-negative integer