+ All Categories
Home > Documents > Dr. Eng. Farag Elnagahy [email protected] Office Phone: 67967 King ABDUL AZIZ University...

Dr. Eng. Farag Elnagahy [email protected] Office Phone: 67967 King ABDUL AZIZ University...

Date post: 02-Jan-2016
Category:
Upload: naomi-merritt
View: 221 times
Download: 0 times
Share this document with a friend
Popular Tags:
27
Dr. Eng. Farag Elnagahy [email protected] m Office Phone: 67967 King ABDUL AZIZ University Faculty Of Computing and Information Technology CPCS 222 Discrete Structures I Counting Counting
Transcript
Page 1: Dr. Eng. Farag Elnagahy farahelnagahy@hotmail.com Office Phone: 67967 King ABDUL AZIZ University Faculty Of Computing and Information Technology CPCS 222.

Dr. Eng. Farag [email protected]

omOffice Phone: 67967

King ABDUL AZIZ University

Faculty Of Computing and Information Technology

CPCS 222Discrete Structures I

CountingCounting

Page 2: Dr. Eng. Farag Elnagahy farahelnagahy@hotmail.com Office Phone: 67967 King ABDUL AZIZ University Faculty Of Computing and Information Technology CPCS 222.

22

The Basics of countingThe Basics of counting

Combinatorics is the mathematics of counting Combinatorics is the mathematics of counting and arranging objects.and arranging objects.

CountingCounting of objects with certain properties of objects with certain properties (enumeration) is required to solve many (enumeration) is required to solve many different types of problems. For example, different types of problems. For example, counting is used to:counting is used to:

Determine number of ordered or unordered Determine number of ordered or unordered arrangement of objects.arrangement of objects. Generate all the arrangements of a specified Generate all the arrangements of a specified kindkind which is important in computer simulations.which is important in computer simulations. Compute probabilities of events.Compute probabilities of events. Analyze the chance of winning games, lotteries Analyze the chance of winning games, lotteries etc.etc. Determine the complexity of algorithms.Determine the complexity of algorithms.

Page 3: Dr. Eng. Farag Elnagahy farahelnagahy@hotmail.com Office Phone: 67967 King ABDUL AZIZ University Faculty Of Computing and Information Technology CPCS 222.

33

The Basics of countingThe Basics of counting

Two basic counting principlesTwo basic counting principles The Sum RuleThe Sum Rule The Product RuleThe Product Rule

Let us consider two tasks:Let us consider two tasks: mm is the number of ways to do is the number of ways to do task1task1 nn is the number of ways to do is the number of ways to do task2task2Performing task1 does not accomplish task2 and Performing task1 does not accomplish task2 and vice versa (task1 and task2 are vice versa (task1 and task2 are independentindependent of of each other).each other).

• Sum rule:Sum rule: the number of ways that “ the number of ways that “eithereither task1 task1 oror task2 can be done, but not both”, is task2 can be done, but not both”, is m+nm+n..

• Product rule:Product rule: the number of ways that “ the number of ways that “bothboth task1 task1 andand task2 can be done” in task2 can be done” in mnmn..

Page 4: Dr. Eng. Farag Elnagahy farahelnagahy@hotmail.com Office Phone: 67967 King ABDUL AZIZ University Faculty Of Computing and Information Technology CPCS 222.

44

The Basics of counting The Basics of counting (Examples)(Examples)

Example:Example: A student can choose a computer A student can choose a computer project from one of three lists. The three lists project from one of three lists. The three lists contain 23, 15, and 19 possible projects contain 23, 15, and 19 possible projects respectively. How many possible projects are respectively. How many possible projects are there to choose from? (there to choose from? (23+15+19=5723+15+19=57))

Example:Example: The chairs of an auditorium are to be The chairs of an auditorium are to be labeled with a letter and a positive integer not labeled with a letter and a positive integer not to exceed 100. What is the largest number of to exceed 100. What is the largest number of chairs that can be labeled differently? chairs that can be labeled differently? ((26x100=260026x100=2600))

Page 5: Dr. Eng. Farag Elnagahy farahelnagahy@hotmail.com Office Phone: 67967 King ABDUL AZIZ University Faculty Of Computing and Information Technology CPCS 222.

