# Discrete Math for CS Chapter 6: Combinatorics. Discrete Math for CS How to Count: Combinatorics is about counting. Pigeonhole was about counting too.

## Presentation on theme: "Discrete Math for CS Chapter 6: Combinatorics. Discrete Math for CS How to Count: Combinatorics is about counting. Pigeonhole was about counting too."— Presentation transcript:

Discrete Math for CS Chapter 6: Combinatorics

Discrete Math for CS How to Count: Combinatorics is about counting. Pigeonhole was about counting too. General Counting Problem:  considers the selection of items from a bigger set either with or without repetition and with or without a specific order of selection

Discrete Math for CS Example: Problem 1: At the end of the day a small bakery has a number of unsold cakes:  4 vanilla cakes,  2 chocolate cakes and  3 fruit cakes If a customer comes in in a rush and orders 1 cake, how many different choices does the customer have? ANS: 4+2+3 = 9 choices

Discrete Math for CS Example: Problem 2: A mixed-double team is to be chosen to represent a local tennis club. There are:  6 male players and,  9 female players How many different possible teams are there? ANS: any one of 6 males and 9 females so 6x9 = 54 possible teams

Discrete Math for CS Addition Principle: If A and B are disjoint events and n 1 ways A can happen while n 2 ways B can happen then there are n 1 + n 2 ways either A or B can happen. Problem 1 is an example of the Addition Principle

Discrete Math for CS Multiplication Principle: If there is a sequence of k events and n 1 ways the first event can happen, n 2 ways the second event can happen,etc. and finally n k ways the k th event can happen then the total number of possible outcomes of all k events is are n 1 x n 2 x... x n k. Problem 2 is an example of the Mulitplication Principle

Discrete Math for CS Inclusion and Exclusion Principle: From Chapter 3 we have the following formula The Addition Principle is a special case of this formula where the two sets are disjoint. Hence A ∩ B =  | A ∪ B | = | A | + | B | - | A ∩ B |

Discrete Math for CS Multiplication Principle seen Mathematically: Let A i denote the n i outcomes of i th event, i = 1,...,k. Then any sequence of k events is corresponds to an element of the cartesian product so the number of different sequences is equal to the number of elements in the cartesian product A 1 x A 2 x... x A k |A 1 | x |A 2 | x... x |A k |

Discrete Math for CS Example: Problem 3: How many 3-digit numbers begin with 3 or 4?: ANS: Two disjoint cases: 3-digit numbers beginning with 3 and 3-digit numbers beginning with 4. We use the Addition Principle to add the number of outcomes of each case. Case 1: there are 10 choices of the 2 nd digit and 10 more choices of the 3 rd so 100 choices of a 3-digit number beginning with 3. This is just the Multiplication Principle. Case 2: 100 choices of a 3-digit number beginning with 4. A total of 100+100 = 200 different numbers.

Discrete Math for CS Example 1: I take two pieces of fruit each day for lunch. I have 3 bananas, 4 apples and 2 pears at home. How many different ways can I select two fruit for lunch? ANS: If I select a banana and apple I have 4 x 3 choices If I choose a banana and pear I have 4 x 2 = 8 choices If I choose an apple and a pear I have 3 x 2 = 6 choices. I have 12 + 8 + 6 = 26 different ways to choose the fruit for lunch.

Discrete Math for CS Example 2: How many different licence plates are there consisting of 6 characters, the first 3 characters are letters and the last 6 are digits. ANS: I have 26 x 26 x 26 = 17,576 ways to choose the letters. I have 10 x 10 x 10 = 1,000 ways to choose the digits. I have 17,576 x 1,000 = 17,576,000 different possible licence plate combinations.

Discrete Math for CS Counting Formulae: A child has a bag of three kinds of candies Christmas Stripe(A), butterscotch(B) and Hershey's(C) -. In how many ways can the child select two candies. Hard to answer this question because we don't know if it is permitted to choose two of the same kind or if choosing a butterscotch candy and then a Hershey's is the same as choosing a Hershey's (BC) and then a butterscotch (CB)‏

Discrete Math for CS 4 Cases Repeats allowed and Order matters: Repeats allowed but Order does not matter: Repeats not allowed but Order matters: Repeats not allowed and Order does not matter: {AA, AB, AC, BA, BB, BC, CA, CB, CC} – total of 9 ways {AA, AB, AC, BB, BC, CC} – total of 6 ways { AB, AC, BC, BA, CA, BC} – total of 6 ways { AB, AC, BC} – total of 3 ways

