CS1800: Permutations & Combinations. Professor Kevin Gold

Similar documents
Well, there are 6 possible pairs: AB, AC, AD, BC, BD, and CD. This is the binomial coefficient s job. The answer we want is abbreviated ( 4

MTH 245: Mathematics for Management, Life, and Social Sciences

Permutations and Combinations

Reading 14 : Counting

CHAPTER 8 Additional Probability Topics

Welcome to Introduction to Probability and Statistics Spring

About Permutations and Combinations: Examples

Coding Theory on the Generalized Towers of Hanoi

LESSON 4 COMBINATIONS

Permutations and Combinations

Permutations and Combinations

COUNTING AND PROBABILITY

CS1800: More Counting. Professor Kevin Gold

Permutations, Combinations and The Binomial Theorem. Unit 9 Chapter 11 in Text Approximately 7 classes

Combinatorial Proofs

Topics to be covered

elements in S. It can tricky counting up the numbers of

Generalized Permutations and The Multinomial Theorem

With Question/Answer Animations. Chapter 6

Mathematics Probability: Combinations

Discrete Mathematics: Logic. Discrete Mathematics: Lecture 15: Counting

Chapter 11, Sets and Counting from Applied Finite Mathematics by Rupinder Sekhon was developed by OpenStax College, licensed by Rice University, and

Permutations and Combinations

In this section, we will learn to. 1. Use the Multiplication Principle for Events. Cheesecake Factory. Outback Steakhouse. P.F. Chang s.

12th Bay Area Mathematical Olympiad

Finite Math - Fall 2016

The Product Rule The Product Rule: A procedure can be broken down into a sequence of two tasks. There are n ways to do the first task and n

UNIT 2. Counting Methods

Date Topic Notes Questions 4-8

COMPSCI 575/MATH 513 Combinatorics and Graph Theory. Lecture #30: The Cycle Index (Tucker Section 9.3) David Mix Barrington 30 November 2016

Introductory Probability

CS100: DISCRETE STRUCTURES. Lecture 8 Counting - CH6

Course Learning Outcomes for Unit V

Elementary Combinatorics

Counting Things. Tom Davis March 17, 2006

W = {Carrie (U)nderwood, Kelly (C)larkson, Chris (D)aughtry, Fantasia (B)arrino, and Clay (A)iken}

Counting. Chapter 6. With Question/Answer Animations

Organization in Mathematics

Discrete Structures Lecture Permutations and Combinations

chapter 2 COMBINATORICS 2.1 Basic Counting Techniques The Rule of Products GOALS WHAT IS COMBINATORICS?

Triangle Similarity Bundle

NINJA CHALLENGE INSTRUCTIONS CONTENTS

Exercises Exercises. 1. List all the permutations of {a, b, c}. 2. How many different permutations are there of the set {a, b, c, d, e, f, g}?

CIS 2033 Lecture 6, Spring 2017

Poker: Probabilities of the Various Hands

Combinational Mathematics - I

Section Summary. Permutations Combinations Combinatorial Proofs

Unit 10 Arcs and Angles of Circles

DISCRETE STRUCTURES COUNTING

Unit 5 Radical Functions & Combinatorics

JIGSAW ACTIVITY, TASK # Make sure your answer in written in the correct order. Highest powers of x should come first, down to the lowest powers.

Solutions of problems for grade R5

6/24/14. The Poker Manipulation. The Counting Principle. MAFS.912.S-IC.1: Understand and evaluate random processes underlying statistical experiments

Math 166: Topics in Contemporary Mathematics II

Permutations And Combinations Questions Answers

Combinatorial Choreography

LAMC Junior Circle February 3, Oleg Gleizer. Warm-up

aabb abab abba baab baba bbaa permutations of these. But, there is a lot of duplicity in this list, each distinct word (such as 6! 3!2!1!

Grade 6 Math Circles March 9, 2011 Combinations

Strings. A string is a list of symbols in a particular order.

Fundamental Counting Principle

Chapter 10A. a) How many labels for Product A are required? Solution: ABC ACB BCA BAC CAB CBA. There are 6 different possible labels.

Permutations and Combinations. Quantitative Aptitude & Business Statistics

n! = n(n 1)(n 2) 3 2 1

Chapter 2. Permutations and Combinations

Solutions to Problem Set 7

10.2.notebook. February 24, A standard deck of 52 playing cards has 4 suits with 13 different cards in each suit.

Discrete Structures for Computer Science

Combinational Mathematics Part 1

Jong C. Park Computer Science Division, KAIST

MATH 2420 Discrete Mathematics Lecture notes

Math 1111 Math Exam Study Guide

Poker: Probabilities of the Various Hands

CS1802 Week 3: Counting Next Week : QUIZ 1 (30 min)

Lecture 18 - Counting

Sec$on Summary. Permutations Combinations Combinatorial Proofs

CS1800: Intro to Probability. Professor Kevin Gold

Counting Methods and Probability

The Fundamental Counting Principle & Permutations

Permutations and Combinations Section

Math 3012 Applied Combinatorics Lecture 2

Section : Combinations and Permutations

MATH CIRCLE, 10/13/2018

Week 1: Probability models and counting

MATH 22. Lecture B: 9/4/2003 COUNTING. I counted two and seventy stenches, All well-defined, and several stinks.

Day 1 Counting Techniques

Objectives: Permutations. Fundamental Counting Principle. Fundamental Counting Principle. Fundamental Counting Principle

Permutations. and. Combinations

PS 3.8 Probability Concepts Permutations & Combinations

POKER (AN INTRODUCTION TO COUNTING)

CISC 1400 Discrete Structures

CPCS 222 Discrete Structures I Counting

Math Steven Noble. November 22nd. Steven Noble Math 3790

Counting (Enumerative Combinatorics) X. Zhang, Fordham Univ.

With Question/Answer Animations. Chapter 6

Problems from 9th edition of Probability and Statistical Inference by Hogg, Tanis and Zimmerman:

Honors Precalculus Chapter 9 Summary Basic Combinatorics

Simple Counting Problems

Learning Objectives for Section 7.4 Permutations and Combinations. 7.4 Permutations and Combinations

3 ky. t x 1) 1/3, -1/2 2) = 140 4) 74 units 5) a) 2400 b) $12 c) 96 students. 6) a) y = 1.10x x b) points c) 1984, 2003

