Section 4.2 Counting

32
CS 130 – Discrete Structures Section 4.2 Counting

Transcript of Section 4.2 Counting

CS 130 – Discrete Structures

Section 4.2 Counting

CS 130 – Discrete Structures 27

Counting: Multiplication Principle

• The idea is to find out how many members are present in a finite set

• Multiplication Principle: If there are n possible outcomes for a first event and m possible outcomes for a second event, then there are n*m possible outcomes for the sequence of two events.

• From the multiplication principle, it follows that for 2 sets A and B, |A x B| = |A|x|B|– A x B consists of all ordered pairs with first component

from A and second component from B

CS 130 – Discrete Structures 28

Examples

• How many four digit number can there be if repetitions of numbers is allowed?

• And if repetition of numbers is not allowed?

• If a man has 4 suits, 8 shirts and 5 ties, how many outfits can he put together?

CS 130 – Discrete Structures 29

Counting: Addition Principle

• Addition Principle: If A and B are disjoint events with n and m outcomes, respectively, then the total number of possible outcomes for event “A or B” is n+m

• If A and B are disjoint sets, then |A B| = |A| + |B| using the addition principle

• Example: A customer wants to purchase a vehicle from a dealer. The dealer has 23 autos and 14 trucks in stock. How many selections does the customer have?

CS 130 – Discrete Structures 30

More On Addition Principle

• If A and B are disjoint sets, then |A B| = |A| + |B|• Prove that if A and B are finite sets then|A-B| = |A| - |A B| and |A-B| = |A| - |B| if B A(A-B) (A B) = (A B) (A B)

= A (B B)= A U= A

Also, A-B and A B are disjoint sets, therefore using the addition principle, |A| = | (A-B) (A B) | = |A-B| + |A B|Hence, |A-B| = |A| - |A B|

If B A, then A B = BHence, |A-B| = |A| - |B|

CS 130 – Discrete Structures 31

Using Principles Together

• How many four-digit numbers begin with a 4 or a 5

• How many three-digit integers (numbers between 100 and 999 inclusive) are even?

• Suppose the last four digit of a telephone number must include at least one repeated digit. How many such numbers are there?– Use subtraction

CS 130 – Discrete Structures

Section 4.3: Principle of Inclusion and Exclusion; Pigeonhole Principle

CS 130 – Discrete Structures 33

Principle of Inclusion and Exclusion

• If A and B are any subsets of a universal set S, then A – B, B – A, and A B are mutually distinct, why? (Draw the Venn Diagram)

• We could see that (A-B) (B-A) (A B) is exactly A B

• For three disjoint sets– |(A-B) (B-A) (A B)| = |A-B| + |B-A| + |A B|

• |A-B| = |A| - |A B|• |B-A| = |B| - |A B|• We finally get:

– |A B| = |A| + |B| - |A B|

CS 130 – Discrete Structures 34

Inclusion and Exclusion

• |A B| = |A| + |B| - |A B|• The above equation represents the principle of

inclusion and exclusion• The name comes from the fact that to

calculate the elements in a union:– We include the individual elements of A and B – but exclude the elements common to A and B so that

we don't count them twice

CS 130 – Discrete Structures 35

Example

How many integers from 1 to 1000 are either multiples of 3 or multiples of 5 ?We assume that A = {set of all integers from 1-1000 that are multiples of 3}We assume that B = {set of all integers from 1-1000 that are multiples of 5}We have

A B = {set of all integers from 1 to 1000 that are multiples of either 3 or 5} A B = {set of all integers that are both multiples of 3 and 5, which is 15}

To use the inclusion/exclusion principle to obtain |A B| , we need |A| ,|B| and |A B|

From 1 to 1000, every third integer is a multiple of 3,each of this multiple can berepresented as 3p, for any integer p from 1 through 333, Hence |A| = 333Similarly for multiples of 5, each multiple of 5 is of the form 5q for some integer q from 1 through 200. Hence, we have |B| = 200 To determine the number of multiples of 15 from 1 through 1000, each multiple of 15 is of the form 15r for some integer r from 1 through 66. Hence, |A B| = 66From the principle, we have the number of integer either multiples of 3 or multiples of 5 from 1 to 1000 given by|A B| = 333 + 200 – 66 = 467

CS 130 – Discrete Structures 36

Inclusion/Exclusion Principle for 3 Sets

• In a class of students undergoing a computer course the following were observed. – Out of a total of 50 students: – 30 know Java, – 18 know Python, – 26 know C++, – 9 know both Java and Python, – 16 know both Java and C++, – 8 know both Python and C++, – 47 know at least one of the three languages.

