73
CS 103 Discrete Structures Lecture 16 Counting (1)

CS 103 Discrete Structures Lecture 16 Counting (1)

Embed Size (px)

Citation preview

Induction and recursion

CS 103 Discrete Structures Lecture 16Counting (1)

1Chapter 1 section 1.1 by Dr. Mosaad HassanCountingChapter 6With Question/Answer Animations2Chapter SummaryThe Basics of CountingThe Pigeonhole PrinciplePermutations and CombinationsBinomial Coefficients and Identities

3The Basics of CountingSection 6.14Section SummaryThe Product RuleThe Sum RuleThe Subtraction RuleThe Division RuleExamples, Examples, and ExamplesTree Diagrams5The Basics of CountingSuppose that a password on a computer system consists of 6 to 8 charactersEach of these characters must be a digit (0-9: 10 possibilities) or a capital letter (A-Z: 26 possibilities) of the alphabetEach 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 chapter6Basic Counting Principles We will discuss the following basic counting principles:Product RuleSum RuleSubtraction 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 tasksIf 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 taskThen there are n1 n2 ways to do the procedure7The Product Rule: Example 1How many bit strings of length 8 are there? b b b b b b b bSolution: 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 strings8The Product Rule: Example 2How 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 dSolution: By the product rule, there are17 17 17 10 10 10 10 = 49,130,000different possible license platesNote: The actual number of different license plates is lower as some of the letter combinations are not allowed9Product Rule: Mobile NumbersOperatorFormatPossibilitiesSTC050-xxx-xxxx053-xxx-xxxx055-xxx-xxxx10710710730,000,000Mobily054-xxx-xxxx056-xxx-xxxx10710720,000,000

Zain058-0xx-xxxx058-1xx-xxxx059-xxx-xxxx10610610712,000,000

Virgin057-0xx-xxxx057-1xx-xxxx057-2xx-xxxx1061061063,000,000

Lebara057-6xx-xxxx057-7xx-xxxx057-8xx-xxxx1061061063,000,000

68,000,00010Product Rule: Example 4What is the value of k after thethis code has been executed?

Solution: Initially, k is 0Each time the nested loop is traversed, 1 is added to kBy the product rule, it follows that the nested loop is traversed n1 n2 n3 ... nm timesthe final value of k = n1 n2 n3 ... nm

11Product Rule: Example 5A 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 waysAnd then assign an office to Ali different from the office assigned to Sami. This can be done in 11 waysBy the product rule, there are 12 11 = 132 ways to assign offices to the 2 employees12Product Rule: Example 6How 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 possibilities2nd digit: 9 possibilities (all but the 1st digit)3rd digit: 8 possibilities4th digit: 7 possibilitiesTotal = 10 9 8 7 = 5,040 strings13Counting Principles: Sum RuleIfa task can be done either in one of n1 ways or in one of n2 ways,

wherenone of the set of n1 ways is the same as any of the set of n2 ways,

thenthere are a total of n1 + n2 ways to do the task

14Sum Rule: Example 1Suppose 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 studentChoosing a teacher is never the same as choosing a student because no one is bothBy the sum rule, there are 37 + 83 = 120 possible ways to pick this representative15Sum Rule: Example 2A 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 project16Sum Rule: Example 3What is the value of k after this code has been executed?Solution: Initially, k = 0This block of code is made up of m different loopsEach time a loop is traversed, 1 is added to kBecause we only traverse one loop at a timeThe sum rule shows that the final value of k isn1 + n2 + n3 + ... + nm

17Sum Rule: Set TerminologyThe 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 disjoint18Combining 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 = 28619Example 2: Counting PasswordsHow 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 8By the sum rule, P = P6 + P7 + P8To 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,560P7 = 367 267 =78,364,164,096 8,031,810,176 = 70,332,353,920P8 = 368 268 = 2,821,109,907,456 208,827,064,576 = 2,612,282,842,880P = P6 + P7 + P8 = 2,684,483,063,36020Counting Principles: Subtraction RuleThe subtraction rule generalizes the sum rule to non-disjoint setsIf 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 waysIn set notation, for arbitrary (but finite) sets A, B:|A B| = |A| + |B| - |A B|Example: Let |A| = 5, |B| = 3, and |A B| = 1Different elements in the union are 5 + 3 - 1 = 7 not 8

This rule is also known as the inclusion-exclusion principle 21Subtraction Rule: Example 1How 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 lengtheight that begin with 1 or end with a 00, is128 + 64 - 32 = 160

22Subtraction Rule: Example 2In a university has1000 students in total200 students are taking a course in mathematics300 are taking a course in physics50 are taking bothHow many students are taking at least one of those courses?

Solution: If M = set of students taking Mathematics P = set of students taking Physicsthen |M P| = |M| + |P| - |M P| = 300 + 200 - 50 = 45023Subtraction Rule: Example 3A company received 350 applications for a job:220 of the applicants majored in CS147 majored in business51 majored in CS as well as in business

How many of the applicants majored neither in CS nor in business?24Example 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 business25Tree Diagrams for CountingCounting problems can be solved using tree diagrams

A tree consists of:A rootA number of branches leaving the rootPossible additional branches leaving the endpoints of other branches