Transcription:

CS1800: Permutations & Combinations Professor Kevin Gold

Permutations A permutation is a reordering of something. In the context of counting, we re interested in the number of ways to rearrange some items. For example, there are 6 ways to rearrange the letters ABC: ABC ACB BAC BCA CAB CBA ABCD ABDC ACBD ACDB ADBC ADCB BACD BADC BCAD BCDA

There Are N! Permutations Of N Things Recall N! ( N factorial) is N * (N-1) * * 3 * 2 * 1 There are N! permutations of N items. You have N choices for the first item, then N-1 for the second (since you can t choose the first item again), then N-2 for the third, and so on until there s only one item left. A C B A B B C A C C B C A B A ABC ACB BAC BCA CAB CBA

3! = 6 4! = 24 5! = 120 6! = 720 7! = 5040 10! = 3,628,800 20! = 2,432,902,008,176,640,000 30! = N! Grows Very Quickly You would not want to write a program that tries all permutations unless the number of inputs is very small. That s because N! grows faster than exponentially Consider any fixed base exponential function, like 100 N. Once N > 100, N! is multiplying * 101 * 102 * 103 The same is true of any b N. N! will eventually surpass it. Age of universe in seconds: 432,000,000,000,000,000 265,252,859,812,191,058,636,308,480,000,000

r-permutations Suppose we are not permuting all N items, but choosing a subset of them and an order for the subset. There are N choices for what goes first, then N-1 for what goes second, down to N-r+1 choices, where r is the number of items. Example: 10 person race, how many ways to have a gold/ silver/bronze finish? 10 choices for gold, then 9 for silver, then 8 for bronze. 10*9*8 = 720. This is called an r-permutation and the particular value is often abbreviated P(n, r) for example, above, P(10,3).

An r-permutation Formula, And Its Issues If we would like to multiply out N*(N-1)* *(N-r+1), we could write this instead as N!/(N-r)! The terms (N-r)*(N-r-1)* *3*2*1 cancel on the top and bottom, leaving N*(N-1)* *(N-r+1) This form N!/(N-r)! looks elegant, but isn t usually what you want to work with When working by hand, you ll cancel all the terms anyway: P(10,3) = 10*9*8*7*6*5*4*3*2*1/7*6*5*4*3*2*1 When working on a computer, trying to compute large factorials may result in overflow - errors resulting from numbers that are too big Plus it s slower to do all that multiplication N*(N-1)* *(N-r+1) has an intuitive meaning and a sanity check: don t multiply more terms than you have items