55

The Basics of counting (Set The Basics of counting (Set version)version)

If If AA is the is the setset of ways to do task1, and B the set of ways to do task1, and B the set of ways to do task2, and if A and B are of ways to do task2, and if A and B are disjointdisjoint, , then:then: The ways to do The ways to do eithereither task1 task1 oror task2 are task2 are

AAB, and |AB, and |AB|=|A|+|B|B|=|A|+|B| The ways to do The ways to do bothboth task1 task1 andand task2 are task2 are

AAB, and |AB, and |AB|=|A|·|B|B|=|A|·|B|

The number of The number of different subsetsdifferent subsets of a finite set(s) of a finite set(s) is ?is ?

000000000000000000000000

100000000000100000000000

010000000000 010000000000 22|s||s|

……..

111111111111111111111111

Page 6: Dr. Eng. Farag Elnagahy farahelnagahy@hotmail.com Office Phone: 67967 King ABDUL AZIZ University Faculty Of Computing and Information Technology CPCS 222.

66

The Basics of counting (Examples)The Basics of counting (Examples)

What is the value of k after the following code What is the value of k after the following code has been executed?has been executed?

K:=0K:=0for ifor i11:=1 to :=1 to nn11

K:= K +1K:= K +1for ifor i22=1 to =1 to nn22

K:= K +1K:= K +1for ifor i33=1 to =1 to nn33

K:= K +1K:= K +1

KK= = nn11+ + nn22+ + nn33

K:=0K:=0for ifor i11:=1 to :=1 to nn11

for ifor i22:=1 to :=1 to nn22

for ifor i33:=1 to :=1 to nn33

K:= K +1K:= K +1

KK= = nn11x x nn22x x nn33

Page 7: Dr. Eng. Farag Elnagahy farahelnagahy@hotmail.com Office Phone: 67967 King ABDUL AZIZ University Faculty Of Computing and Information Technology CPCS 222.

77

The Basics of counting (Examples)The Basics of counting (Examples)

Count the number of print statements in this Count the number of print statements in this algorithm:algorithm:

The total number of print statements executed The total number of print statements executed is is nn · ( · (nn++nn) = 2n) = 2n22..

for i := 1 to for i := 1 to nn beginbegin for j := 1 to for j := 1 to nn print “hello”print “hello” for k := 1 to for k := 1 to nn print “hello”print “hello” endend

Page 8: Dr. Eng. Farag Elnagahy farahelnagahy@hotmail.com Office Phone: 67967 King ABDUL AZIZ University Faculty Of Computing and Information Technology CPCS 222.

88

The Basics of counting (Examples)The Basics of counting (Examples)

Count the number of print statements in this Count the number of print statements in this algorithm:algorithm:

for each for each ii, the number of print statements , the number of print statements executed is executed is ii in the in the j loopj loop plus plus n−in−i in the in the k loopk loop. . Therefore, for each Therefore, for each ii, the number of print , the number of print statements is i + (n−i) = n.statements is i + (n−i) = n.Therefore the total number of print statements Therefore the total number of print statements executed is n · n = executed is n · n = nn22..

for i := 1 to nfor i := 1 to n beginbegin for j := 1 to ifor j := 1 to i print “hello”print “hello” for k := i + 1 to nfor k := i + 1 to n print “hello”print “hello” endend

Page 9: Dr. Eng. Farag Elnagahy farahelnagahy@hotmail.com Office Phone: 67967 King ABDUL AZIZ University Faculty Of Computing and Information Technology CPCS 222.

99

The Basics of counting (Examples)The Basics of counting (Examples)

In a computer language In a computer language The name of a variable is a string of The name of a variable is a string of oneone or or twotwo alphanumericalphanumeric characters. characters. uppercase and lowercase letters are not uppercase and lowercase letters are not distinguished.distinguished. 26 English letter , 10 digits.26 English letter , 10 digits. the variable name must begin with letter.the variable name must begin with letter. there are five strings of two characters that there are five strings of two characters that are reserved for programming use.are reserved for programming use.How manyHow many different variable names are there ? different variable names are there ?

