Presentation is loading. Please wait.

Presentation is loading. Please wait.

Computing Fundamentals 2 Lecture 5 Combinatorial Analysis Lecturer: Patrick Browne Based on Chapter 16. A Logical approach.

Similar presentations


Presentation on theme: "Computing Fundamentals 2 Lecture 5 Combinatorial Analysis Lecturer: Patrick Browne Based on Chapter 16. A Logical approach."— Presentation transcript:

1 Computing Fundamentals 2 Lecture 5 Combinatorial Analysis Lecturer: Patrick Browne http://www.comp.dit.ie/pbrowne/ Based on Chapter 16. A Logical approach to Discrete Math By David Gries and Fred B. Schneider

2 Combinatorial Analysis Counting Permutations Combinations The Pigeonhole Principle Examples

3 Fruit salad is a combination of apples, grapes and bananas We don't care what order the fruits are in. The permutation that will open the lock is 942, we do care about the order. Permutation Combination

4 Combinatorial Analysis Combinatorial analysis deals with permutations of a set or bag and also combinations of a set, which lead to binomial coefficients and the Binomial Theorem. Cardinality of set is denoted as |A| Example if A = {1,3,6} then |A|=3

5 Rules of Counting Rule of sums (addition): The size of the union on n finite pair wise disjoint sets is the sum of their sizes. Rule of product (multiplication): The size of the cross product of n sets is the product of their sizes. Rule of difference: The size of a set with a subset removed is the size of the set minus the size of the subset.

6 Rules of Counting Difference Rule If B ⊆ A or A ⋂ B then | A - B| is |A| - |B| A B As a logical statement

7 Rules of Counting Inclusion–exclusion principle

8 Rules of Counting Inclusion–exclusion principle The numbers > 1 represents duplicates.

9 Product Rule Example If each license plate contains 3 letters and 2 digits. How many unique licenses could there be? Using the rule of products. 26  26  26  10  10 = 1,757,600

10 Multiplication Principle for Counting In general, if n operations O 1, O 2 …O n, are performed in order, with possible number of outcomes N 1, N 2 …N n, respectively, then there are N 1,× N 2... ×N n, possible combined outcomes of the operations performed in the given order.

11 Multiplication Principle for Counting

12 Addition Principle or Counting For any two sets of A and B, |A ∪ B| = |A| + |B| – |A ∩ B| If A and B are disjointed then, |A ∪ B| = |A| + |B|

13 Permutation of a set A permutation of a set of elements is a linear ordering (or sequence) of the elements e.g. Given set S = {1,4,5} Permutation A : 1, 4, 5 Permutation B : 1, 5, 4 An anagram is a permutation of words. There are n  (n – 1)  (n - 2).. 1 permutations of a set of n elements. This is called factorial n, written n!

14 Calculating Factorial module FACT { protecting(INT) -- Two notations for factorial op _! : Nat -> NzNat {prec 10} op fact : Nat -> NzNat var N : Nat -- Notation 1 eq 0 ! = 1. ceq N ! = N * (N - 1) ! if N > 0. -- Notation 2 eq fact(0) = 1. ceq fact(N) = N * fact(N - 1) if N > 0. } open FACT red 4 !. red fact(4).

15 Permutation of a set Sometimes we want a permutation of size r from a set of size n. (16.4) P(n,r) = n!/(n-r)! The number of 2 permutations of BYTE is P(4,2) = 4!/(4-2)! = 4  3 = 12 BY,BT,BE,YB,YT,YE,TB,TY,TE,EB,EY,ET P(n,0) = 1 P(n,n-1) = P(n,n) = n! P(n,1) = n

16 Calculating Permutations and Combinations of sets mod CALC{ pr(FACT) op permCalc : Int Int -> Int op combCalc : Int Int -> Int vars N R : Int -- Compute permutation where order matters abc =/= bac -- A permutation is an ordered combination. -- perm calculates how many ways R items can be selected from N items eq permCalc(N, R) = fact(N) quo fact(N - R). -- combination of N things taking R at a time -- Note extra term in divisor. eq combCalc(N, R) = fact(N) quo (fact(N - R) * fact(R)).} open CALC -- Permutation from 10 items taking 7 at a time red permCalc(10,7). – gives 604800 -- Combination from 10 items taking 7 at a time red combCalc(10,7). – gives 120

