computing fundamentals 2 lecture 5 combinatorial analysis
DESCRIPTION
Computing Fundamentals 2 Lecture 5 Combinatorial Analysis. Lecturer: Patrick Browne http://www.comp.dit.ie/pbrowne/ Room K408 Based on Chapter 16. A Logical approach to Discrete Math By David Gries and Fred B. Schneider. Combinatorial Analysis. Counting Permutations Combinations - PowerPoint PPT PresentationTRANSCRIPT
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
Combinatorial Analysis
• Counting
• Permutations (in EXCEL =PERMUT(6,2))
• Combinations (in EXCEL =COMBIN(6,2))
• The Pigeonhole Principle
• Examples
• 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
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
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. We will look at the non-disjoin case later.
• 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.
Rules of Counting
• Difference Rule • If B ⊆ A or A ⋂ B then |A - B| is |A| - |B|
A BAs a logical statement
Addition Principle or Counting
• For any two sets A and B, |A ∪ B| = |A| + |B| – |A ∩ B|
If we shade each of A and B once, then we shade A ∩ B twice
• If A and B are disjoint then, no overlap
|A ∪ B| = |A| + |B|
Rules of Counting
• Inclusion–exclusion principle
Rules of Counting
• Inclusion–exclusion principle
• The numbers > 1 represents duplicates.
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
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.
Multiplication Principle for Counting
Permutation of a set
• A permutation of a set of elements is a linear ordering (or sequence) of the elements e.g.
• Consider set S = {1,4,5} with two distinct permutations:– 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!
Calculating Factorialmodule FACT {protecting(INT) -- import-- Two notations for factorialop _! : Int -> Int {prec 10}op fact : Int -> Intvar N : Int-- 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 FACT .red 4 ! .red fact(4) .
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
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
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 but not in the original set.
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
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!
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
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
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,
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.
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}
Combinations of a Set
• The binomial coefficient, “n choose r” is written
)!(!
!
rnr
n
r
n
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
Special Combinations of a Set
10
n1
n
n
nn
1
nn
n
1
Generally we label N choose R as:
Examples in CafeOBJred combCalc(5,0) . -- 1red combCalc(2,0) . -- 1red combCalc(5,5) . -- 1red combCalc(5,1) . -- 5red combCalc(5,4) . -- 5
Calculating factorial and division
5678!6
!678
!6
!8
We can divide above and below by 6!, which simplifies the calculation.
Calculating "n choose k".
2821
78
2
8
1264321
6789
4
9
79254321
89101112
5
12
k
n
)!(!
!
rnr
n
r
n
Simplifying
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
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.
Combinations with repetitions of a Set
• For example, there are 6 2-combinations with repetition of SON (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
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
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
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.
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
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?
Permutations of a bag
How many possible outcomes are there?Rule of product giving 25=32 possible
outcomes.
Permutations of a bag
How many outcomes have one Head? Permutation of a bag with 1 Head and four Tails.
5!4!1
!5
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
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
The Pigeonhole Principle
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”.
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 in bag S.
• M T W T F S S
•S = 1, 1, 1, 1, 1, 1, 2 • Note as a set this would be {1,2}.
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.