54
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

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

  • View
    221

  • Download
    4

Embed Size (px)

Citation preview

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

Computing Fundamentals 2Lecture 5

Combinatorial Analysis

Lecturer: Patrick Brownehttp://www.comp.dit.ie/pbrowne/

Based on Chapter 16. A Logical approach to Discrete Math By David Gries and Fred B. Schneider

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

Combinatorial Analysis

• Counting

• Permutations

• Combinations

• The Pigeonhole Principle

• Examples

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

• 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

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

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

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

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.

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

Rules of Counting

• Difference Rule • If B ⊆ A or A ⋂ B then |A - B| is |A| - |B|

A BAs a logical statement

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

Rules of Counting

• Inclusion–exclusion principle

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

Rules of Counting

• Inclusion–exclusion principle

• The numbers > 1 represents duplicates.

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

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

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

Multiplication Principle for Counting

• In general, if n operations O1, O2…On, are performed in order, with possible number of outcomes N1, N2…Nn, respectively, then there are N1,× N2 ... ×Nn, possible combined outcomes of the operations performed in the given order.

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

Multiplication Principle for Counting

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

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|

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

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!

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

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

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

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

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

Calculating Permutations and Combinations of sets

mod CALC{pr(FACT)

op permCalc : Int Int -> Intop 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 itemseq 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 timered permCalc(10,7) . – gives 604800-- Combination from 10 items taking 7 at a timered combCalc(10,7) . – gives 120

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

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 nr, repetition is allowed in the r-permutation not in the original set.

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

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

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

Permutation of a bag: General Rule

• (16.7) The number of permutations of a bag of size n with k distinct elements occurring n1, n2, n3,.. nk times is:

n!

n1! n2! n3! ... nk!

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

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

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

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

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

Permutation of a bag• O a single permutation

• M1,O, M2 , label the two copies of M.

• We could distinguish the Ms.

M1M2O,M2M1O,M1OM2,M2OM1,OM1M2,OM2M1,

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

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.

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

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}

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

Combinations of a Set

• The binomial coefficient, “n choose r is written”:

)!(!

!

rnr

n

r

n

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

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

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

Special Combinations of a Set

10

n1

n

n

nn

1

nn

n

1

Generally we label N choose R as:

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

Calculating factorial and division

5678!6

!678

!6

!8

We can divide above and below by 6!

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

Calculating "n choose k".

2821

78

2

8

1264321

6789

4

9

79254321

89101112

5

12

k

n

)!(!

!

rnr

n

r

n

Simplifying

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

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?

84123

789

!3!6

!6789

!3!6

!9

6

9

)!(!

!

rnr

n

r

n

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

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.

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

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:

• <S,S>,<S,O>,<S,N>,<O,S>,<O,O>,<O,N>,<N,S>,<N,O>,<N,N>

Note SO and OS are distinct permutations

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

Combinations with repetitions of a Set

• (16.12) The number of r-combinations with repetition of a set of size n is:

r

rn 1

Repetitions

size

Combination

size

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

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.

36!2!7

!9

7

173

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

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.

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

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?

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

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=42=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

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

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

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

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

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

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

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

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?

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

Permutations of a bag

How many possible outcomes are there?Rule of product giving 25=32 possible

outcomes.

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

Permutations of a bag

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

5!4!1

!5

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

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

1!5!0

!5

5!4!1

!5

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

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?

53130!20!5

!25

5

25

10626!20!4

!24

4

24

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

Permutations & Combinations in Excel

• In EXCEL• =PERMUT(6,2)• =COMBIN(6,2)

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

The Pigeonhole Principle

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

The Pigeonhole Principle

• (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”.

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

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}.

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

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

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

The Pigeonhole Principle

• (16.46) Pigeonhole Principle.

av.S max.S • Where

3.1 = 4 ceiling of real number

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

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.

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

Example: The Pigeonhole Principle

max.S <Pigeonhole Prin.- S contains integers > av.S= <S has seven values that sum to 8> 8/7= <definition of ceiling> 2

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

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 26–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.