• From this we have to determine – a. How many students know none of these languages ? – b. How many students know all three languages ?

CS 130 – Discrete Structures 37

The Answer

a. We know that 47 students know at least one of the three languages in the class of 50. The number of students who do not know any of three languages is given by the difference between the number of students in class and the number of students who know at least one language. Hence, the students who know none of these languages = 50 – 47 = 3

b. Let us assume that – A = All the students who know Java in class. – B = All the students who know Python in the class.– C = All the students who know C++ in class. – So, we need to find |A B C|

CS 130 – Discrete Structures 38

The Formula For 3 Sets

• We have to derive the inclusion/exclusion formula for 3 sets

|A B C| = |A (B C)| = |A| + |B C| - |A (B C)|= |A| + |B| + |C| - |B C| - |(A B) (A C)|= |A| + |B| + |C| - |B C| - (|A B| + |A C| - |A B C|)= |A| + |B| + |C| - |B C| - |A B| - |A C| + |A B C|

• We know that:– |B C| = 8– |A B| = 9– |A C| =16– |A B C| = 47

• Hence, using the above formula, we have• 47 = 30 + 26 + 18 -9 -16 -8 + |A B C|• Hence, |A B C| = 6

CS 130 – Discrete Structures 39

General Principle of Inclusion/Exclusion

• Given the finite sets A1,……,An, n ≥ 2, then

• In this above equation, the term

• Says add together elements in all intersections of the form, where i < j

• What are these terms for n=3?

1

| |i j

i j n

A A

11 1

1 1 1

| ... | | | | | | | ... ( 1) | ... |nn i i j i j k n

i n i j n i j k n

A A A A A A A A A A

CS 130 – Discrete Structures 40

Pigeonhole Principle

• If more than k items are placed into k bins, then at least one bin has more than one item

from Wikipedia

CS 130 – Discrete Structures 41

Pigeonhole Examples

• How many people must be in a room to guarantee that two people have the last name begin with the same initial?

• How many times must a single die be rolled in order to guarantee getting the same value twice?

• Assume that in a box there are 10 black socks and 12 blue socks and you need to get one pair of socks of the same color. Suppose you can take socks out of the box one at a time without looking, how many socks do you have to pull out together?

26 alphabets hence, have to have 27 people.

6 possible outcomes, hence 7 times.

2 colors, hence 3 times.

CS 130 – Discrete Structures

Section 4.4 Permutations and Combinations

CS 130 – Discrete Structures 43

Permutations

• An ordered arrangement of objects is called a permutation.– Hence, a permutation of n distinct elements is an ordering

of these n elements.

• P(n,r) denotes the number of permutations of r distinct objects chosen from n distinct objects

• A formula for P(n,r) can be written using the factorial functions, for 0 <= r <= n

• Example: 4-digit telephone number without repetition

! ( 1)...( 1)( )!

( , ) ( 1)...( 1)! !

n n n n r n rP n r n n n r

n r n r

CS 130 – Discrete Structures 44

Special Cases of Permutation

• P(n, 0)– P(n, 0) = n! / (n-0)! = n! / n! = 1– There is only one ordered arrangement of zero

objects – the empty set

• P(n, 1)– P(n, 1) = n! / (n-1)! = n– There’re n ordered arrangements of one object

• P(n, n)– P(n, n) = n! / (n-n)! = n! / 0! = n!– There’re n! ordered arrangements of n distinct

objects

CS 130 – Discrete Structures 45

Some Examples

1. Ten athletes compete in an Olympic event. Gold, silver and bronze medals are awarded to the first three in the event respectively. How many ways can the awards be made in?

2. How many ways can 6 people be seated on six chairs?

Hence, 3 objects from a pool of 10 = P(10,3) = 720

P(6,6) = 6! = 720

CS 130 – Discrete Structures 46

More Examples

3. How many permutations of the letters ABCDEF contain the letters DEF together in any order?

4. The professor’s dilemma: how to arrange 4 books on OS, 7 on programming and 3 on data structures on a shelf such that books on the same subject must be together?

(4!*7!*3!)*3! = 24*5040*6*6 = 4,354,560

If DEF is considered as one letter, then we have 4 letters A B C DEF whichcan be permuted in 4! waysDEF can be ordered by its letters in 3! WaysHence, by the multiplication principle, total number of orderings possible = 4!*3! = 24*6 = 144

CS 130 – Discrete Structures 47

Combinations

