Arrangements and Selections (and how they may be counted)
Running our 'megawin.py' demo Last time we described the idea for a Python program that would compute the probability of winning the MegaMillions lottery jackpot It was based on a 'recursive' implementation for the function that computes binomial coefficients 0, if n<0 or k<0 or n<k b(n;k) = 1, if n==0 or k==0 or n==k b(n-1;k-1) + b(n-1;k), otherwise {
Let's take time now to execute this demo $ python megawin.py _______________________________________ NOTE: We can use the 'time' command to find out how long it takes to execute this program: $ time python megawin.py
Output from 'megawin.py'
has the potential for faster execution Can we get 'speedup'? A formula for evaluating binomial coefficients without resort to recursive function-calls has the potential for faster execution
Special case: binomial(56;5) Let's first consider counting all arrangements of 5 integers selected from { 1, 2, 3, ... , 56 } By an 'arrangement' we mean a way of listing the five selected numbers in some definite order, without repetitions or omissions Example-selection: { 1, 2, 3, 4, 5 } Then two possible arrangements are: ( 5, 4, 3, 2, 1 ) and ( 2, 4, 1, 3, 5 )
The number of arrangements There is more than one way to organize the counting of all the arrangements of 5 integers selected from the set { 1, 2, 3, ... , 56 } Method #1: we make the ordered list directly Method #2: we first select the 5-member subset, then we arrange its members in an ordered list
Method #1 ( __, __, __, __, __ ) 56 ways to choose the 1st integer 55 ways to choose the 2nd integer 54 ways to choose the 3rd number 53 ways to choose the 4th number 52 ways to choose the 5th number By FCP the total number of arrangements will be 56 x 55 x 54 x 53 x 52
Method #2 ( __, __, __, __, __ ) First select the 5-member subset b(56;5) represents the number of ways to do it Then arrange these selected integers 5 x 4 x 3 x 2 x 1 = number of ways to do that There are b(56;5) x 5! ways to do both tasks
The two answers must be equal b(56;5) x 5x4x3x2x1 = 56x55x54x53x52 (method #2 answer) (method #1 answer) From this equality we can infer that b(56;5) = 56x55x54x53x52 / 5x4x3x2x1 Or more simply: b(56;5) = 56! / (5! x 51!)
General formula for b(n;k) The same ideas we just employed lead us to a general formula for binomial coefficients: b(n;k) = n! / k! (n-k)! So this is the number of ways of choosing k integers from a set containing n integers (i.e., the number of k-element subsets of {1,...,n}
Applying formula in Python program def binomial( int n, int k ): num = 1 den = 1 for j in range( n ): num *= (n – j) den *= (k – j) return num / den _______________________________________ (Trouble if n or k is negative or zero -- or equal)
Our Python code
Output from 'megawin2.py' So this revised version of our 'megawin' program ran more than 250 times faster!
Poker hands In the game of 'poker' a player is dealt 5 cards selected from a 'shuffled' deck of 52 cards How many different poker hands are possible? The answer is another binomial coefficient: b(52;5) = 52x51x50x49x48 / 5x4x3x2x1
Evaluating without a computer Look for 'common factors' that can be cancelled: (52)(51)(50)(49)(48) ((13)(4))((17)(3))((10)(5))(49)((24)(2)) ---------------------------- = -------------------------------------------------- (5)(4)(3)(2)(1) (5)(4)(3)(2)(1) = (13)(17)(10)(49)(24) = (221)(10)(1176) = 2598960 So more than two-and-a-half million different poker hands are possible
Royal Flush The highest ranking poker hand is one with 'ace', 'king', 'queen', 'jack', 'ten' in a single suit (i.e., 'hearts', 'spades', 'diamonds' or 'clubs') What are your chances of being dealt a 'Royal Flush'? Answer: 4 chances in 2598960 (or 1 chance in 649740)
One Pair This is a poker hand in which exactly two cards have the same face-value (i.e., a 'pair') while the other three cards have three other face-values How likely is this type of poker hand? Answer: This probability is given by b(13;1) x b(4;2) x b(12;3) x b(4;1)3 / b(52;5)
Personal Identification Numbers Suppose you are asked to select a PIN that consists of 4 different uppercase letters How many choices do you have? Answer: b(26;4) x 4! (= 26! / 22!) If you choose a PIN at random, what are your chances of including just one vowel (A,E,I,O,U)?
Solution #1 ( __, __, __, __ ) First choose your vowel: b(5;1) ways to do it Next choose 3 consonants: b(21;3) ways to do it Then decide their ordering: 4! ways to do it Apply the Fundamental Counting Principle probability = b(5;1)*b(21;3)*4! / b(26;4)*4!
Solution #2 ( __, __, __, __ ) First decide where your vowel goes: b(4;1) ways Then decide which vowel to use: b(5;1) ways Now pick your consonants, in left-to-right order: 21 choices for 1st consonant 20 choices for 2nd consonant 19 choices for 3rd consonant probability = 4x5x21x20x19 / 26x25x24x23
Are these answers equal? Answer #1: b(5;1)xb(21;3)x4! / b(26;4)x4! Answer #2: 4x5x21x20x19 / 26x25x24x23
Successive repetition If you toss a coin n times, what are the chances of getting the same side twice in succession? Answer: 2 chances in 2n why? HINT: It's often easier to count outcomes in the complementary event