VV11 string (one character) ,V string (one character) ,V22 string (two string (two characters)characters)V=VV=V11+V+V2 2

VV11=26=26VV22=26x(26+10) – 5 =26x36 –5 =931=26x(26+10) – 5 =26x36 –5 =931V=26+931=V=26+931=957957

Page 10: Dr. Eng. Farag Elnagahy farahelnagahy@hotmail.com Office Phone: 67967 King ABDUL AZIZ University Faculty Of Computing and Information Technology CPCS 222.

1010

The Basics of counting (Examples)The Basics of counting (Examples)

How many different license plates are available How many different license plates are available if each plate contains a sequence of three if each plate contains a sequence of three letters followed by three digits?letters followed by three digits?

LL11 L L22 L L33 D D11 D D22 D D33

Each of the three letters can be written in 26 Each of the three letters can be written in 26 different ways, and each of the three digits can different ways, and each of the three digits can be written in 10 different ways.be written in 10 different ways.

Hence, by the product rule, there is a total of Hence, by the product rule, there is a total of

26 26 26 26 26 26 10 10 10 10 10 = 10 = 17,576,00017,576,000

different license plates possible.different license plates possible.

Page 11: Dr. Eng. Farag Elnagahy farahelnagahy@hotmail.com Office Phone: 67967 King ABDUL AZIZ University Faculty Of Computing and Information Technology CPCS 222.

1111

The Basics of counting (Examples)The Basics of counting (Examples)

Each user on a computer system has a Each user on a computer system has a password, which is six to eight characters long, password, which is six to eight characters long, where each character is an uppercase letter or where each character is an uppercase letter or digit. Each password must contain digit. Each password must contain at least one at least one digitdigit. How many passwords are there?. How many passwords are there?

(string includes Letters & Digits)- (string with (string includes Letters & Digits)- (string with no digits)no digits)

P=PP=P66+P+P77+P+P88 PP66=36=3666-26-2666=1 867 866 560=1 867 866 560PP77=36=3677-26-2677=70 332 353 920=70 332 353 920PP88=36=3688-26-2688=2 612 282 842 880=2 612 282 842 880PP=P=P66+P+P77+P+P88==2 684 483 063 3602 684 483 063 360

Page 12: Dr. Eng. Farag Elnagahy farahelnagahy@hotmail.com Office Phone: 67967 King ABDUL AZIZ University Faculty Of Computing and Information Technology CPCS 222.

1212

The Basics of counting (Examples)The Basics of counting (Examples)

In version 4 of the Internet Protocol (IPv4)In version 4 of the Internet Protocol (IPv4)The internet address is a string of 32 bits as The internet address is a string of 32 bits as follows: follows: Class Class AA address [ 0 -netid(7 bits) – hostid (24 address [ 0 -netid(7 bits) – hostid (24 bits)]bits)]Class Class BB address [ 10 -netid(14 bits) – hostid (16 address [ 10 -netid(14 bits) – hostid (16 bits)]bits)]Class Class CC address [ 110 -netid(21 bits) – hostid (8 address [ 110 -netid(21 bits) – hostid (8 bits)]bits)]ClassClass DD address [ 1110 –multicast address (28 address [ 1110 –multicast address (28 bits)]bits)]ClassClass EE address [ 11110 - address (27 bits)] address [ 11110 - address (27 bits)] Where, Network number(Where, Network number(netidnetid) -host ) -host number(number(hostidhostid))Restrictions Restrictions 1111111 is unavailable in netid1111111 is unavailable in netid All 0s and all 1s are unavailable in hostidAll 0s and all 1s are unavailable in hostid The computer on the Internet has either class The computer on the Internet has either class A or B or C addresses. A or B or C addresses. How manyHow many different IPv4 different IPv4 addresses are available for computers on the addresses are available for computers on the Internet? Internet? (2(277-1)(2-1)(22424-2)+(2-2)+(21414-1)(2-1)(21616-2)+(2-2)+(22121-1)(2-1)(288-2)-2)

Page 13: Dr. Eng. Farag Elnagahy farahelnagahy@hotmail.com Office Phone: 67967 King ABDUL AZIZ University Faculty Of Computing and Information Technology CPCS 222.

