Combinatorics CSE235 Combinatorics Introduction
Counting PIE Slides by Christopher M. Bourke Pigeonhole Principle Instructor: Berthe Y. Choueiry Permutations
Combinations
Binomial Coefficients Generalizations Spring 2006 Algorithms
More Examples Computer Science & Engineering 235 Introduction to Discrete Mathematics Sections 4.1-4.6 & 6.5-6.6 of Rosen 1 / 105 [email protected] Combinatorics I Introduction
Combinatorics
CSE235
Introduction Counting Combinatorics is the study of collections of objects. PIE Specifically, counting objects, arrangement, derangement, etc. Pigeonhole of objects along with their mathematical properties. Principle Permutations Counting objects is important in order to analyze algorithms Combinations and compute discrete probabilities. Binomial Coefficients Originally, combinatorics was motivated by gambling: counting Generalizations configurations is essential to elementary probability. Algorithms
More Examples
2 / 105 Combinatorics II Introduction
Combinatorics
CSE235
Introduction Counting A simple example: How many arrangements are there of a deck PIE of 52 cards? Pigeonhole Principle In addition, combinatorics can be used as a proof technique. Permutations Combinations A combinatorial proof is a proof method that uses counting Binomial Coefficients arguments to prove a statement.
Generalizations
Algorithms
More Examples
3 / 105 Product Rule
Combinatorics
CSE235
Introduction If two events are not mutually exclusive (that is, we do them
Counting separately), then we apply the product rule. PIE Theorem (Product Rule) Pigeonhole Principle Suppose a procedure can be accomplished with two disjoint Permutations subtasks. If there are n1 ways of doing the first task and n2 Combinations ways of doing the second, then there are Binomial Coefficients Generalizations n1 · n2 Algorithms
More ways of doing the overall procedure. Examples
4 / 105 Sum Rule I
Combinatorics
CSE235
Introduction If two events are mutually exclusive, that is, they cannot be Counting done at the same time, then we must apply the sum rule. PIE
Pigeonhole Principle Theorem (Sum Rule)
Permutations If an event e1 can be done in n1 ways and an event e2 can be Combinations done in n2 ways and e1 and e2 are mutually exclusive, then the Binomial Coefficients number of ways of both events occurring is
Generalizations
Algorithms n1 + n2
More Examples
5 / 105 Sum Rule II
Combinatorics CSE235 There is a natural generalization to any sequence of m tasks; Introduction namely the number of ways m mutually exclusive events can Counting occur is PIE n1 + n2 + ··· nm−1 + nm Pigeonhole Principle
Permutations We can give another formulation in terms of sets. Let Combinations A1,A2,...,Am be pairwise disjoint sets. Then Binomial Coefficients |A1 ∪ A2 ∪ · · · ∪ Am| = |A1| + |A2| + ··· + |Am| Generalizations Algorithms In fact, this is a special case of the general Principle of More Examples Inclusion-Exclusion.
6 / 105 Principle of Inclusion-Exclusion (PIE) I Introduction
Combinatorics
CSE235
Introduction
Counting PIE Say there are two events, e and e for which there are n and Examples 1 2 1 Derangements n2 possible outcomes respectively. Pigeonhole Principle Now, say that only one event can occur, not both. Permutations Combinations In this situation, we cannot apply the sum rule? Why? Binomial Coefficients
Generalizations
Algorithms
More Examples
7 / 105 Principle of Inclusion-Exclusion (PIE) II Introduction
Combinatorics
CSE235
Introduction We cannot use the sum rule because we would be over
Counting counting the number of possible outcomes.
PIE Examples Instead, we have to count the number of possible outcomes of Derangements e1 and e2 minus the number of possible outcomes in common Pigeonhole Principle to both; i.e. the number of ways to do both “tasks”. Permutations If again we think of them as sets, we have Combinations
Binomial Coefficients |A1| + |A2| − |A1 ∩ A2| Generalizations
Algorithms
More Examples
8 / 105 Principle of Inclusion-Exclusion (PIE) III Introduction
Combinatorics
CSE235 More generally, we have the following.
Introduction Lemma Counting Let A, B be subsets of a finite set U. Then PIE Examples 1 |A ∪ B| = |A| + |B| − |A ∩ B| Derangements Pigeonhole 2 |A ∩ B| ≤ min{|A|, |B|} Principle 3 Permutations |A \ B| = |A| − |A ∩ B| ≥ |A| − |B| Combinations 4 |A| = |U| − |A| Binomial 5 Coefficients |A ⊕ B| = |A ∪ B| − |A ∩ B| = A + B − 2|A ∩ B| =
Generalizations |A \ B| + |B \ A| Algorithms 6 |A × B| = |A| × |B| More Examples
9 / 105 Principle of Inclusion-Exclusion (PIE) I Theorem
Combinatorics Theorem CSE235 Let A1,A2,...,An be finite sets, then Introduction
Counting X |A1 ∪ A2 ∪ · · · ∪ An| = |Ai| PIE i Examples Derangements X − |Ai ∩ Aj| Pigeonhole Principle i More Examples Each summation is over all i, pairs i, j with i < j, triples i, j, k with i < j < k etc. 10 / 105 Principle of Inclusion-Exclusion (PIE) II Theorem Combinatorics CSE235 Introduction Counting To illustrate, when n = 3, we have PIE Examples Derangements |A1 ∪ A2 ∪ A3| = |A1| + |A2| + |A3| Pigeonhole Principle − |A1 ∩ A2| + |A1 ∩ A3| + |A2 ∩ A3| Permutations +|A1 ∩ A2 ∩ A3| Combinations Binomial Coefficients Generalizations Algorithms More Examples 11 / 105 Principle of Inclusion-Exclusion (PIE) III Theorem Combinatorics CSE235 To illustrate, when n = 4, we have Introduction Counting |A1 ∪ A2 ∪ A3 ∪ A4| = |A1| + |A2| + |A3| + |A4| PIE h Examples − |A1 ∩ A2| + |A1 ∩ A3| + +|A1 ∩ A4| Derangements i Pigeonhole Principle |A2 ∩ A3| + |A2 ∩ A4| + |A3 ∩ A4| Permutations h + |A1 ∩ A2 ∩ A3| + |A1 ∩ A2 ∩ A4| + Combinations Binomial i Coefficients |A1 ∩ A3 ∩ A4| + |A2 ∩ A3 ∩ A4| Generalizations −|A1 ∩ A2 ∩ A3 ∩ A4| Algorithms More Examples 12 / 105 Principle of Inclusion-Exclusion (PIE) I Example I Combinatorics CSE235 Example Introduction Counting How many integers between 1 and 300 (inclusive) are PIE 1 Divisible by at least one of 3, 5, 7? Examples Derangements 2 Divisible by 3 and by 5 but not by 7? Pigeonhole Principle 3 Divisible by 5 but by neither 3 nor 7? Permutations Combinations Binomial Let Coefficients A = {n | 1 ≤ n ≤ 300 ∧ 3 | n} Generalizations B = {n | 1 ≤ n ≤ 300 ∧ 5 | n} Algorithms C = {n | 1 ≤ n ≤ 300 ∧ 7 | n} More Examples 13 / 105 Principle of Inclusion-Exclusion (PIE) II Example I Combinatorics CSE235 Introduction Counting How big are each of these sets? We can easily use the floor PIE function; Examples Derangements |A| = b300/3c = 100 Pigeonhole |B| = b300/5c = 60 Principle |C| = b300/7c = 42 Permutations Combinations Binomial For (1) above, we are asked to find |A ∪ B ∪ C|. Coefficients Generalizations Algorithms More Examples 14 / 105 Principle of Inclusion-Exclusion (PIE) III Example I Combinatorics CSE235 By the principle of inclusion-exclusion, we have that Introduction Counting |A ∪ B ∪ C| = |A| + |B| + |C| PIE Examples h i Derangements − |A ∩ B| + |A ∩ C| + |B ∩ C| Pigeonhole Principle +|A ∩ B ∩ C| Permutations Combinations It remains to find the final 4 cardinalities. Binomial Coefficients All three divisors, 3, 5, 7 are relatively prime. Thus, any integer Generalizations that is divisible by both 3 and 5 must simply be divisible by 15. Algorithms More Examples 15 / 105 Principle of Inclusion-Exclusion (PIE) IV Example I Combinatorics CSE235 Using the same reasoning for all pairs (and the triple) we have Introduction Counting |A ∩ B| = b300/15c = 20 PIE Examples |A ∩ C| = b300/21c = 14 Derangements |B ∩ C| = b300/35c = 8 Pigeonhole Principle |A ∩ B ∩ C| = b300/105c = 2 Permutations Combinations Therefore, Binomial Coefficients |A ∪ B ∪ C| = 100 + 60 + 42 − 20 − 14 − 8 + 2 = 162 Generalizations Algorithms More Examples 16 / 105 Principle of Inclusion-Exclusion (PIE) V Example I Combinatorics CSE235 Introduction Counting For (2) above, it is enough to find PIE Examples |(A ∩ B) \ C| Derangements Pigeonhole Principle By the definition of set-minus, Permutations Combinations |(A ∩ B) \ C| = |A ∩ B| − |A ∩ B ∩ C| = 20 − 2 = 18 Binomial Coefficients Generalizations Algorithms More Examples 17 / 105 Principle of Inclusion-Exclusion (PIE) VI Example I Combinatorics CSE235 For (3) above, we are asked to find Introduction Counting |B \ (A ∪ C)| = |B| − |B ∩ (A ∪ C)| PIE Examples Derangements By distributing B over the intersection, we get Pigeonhole Principle |B ∩ (A ∪ C)| = |(B ∩ A) ∪ (B ∩ C)| Permutations = |B ∩ A| + |B ∩ C| − |(B ∩ A) ∩ (B ∩ C)| Combinations = |B ∩ A| + |B ∩ C| − |B ∩ A ∩ C| Binomial Coefficients = 20 + 8 − 2 = 26 Generalizations Algorithms So the answer is |B| − 26 = 60 − 26 = 34. More Examples 18 / 105 Principle of Inclusion-Exclusion (PIE) I Example II Combinatorics CSE235 The principle of inclusion-exclusion can be used to count the Introduction number of onto functions. Counting PIE Theorem Examples Derangements Let A, B be non-empty sets of cardinality m, n with m ≥ n. Pigeonhole Then there are Principle Permutations n n n nm − (n−1)m + (n−2)m −· · ·+(−1)n−1 1m Combinations 1 2 n − 1 Binomial Coefficients Pn−1 i n m Generalizations i.e. i=0 (−1) i (n − i) onto functions f : A → B. Algorithms More Examples See textbook page 460. 19 / 105 Principle of Inclusion-Exclusion (PIE) II Example II Combinatorics CSE235 Example Introduction How many ways of giving out 6 pieces of candy to 3 children if Counting each child must receive at least one piece? PIE Examples Derangements Pigeonhole This can be modeled by letting A represent the set of candies Principle and B be the set of children. Permutations Combinations Then a function f : A → B can be interpreted as giving candy Binomial ai to child cj. Coefficients Generalizations Since each child must receive at least one candy, we are Algorithms considering only onto functions. More Examples 20 / 105 Principle of Inclusion-Exclusion (PIE) III Example II Combinatorics CSE235 Introduction Counting To count how many there are, we apply the theorem and get PIE Examples (for m = 6, n = 3), Derangements Pigeonhole 3 3 Principle 36 − (3 − 1)6 + (3 − 2)6 = 540 Permutations 1 2 Combinations Binomial Coefficients Generalizations Algorithms More Examples 21 / 105 Derangements I Combinatorics CSE235 Introduction Counting Consider the hatcheck problem. PIE Examples Derangements An employee checks hats from n customers. Pigeonhole Principle However, he forgets to tag them. Permutations When customer’s check-out their hats, they are given one Combinations at random. Binomial Coefficients What is the probability that no one will get their hat back? Generalizations Algorithms More Examples 22 / 105 Derangements II Combinatorics CSE235 This can be modeled using derangements: permutations of Introduction objects such that no element is in its original position. Counting For example, 21453 is a derangement of 12345, but 21543 is PIE Examples not. Derangements Pigeonhole Theorem Principle Permutations The number of derangements of a set with n elements is Combinations 1 1 1 n 1 Binomial Dn = n! 1 − + − + ··· (−1) Coefficients 1! 2! 3! n! Generalizations Algorithms See textbook page 461. More Examples 23 / 105 Derangements III Combinatorics Thus, the answer to the hatcheck problem is CSE235 Introduction Dn Counting n! PIE Examples Derangements Its interesting to note that Pigeonhole Principle −1 1 1 1 n 1 Permutations e = 1 − + − + ··· + (−1) ··· 1! 2! 3! n! Combinations Binomial So that the probability of the hatcheck problem converges; Coefficients Generalizations Dn −1 Algorithms lim = e = .368 ... n→∞ n! More Examples 24 / 105 The Pigeonhole Principle I Combinatorics CSE235 Introduction The pigeonhole principle states that if there are more pigeons Counting than there are roosts (pigeonholes), for at least one pigeonhole, PIE more than two pigeons must be in it. Pigeonhole Principle Generalized Theorem (Pigeonhole Principle) Examples Permutations If k + 1 or more objects are placed into k boxes, then there is Combinations at least one box containing two ore more objects. Binomial Coefficients Generalizations This is a fundamental tool of elementary discrete mathematics. Algorithms It is also known as the Dirichlet Drawer Principle. More Examples 25 / 105 The Pigeonhole Principle II Combinatorics It is seemingly simple, but very powerful. CSE235 Introduction The difficulty comes in where and how to apply it. Counting Some simple applications in computer science: PIE Pigeonhole Principle Calculating the probability of Hash functions having a Generalized collision. Examples Permutations Proving that there can be no lossless compression Combinations algorithm compressing all files to within a certain ratio. Binomial Coefficients Lemma Generalizations Algorithms For two finite sets A, B there exists a bijection f : A → B if More and only if |A| = |B|. Examples 26 / 105 Generalized Pigeonhole Principle I Combinatorics CSE235 Introduction Theorem Counting If N objects are placed into k boxes then there is at least one PIE box containing at least Pigeonhole Principle N Generalized Examples k Permutations Combinations Example Binomial Coefficients In any group of 367 or more people, at least two of them must Generalizations have been born on the same date. Algorithms More Examples 27 / 105 Generalized Pigeonhole Principle II Combinatorics CSE235 Introduction A probabilistic generalization states that if n objects are Counting PIE randomly put into m boxes with uniform probability (each Pigeonhole object is placed in a given box with probability 1/m) then at Principle least one box will hold more than one object with probability, Generalized Examples Permutations m! 1 − Combinations (m − n)!mn Binomial Coefficients Generalizations Algorithms More Examples 28 / 105 Generalized Pigeonhole Principle III Combinatorics CSE235 Example Introduction Among 10 people, what is the probability that two or more will Counting have the same birthday? PIE Pigeonhole Principle Generalized Here, n = 10 and m = 365 (ignore leapyears). Thus, the Examples probability that two will have the same birthday is Permutations Combinations 365! Binomial 1 − 10 ≈ .1169 Coefficients (365 − 10)!365 Generalizations Algorithms So less than a 12% probability! More Examples 29 / 105 Pigeonhole Principle I Example I Combinatorics CSE235 Example Introduction Show that in a room of n people with certain acquaintances, Counting some pair must have the same number of acquaintances. PIE Pigeonhole Principle Note that this is equivalent to showing that any symmetric, Generalized Examples irreflexive relation on n elements must have two elements with Permutations the same number of relations. Combinations Binomial We’ll show by contradiction using the pigeonhole principle. Coefficients Generalizations Assume to the contrary that every person has a different Algorithms number of acquaintances; 0, 1, . . . , n − 1 (we cannot have n More Examples here because it is irreflexive). Are we done? 30 / 105 Pigeonhole Principle II Example I Combinatorics CSE235 No, since we only have n people, this is okay (i.e. there are n Introduction possibilities). Counting PIE We need to use the fact that acquaintanceship is a symmetric, Pigeonhole irreflexive relation. Principle Generalized Examples In particular, some person knows 0 people while another knows Permutations n − 1 people. Combinations Binomial In other words, someone knows everyone, but there is also a Coefficients person that knows no one. Generalizations Algorithms Thus, we have reached a contradiction. More Examples 31 / 105 Pigeonhole Principle I Example II Combinatorics Example CSE235 Show that in any list of ten nonnegative integers, A0,...,A9, Introduction there is a string of consecutive items of the list a , a ,... Counting l l+1 PIE whose sum is divisible by 10. Pigeonhole Principle Generalized Consider the following 10 numbers. Examples Permutations a0 Combinations a0 + a1 Binomial Coefficients a0 + a1 + a2 . Generalizations . Algorithms a0 + a1 + a2 + ... + a9 More Examples If any one of them is divisible by 10 then we are done. 32 / 105 Pigeonhole Principle II Example II Combinatorics CSE235 Otherwise, we observe that each of these numbers must be in Introduction one of the congruence classes Counting PIE 1 mod 10, 2 mod 10,..., 9 mod 10 Pigeonhole Principle Generalized By the pigeonhole principle, at least two of the integers above Examples 0 Permutations must lie in the same congruence class. Say a, a lie in the Combinations congruence class k mod 10. Binomial Coefficients Then 0 Generalizations (a − a ) ≡ k − k(mod 10) Algorithms and so the difference (a − a0) is divisible by 10. More Examples 33 / 105 Pigeonhole Principle I Example III Combinatorics CSE235 Introduction Example Counting PIE Say 30 buses are to transport 2000 Cornhusker fans to Pigeonhole Colorado. Each bus has 80 seats. Show that Principle Generalized 1 One of the buses will have 14 empty seats. Examples Permutations 2 One of the buses will carry at least 67 passengers. Combinations Binomial Coefficients For (1), the total number of seats is 30 · 80 = 2400 seats. Thus Generalizations there will be 2400 − 2000 = 400 empty seats total. Algorithms More Examples 34 / 105 Pigeonhole Principle II Example III Combinatorics CSE235 By the generalized pigeonhole principle, with 400 empty seats among 30 buses, one bus will have at least Introduction Counting 400 = 14 PIE 30 Pigeonhole Principle Generalized empty seats. Examples Permutations For (2) above, by the pigeonhole principle, seating 2000 Combinations passengers among 30 buses, one will have at least Binomial Coefficients 2000 Generalizations = 67 30 Algorithms More Examples passengers. 35 / 105 Permutations I Combinatorics CSE235 Introduction A permutation of a set of distinct objects is an ordered Counting arrangement of these objects. An ordered arrangement of r PIE elements of a set is called an r-permutation. Pigeonhole Principle Theorem Permutations The number of r permutations of a set with n distinct Combinations elements is Binomial Coefficients r−1 Generalizations Y P (n, r) = (n − i) = n(n − 1)(n − 2) ··· (n − r + 1) Algorithms i=0 More Examples 36 / 105 Permutations II Combinatorics CSE235 Introduction It follows that n! Counting P (n, r) = PIE (n − r)! Pigeonhole Principle In particular, Permutations P (n, n) = n! Combinations Binomial Coefficients Again, note here that order is important. It is necessary to Generalizations distinguish in what cases order is important and in which it is Algorithms not. More Examples 37 / 105 The first woman can be partnered with any of the 20 men. The second with any of the remaining 19, etc. To partner all 12 women, we have P (20, 12) Permutations Example I Combinatorics CSE235 Example Introduction Counting How many pairs of dance partners can be selected from a PIE group of 12 women and 20 men? Pigeonhole Principle Permutations Combinations Binomial Coefficients Generalizations Algorithms More Examples 38 / 105 To partner all 12 women, we have P (20, 12) Permutations Example I Combinatorics CSE235 Example Introduction Counting How many pairs of dance partners can be selected from a PIE group of 12 women and 20 men? Pigeonhole Principle Permutations The first woman can be partnered with any of the 20 men. The Combinations second with any of the remaining 19, etc. Binomial Coefficients Generalizations Algorithms More Examples 39 / 105 Permutations Example I Combinatorics CSE235 Example Introduction Counting How many pairs of dance partners can be selected from a PIE group of 12 women and 20 men? Pigeonhole Principle Permutations The first woman can be partnered with any of the 20 men. The Combinations second with any of the remaining 19, etc. Binomial Coefficients To partner all 12 women, we have Generalizations Algorithms P (20, 12) More Examples 40 / 105 The number of ways of arranging 10 letters between a and z is P (24, 10). Since we can choose either a or z to come first, there are 2P (24, 10) arrangements of this 12-letter block. For the remaining 14 letters, there are P (15, 15) = 15! arrangements. In all, there are 2P (24, 10) · 15! Permutations Example II Combinatorics CSE235 Example Introduction In how many ways can the English letters be arranged so that Counting there are exactly ten letters between a and z? PIE Pigeonhole Principle Permutations Combinations Binomial Coefficients Generalizations Algorithms More Examples 41 / 105 For the remaining 14 letters, there are P (15, 15) = 15! arrangements. In all, there are 2P (24, 10) · 15! Permutations Example II Combinatorics CSE235 Example Introduction In how many ways can the English letters be arranged so that Counting there are exactly ten letters between a and z? PIE Pigeonhole Principle The number of ways of arranging 10 letters between a and z is Permutations P (24, 10). Since we can choose either a or z to come first, Combinations there are 2P (24, 10) arrangements of this 12-letter block. Binomial Coefficients Generalizations Algorithms More Examples 42 / 105 Permutations Example II Combinatorics CSE235 Example Introduction In how many ways can the English letters be arranged so that Counting there are exactly ten letters between a and z? PIE Pigeonhole Principle The number of ways of arranging 10 letters between a and z is Permutations P (24, 10). Since we can choose either a or z to come first, Combinations there are 2P (24, 10) arrangements of this 12-letter block. Binomial Coefficients For the remaining 14 letters, there are P (15, 15) = 15! Generalizations arrangements. In all, there are Algorithms More 2P (24, 10) · 15! Examples 43 / 105 The number of total permutations is P (7, 7) = 7!. If we fix the pattern bge, then we can consider it as a single block. Thus, the number of permutations with this pattern is P (5, 5) = 5!. Permutations Example III Combinatorics CSE235 Introduction Example Counting PIE How many permutations of the letters a, b, c, d, e, f, g contain Pigeonhole neither the pattern bge nor eaf? Principle Permutations Combinations Binomial Coefficients Generalizations Algorithms More Examples 44 / 105 If we fix the pattern bge, then we can consider it as a single block. Thus, the number of permutations with this pattern is P (5, 5) = 5!. Permutations Example III Combinatorics CSE235 Introduction Example Counting PIE How many permutations of the letters a, b, c, d, e, f, g contain Pigeonhole neither the pattern bge nor eaf? Principle Permutations Combinations The number of total permutations is P (7, 7) = 7!. Binomial Coefficients Generalizations Algorithms More Examples 45 / 105 Permutations Example III Combinatorics CSE235 Introduction Example Counting PIE How many permutations of the letters a, b, c, d, e, f, g contain Pigeonhole neither the pattern bge nor eaf? Principle Permutations Combinations The number of total permutations is P (7, 7) = 7!. Binomial Coefficients If we fix the pattern bge, then we can consider it as a single Generalizations block. Thus, the number of permutations with this pattern is Algorithms P (5, 5) = 5!. More Examples 46 / 105 Is this correct? No. We have taken away too many permutations: ones containing both eaf and bge. Here there are two cases, when eaf comes first and when bge comes first. Permutations Example III - Continued Combinatorics CSE235 Fixing the pattern eaf we have the same number, 5!. Introduction Counting Thus we have PIE 7! − 2(5!) Pigeonhole Principle Permutations Combinations Binomial Coefficients Generalizations Algorithms More Examples 47 / 105 No. We have taken away too many permutations: ones containing both eaf and bge. Here there are two cases, when eaf comes first and when bge comes first. Permutations Example III - Continued Combinatorics CSE235 Fixing the pattern eaf we have the same number, 5!. Introduction Counting Thus we have PIE 7! − 2(5!) Pigeonhole Principle Permutations Combinations Is this correct? Binomial Coefficients Generalizations Algorithms More Examples 48 / 105 Here there are two cases, when eaf comes first and when bge comes first. Permutations Example III - Continued Combinatorics CSE235 Fixing the pattern eaf we have the same number, 5!. Introduction Counting Thus we have PIE 7! − 2(5!) Pigeonhole Principle Permutations Combinations Is this correct? Binomial Coefficients No. We have taken away too many permutations: ones Generalizations containing both eaf and bge. Algorithms More Examples 49 / 105 Permutations Example III - Continued Combinatorics CSE235 Fixing the pattern eaf we have the same number, 5!. Introduction Counting Thus we have PIE 7! − 2(5!) Pigeonhole Principle Permutations Combinations Is this correct? Binomial Coefficients No. We have taken away too many permutations: ones Generalizations containing both eaf and bge. Algorithms More Here there are two cases, when eaf comes first and when bge Examples comes first. 50 / 105 If bge comes first, it must be the case that we have bgeaf as a single block and so we have 3 blocks or 3! arrangements. Altogether we have 7! − 2(5!) + 3! = 4806 Permutations Example III - Continued Combinatorics CSE235 Introduction Counting eaf cannot come before bge, so this is not a problem. PIE Pigeonhole Principle Permutations Combinations Binomial Coefficients Generalizations Algorithms More Examples 51 / 105 Altogether we have 7! − 2(5!) + 3! = 4806 Permutations Example III - Continued Combinatorics CSE235 Introduction Counting eaf cannot come before bge, so this is not a problem. PIE If bge comes first, it must be the case that we have bgeaf as a Pigeonhole Principle single block and so we have 3 blocks or 3! arrangements. Permutations Combinations Binomial Coefficients Generalizations Algorithms More Examples 52 / 105 Permutations Example III - Continued Combinatorics CSE235 Introduction Counting eaf cannot come before bge, so this is not a problem. PIE If bge comes first, it must be the case that we have bgeaf as a Pigeonhole Principle single block and so we have 3 blocks or 3! arrangements. Permutations Altogether we have Combinations Binomial Coefficients 7! − 2(5!) + 3! = 4806 Generalizations Algorithms More Examples 53 / 105 Combinations I Definition Combinatorics CSE235 Introduction Counting Whereas permutations consider order, combinations are used PIE when order does not matter. Pigeonhole Principle Definition Permutations Combinations An k-combination of elements of a set is an unordered selection Binomial of k elements from the set. A combination is simply a subset of Coefficients cardinality k. Generalizations Algorithms More Examples 54 / 105 Combinations II Definition Combinatorics CSE235 Introduction Theorem Counting The number of k-combinations of a set with cardinality n with PIE 0 ≤ k ≤ n is Pigeonhole Principle n n! Permutations C(n, k) = = Combinations k (n − k)!k! Binomial Coefficients n Generalizations Note: the notation, k is read, “n choose k”. In TEX use {n Algorithms choose k} (with the forward slash). More Examples 55 / 105 Combinations III Definition Combinatorics CSE235 Introduction A useful fact about combinations is that they are symmetric. Counting PIE n n Pigeonhole = Principle 1 n − 1 Permutations Combinations n n = Binomial Coefficients 2 n − 2 Generalizations etc. Algorithms More Examples 56 / 105 Combinations IV Definition Combinatorics CSE235 Introduction Counting This is formalized in the following corollary. PIE Corollary Pigeonhole Principle Let n, k be nonnegative integers with k ≤ n, then Permutations Combinations n n Binomial = Coefficients k n − k Generalizations Algorithms More Examples 57 / 105 Combinations I Example I Combinatorics CSE235 Introduction Example Counting In the Powerball lottery, you pick five numbers between 1 and PIE 55 and a single “powerball” number between 1 and 42. How Pigeonhole Principle many possible plays are there? Permutations Combinations Order here doesn’t matter, so the number of ways of choosing Binomial Coefficients five regular numbers is Generalizations 55 Algorithms 5 More Examples 58 / 105 Combinations II Example I Combinatorics CSE235 We can choose among 42 power ball numbers. These events Introduction are not mutually exclusive, thus we use the product rule. Counting PIE 55 55! Pigeonhole 42 = 42 = 146, 107, 962 Principle 5 (55 − 5)!5! Permutations Combinations So the odds of winning are Binomial Coefficients 1 Generalizations < .000000006845 Algorithms 146, 107, 962 More Examples 59 / 105 Combinations I Example II Combinatorics CSE235 Introduction Example Counting PIE In a sequence of 10 coin tosses, how many ways can 3 heads Pigeonhole and 7 tails come up? Principle Permutations Combinations The number of ways of choosing 3 heads out of 10 coin tosses Binomial is Coefficients 10 Generalizations 3 Algorithms More Examples 60 / 105 Combinations II Example II Combinatorics CSE235 Introduction Counting However, this is the same as choosing 7 tails out of 10 coin PIE tosses; Pigeonhole 10 10 Principle = = 120 Permutations 3 7 Combinations Binomial This is a perfect illustration of the previous corollary. Coefficients Generalizations Algorithms More Examples 61 / 105 Combinations I Example III Combinatorics CSE235 Introduction Counting PIE Example Pigeonhole How many possible committees of five people can be chosen Principle from 20 men and 12 women if Permutations Combinations 1 if exactly three men must be on each committee? Binomial 2 Coefficients if at least four women must be on each committee? Generalizations Algorithms More Examples 62 / 105 Combinations II Example III Combinatorics CSE235 Introduction Counting For (1), we must choose 3 men from 20 then two women from PIE 12. These are not mutually exclusive, thus the product rule Pigeonhole Principle applies. Permutations 2012 Combinations 3 2 Binomial Coefficients Generalizations Algorithms More Examples 63 / 105 Combinations III Example III Combinatorics CSE235 Introduction For (2), we consider two cases; the case where four women are Counting chosen and the case where five women are chosen. These two PIE cases are mutually exclusive so we use the addition rule. Pigeonhole Principle Permutations For the first case we have Combinations 2012 Binomial Coefficients 1 4 Generalizations Algorithms More Examples 64 / 105 Combinations IV Example III Combinatorics CSE235 Introduction And for the second we have Counting 2012 PIE Pigeonhole 0 5 Principle Permutations Together we have Combinations Binomial 2012 2012 Coefficients + = 10, 692 Generalizations 1 4 0 5 Algorithms More Examples 65 / 105 Binomial Coefficients I Introduction Combinatorics CSE235 Introduction Counting PIE n The number of r-combinations, r is also called a binomial Pigeonhole Principle coefficient. Permutations They are the coefficients in the expansion of the expression Combinations (multivariate polynomial), (x + y)n.A binomial is a sum of Binomial Coefficients two terms. Generalizations Algorithms More Examples 66 / 105 Binomial Coefficients II Introduction Combinatorics CSE235 Introduction Counting Theorem (Binomial Theorem) PIE Pigeonhole Let x, y be variables and let n be a nonnegative integer. Then Principle n Permutations X n (x + y)n = xn−jyj Combinations j Binomial j=0 Coefficients Generalizations Algorithms More Examples 67 / 105 Binomial Coefficients III Introduction Combinatorics CSE235 Expanding the summation, we have Introduction Counting n n n n n−1 n n−2 2 (x + y) = 0 x + 1 x y + 2 x y + ··· PIE n n−1 n n + n−1 xy + n y Pigeonhole Principle Permutations For example, Combinations Binomial (x + y)3 = (x + y)(x + y)(x + y) Coefficients 2 2 Generalizations = (x + y)(x + 2xy + y ) 3 2 2 3 Algorithms = x + 3x y + 3xy + y More Examples 68 / 105 Binomial Coefficients I Example Combinatorics Example CSE235 What is the coefficient of the term x8y12 in the expansion of 20 Introduction (3x + 4y) ? Counting PIE By the Binomial Theorem, we have Pigeonhole Principle 20 Permutations X 20 (3x + 4y)n = (3x)20−j(4y)j Combinations j j=0 Binomial Coefficients Generalizations So when j = 12, we have Algorithms 20 More (3x)8(4y)12 Examples 12 so the coefficient is 20! 38412 = 13866187326750720. 69 / 105 12!8! Binomial Coefficients I More Combinatorics CSE235 Many useful identities and facts come from the Binomial Introduction Theorem. Counting Corollary PIE Pigeonhole Principle n X n n Permutations = 2 k Combinations k=0 Binomial n Coefficients X n (−1)k = 0 n ≥ 1 Generalizations k k=0 Algorithms n X n More 2k = 3n Examples k k=0 70 / 105 Binomial Coefficients II More Combinatorics CSE235 Introduction Counting PIE n n Pigeonhole Check textbook for proofs, which are based on: 2 =(1 + 1) , Principle 0 = 0n=((−1) + 1)n, 3n=(1 + 2)n. Permutations Combinations Binomial Coefficients Generalizations Algorithms More Examples 71 / 105 Binomial Coefficients III More Combinatorics CSE235 Introduction Most of these can be proven by either induction or by a Counting combinatorial argument. PIE Pigeonhole Theorem (Vandermonde’s Identity) Principle Let m, n, r be nonnegative integers with r not exceeding either Permutations m or n. Then Combinations Binomial r Coefficients m + n X m n = Generalizations r r − k k k=0 Algorithms More Examples 72 / 105 Binomial Coefficients IV More Combinatorics Taking n = m = r in the Vandermonde’s identity. CSE235 Corollary Introduction Counting If n is a nonnegative integer, then PIE n 2 2n X n Pigeonhole = Principle n k Permutations k=0 Combinations Binomial Corollary Coefficients Generalizations Let n, r be nonnegative integers, r ≤ n. Then Algorithms n More n + 1 X j = Examples r + 1 r j=r 73 / 105 Binomial Coefficients I Pascal’s Identity & Triangle Combinatorics CSE235 Introduction The following is known as Pascal’s Identity which gives a useful Counting identity for efficiently computing binomial coefficients. PIE Theorem (Pascal’s Identity) Pigeonhole Principle + Let n, k ∈ Z with n ≥ k. Then Permutations Combinations n + 1 n n Binomial = + Coefficients k k − 1 k Generalizations Algorithms Pascal’s Identity forms the basis of a geometric object known More Examples as Pascal’s Triangle. 74 / 105 Pascal’s Triangle Combinatorics CSE235 0 0 Introduction 1 1 Counting 0 1 PIE Pigeonhole 2 2 2 Principle 0 1 2 Permutations 3 3 3 3 Combinations 0 1 2 3 Binomial Coefficients 4 4 4 4 4 Generalizations 0 1 2 3 4 Algorithms 5 5 5 5 5 5 More 0 1 2 3 4 5 Examples 75 / 105 Pascal’s Triangle Combinatorics CSE235 1 Introduction Counting 1 1 PIE Pigeonhole 1 2 1 Principle Permutations Combinations 1 3 3 1 Binomial Coefficients 1 4 6 4 1 Generalizations Algorithms More 1 5 10 10 5 1 Examples 76 / 105 Pascal’s Triangle Combinatorics CSE235 0 0 3 3 4 Introduction 2 + 3 = 3 1 1 Counting 0 1 PIE Pigeonhole 2 2 2 Principle 0 1 2 Permutations 3 3 3 3 Combinations 0 1 2 3 Binomial Coefficients 4 4 4 4 4 Generalizations 0 1 2 3 4 Algorithms 5 5 5 5 5 5 More 0 1 2 3 4 5 Examples 77 / 105 Generalized Combinations & Permutations I Combinatorics CSE235 Introduction Counting Sometimes we are concerned with permutations and PIE combinations in which repetitions are allowed. Pigeonhole Principle Theorem Permutations The number of r-permutations of a set of n objects with Combinations repetition allowed is nr. Binomial Coefficients Generalizations Easily obtained by the product rule. Algorithms More Examples 78 / 105 Generalized Combinations & Permutations II Combinatorics CSE235 Introduction Counting Theorem PIE There are Pigeonhole n + r − 1 Principle Permutations r Combinations r-combinations from a set with n elements when repetition of Binomial Coefficients elements is allowed. Generalizations Algorithms More Examples 79 / 105 Generalized Combinations & Permutations III Combinatorics CSE235 Example Introduction There are 30 varieties of donuts from which we wish to buy a Counting dozen. How many possible ways to place your order are there? PIE Pigeonhole Principle Here n = 30 and we wish to choose r = 12. Order does not Permutations matter and repetitions are possible, so we apply the previous Combinations theorem to get that there are Binomial Coefficients 30 + 12 − 1 Generalizations Algorithms 12 More Examples possible orders. 80 / 105 Generalized Combinations & Permutations IV Combinatorics CSE235 Theorem Introduction The number of different permutations of n objects where there Counting are n1 indistinguishable objects of type 1, n2 of type 2, ..., and PIE n of type k is Pigeonhole k Principle n! Permutations n1!n2! ··· nk! Combinations Binomial Coefficients An equivalent way of interpreting this theorem is the number of Generalizations ways to distribute n distinguishable objects into k Algorithms distinguishable boxes so that ni objects are placed into box i More for i = 1, 2, . . . , k. Examples 81 / 105 Generalized Combinations & Permutations V Combinatorics CSE235 Introduction Example Counting How many permutations of the word “Mississippi” are there? PIE Pigeonhole Principle “Mississippi” contains 4 distinct letters, M, i, s and p; with Permutations 1, 4, 4, 2 occurrences respectively. Combinations Binomial Therefore there are Coefficients 11! Generalizations 1!4!4!2! Algorithms permutations. More Examples 82 / 105 Generating Permutations & Combinations I Introduction Combinatorics CSE235 Introduction In general, it is inefficient to solve a problem by considering all Counting permutations or combinations since there are an exponential PIE Pigeonhole number of such arrangements. Principle Permutations Nevertheless, for many problems, no better approach is known. Combinations When exact solutions are needed, back-tracking algorithms are Binomial used. Coefficients Generalizations Generating permutations or combinations are sometimes the Algorithms basis of these algorithms. Combinations Permutations More Examples 83 / 105 Generating Permutations & Combinations II Introduction Combinatorics CSE235 Introduction Counting PIE Example (Traveling Sales Person Problem) Pigeonhole Principle Consider a salesman that must visit n different cities. He Permutations wishes to visit them in an order such that his overall distance Combinations traveled is minimized. Binomial Coefficients Generalizations Algorithms Combinations Permutations More Examples 84 / 105 Generating Permutations & Combinations III Introduction Combinatorics CSE235 Introduction This problem is one of hundreds of NP-complete problems for Counting which no known efficient algorithms exist. Indeed, it is believed PIE that no efficient algorithms exist. (Actually, Euclidean TSP is Pigeonhole Principle not even known to be in NP!) Permutations The only known way of solving this problem exactly is to try all Combinations n! possible routes. Binomial Coefficients We give several algorithms for generating these combinatorial Generalizations objects. Algorithms Combinations Permutations More Examples 85 / 105 Generating Combinations I Combinatorics CSE235 Recall that combinations are simply all possible subsets of size r. For our purposes, we will consider generating subsets of Introduction Counting {1, 2, 3, . . . , n} PIE Pigeonhole Principle The algorithm works as follows. Permutations Combinations Start with {1, . . . , r} Binomial Coefficients Assume that we have a1a2 ··· ar, we want the next Generalizations combination. Algorithms Combinations Locate the last element ai such that ai 6= n − r + i. Permutations More Replace ai with ai + 1. Examples Replace aj with ai + j − i for j = i + 1, i + 2, . . . , r. 86 / 105 Generating Combinations II Combinatorics The following is pseudocode for this procedure. CSE235 Introduction Algorithm (Next r-Combination) Counting Input : A set of n elements and an r-combination, a1 ··· ar. PIE Output : The next r-combination. Pigeonhole Principle 1 i = r Permutations 2 while ai = n − r + i do 3 i = i − 1 Combinations 4 end Binomial Coefficients 5 ai = ai + 1 Generalizations 6 for j = (i + 1) . . . r do 7 aj = ai + j − i Algorithms Combinations 8 end Permutations More Examples 87 / 105 Generating Combinations III Combinatorics Example CSE235 Find the next 3-combination of the set {1, 2, 3, 4, 5} after Introduction Counting {1, 4, 5} PIE Pigeonhole Here, a1 = 1, a2 = 4, a3 = 5, n = 5, r = 3. Principle Permutations The last i such that ai 6= 5 − 3 + i is 1. Combinations Binomial Thus, we set Coefficients a1 = a1 + 1 = 2 Generalizations a2 = a1 + 2 − 1 = 3 Algorithms Combinations a3 = a1 + 3 − 1 = 4 Permutations More Examples So the next r-combination is {2, 3, 4}. 88 / 105 Generating Permutations Combinatorics CSE235 Introduction The text gives an algorithm to generate permutations in Counting lexicographic order. Essentially the algorithm works as follows. PIE Pigeonhole Principle Given a permutation, Permutations Choose the left-most pair aj, aj+1 where aj < aj+1. Combinations Binomial Choose the least item to the right of aj greater than aj. Coefficients Swap this item and a . Generalizations j Algorithms Arrange the remaining (to the right) items in order. Combinations Permutations More Examples 89 / 105 Generating Permutations Lexicographic Order Algorithm (Next Permutation (Lexicographic Order)) Input : A set of n elements and an r-permutation, a1 ··· ar. Output : The next r-permutation. 1 j = n − 1 2 while aj > aj+1 do 3 j = j − 1 4 end //j is the largest subscript with aj < aj+1 5 k = n 6 while aj > ak do 7 k = k − 1 8 end //ak is the smallest integer greater than aj to the right of aj 9 swap(aj , ak) 10 r = n 11 s = j + 1 12 while r > s do 13 swap(ar, as) 14 r = r − 1 15 s = s + 1 16 end Generating Permutations I Combinatorics CSE235 Often there is no reason to generate permutations in Introduction lexicographic order. Moreover, even though generating Counting permutations is inefficient in itself, lexicographic order induces PIE even more work. Pigeonhole Principle An alternate method is to fix an element, then recursively Permutations permute the n − 1 remaining elements. Combinations Binomial Johnson-Trotter algorithm has the following attractive Coefficients properties. Not in your textbook, not on the exam, just for Generalizations your reference/culture. Algorithms Combinations Permutations It is bottom-up (non-recursive). More Examples It induces a minimal-change between each permutation. 91 / 105 Generating Permutations II Combinatorics CSE235 Introduction The algorithm is known as the Johnson-Trotter algorithm. Counting PIE We associate a direction to each element, for example: Pigeonhole −→←−−→←− Principle 3 2 4 1 Permutations Combinations Binomial A component is mobile if its direction points to an adjacent Coefficients component that is smaller than itself. Here 3 and 4 are mobile Generalizations and 1 and 2 are not. Algorithms Combinations Permutations More Examples 92 / 105 Generating Permutations III Combinatorics CSE235 Algorithm (JohnsonTrotter) Introduction Counting Input : An integer n. PIE Output : All possible permutations of h1, 2, . . . ni. ←−←− ←− Pigeonhole 1 π = 1 2 ... n Principle 2 while There exists a mobile integer k ∈ π do Permutations 3 k = largest mobile integer Combinations 4 swap k and the adjacent integer k points to Binomial 5 reverse direction of all integers > k Coefficients 6 Output π Generalizations 7 end Algorithms Combinations Permutations More Examples 93 / 105 More Examples Combinatorics CSE235 Introduction Counting PIE Pigeonhole Principle As always, the best way to learn new concepts is through Permutations practice and examples. Combinations Binomial Coefficients Generalizations Algorithms More Examples 94 / 105 Example I I Combinatorics CSE235 Introduction Counting Example PIE How many bit strings of length 4 are there such that 11 never Pigeonhole Principle appears as a substring? Permutations Combinations We can represent the set of string graphically using a diagram Binomial Coefficients tree. Generalizations See textbook page 309. Algorithms More Examples 95 / 105 Example I II Combinatorics 1 1010 CSE235 0 1 Introduction 0 1001 1 0 Counting 0 0010 PIE 1 0101 Pigeonhole Principle 1 0 0 0 0100 Permutations Combinations 1 1000 0 Binomial 0 Coefficients 1 0001 Generalizations 0 0 0000 Algorithms More Examples Therefore, the number of such bit string is 8. 96 / 105 Example: Counting Functions I I Combinatorics CSE235 Introduction Counting Example PIE Let S, T be sets such that |S| = n, |T | = m. How many Pigeonhole Principle functions are there mapping f : S → T ? How many of these Permutations functions are one-to-one (injective)? Combinations Binomial Coefficients A function simply maps each si to some tj, thus for each n we Generalizations can choose to send it to any of the elements in T . Algorithms More Examples 97 / 105 Example: Counting Functions I II Combinatorics CSE235 Each of these is an independent event, so we apply the multiplication rule; Introduction Counting m × m × · · · × m = mn PIE | {z } Pigeonhole n times Principle Permutations If we wish f to be one-to-one (injective), we must have that Combinations n ≤ m, otherwise we can easily answer 0. Binomial Coefficients Generalizations Now, each si must be mapped to a unique element in T . For Algorithms s1, we have m choices. However, once we have made a More mapping (say tj), we cannot map subsequent elements to tj Examples again. 98 / 105 Example: Counting Functions I III Combinatorics CSE235 In particular, for the second element, s2, we now have m − 1 choices. Proceeding in this manner, s3 will have m − 2 choices, Introduction etc. Thus we have Counting PIE m · (m − 1) · (m − 2) ····· (m − (n − 2)) · (m − (n − 1)) Pigeonhole Principle Permutations An alternative way of thinking about this problem is by using Combinations the choose operator: we need to choose n elements from a set Binomial Coefficients of size m for our mapping; Generalizations Algorithms m m! = More n (m − n)!n! Examples 99 / 105 Example: Counting Functions I IV Combinatorics CSE235 Introduction Counting Once we have chosen this set, we now consider all PIE permutations of the mapping, i.e. n! different mappings for this Pigeonhole set. Thus, the number of such mappings is Principle Permutations m! m! Combinations · n! = (m − n)!n! (m − n)! Binomial Coefficients Generalizations Algorithms More Examples 100 / 105 Example: Counting Functions II Combinatorics CSE235 Introduction Counting Recall this question from the midterm exam: PIE Pigeonhole Example Principle Let S = {1, 2, 3},T = {a, b}. How many onto functions are Permutations there mapping S → T ? How many one-to-one (injective) Combinations functions are there mapping T → S? Binomial Coefficients Generalizations See Theorem 1, page 461. Algorithms More Examples 101 / 105 Example: Counting Primes I Combinatorics CSE235 Example Introduction Give an estimate for how many 70 bit primes there are. Counting PIE Recall that the number of primes not more than n is about Pigeonhole Principle n Permutations ln n Combinations Binomial Coefficients See slides on Number Theory, page 33. Generalizations Using this fact, the number of primes not exceeding 270 is Algorithms More 70 Examples 2 ln 270 102 / 105 Example: Counting Primes II Combinatorics CSE235 However, we have over counted—we’ve counted 69-bit, 68-bit, Introduction etc primes as well. Counting The number of primes not exceeding 269 is about PIE Pigeonhole 69 Principle 2 Permutations ln 269 Combinations Binomial Coefficients Thus the difference is Generalizations 70 69 2 2 19 Algorithms − ≈ 1.19896 × 10 ln 270 ln 269 More Examples 103 / 105 Example: More sets I Combinatorics CSE235 Introduction Example Counting How many integers in the range 1 ≤ k ≤ 100 are divisible by 2 PIE or 3? Pigeonhole Principle Permutations Let Combinations A = {x | 1 ≤ x ≤ 100, 2 | x} Binomial Coefficients B = {y | 1 ≤ x ≤ 100, 3 | y} Generalizations Clearly, |A| = 50, |B| = b 100 c = 33, so is it true that Algorithms 3 |A ∪ B| = 50 + 33 = 83? More Examples 104 / 105 Example: More sets II Combinatorics CSE235 Introduction Counting No; we’ve over counted again—any integer divisible by 6 will PIE be in both sets. How much did we over count? Pigeonhole Principle The number of integers between 1 and 100 divisible by 6 is 100 Permutations b 6 c = 16, so the answer to the original question is Combinations Binomial |A ∪ B| = (50 + 33) − 16 = 67 Coefficients Generalizations Algorithms More Examples 105 / 105