17 Permutation with repetition of a set An r-permutations is a permutation that allows repetition in the r-permutation. Here are all the 2-permutation of the letters in SON: SS,SO,SN,OS,OO,ON,NS,NO,NN. Given a set of size n, in constructing an r- permutation with repetition, for each element we have n choices. (16.6) The number of r permutations with repetition of a set of size n is n r, repetition is allowed in the r-permutation not in the original set.

18 Permutation of a bag A bag may have duplicate elements. Transposition of equal (or duplicate) elements in a permutation does not yield a different permutation e.g. AA=AA. Hence, there will be fewer permutations of a bag than a set of the same size. The permutations on the set {S,O,N} and the bag  M,O,M  are: {S,O,N} = SON,SNO,OSN,ONS,NSO,NOS size=6  M,O,M  = MOM,MMO,OMM size=3

19 Permutation of a bag: General Rule (16.7) The number of permutations of a bag of size n with k distinct elements occurring n 1, n 2, n 3,.. n k times is: n! n 1 !  n 2 !  n 3 !...  n k !

20 CafeOBJ permutation of a Set & Bag Calc. size of {S,O,N} example red permCalc(3,3) gives 6 Calc. size of MOM example red fact(3) quo (fact(1) * fact(2)). O occurs once, M twice, gives 3

21 Permutation of a bag Consider the permutation of the 11 letters of MISSISSIPPI. M occurs 1 time, I occurs 4 times, S occurs 4 times, and P occurs 2 times. red fact(11) quo (fact(1) * fact(2) * fact(4) * fact(4)). Note 0!=1

22 Permutation of a bag  O  a single permutation  M 1,O, M 2 , label the two copies of M. We could distinguish the M s. M 1 M 2 O,M 2 M 1 O,M 1 OM 2,M 2 OM 1,OM 1 M 2,OM 2 M 1,

23 Combinations of a Set An r -combination of a set is a subset of size r. A permutation is a sequence while a combination is a set.

24 2-Permutations & 2-Combinations of a Set The 2-permutations (seq.) of SOHN is: SO,SH,SN,OH,ON,OS,HN,HS,HO,NS,NO,NH The 2-combinations (set) of SOHN is: {S,O},{S,H},{S,N},{O,H},{O,N},{H,N}

25 Combinations of a Set The binomial coefficient, “n choose r is written”:

26 Pascal’s Triangle Beginning with row 0 and place 0, the number 20 appears in row 6, place 3. In CafeOBJ we can check this. red combCalc(6,3). – gives 20 red combCalc(7,4). – gives 35 red combCalc(7,3). – gives 35

27 Special Combinations of a Set Generally we label N choose R as:

28 Calculating factorial and division We can divide above and below by 6!

29 Calculating "n choose k". Simplifying

30 Combinations of a Set (16.10) The number of r-combinations of n elements is A student has to answer 6 out of 9 questions on an exam. How many ways can this be done?

31 Combinations with repetitions of a Set An r-combination with repetitions of a set S of size n is a bag of size r all of whose elements are in S. An r-combination of a set is a subset of that set; an r- combination with repetition of a set is a bag, since its elements need not be distinct.

32 Combinations with repetitions of a Set For example, there are 6 2-combinations with repetition of SON are the bags:  S,O ,  S,N ,  O,N ,  S,S ,  O, O ,  N,N  On the other hand, there are 9 2- permutations with repetition are the sequences:,,,,,,,, Note SO and OS are distinct permutations

33 Combinations with repetitions of a Set (16.12) The number of r -combinations with repetition of a set of size n is: Repetitions size Combination size

34 Combinations with repetitions of a Set Suppose 7 people each gets either a burger, a cheese burger, or fish (3 choices). How many different orders are possible? The answer is the number of 7- combinations with repetition of a set of 3 elements.

35 Rule of sum and product A class has 55 boys and 56 girls. What is the total number of students in the class, and how many different possible boy girl pairs are there? Two disjoint sets, boys and girls, rule of sum implies 55+56=111 students. The rule of product says 55  56 = 3080.