1313

The Basics of counting (Examples)The Basics of counting (Examples)

How many bit string of length eight either How many bit string of length eight either startstart with a 1 bit with a 1 bit oror endend with the two bits 00? with the two bits 00?

11 -- -- -- -- -- -- -- 2277=128 ways=128 ways

-- -- -- -- -- -- 00 00 2266=64 ways=64 ways

11 -- -- -- -- -- 00 00 2255=32 ways=32 ways

128 + 64- 32= 160128 + 64- 32= 160

Page 14: Dr. Eng. Farag Elnagahy farahelnagahy@hotmail.com Office Phone: 67967 King ABDUL AZIZ University Faculty Of Computing and Information Technology CPCS 222.

1414

The Basics of counting (Examples)The Basics of counting (Examples)

A computer company receives 350 applications A computer company receives 350 applications from computer graduates for a job.from computer graduates for a job.Suppose that Suppose that 220220 of these people majored in of these people majored in CSCS, , 147147 majored in majored in businessbusiness, and , and 5151 majored majored both in both in CSCS and in and in businessbusiness. How many of these . How many of these applicants majored neither in CS nor in applicants majored neither in CS nor in business?business?

Let ALet A11 be the set of students who majored in CS be the set of students who majored in CSLet ALet A22 be the set of students who majored in be the set of students who majored in businessbusiness

The number of students who majored either in The number of students who majored either in CS or in business (or both) isCS or in business (or both) is|A|A11AA22|=|A|=|A11|+|A|+|A22|-|A|-|A11AA22|=220+147-51=316|=220+147-51=316The number of applicants who majored neither The number of applicants who majored neither in CS nor in business is 350-316=34in CS nor in business is 350-316=34

Page 15: Dr. Eng. Farag Elnagahy farahelnagahy@hotmail.com Office Phone: 67967 King ABDUL AZIZ University Faculty Of Computing and Information Technology CPCS 222.

1515

The Basics of counting (Examples)The Basics of counting (Examples)

ExercisesExercisesPP. 344-347PP. 344-3471-121-12

Page 16: Dr. Eng. Farag Elnagahy farahelnagahy@hotmail.com Office Phone: 67967 King ABDUL AZIZ University Faculty Of Computing and Information Technology CPCS 222.

1616

The Pigeonhole principleThe Pigeonhole principle

The Pigeonhole principleThe Pigeonhole principle

Suppose there are Suppose there are n pigeonsn pigeons, , k pigeonholesk pigeonholes, , and and n>kn>k. .

If these n pigeons fly into these k pigeonholes, If these n pigeons fly into these k pigeonholes, then some pigeonhole must contain at least two then some pigeonhole must contain at least two pigeons. pigeons.

If k+1 objects are assigned to k places, then at If k+1 objects are assigned to k places, then at least 1 place must be assigned ≥2 objects. least 1 place must be assigned ≥2 objects.

7 pigeons7 pigeons

6 pigeonholes6 pigeonholes

Page 17: Dr. Eng. Farag Elnagahy farahelnagahy@hotmail.com Office Phone: 67967 King ABDUL AZIZ University Faculty Of Computing and Information Technology CPCS 222.

1717

The Pigeonhole principleThe Pigeonhole principle

In terms of the assignment function: In terms of the assignment function:

If f:A→B and |A|≥|B|+1, then some element of If f:A→B and |A|≥|B|+1, then some element of B B

has ≥2 pre-images under f.( f is not one-to-has ≥2 pre-images under f.( f is not one-to-one)one)

How many students must be in class to How many students must be in class to guarantee that at least two students receive guarantee that at least two students receive the same score on the final exam, if the exam is the same score on the final exam, if the exam is graded on a scale from 0 to 100 points? graded on a scale from 0 to 100 points? Greater Greater than 101than 101

Page 18: Dr. Eng. Farag Elnagahy farahelnagahy@hotmail.com Office Phone: 67967 King ABDUL AZIZ University Faculty Of Computing and Information Technology CPCS 222.

1818

The Pigeonhole principle (Examples)The Pigeonhole principle (Examples)

The generalized Pigeonhole principleThe generalized Pigeonhole principle