To use trees in counting:We use a branch to represent each possible choiceWe represent the possible outcomes by the leaves26Tree Diagrams: Example 1How many bit strings of length 4 do not have 2 consecutive 1s?

This tree diagram displaysall bit strings of length 4without 2 consecutive 1s

27Tree Diagrams: Example 2T-shirts come in 5 sizes: S, M, L, XL, and XXLS, M, and L comes in white, red, green, and blackXL comes only in red, green, and blackXXL 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?

1728Tree 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?

2029The Pigeonhole PrincipleSection 6.230Section SummaryThe Pigeonhole PrincipleThe Generalized Pigeonhole Principle31The Pigeonhole PrincipleSuppose 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 it32The Pigeonhole Principle

33The Pigeonhole PrincipleThis principle applies to objects other than pigeons and pigeonholesPigeonhole 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 objectsProof 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 objectsExample 1: Among any group of 367 people, there must be at least 2 with the same birthday, because there are only 366 possible birthdaysExample 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 alphabet34Pigeonhole Principle: ExampleHow 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 score35Generalized 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

36Generalized Pigeonhole Principle If N objects are placed into k boxes, then thereis 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 26ObjectsBoxesObjectsBoxes37CS 103 Discrete Structures Lecture 17Counting (2)

38Chapter 1 section 1.1 by Dr. Mosaad HassanPermutations and CombinationsSection 6.339Section SummaryPermutationsCombinations40PermutationsA 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 SThe ordered arrangement 3, 2 is a 2-permutation of SChapter 5 by Dr. Mosaad Wageh Hassanr-PermutationsThe 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 with1 r n, then there are P(n, r) = n(n 1)(n 2) (n r + 1)r-permutations of a set with n distinct elementsProof: 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 elementNote: P(n, 0) = 1, i.e. there is only 1 way to order 0 elements42r-PermutationsWe can find P(n, r) from the following corollary

Corollary: If n and r are integers with 0 r n, then:

43Permutations: Examples 1 & 2In 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 student4 ways to select the 2nd3 ways to select the 3rdBy the product rule, there are 5 4 3 = 60 = 5! / (5 - 3)! ways to select 3 students out of a group of 5In how many ways can we arrange all 5 of these students in a line?There are 5 ways to select the 1st student4 for 2nd 3 for 3rd 2 for 4th 1 for 5th Consequently, there are5 4 3 2 1 = 5! / (5 - 5)! = 120 ways44Permutations: Example 3Suppose that there are 8 runners in a raceThe winner receives a gold medalThe 2nd-place finisher receives silverThe 3rd-place finisher receives bronzeHow 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 ways45Permutations: Example 4Suppose that a salesman has to visit 8 different citiesHe must begin his trip in a specified city, but he can visit the other 7 cities in any order he wishesHow 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 elementsThe 1st city is determined, but the remaining seven can be ordered arbitrarilyThere are 7! / (7 - 7)! = 5,040 ways46Permutations: Example 5How 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 blockTheses objects to be selected are block ABC and the individual letters D, E, F, G, and HBecause 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 block47CombinationsCombinations 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 elements48r-Combinations49Combinations: Example 2How 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 of5-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)!) = 25250Combinations: Example 3A group of 30 people have been trained as astronauts to go on the first mission to MarsHow 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 matterThe number of such combinations is:C(30, 6) = 30! / (6! (30 - 6)!) = 593,77551Combinations: Example 4How many bit strings of length ncontain exactly r 1s?

Example bit-string: 0101 1100 0000 0011n = 16r = 6

Solution: The positions of r 1s 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 1s52Combinations: Example 5Suppose that there are 9 professors in the math department and 11 in the CS departmentHow 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 elementsC(9, 3) C(11, 4) = [9! / (3! (9 - 3)!)] [11! / (4! (11 - 4)!)]= 84 330 = 27,72053Binomial Coefficients and IdentitiesSection 6.454Section SummaryThe Binomial Theorem Pascals Identity and Triangle55Binomial Coefficients56Powers of Binomial Expressions57The Binomial Theorem

58Binomial Coefficients: Examples

59Binomial Theorem: IdentitiesCorollary 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:

60Pascal's Identity and TrianglePascals identity: where n, k Z+, n kThis identity is the basis for a geometric arrangement of the binomial coefficients in a triangle formThat triangle is known as Pascal's triangle

61Pascal Triangle of Binomial Coefficients

The nth row in the triangle consists of the binomial coefficients, k = 0,1,.,nBy Pascals identity, adding two adjacent bionomial coefficients results is the binomial coefficient in the next row between these two coefficients 626.1 Exercises7. How many different 3-letter initials can peoplehave? 10. How many bit strings are there of length 8? 15. How many strings are there of lowercase lettersof length 4 or less, not counting the empty string? 25. How many strings of 3 decimal digitsdo 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 usingeither 2 uppercase English letters followed by 4 digitsor 2 digits followed by 4 uppercase English letters?636.1 Exercises55. Suppose that a password for a computer musthave between 8 and 12, characters, where eachcharacter in the password is a lowercase letter, anuppercase letter, a digit, or one of the 6 specialcharacters: *, >,