• Sometimes we want to select r objects from a set of n objects, but we don’t care how they are arranged, we count the number of combinations of r distinct objects chosen from n distinct objects, denoted C(n, r)

• For each combination, there are r! ways of ordering those r chosen objects, thus

( , ) !

( , )! ! !

P n r nC n r

r r n r

CS 130 – Discrete Structures 48

Special Cases

• C(n, 0)– C(n, 0) = n! / (0!(n-0)!) = n! / n! = 1– Choose zero objects from n objects

• C(n, 1)– C(n, 1) = n! / (1!(n-1)!) = n! / (n-1)! = n– Choose 1 object from n objects

• C(n, n)– C(n, n) = n! / (n!(n-n)!) = n! / n! = 1– Choose n objects from n objects

CS 130 – Discrete Structures 49

Examples

• How many ways can we select a committee of 3 from 10?

• How many ways can a committee of 2 women and 3 men be selected from a group of 5 different women and 6 different men?

C(10,3) = 120

For selecting 2 out of 5 women, we have C(5,2) ways = 10For selecting 2 out of 6 men, we have C(6,3) ways = 20Total number of ways for selecting the committee = 10*20 = 200

CS 130 – Discrete Structures 50

More Examples

• How many 5-card poker hands can be dealt from a standard 52-card deck?

• How many poker hands contain cards all of the same suit (4 suits: spades, hearts, diamonds, clubs)?

• How many poker hands contain 3 cards of one denomination and 2 cards of another denomination?

C(52,5) = 2,598,960

4*C(13,5) = 5148

Order of events: Select first denomination, select 3 cards from this denomination, select the second denomination, select 2 cards from this denomination13*C(4,3)*12*C(4,2) = 3744

CS 130 – Discrete Structures 51

Eliminating Duplicates

• How many ways can a committee of 2 be chosen from 4 men and 3 women and it must include at least 1 man

Correct answer = C(7,2) – C(3,2) = C(4,1)*C(6,1) – C(4,2)C(4,2) is the number of committees with 2 men on it. It has to be subtracted since we are counting it twice in C(4,1)*C(6,1)C(7,2) = all committees possibleC(3,2) = all committees with no men on it

answer: C(4,1)*C(6,1) incorrect!

CS 130 – Discrete Structures 52

More Examples

• How many distinct permutations can be made from the characters in the word FLORIDA ?

• How many distinct permutations can be made from the characters in the word MISSISSIPPI ?

Simple: 7!

Since we have more than one S, interchanging the Ss at the same position will not result in a distinguishable change. Hence for 4 Ss, 4! possible permutations that look alike. Hence total number of permutations =

!2!4!4!11

CS 130 – Discrete Structures 53

Permutations and Combinations with Repetitions

• P(n, r) and C(n, r) assume that we select robjects out of n objects using each object only once, with r <= n

• What about repetitions? Where r might be greater than n– Counting the number of permutations of r objects

out of n distinct objects with repetition: nr, for example the 4-digit telephone example

– Counting the number of combinations of r objects out of n distinct objects with repetition, we need a clever idea

CS 130 – Discrete Structures 54

The Jeweler Design Example

• A jeweler designing a pin has decided to use five stones chosen from diamonds, rubies, and emeralds. In how many ways can the stones be selected?

*|**|**

7 slots (five germs and the two markers)

The problem becomes counting the number of ways to choose 5 items out of seven, which is C(7, 5) or 7!/(2!*5!)

CS 130 – Discrete Structures 55

Combination with Repetition

• General principle of combination with repetition:– If we want to select r objects out of n distinct objects

with repetition allowed, there must be n-1 markers– This gives r + (n-1) slots to fill– And we want to know the number of ways to select r

of these– Thus we want

( 1)! ( 1)!

( 1, )! 1 ! ! 1 !

r n r nC r n r

r r n r r n

CS 130 – Discrete Structures 56

Exercises

• Six children choose one lollipop each from among a selection of red, yellow, and green lollipops. In how many ways can this be done? (We do not care which child gets which.)

CS 130 – Discrete Structures 57

Counting in General

Count the number of… Technique to use

Subsets of an n-element set Use formula 2n

Outcomes of successive events Multiplication principle

Outcomes of disjoint events Addition principle

Elements in overlapping sections of related sets

Use principle of inclusion and exclusion

Ordered arrangements of r out of n distinct objects

P(n, r)

Ways to select r out of n distinct objects

C(n, r)

Ways to select r out of n distinct objects with repetition

C(r+n-1, r)