1 Combinatorics Rosen 6 th ed., §5.1-5.3, § 5.5. 2 Combinatorics Count the number of ways to put...

44
1 Combinatorics Rosen 6 Rosen 6 th th ed., §5.1-5.3, § ed., §5.1-5.3, § 5.5 5.5

Transcript of 1 Combinatorics Rosen 6 th ed., §5.1-5.3, § 5.5. 2 Combinatorics Count the number of ways to put...

1

Combinatorics

Rosen 6Rosen 6thth ed., §5.1-5.3, § 5.5 ed., §5.1-5.3, § 5.5

2

Combinatorics

• Count the number of ways to put things Count the number of ways to put things together into various combinations.together into various combinations. e.g.e.g. If a password is 6-8 letters and/or digits, If a password is 6-8 letters and/or digits,

how many passwords can there be?how many passwords can there be?

• Two main rules:Two main rules:– Sum ruleSum rule– Product ruleProduct rule

3

Sum 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 task 1task 1– nn is the number of ways to do is the number of ways to do task 2task 2– Tasks are Tasks are independentindependent of each other, i.e., of each other, i.e.,

• Performing Performing task 1task 1 does not accomplish does not accomplish task 2task 2 and and vice versa.vice versa.

• Sum ruleSum rule: the number of ways that “: the number of ways that “eithereither task 1task 1 or or task 2 can be done, but task 2 can be done, but not bothnot both”, ”, is is mm++nn..

• Generalizes to multiple tasks ... Generalizes to multiple tasks ...

4

Example

• A student can choose a computer project from one of three A student can choose a computer project from one of three lists. The three lists contain 23, 15, and 19 possible lists. The three lists contain 23, 15, and 19 possible projects respectively. How many possible projects are projects respectively. How many possible projects are there to choose from?there to choose from?

5

Set Theoretic Version

• If If AA is the set of ways to do task 1, and is the set of ways to do task 1, and BB the set of ways to do task 2, and if the set of ways to do task 2, and if AA and and BB are are disjointdisjoint, then:, then:

“ “the ways to do either task 1 or 2 are the ways to do either task 1 or 2 are

AABB, and |, and |AABB|=||=|AA|+||+|BB|”|”

6

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 task 1task 1– nn is the number of ways to do is the number of ways to do task 2task 2– Tasks are Tasks are independentindependent of each other, i.e., of each other, i.e.,

• Performing task 1does not accomplish task 2 and Performing task 1does not accomplish task 2 and vice versa.vice versa.

• PProduct ruleroduct rule: the number of ways that : the number of ways that ““bothboth tasks 1 and 2 can be done” in tasks 1 and 2 can be done” in mnmn..

• Generalizes to multiple tasks ...Generalizes to multiple tasks ...

7

Example

• The chairs of an auditorium are to be labeled with a letter The chairs of an auditorium are to be labeled with a letter and a positive integer not to exceed 100. What is the and a positive integer not to exceed 100. What is the largest number of chairs that can be labeled differently?largest number of chairs that can be labeled differently?

8

Set Theoretic Version

• If If AA is the set of ways to do task 1, and is the set of ways to do task 1, and BB the set of ways to do task 2, and if the set of ways to do task 2, and if AA and and BB are are disjointdisjoint, then:, then:

• The ways to do both task 1 and 2 can be The ways to do both task 1 and 2 can be represented as represented as AABB, and , and |A|AB|=|AB|=|A|·||·|BB||

9

More Examples

• How many different bit strings are there of How many different bit strings are there of length seven?length seven?

10

More Examples

• Suppose that either a member of the CS faculty or a Suppose that either a member of the CS faculty or a student who is a CS major can be on a university student who is a CS major can be on a university committee. How many different choices are there if there committee. How many different choices are there if there are 37 CS faculty and 83 CS majors ? are 37 CS faculty and 83 CS majors ?

11

More Examples

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

12

More Examples

• What is the number of different subsets of a What is the number of different subsets of a finite set S ? finite set S ?

13

Example Using Both Rules

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

14

IP Address Example(Internet Protocol vers. 4)

• Main computer addresses are in one of 3 types:Main computer addresses are in one of 3 types:– Class A:Class A: address contains a 7-bit “netid” ≠ address contains a 7-bit “netid” ≠ 1177, and a 24-bit “hostid”, and a 24-bit “hostid”

– Class B:Class B: address has a 14-bit netid and a 16-bit hostid. address has a 14-bit netid and a 16-bit hostid.

– Class C:Class C: address has 21-bit netid and an 8-bit hostid. address has 21-bit netid and an 8-bit hostid.

– Hostids that are all 0s or all 1s are Hostids that are all 0s or all 1s are not allowednot allowed..

• How many valid computer addresses are there?How many valid computer addresses are there?