r-permutation Examples 10 Star Wars movies, how many ways to choose 4 to watch in a marathon and an order to watch them in? 10*9*8*7 = 5040 possible orderings of 4 movies If there are 1000 students in an organization, how many possible tickets for President, VP, and Treasurer? 1000 * 999 * 998 = 997,002,000

Special Roles are Essentially Orderings President VP Treasurer (Alice, Bob, Eve) President VP Treasurer (Bob, Alice, Eve)

Contrasting Permutations With Simple Use of the Product Rule The mathematics of permutations are appropriate when we use up options. If you can pick the same thing over and over, don t decrease the number of options by one; it s not an r- permutation. 8-character passwords in which each character is a lowercase letter: 26 8 The same situation, but we re not allowed to reuse letters: 26*25*24*23*22*21*20*19 = P(26, 8)

Maximum Number of Directed Edges in a Graph is an r-permutation How many possible directed edges are there, assuming no self-loops? To choose a single edge, we would choose from all V vertices, then choose where it goes from the remaining V -1. That s V ( V -1) possibilities in all, or P( V, 2). We can think of the undirected case as dividing this number by 2 to get rid of the 2 directions - or we can think of it as a combination, our next topic 4*3 = 12

Combinations If we are choosing a subset of items, and we only care about what is in the subset and not its order, this is a combination. Some examples: Choosing 4 people from 100 to be on a committee. Choosing 3 movies to watch from 10, but we don t consider different orderings of the same 3 movies to be different. Choosing two vertices in an undirected graph to have an edge (the vertices aren t ordered in an undirected graph) The difference from a permutation is that we do not get to choose an ordering for the items - the order doesn t matter or is chosen for us

Deriving the Formula for Combinations (Part 1) Suppose we just use our tool for counting r-permutations to count combinations. Ways to choose 3 items from 4, with ordering: 4*3*2 = 24 But if we list all r-permutations, we will list the same set repeatedly. In fact, we ll list it once for every way its items can be ordered. With combinations, we just want the number of sets. If items are {A, B, C, D}: ABC ACB BAC BCA CAB CBA <= All {A,B,C} ABD ADB BAD BDA DAB DBA <= All {A,B,D} ACD ADC CAD CDA DAC DCA <= All {A,C,D} BCD BDC CBD CDB DBC DCB <= All {B,C,D}

Deriving the Formula for Combinations (Part 2) We saw that if we try to count combinations of 3 items from 4 using r-permutations, we overcount by a factor of 6. The fact that we re overcounting by a factor of 6 stems from the fact that there are 3! = 6 possible permutations of each set of 3 items, and we will list them all instead of counting them once We can fix this by dividing by 6 to get the true count: 4*3*2/6 = 4 In general, if we try to count combinations (sets where order doesn t matter) by counting r-permutations (tuples where order does matter), we will overcount by a factor of r!, the number of ways to reorder the chosen items. So our strategy is to use the r-permutation formula but divide by r!

The Combinations Formula The number of ways to choose a subset of r items n from n items can be written C(n,r) or ( r ) ( n choose r ) and is equal to n! or P(n,r) (n-r)!r! r! It s the r-permutation formula divided by r!, the number of ways to rearrange the r chosen items; this is the factor by which using r-permutations would overcount For example, C(10, 3) = 10!/(3!7!) = 10*9*8/3*2*1 = 120

Illustrating C(4,2) Four items {A, B, C, D}, choosing a subset of 2 The r-permutations of 2 Rearrange to illustrate the items are: repeated items: AB CA AB BA AC CB AC CA AD CD 4*3 = 12 AD DA 12/2! = 6 BA DA BC CB BC DB BD DB BD DC CD DC

Combinations Examples Choosing 4 people from 100 to be on a committee: C(100,4) = 100*99*98*97/4*3*2*1 = 3921225 Possible 5-card hands to be dealt from 52 card deck (order of the hand doesn t matter): C(52, 5) = 52*51*50*49*48/5*4*3*2*1 = 2598960 Number of 2-card hands that are both Jacks or better (16 cards like that in deck): C(16,2) = 16*15/2 = 120