Discrete Math for CS How to Decide Which Case Applies? Perhaps we are told that the child blindly puts her hand into a bag containing the candy. This implies repeats are allowed but really doesn't tell us if the girl cares about the order. If we are told that she picks the candies out and gives the first chosen candy to friend X and the second chosen candy to friend Y then order clearly matters, at least to X and Y.

Discrete Math for CS Order Matters and Repetition Allowed: We choose k items from a set of n items. We call such a selection a k-sample. Since repetition is allowed there are n ways to choose an item each time a choice is made. The Multiplication Principle tells us there are possible k-samples. n x n x... x n = n k

Discrete Math for CS Example: A computer represents integers using N binary digits. The first digit indicates sign (+ or -). The remaining N-1 digits represent the magnitude of the integer. How many distinct integers can be represented? ANS: There are 2 N different binary strings. Each one is a separate integer except for 00...0 and 10...0. Both these have magnitude 0 and opposite signs so represnt the same integer; namely 0. So there are 2 N -1 different integers represented this way.

Discrete Math for CS k-permutations: Select k objects from a set of n objects and repetitions are not allowed and order matters. Such a selection is called a k-permutation. Example: You have n distinct things and each time you pick one it is no longer available to be chosen again. Pick 5 students from a class of 20 to become the class leaders. How many different executive teams of students can you form?