If N objects are placed into k boxes, then If N objects are placed into k boxes, then there is at least one box containing at least there is at least one box containing at least N/KN/K objects. objects.

e.g.e.g., there are , there are NN=280 students in this class. =280 students in this class. There are There are kk=52 weeks in the year.=52 weeks in the year.

Therefore, there must be at least 1 week during Therefore, there must be at least 1 week during which at least which at least 280/52280/52= = 5.385.38=6 students in =6 students in the class have a birthday.the class have a birthday.

Page 19: Dr. Eng. Farag Elnagahy farahelnagahy@hotmail.com Office Phone: 67967 King ABDUL AZIZ University Faculty Of Computing and Information Technology CPCS 222.

1919

The Pigeonhole principle (Examples)The Pigeonhole principle (Examples)

There are 280 students in the class. Without There are 280 students in the class. Without knowing anybody’s birthday, what is the knowing anybody’s birthday, what is the largest value of largest value of nn for which we can prove that for which we can prove that at least at least nn students must have been born in the students must have been born in the same month?same month?

280/12280/12 = = 23.323.3 = 24 = 24 What is the What is the minimumminimum number of students number of students required in a discrete math class to be sure that required in a discrete math class to be sure that at least six will receive the same grade, if there at least six will receive the same grade, if there are five possible grades, A, B, C, D, and F?are five possible grades, A, B, C, D, and F?

Page 20: Dr. Eng. Farag Elnagahy farahelnagahy@hotmail.com Office Phone: 67967 King ABDUL AZIZ University Faculty Of Computing and Information Technology CPCS 222.

2020

Permutations and Permutations and CombinationsCombinationsPermutationsPermutationsA.A. In how many ways can we In how many ways can we selectselect 3 students 3 students

from a group of from a group of 55 students to stand in line students to stand in line for a picture?for a picture?

B.B. In how many ways can we arrange all 5 of In how many ways can we arrange all 5 of these students in a line for a picture?these students in a line for a picture?

Note that Note that the order in which we select the the order in which we select the students matters.students matters.

55 ways to select the first student ways to select the first student44 ways to select the second student ways to select the second student33 ways to select the third student ways to select the third student22 ways to select the fourth student ways to select the fourth student11 way to select the fifth student way to select the fifth student

A.A. 5.4.3=60 5.4.3=60B.B. 5.4.3.2.1=120 5.4.3.2.1=120

Page 21: Dr. Eng. Farag Elnagahy farahelnagahy@hotmail.com Office Phone: 67967 King ABDUL AZIZ University Faculty Of Computing and Information Technology CPCS 222.

2121

Permutations and Permutations and CombinationsCombinationsPermutationsPermutations A A permutationpermutation of a set S of objects is an of a set S of objects is an

orderedordered arrangement of the elements of S arrangement of the elements of S where each element appears only where each element appears only onceonce::

e.g., 1 2 3, 2 1 3, 3 1 2e.g., 1 2 3, 2 1 3, 3 1 2

An ordered arrangement of An ordered arrangement of rr distinctdistinct elements of S is called an elements of S is called an r-permutationr-permutation..

The number of r-permutations of a set S The number of r-permutations of a set S with n=|S| elements is with n=|S| elements is

P(n,r) = n(n−1)(n−2) …(n−r+1) = P(n,r) = n(n−1)(n−2) …(n−r+1) =

n!/(n−r)!n!/(n−r)!

Page 22: Dr. Eng. Farag Elnagahy farahelnagahy@hotmail.com Office Phone: 67967 King ABDUL AZIZ University Faculty Of Computing and Information Technology CPCS 222.

2222

Permutations and Permutations and CombinationsCombinationsPermutations (Permutations (examplesexamples)) How many ways are there to select a first-How many ways are there to select a first-

prize winner, a second-prize winner,and a prize winner, a second-prize winner,and a third-prize winner from 100 different people third-prize winner from 100 different people who have entered a contest?who have entered a contest?

P(100,3)=P(100,3)=100.99.98=970200100.99.98=970200

• How many permutations of the letters How many permutations of the letters ABCDEFGH contain the string ABC ?ABCDEFGH contain the string ABC ?