Number of Edges in a Complete Undirected Graph We choose a subset of 2 distinct vertices to choose an edge That is a choice of 2 items from V : C( V, 2) Which is the formula we saw before, V ( V -1)/2

Number of Triangles in a Complete Undirected Graph We choose a subset of 3 distinct vertices to choose an a triangle That is a choice of 3 items from V : C( V, 3) So V ( V -1)( V -2)/6 C(4,3) = 4

Combinations and Order Doesn t Matter Sometimes combinations are said to be used when order doesn t matter for the items. Pick 3 movies from 10, only care about the set and not the order: C(10,3) Pick 3 movies and an ordering for them: P(10,3) But be careful - if an ordering is forced on us, we re essentially only picking the set and not the ordering. And then we still use combinations. 10 movies, choose 3 to watch in chronological order: C(10, 3) = 10*9*8/6 = 120.

Choosing What s In, Choosing What s Out Sometimes it s easier to think about choosing what won t go into your subset. Choosing who s not in the club is the same as choosing who s in it. This is reflected in the math of combinations. n ( ) = ( ). (C(n,r) = C(n,n-r)) r n n-r The number of ways to choose items that are included, is equal to the number of ways to choose what is not included. In our earlier example, we showed C(4,3) = 4. This is obvious if we think of choosing 3 items as choosing which item to exclude. _ Choose 2 or choose 3: C(5,2)=C(5,3)=10

r-permutations versus Combinations A C E D B A C E D B ABE BEA AEB EAB BAE EBA A B D A B E P(5,3) orderings of size 3 C(5,3) subsets of size 3

r-permutations versus Combinations If r-permutations are choosing orderings and combinations are choosing subsets, which one is appropriate? And what s the calculation? Number of ways to assign five roles - captain, first mate, science officer, communications officer, ensign - among 200 people Number of 8-bit strings like 00001111 that have exactly 4 bits set to 1 Number of iterations necessary for a brute force sorting algorithm that tries all possible orderings of the data Number of racks possible in a Scrabble-like game where you draw 7 tiles from 60, assuming each tile is unique and you can rearrange your rack at will

r-permutations versus Combinations If r-permutations are choosing orderings and combinations are choosing subsets, which one is appropriate? And what s the calculation? Number of ways to assign five roles - captain, first mate, science officer, communications officer, ensign - among 200 people P(200,5) Number of 8-bit strings like 00001111 that have exactly 4 bits set to 1 C(8,4) Number of iterations necessary for a brute force sorting algorithm that tries all possible orderings of the data N! Number of racks possible in a Scrabble-like game where you draw 7 tiles from 60, assuming each tile is unique and you can rearrange your rack at will C(60, 7)

Two-Step Problems Sometimes we re trying to count something complicated, which would be chosen over a series of steps. When that happens, break the problem down into steps, and multiply the choices you had at each step. Try to have a clear idea of the situation after each choice is made. Did you eliminate possibilities with your earlier choices?

Two-Step Problems I have 250 students, and we re going to choose two quiz bowl type teams with 5 people each - a red team and a blue team. How many ways are there to choose the red team and the blue team (assume it s different if your team is a different color)?

Two Team Solution With Team Names, and a Problem of Symmetry I have 250 students, and we re going to choose two quiz bowl type teams with 5 people each - a red team and a blue team. How many ways are there to choose the red team and the blue team (assume it s different if your team is a different color)? We choose 5 from the 250, and then we ll have 245 left from which to choose 5 more. C(250, 5) * C(245, 5) = 7817031300*7060140549 5.5x10 19 Now suppose I didn t care who was red and who was blue - if I swap team colors, it counts as the same teams. How does that affect the number of different teams?

Two Team Solution With Team Names, and a Problem of Symmetry I have 250 students, and we re going to choose two quiz bowl type teams with 5 people each - a red team and a blue team. How many ways are there to choose the red team and the blue team (assume it s different if your team is a different color)? We choose 5 from the 250, and then we ll have 245 left from which to choose 5 more. C(250, 5) * C(245, 5) = 7817031300*7060140549 5.5x10 19 Now suppose I didn t care who was red and who was blue - if I swap team colors, it counts as the same teams. How does that affect the number of different teams? Divide by two