36 Rule of sum and product A student can pass the language requirement on a course by (i) gaining proficiency in French, German, or Japanese. (ii) gaining minimal qualification, which involves two semester of (ii)(a) German, Japanese, or Italian and (ii)(b) two semesters of Korean or Hindi. In how many different ways can the language requirement be satisfied?

37 Rule of sum and product The set P of ways in which proficiency can be gained has 3 elements. Let the set S represent the way in which minimal qualification can be satisfied. Each element of S is a pair whose first element is either French, German, Japanese, or Italian and whose second element is either Korean or Hindi. The rule of products gives #S=4  2=8. Adding these using the rule of sums gives #P+#S=3+8=11 (i)P=F or G or J (ii)S= J(G,J) or I and K or H

38 Rule of sum and product In how many different ways can the language requirement be satisfied? #Proficiency = 3 #MinimalQualification = 4  2 = 8 #Proficiency + #MinimalQualification = 8 + 3 = 11

39 Example Two bags. One bag contains a red ball and a black ball (2). A second bag contains a red ball, a green ball, and a blue ball (3). A person randomly picks first a bag and then a ball. In what fraction of cases will a red ball be selected? #PossibleSelections = 2+3 = 5 #PossibleRed = 2 Fraction of red picked = 2/5

40 Permutations How many permutations of the letters are there in the following words: LIE: n=3, 3! = 6 BRUIT: n=5, 5! = 120 CALUMMNY: n=7, 7!=5040

41 Permutations of a bag A coin is tossed 5 times, landing Head or Tails to form an outcome. One possible outcome is HHTTT. Are we choosing from a set or a bag? Is the permutation a set or a sequence? How many possible outcomes are there? How many outcomes have one Head? How many outcomes contain at most one Head?

42 Permutations of a bag How many possible outcomes are there? Rule of product giving 2 5 =32 possible outcomes.

43 Permutations of a bag How many outcomes have one Head? Permutation of a bag with 1 Head and four Tails.

44 Permutations of a bag How many outcomes contain at most one Head? One Head No Heads At most one Head 1 + 5 = 6 (rule of sums) Note 0!=1

45 Combinations of Set A chairman has to select a committee of 5 from a facility of 25. How many possibilities are there? How many possibilities are there if the chairman should be on the committee?

46 Permutations & Combinations in Excel In EXCEL =PERMUT(6,2) =COMBIN(6,2)

47 The Pigeonhole Principle

48 (16.43) If more than n pigeons are placed in n holes, at least one hole will contain more than one pigeon. With more than n pigeons in n holes the average number of pigeons per hole is greater than one. The statement “at least one hole will contain more than one pigeon” is equivalent to “the maximum number of pigeons in any whole is greater than one”.

49 Bags For each day of the week let the bag S contain the number of people whose birthday is on that day. There are 8 people. M T W T F S S  1, 1, 1, 1, 1, 1, 2  Note as a set this would be {1,2}.

50 The Pigeonhole Principle Abstracting from pigeons and holes. Let av.S denote the average number of elements in bag S. Let max.S denote the maximum number of elements in bag S. av.S > 1 implies max.S > 1 (16.45) Pigeonhole Principle. av.S  max.S

51 The Pigeonhole Principle (16.46) Pigeonhole Principle. av.S  max.S Where 3.1 = 4 ceiling of real number

52 Example: The Pigeonhole Principle (16.47) Prove that in a room of eight people, at least two of them have birthdays on the same day of the week. Let bag S contain, for each day of the week the number of people in the room whose birthday is on that day. The number of people is 8 the number of days is 7.

53 Example: The Pigeonhole Principle max.S  av.S = 8/7 = 2

54 Example 2: The Pigeonhole Principle Suppose S is a set of six integers, each between 1 and 12 inclusive. Prove that there must be two distinct nonempty subsets of S that have the same sum. Proof: The sum of all the elements of S is at most 7+8+9+10+11+12 = 57. So the sum of the elements of any nonempty subset of S is at least 1 and at most 57; there are 57 possibilities. But there are 2 6 –1 = 63 nonempty subsets of S. Hence there must be two with the same sum. Note size of the power set is 2 to the power of the size of the set.


Download ppt "Computing Fundamentals 2 Lecture 5 Combinatorial Analysis Lecturer: Patrick Browne Based on Chapter 16. A Logical approach."

Similar presentations


Ads by Google