<<

Section 4.2 Counting

CS 130 – Discrete Structures Counting: Multiplication Principle

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

• 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 27 Examples

• How many four digit can there be if repetitions of 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 28 Counting: 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 29 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 = B Hence, |A-B| = |A| - |B|

CS 130 – Discrete Structures 30 Using Principles Together

• How many four-digit numbers begin with a 4 or a 5 • How many three-digit (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 31 Section 4.3: Principle of Inclusion and Exclusion;

CS 130 – Discrete Structures 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 33 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 34 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 is a multiple of 3,each of this multiple can be represented as 3p, for any integer p from 1 through 333, Hence |A| = 333 Similarly 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| = 66 From 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 35 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 36 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 37 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 38 General Principle of Inclusion/Exclusion

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

n1 |AA1 ...  n | | A i |  | AA i  j |  | AAA i  j k | ... ( 1) | AA 1  ... n | 1i n 1  i j n 1  i j k n

• In this above equation, the term  |Ai A j | 1i j  n

• Says add together elements in all intersections of the form, where i < j • What are these terms for n=3?

CS 130 – Discrete Structures 39 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 40 Pigeonhole Examples

• How many people must be in a room to guarantee that two people have the last name begin with the same initial? 26 alphabets hence, have to have 27 people.

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

6 possible outcomes, hence 7 times.

• 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? 2 colors, hence 3 times.

CS 130 – Discrete Structures 41 Section 4.4 and Combinations

CS 130 – Discrete Structures Permutations

• An ordered arrangement of objects is called a . – 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

n! nn ( 1)...( nr  1)( nr  )! Pnr(,)  nnnr (1)...( 1) nr ! nr   !

• Example: 4-digit telephone number without repetition

CS 130 – Discrete Structures 43 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 44 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?

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

2. How many ways can 6 people be seated on six chairs? P(6,6) = 6! = 720

CS 130 – Discrete Structures 45 More Examples

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

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

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

CS 130 – Discrete Structures 46 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 n ! Cnr(, )   r! rnr !  !

CS 130 – Discrete Structures 47 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 48 Examples

• How many ways can we select a committee of 3 from 10? C(10,3) = 120

• 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?

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

CS 130 – Discrete Structures 49 More Examples

• How many 5-card poker hands can be dealt from a standard 52-card deck? C(52,5) = 2,598,960 • How many poker hands contain cards all of the same suit (4 suits: spades, hearts, diamonds, clubs)? 4*C(13,5) = 5148 • How many poker hands contain 3 cards of one denomination and 2 cards of another denomination? Order of events: Select first denomination, select 3 cards from this denomination, select the second denomination, select 2 cards from this denomination 13*C(4,3)*12*C(4,2) = 3744

CS 130 – Discrete Structures 50 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

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

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 possible C(3,2) = all committees with no men on it

CS 130 – Discrete Structures 51 More Examples

• How many distinct permutations can be made from the characters in the word FLORIDA ? Simple: 7!

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

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. 11! Hence total number of permutations = 4!4!2!

CS 130 – Discrete Structures 52 Permutations and Combinations with Repetitions

• P(n, r) and C(n, r) assume that we select r objects 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 53 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 54 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 (r n 1)! ( r  n 1)! Crn( 1,) r  r! r n 1!!1! r  r n  

CS 130 – Discrete Structures 55 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 56 Counting in General

Count the number of… Technique to use

Subsets of an n- set Use formula 2n

Outcomes of successive events Multiplication principle

Outcomes of disjoint events Addition principle

Elements in overlapping sections of Use principle of inclusion and related sets exclusion Ordered arrangements of r out of n P(n, r) distinct objects Ways to select r out of n distinct C(n, r) objects Ways to select r out of n distinct C(r+n-1, r) objects with repetition

CS 130 – Discrete Structures 57