Combinations and the Power Set Recall that the power set is the number of subsets of any size, while for a combination, we re looking at the number of subsets of a particular size. That suggests these values should be related - and they are. In general: N N N 0 1 N ( ) + ( ) + + ( ) = 2 N Because the total number of subsets is equal to the number of subsets of size 0, plus the number of subsets of size 1, plus the number of subsets of size 2

A Power Set Problem My neighbors held a costume contest for 10 people in which they would award a first place, a second place, and then some arbitrary number of honorable mentions (maybe none, maybe everybody who didn t win first or 2nd). How many results were possible for this contest?

A Power Set Problem My neighbors held a costume contest for 10 people in which they would award a first place, a second place, and then some arbitrary number of honorable mentions (maybe none, maybe everybody who didn t win first or 2nd). How many results were possible for this contest? The long way:10*9*(c(8,0)+c(8,1)+c(8,2)+ +C(8,8)) = 10*9*(1 + 8 + 28 + 56 + 70 + 56 + 28 + 8 + 1) = 23040 Recognizing this is a power set: 10*9*2 8 = 23040

Combinations and Permutations in Multistep Problems Some more complex counting problems might include the following: I want to make a playlist of 5 Beatles songs (out of 10 in my library) and 7 Pomplamousse songs (out of 13 in my library). I don t want any repeat songs. How many playlists are possible? This is both a permutation and a combination problem. I first need to choose where in the playlist the different kinds of songs will go, Beatles versus Pomplamousse. Choosing where is a combination. It s a subset of places. Then I need to choose the songs and orderings for each artist. That s an r-permutation in each case.

Solving the Playlist Problem: A Combination for Special Locations Recall: playlist of 5 out of 10 Beatles, 7 out of 13 Pomplamousse, no repeats So I have 5+7=12 slots in the playlist to fill, to start. I need to break the construction down into a series of decisions. My first will be, where will the Beatles songs go? That s a choice of a set of slots in the playlist - it doesn t make sense to choose their order. C(12, 5)

Solving the Playlist Problem: r-permutations to fill in the rest Recall: playlist of 5 out of 10 Beatles songs, 7 out of 13 Pomplamousse, no repeats I don t need to also choose the Pomplamousse song locations. That s implicit from the Beatles locations; nothing left to choose there So now I can pick what the Beatles songs are. Order matters for a playlist, so it s an r-permutation. DP _ SFF _ HJ IatW HS _ P(10,5) Then I can pick what the Pomplamousse songs are, filling in the rest of the blanks. P(13, 7) The total number of possibilities is the product of the number of options I had at each juncture. C(12, 5)*P(10,5)*P(13,7) = 792*30240*8648640 2x10 14

Anagrams How many different ways are there to rearrange N letters? If they re all different, N! But if there are repeated letters, that would overcount. MOON with the middle two letters swapped doesn t result in a different word. We can again take a multistep approach. Where will the O s go? _ O _ O C(4,2) = 6 Fill in the rest of the letters: P(2, 2) = 2 Total possibilities, 6 * 2 = 12. Another option would have been to realize that listing all permutations would list each pattern twice, once for each ordering of the O s. 4!/2 = 12.

Anagram With Multiple Doubled Letters Anagrams for the XYZZY, an award for interactive fiction: Choose where the Y s go: C(5,2) Y Y _ Choose where the Z s go: C(3,2) Y Z _ Y Z Choose where the X goes: no choice! Y Z X Y Z C(5,2) * C(3,2) = 10*3 = 30.

Pascal s Triangle Pascal s Triangle is an interesting alternate way to generate the combination numbers, especially if you need a lot of them for the same n. Start with a 1 at the top, then 1 1. Then for every row after, start and end with a 1, and otherwise sum the two numbers above it in the triangle. n Row n, entry r will be ( r ), if you count the first row as n=0 and the first entry of the row as r=0. C(4,2) = 6 C(5,2) = 10 etc