ABC, D, E, F, G, H we have 6 objectsABC, D, E, F, G, H we have 6 objectsTheses object can occur in any orderTheses object can occur in any order

6.5.4.3.2.16.5.4.3.2.1 There are 6!=720 There are 6!=720 permutations permutations

Page 23: Dr. Eng. Farag Elnagahy farahelnagahy@hotmail.com Office Phone: 67967 King ABDUL AZIZ University Faculty Of Computing and Information Technology CPCS 222.

2323

Permutations and Permutations and CombinationsCombinationsCombinationsCombinations The number of ways of choosing r elements The number of ways of choosing r elements

from S (order does not matter).from S (order does not matter).

S={1,2,3}S={1,2,3}

e.g., 1 2 , 1 3, 2 e.g., 1 2 , 1 3, 2

The number of The number of r-combinationsr-combinations C(n,r) of a C(n,r) of a set with n=|S| elements is set with n=|S| elements is

Page 24: Dr. Eng. Farag Elnagahy farahelnagahy@hotmail.com Office Phone: 67967 King ABDUL AZIZ University Faculty Of Computing and Information Technology CPCS 222.

2424

Permutations and Permutations and CombinationsCombinationsExampleExampleS={1,2,3},all permutations={(1,2,3),(2,1,3),(1,3,2),

(2,3,1),(3,1,2),(3,2,1)}all 2-permutations={(1,2),(2,1),(1,3),(3,1),

(2,3),(3,2)}P(3,3)=3*2*1=6, P(3,2)=3*2=6

S={1,2,3}, all 2-combinations={{1,2},{1,3},{2,3}}

Comparing to all 2-permutations, we see we ignore order,

Page 25: Dr. Eng. Farag Elnagahy farahelnagahy@hotmail.com Office Phone: 67967 King ABDUL AZIZ University Faculty Of Computing and Information Technology CPCS 222.

2525

Permutations and Permutations and CombinationsCombinationsExampleExampleHow many ways are there to choose a How many ways are there to choose a

committee of size five consisting of three committee of size five consisting of three women and two men from a group of ten women and two men from a group of ten women and seven men?women and seven men?

The number of ways to choose three women The number of ways to choose three women is is CC(10(10, , 3) 3)

The number of ways to choose two men is The number of ways to choose two men is CC(7(7, , 2).2).

Using the product rule to choose three Using the product rule to choose three women and two men, the answer iswomen and two men, the answer is

CC(10(10, , 3) 3) · C· C(7(7, , 2) = 22) = 2, , 520.520.

Page 26: Dr. Eng. Farag Elnagahy farahelnagahy@hotmail.com Office Phone: 67967 King ABDUL AZIZ University Faculty Of Computing and Information Technology CPCS 222.

2626

Permutations and Permutations and CombinationsCombinationsExampleExampleA class has 20 women and 16 men. In how many ways A class has 20 women and 16 men. In how many ways

can youcan you(a) Put all the students in a row?(a) Put all the students in a row?(b) Put 7 of the students in a row?(b) Put 7 of the students in a row?(c) Put all the students in a row if all the women are on (c) Put all the students in a row if all the women are on

the left and all the men are on the right?the left and all the men are on the right?

Solution:Solution:(a) There are 36 students. They can be put in a row in (a) There are 36 students. They can be put in a row in

36! ways.36! ways.(b) You need to have an ordered arrangement of 7 out (b) You need to have an ordered arrangement of 7 out

of 36 students. The number of such arrangementsof 36 students. The number of such arrangementsis is PP(36(36, , 7).7).(c) You need to have an ordered arrangement of all 20 (c) You need to have an ordered arrangement of all 20

women AND and ordered arrangement of all 16 men.women AND and ordered arrangement of all 16 men.By the product rule, this can be done in 20!By the product rule, this can be done in 20!··16! ways.16! ways.

Page 27: Dr. Eng. Farag Elnagahy farahelnagahy@hotmail.com Office Phone: 67967 King ABDUL AZIZ University Faculty Of Computing and Information Technology CPCS 222.

2727

Permutations and Permutations and CombinationsCombinationsExercisesExercisesPP. 360-362PP. 360-36211445577


Recommended