Discrete Math for CS k-permutations: P(n,k) = the number of ways of choosing k objects from a set of n objects without repetition and order matters There are n ways to chose the first item For the second choice, the first-chosen item is missing so n-1 ways of choosing the second item. For the third choice, the first two-chosen items are missing so n-2 ways of choosing the third item. P(n,k) = n(n-1)(n-2)...((n-k+1) = n!/(n-1)!

Discrete Math for CS Example: How many four-letter words can you make from distinct letters in the list a, g, m, o, p and r? NOTE: Saying “distinct” means no repetitions. Order matters since a different letter order gives a different word. P(6,4) = 6!/(6-4)! = 6!/2! = 6  5  4  3 = 360 Please ignore the text on this example.

Discrete Math for CS k-combination: Select k objects from a set of n objects where order does not matter and repetitions are not allowed. Such a selection is called a k-combination. Example: You have n things whose differences are not important to you and each time you pick one it is no longer available to be chosen again. Pick 5 students from a class of 20 to set up chairs for a school assembly. How many different teams of students can you form?

Discrete Math for CS k-combinations: C(n,k) = the number of ways of choosing k objects from a set of n objects without repetition and order does not matter If order matters we get the number P(n,k). By the multiplication principle there are k! orderings of each distinct choice of k elements. So the number of k-permutations of k distinct objects selected from n objects is the same as the number of unordered ways to select the same objects times the number of ways to order the objects chosen. P(n,k) = C(n,k)  k! So C(n,k) = P(n,k)  k!

Discrete Math for CS Example: In a Chinese Restaurant you can order any three dishes for what they call the 3-combination meal. How many different 3- combination meals are there if there are 7 main dishes on teh menu. It doesn't make any difference what order you receive the dishes in and we assume you must order three different dishes. So order doesn't matter and there is selection without replacement. C(7,3) = 7!/4!3! = 35

Discrete Math for CS k-selection: We finally consider an unordered selection of k objects from a collection of n objects with repetitions allowed. This is called k-selection Example: Since order is unimportant and repetitions are allowed we can group all like-objects together. Suppose we want to make a k-selection of 5 objects from the letters a, b and c. | | For example aa|b|cc or aaa||cc. This reduces to finding out how many ways we can insert the two markers, | and |, in a sequence of 7 slots. The other 5 slots are letters. This number is C(7,2) = 21.

Discrete Math for CS Example: Five dice are thrown. How many different outcomes are possible if an outcome is a list of the upper faces in ascending order. Each dice has 6 outcomes. If 5 dice are thrown we have the result | |...| and we are really asking how many ways we can insert 5 markers in 10 slots. For example 111||3||5|. This is C(10,5) = 252.

Discrete Math for CS k-selection formula: The number of k-selections from n items is: C(n+k-1,n-1) = (n+k-1)!/k!(n-1)!

Discrete Math for CS Summary:

Discrete Math for CS Further Examples: The key is to know what formula to use. In a National Lottery a twice-weekly drawing takes place in which 6 numbers are randomly drawn from the numbers 1..49. Problem: Find the probability of winning. Analysis: Without replacement and order does not matter. Solution: k-combination: C(49,6) ways to choose the numbers Prob(winning) = 1/C(49,6) = 1/13,983,816

Discrete Math for CS Further Analysis: Should I buy a ticket? Statistically this equates to calculating the statistical outcome of buying a ticket. Simplify the situation and assume there is only one winner. The initial lottery pot in NYS is about \$3M. Outcome = amount won * prob of winning + amount lost * prob of losing = 3,000,000 * 1/13,983,816 + -1 * 13,983,815/13,983,816 = -0.79 So the outcome is negative and this is not a good bet.

Discrete Math for CS Further Analysis: What if you win \$10 if you get three numbers out of the 6? What is the probability of this happening? Prob(guessing 3 numbers correctly) = number of ways of guessing three correct numbers/ number of ways of guessing. Guessing 3 correct numbers from the 6 correct numbers can be done C(6,3) ways – no repeats, order unimportant Guessing three incorrect numbers can be done C(43,3) ways. Total number of winning combinations is: C(6,3) x C(43,3) = 246,820 Prob(winning something) = 246,820/13,983,816 = 0.017

Discrete Math for CS Example: Twelve people, including Peter and Mary are candidates for a committee of 5. How many different committees are possible?  including Peter and Mary,  excluding Peter and Mary,  containing either Peter and Mary but not both? Answers: How many committees? No replacement, order unimportant – C(12,5) = 792. Including Peter and Mary? Peter and Mary are selected so we need to choose three more from 10 people – C(10,3) = 120 excluding Mary and Peter? Select 5 from 10 – C(10,5) = 252 either Peter or Mary? Mary only – C(10,4). Peter only – C(10,4)‏ either Mary or Peter = 2 x C(10,4) = 420 Alternatively, the 792 possible committees fall into one of the above categories. So the third category has 792 – 120 – 252 = 420 members.

Discrete Math for CS Binomial Expansion: The numbers C(n,k) are the coefficients of the expansion of (a+b) n. This turns out to be true because when we multiply out (a+b)(a+b)(a+b) we are essentially selecting either an a or a b from each term (a+b) and the coefficient of a k b n-k is the number of ways of selecting k a's from n without repetition and ignoring order. Of course, 0! = 1 by definition. Example: (a+b) 3 = (a+b)(a+b)(a+b) = a 3 + 3a 2 b + 3ab 2 + b 3 and C(3,0) = 1, C(3,1) = 3, C(3,2) = 3 and C(3,3) = 1

Discrete Math for CS (a+b) n : The above is called the binomial expansion of (a+b) n. Each C(n,k) is called a binomial coefficient. (a+b) n = C(n,0)a n + C(n,1)a n-1 b + C(n,2)a n-2 b 2 +... + C(n,n-1)ab n-1 + C(n,n)b n

Discrete Math for CS Pascal's Triangle: 1 11 112 1133 11446

Discrete Math for CS Pascal's Triangle: 1 11 112 1133 11446 C(n,k) = C(n-1,k-1) + C(n-1,k)‏

Discrete Math for CS Theorem: C(n,k) = C(n-1,k-1) + C(n-1,k). Proof: C(n-1,k-1) + C(n-1,k) = (n-1)!/(k-1)!(n-k)! + (n-1)!/k!(n-k-1)! = (n-1)!/(k-1)!(n-k-1)! x (1/(n-k) + 1/k)‏ = (n-1)!/(k-1)!(n-k-1)! x n/((n-k)k)‏ = n!/(n-k)!k! = C(n,k)‏

Discrete Math for CS Final Topic: How many arrangements can be made of objects (possibly repeated) from a given set? Example: Consider the word “defender”. This contains 4 distinct letters, some repeated more than once. We can arrange these letters 8! ways, but then we will see some are repetitions since we can't tell one from another., for example. Since there are 2 d's there are 2! ways of arranging these. Since there are 3 e's there are 3! ways of arranging these. There are a total of 8!/2!3! distinct orderings of the letters of “defender”.

Discrete Math for CS Rearrangement Theorem: There are n!/(n 1 !n 2 !n 3 !...n k !) ways of rearranging a collection of n objects where there are n 1 identical objects of one type, n 2 identical objects of another type,..., and n k identical objects of a final type. Clearly n = n 1 + n 2 +... +n k.

Discrete Math for CS Example: How many ways can we divide 15 students into 3 groups of 5 so that each group studies a different topic? We have 15 objects divided into 3 identical groups of 5 each. This can be done 15!/(5!5!5! ) = 68,796 ways. Alternatively, imagine lining the 15 people up so that the first 5 go into group 1, the next 5 into group 2 and the last 5 into group 3. For each distinct set of the first 5 students there are 5! different orders we can ignore; likewise for the other two groups. So there are 15!/(5!5!5!) different group assignment possibilities.

Discrete Math for CS Multinomial: The term n!/(n 1 !n 2 !n 3 !...n k !) is called a multinomial. It is the form of the coefficients of the expansion of This follows since the term x 1 n1 x 2 n2...x k nk gets its coefficient by selecting n 1 instances of x 1, n 2 instances of x 2, etc from the bracketed terms (x 1 + x 2 +... + x k ). This is the same as arranging n objects, n 1 of type x 1, n 2 of type x 2, etc. (x 1 + x 2 +... + x k ) n

Discrete Math for CS Example: Find the coefficient of x 3 y 2 z 4 in the expansion of (x+y+z) 9. Find the coefficient of x 3 y 2 in the expansion of (x+y+3) 7. 9!/(3!2!4!) = 1260 If this were (x+y+z) 7, then the coefficient of x 3 y 2 z 2 would be 7!/(3!2!2!) = 210. Since z = 3, the coefficient is 210x3 2. = 1890.

Discrete Math for CS Algorithm Efficiency: Time and Space efficiency of a computer algorithm is a big deal. We often measure these aspects of an algorithm by counting how much work is done or how much time is consumed as a function of n, the size of the initial problem. Example: Find if a particular word, X, is in a dictionary of size n. Sequential Search: This search compares X with the first word, the second word and so on until either it is found or not found. The “worse case” requires n comparisons – not there and X starts with a lot of zzz. Binary Search: Compare X to the middle word and use lexicographical ordering of words to decide whether to continue the search in the first half or last half of the dictionary. This method requires 1 + log 2 n comparisons.

Discrete Math for CS Example: Suppose 5 algorithms – A, B, C, D and E – involve n, 3n 2, 2n 2 +4n, n 3 and 2 n elementary operations. If each operation takes 1 millisecond estimate the running times for n = 1, 10, 100 and 1000. polynomial timeexponential timelinear time Polynomial time algorithms with the same degree are in the same ballpark.

Discrete Math for CS Time Complexity Functions: Suppose f(n) and g(n) measure the time complexity of two algorithms. We say f(n) is of order at most g(n) (written O(g(n))) if there exists a positive constant C such that |f(n)| <= C|g(n)| for all but a finite number of distinct values of n. Colloquially we say “f(n) is big-O of g(n)”.

Discrete Math for CS Example: Show 2n 2 +4n is O(n 2 ). Note: It is also the case that n 2 =1 so we can say n 2 is O(2n 2 +4n) or that these two time complexity functions have the same order of magnitude. n = 1. 2n 2 +4n = 1 So letting C=6 in the above definition we can conclude 2n 2 +4n is O(n 2 )‏

Discrete Math for CS Order of Magnitude Hierarchy There is a hierarchy of functions, each of which is a greater order of magnitude than its predecessors. There are actually many such hierarchies but one is: 1 log n n n 2 n 3 n 4... n k... 2 n Exercise: Where does nlogn (n(log n)) fit in? As n increases the values of the above time complexity functions increase more rapidly on the right than on the left.

Discrete Math for CS n 2048 1024 512 256 128 64 32 16 8 4 2 1

Discrete Math for CS Example: A function like f(n) = 9n+3n 6 +7log n is assigned the time complexity of O(n 6 ) because 9n is O(n), 3n 6 is O(n 6 ) and 7log n is O(log n). Since n and 7log n occur earlier in the hierarchy than n 6 we can say both n and 7log n are O(n 6 ). Since all three terms of the function f(n) are O(n 6 ) the function itself is O(n 6 ).

Discrete Math for CS What to Count: Consider the following pseudocode: Determine the time complexity by counting the number of times the assignment statement executes. begin for i:= 1 to 2n do for j:= 1 to n do for k := 1 to j do x := x + 1 end The outer loop executes 2n times. The loop indexed by j executes n times for each i For each value of j the statement x := x+1 executes j times So for each value of i, the statement x := x+1 executes 1 + 2 +... + n = 1/2(n(n+1)) times. So the time complexity of the loops is T(n) = 2n(1/2(n(n+1))) = n 2 (n+1) So T(n) is O(n 3 ).

Discrete Math for CS Pascal's Triangle:

Download ppt "Discrete Math for CS Chapter 6: Combinatorics. Discrete Math for CS How to Count: Combinatorics is about counting. Pigeonhole was about counting too."

Similar presentations