15

Example Using Both Rules:IP address solution

• (# addrs) (# addrs) = (# class A) + (# class B) + (# class C) = (# class A) + (# class B) + (# class C)(by sum rule)(by sum rule)

• # class A = (# valid netids)·(# valid hostids)# class A = (# valid netids)·(# valid hostids)(by product rule)(by product rule)

• (# valid class A netids) = 2(# valid class A netids) = 277 − 1 = 127. − 1 = 127.• (# valid class A hostids) = 2(# valid class A hostids) = 22424 − 2 = 16,777,214. − 2 = 16,777,214.• Continuing in this fashion we find the answer is:Continuing in this fashion we find the answer is:

3,737,091,842 (3.7 billion IP addresses)3,737,091,842 (3.7 billion IP addresses)

16

Inclusion-Exclusion Principle(relates to the “sum rule”)

• Suppose that Suppose that kkmm of the ways of doing task of the ways of doing task 1 also simultaneously accomplishes task 2. 1 also simultaneously accomplishes task 2. (And thus are also ways of doing task 2.)(And thus are also ways of doing task 2.)

• Then the number of ways to accomplish Then the number of ways to accomplish “Do either task 1 or task 2” is “Do either task 1 or task 2” is mmnnkk..

• Set theory:Set theory: If If AA and and BB are not disjoint, then are not disjoint, then ||AABB|=||=|AA||||BB||||AABB|.|.

17

Example

• How many strings of length eight either How many strings of length eight either start with a 1 bit or end with the two bit start with a 1 bit or end with the two bit string 00?string 00?

18

More Examples

• Hypothetical rules for passwords:Hypothetical rules for passwords:– Passwords must be 2 characters long.Passwords must be 2 characters long.– Each password must be a letter a-z, a digit 0-9, Each password must be a letter a-z, a digit 0-9,

or one of the 10 punctuation characters !@#$or one of the 10 punctuation characters !@#$%^&*().%^&*().

– Each password must contain at least 1 digit or Each password must contain at least 1 digit or punctuation character.punctuation character.

19

Sol. Cont’d

• A legal password has a digit or puctuation A legal password has a digit or puctuation character in position 1 character in position 1 oror position 2. position 2. – These cases overlap, so the principle applies.These cases overlap, so the principle applies.

• (# of passwords w. OK symbol in (# of passwords w. OK symbol in position #1) = (10+10)·(10+10+26)position #1) = (10+10)·(10+10+26)

• (# w. OK sym. in pos. #2): also 20·46(# w. OK sym. in pos. #2): also 20·46

• (# w. OK sym both places): 20·20(# w. OK sym both places): 20·20

• Answer: 920+920−400 = 1,440Answer: 920+920−400 = 1,440

20

Pigeonhole Principle

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

• In terms of the assignment function: In terms of the assignment function: If If ff::AA→→BB and | and |AA|≥||≥|BB|+1, then some element of |+1, then some element of BB

has ≥2 pre-images under has ≥2 pre-images under ff..

i.e., i.e., ff is notis not one-to-one. one-to-one.

21

Example

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

22

Generalized Pigeonhole Principle

• If If NN≥≥k+1k+1 objects are assigned to objects are assigned to kk places, places, then then at leastat least one place must be assigned at one place must be assigned at leastleast NN//kk 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 atTherefore, there must be at least least 1 week during 1 week during

which which at leastat least 280/52280/52= = 5.385.38=6 students in =6 students in the class have a birthday.the class have a birthday.

23

Proof of G.P.P.

• By By contradictioncontradiction. Suppose . Suppose everyevery place has place has < < NN//kk objects, thus ≤ objects, thus ≤ NN//kk−1.−1.

• Then the total number of objects is at mostThen the total number of objects is at most

• So, there are less than So, there are less than NN objects, which objects, which contradicts our assumption of contradicts our assumption of NN objects! □ objects! □

Nk

Nk

k

Nk

k

Nk

111

24

G.P.P. Example

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

• Answer:Answer:

280/12 = 23.3 = 24

25

More Examples

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

26

Permutations

• A A permutationpermutation of a set of a set SS of objects is an of objects is an ordered ordered arrangement of the elements of S where each arrangement of the elements of S where each element appears only 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 elements of SS

is called an is called an r-permutationr-permutation..• The number of The number of rr-permutations of a set -permutations of a set S with with nn=|=|S|S|

elements is elements is PP((nn,,rr)) = = nn((nn−1)…(−1)…(nn−−rr+1) = +1) = nn!/(!/(nn−−rr)!)!

27

Example

• How many ways are there to select a third-How many ways are there to select a third-prize winner from 100 different people who prize winner from 100 different people who have entered a contest?have entered a contest?

28

More Examples

• A terrorist has planted an armed nuclear bomb in A terrorist has planted an armed nuclear bomb in your city, and it is your job to disable it by cutting your city, and it is your job to disable it by cutting wires to the trigger device. wires to the trigger device.

• There are 10 wires to the device. There are 10 wires to the device. • If you cut exactly the right three wires, in exactly If you cut exactly the right three wires, in exactly

the right order, you will disable the bomb, the right order, you will disable the bomb, otherwise it will explode! otherwise it will explode!

• If the wires all look the same, what are your If the wires all look the same, what are your chances of survival?chances of survival?

P(10,3) = 10·9·8 = 720, so there is a 1 in 720 chance that you’ll survive!

29

More Examples

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

30

Combinations

• The number of ways of choosing The number of ways of choosing rr elements elements from S (order from S (order does notdoes not matter). matter).

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

e.g., 1 2 , 1 3, 2 3e.g., 1 2 , 1 3, 2 3• The number of The number of r-r-combinations combinations C(n,r)C(n,r) of a set of a set

with with nn=|=|SS| elements is | elements is

!( , )

!( )!

n nC n r

r r n r

31

Combinations vs Permutations

• Essentially Essentially unordered permutationsunordered permutations … …

• Note that Note that CC((nn,,rr) = ) = CC((nn, , nn−−rr))

)!(!

!

!

)!/(!

),(

),(),(

rnr

n

r

rnn

rrP

rnP

r

nrnC

( , ) ( , ) ( , )P n r C n r P r r

32

Combination Example

• How many distinct 7-card hands can be How many distinct 7-card hands can be drawn from a standard 52-card deck?drawn from a standard 52-card deck?– The order of cards in a hand doesn’t matter.The order of cards in a hand doesn’t matter.

• Answer Answer CC(52,7) = (52,7) = PP(52,7)/(52,7)/PP(7,7)(7,7)= 52·51·50·49·48·47·46 / 7·6·5·4·3·2·1= 52·51·50·49·48·47·46 / 7·6·5·4·3·2·1

710 82

17

52·17·10·7·47·46 = 133,784,560

33

More Examples

• How many ways are there to select a committee to develop How many ways are there to select a committee to develop a discrete mathematics course if the committee is to a discrete mathematics course if the committee is to consist of 3 faculty members from the Math department consist of 3 faculty members from the Math department and 4 from the CS department, if there are 9 faculty and 4 from the CS department, if there are 9 faculty members from Math and 11 from CS?members from Math and 11 from CS?

34

Generalized Permutations and Combinations

• How to solve counting problems where elements may be used more than once?

• How to solve counting problems in which some elements are not distinguishable?

• How to solve problems involving counting the ways we to place distinguishable elements in distinguishable boxes?

35

Permutations with Repetition

• The number of r-permutations of a set of n objects with repetition allowed is

• Example: How many strings of length n can be formed from the English alphabet?

rn

36

Combinations with Repetition

• The number of r-combinations from a set with n elements when repetition of elements is allowed are C(n+r-1,r)

37

Combinations with Repetition

Example: How many ways are there to select 5 bills from a cash box containing $1 bills, $2 bills, $5 bills, $10 bills, $20 bills, $50 bills, and $100 bills? Assume that the order in which bills are chosen does not matter and there are at least 5 bills of each type.

38

Combinations with Repetition

Approach: Place five markers in the compartments i.e., # ways to arrange five stars and six bars ...Solution: Select the positions of the 5 stars from 11 possible positions !

n=7r=5

compartmentsand

dividers markers

C(n+r-1,5)= C(7+5-1,5)=C(11,5)

39

Combinations with Repetition

• Example: How many ways are there to place 10 non-distinguishable balls into 8 distinguishable bins?

40

Permutations and Combinations with and without Repetition

41

Permutations with non-distinguishable objects

• The number of different permutations of n objects, where there are non-distinguishable objects of type 1, non-distinguishable objects of type 2, …, and non-distinguishable objects of type k, is

i.e., i.e., C(n, )C(n- , )…C(n- - -…- , )

1 2

!! !... !k

nn n n

1n

2nkn

1n 1n 2n 1n 2n 1kn kn

1 2 ... kn n n n

42

Permutations with non-distinguishable objects

• Example: How many different strings can be made by reordering the letters of the word

SUCCESS

43

Distributing DistinguishableObjects into Distinguishable Boxes

• The number of ways to distribute n distinguishable objects into k distinguishable boxes so that objects are placed into box i, i=1,2,…,k, equals

in

1 2

!! !... !k

nn n n

44

Distributing DistinguishableObjects into Distinguishable Boxes

• Example: How many ways are there to distribute hands of 5 cards to each of 4 players from the standard deck of 52 cards?