Why Should That Work? To really prove this, we d need induction, the tool for showing that a pattern keeps working But intuitively, there are two ways to get C(n,r): Use the last item added and then r-1 items from the previous n-1 items. C(n-1,r-1) Use only items from the previous n-1 items. C(n-1, r) Thus C(n,r) = C(n-1, r-1) + C(n-1,r) and that is how Pascal s triangle is constructed. C(4,2) = C(3,1) + C(3,2) ABC ABC D + ABC ABC ABC ABC 3 + 3 6 1 1 1 1 2 1 1 3 3 1 1 4 6 4 1

The Quincunx/Galton Board Suppose we arrange a board with a pyramid of pins in it arranged just right, and drop a ball on the top pin It has a 50-50 chance of going left or right, and at the next level, it will have another 50-50 chance of going left or right where it lands The number of balls in each bin at the bottom will be proportional to the number of paths to that bin To land in bin r, the ball needs to bounce left (n-r) times and bounce right r times The number of paths to a particular bin is therefore C(n,r) - the thing we are choosing is when we bounce right The overall effect is a distribution at the bottom proportional to a row of Pascal s triangle! https://www.youtube.com/watch?v=ausktk9enzg @1:00, 3:00

The Binomial Theorem (x + y) 2 = x 2 + 2xy + y 2 (x + y) 3 = x 3 + 3x 2 y + 3xy 2 + y 3 (x + y) 4 = x 4 + 4x 3 y + 6x 2 y 2 + 4xy 3 + y 4 (x + y) 5 = x 5 + 5x 4 y + 10x 3 y 2 + 10x 2 y 3 + 5xy 4 + y 5 Where have we seen these coefficients before?

The Binomial Theorem The Binomial Theorem states that (x + y) n = Σ( )x n-j y j n j=0 n j We could rephrase this as stating that the coefficients are equal to the nth row of Pascal s triangle, where item j is the coefficient ( ) Sometimes these numbers are called binomial coefficients for this reason Why should this work? n j

Binomial Theorem Explained When multiplying (x+y)(x+y) (x+y), we generate all possible terms that result from choosing x or y from each (x+y). Example: (x+y)(x+y)(x+y) = x*x*x + x*x*y + x*y*x + x*y*y + y*x*x + y*x*y + y*y*x + y*y*y = x 3 + 3x 2 y + 3xy 2 + y 3 Each term is a way to choose a subset of terms to be y instead of x, ranging from none of them to all of them The coefficient of x n-j y j is the number of terms in the expansion that have j y s - and this is equal to the number of ways to choose j of n terms to get a y from. Above, for example, there are C(3,1) ways to choose one term to contribute a y: x*x*y, x*y*x, y*x*x

Binomial Theorem Explained Another example with (x+y) 4 : (x+y)(x+y)(x+y)(x+y) = Terms from choosing 0 y s: x*x*x*x = x 4 Terms from choosing 1 y: x*x*x*y + + y*x*x*x = 4x 3 y Choosing 2 y s: x*x*y*y + + y*y*x*x = C(4,2)x 2 y 2 = 6x 2 y 2 Choosing 3 y s: x*y*y*y + + y*y*y*x = 4xy 3 Choosing 4 y s: y*y*y*y = y 4 (x+y) 4 = x 4 + 4x 3 y + 6x 2 y 2 + 4xy 3 + y 4 = C(4,0)x 4 + C(4,1)x 3 y + C(4,2)x 2 y 2 + C(4,3)xy 3 + C(4,4)y 4

A minor point about tricky binomial theorem problems If multiplying a term that already has coefficients, like (2x + 3y) 3, just keep in mind that an existing coefficient will be raised to the same power as its variable, then multiplied by the binomial coefficient. (2x + 3y) 3 - Pascal s triangle is 1, 3, 3, 1 1*(2x) 3 + 3*(2x) 2 (3y) + 3*(2x)(3y) 2 + 1*(3y) 3 = 8x 3 + 36x 2 y + 54xy 2 + 27y 3

Summary Use r-permutations P(n,r) when choosing an ordering for a subset of items. N*(N-1)* *(N-r+1) n Use combinations C(n,r) = ( ) when choosing a subset without r ordering it. Use neither - just the product rule - if you re not eliminating an option by picking it (for example, with passwords with reusable letters) Break multistep problems into steps, and multiply the number of possibilities for each step. Combinations come up in the binomial theorem because multiplying out (x+y) n results in all possible ways to choose 0, 1, 2, 3,, n terms to be y s.