CS1800: More Counting Professor Kevin Gold
Today Dealing with illegal values Avoiding overcounting Balls-in-bins, or, allocating resources Review problems
Dealing with Illegal Values Password systems often have rules such as must have at least one special character We could think of this as a union of many sets (passwords with exactly one s.c., exactly two, ) but this turns out to be both tricky and cumbersome It s often better to find the size of the set of values that aren t valid, and subtract: A - B = A - B
Example of Subtracting Illegal Values Suppose our passwords consist of 8 letters, either uppercase or lowercase (52 possibilities). If the password must contain at least one uppercase letter, how many possible passwords are there? All passwords of 8 uc/lc characters: 52 8 All passwords with no uppercase letters: 26 8 Total possibilities: 52 8-26 8 All passwords no uc
Multiple Categories of Illegal Values Often there s more than one rule about what is illegal, and an item could be excluded for multiple reasons. Inclusion-Exclusion ( A U B = A + B - A B etc) is likely to come into play in these cases. Suppose we re counting 8-character passwords of letters (26*2 = 52), digits (10), and special characters (12) with at least 1 digit and 1 special character (repetition allowed) We want all passwords - bad passwords = all passwords - no digits U no special characters all passwords - ( no digits + no special - no digits & no special ) (52+10+12) 8 - ((52+12) 8 + (52+10) 8-52 8 )
A Tempting But Incorrect Approach to At Least One There s a strategy we often see students try to apply in this course that is incorrect. We ll illustrate on a small problem: How many 3-digit PIN numbers have at least one even digit? Strategy: Choose where the even digit goes: 3 ways. _ 5 possibilities for the even digit. 4 All the other numbers can be anything. 10 each. 4 3 4 3 * 5 * 10 * 10 = 1500 possibilities. Uh oh. That s more than 1000. But suppose we just subtract all PINs - all odd = 1000-5 3 = 875. This turns out to be correct but what went wrong the other way?
Be Careful Not to Choose Schemes that Double-Count Suppose we actually drew out the possibilities for the method we outlined in the previous slide. In one branch of our tree, we can choose: Pick first number even, pick even number = 4, pick 2nd=3, pick 3rd=4. 4 3 4 And in another branch of our tree, we can choose: Pick last number is even, pick even=4, pick 1st=4, pick 2nd=3. 4 3 4 These aren t different outcomes; they only differ in which digit we think is special. We have overcounted this number.
Be Careful Not to Choose Schemes that Double-Count Which place even? 1st even 2nd even 3rd even Even value? 4 Other value #1? 3 4 4 4 3 Other value #2? 4 3 4 [most of choice tree is abridged] 4 3 4 The method of multiplying choices assumes that different choice sequences lead to different outcomes. If that s not true, you need to correct for the overcounting or use a different method.
A Somewhat Messy Fix A fix for the method we just presented that didn t work is to use Inclusion-Exclusion. Our method essentially tried to count the union of three sets: first digit even, second digit even, third digit even. Those were the three initial options in the tree of choices. But as we just noted, these sets can overlap. In fact, all three can overlap (222, 224, ). EE E _ E EEE _EE E_E E E = even _ = other digit
A Somewhat Messy Fix Let A, B, C, be the sets where the first, second, or third digit is known to be even. Each of these has 5*10*10 possibilities (because 5 even digits). But they overlap. Each intersection like A B has two even digits, so 5*5*10 possibilities. A B C has three even digits; 5*5*5. AUBUC = A + B + C - A B - A C - B C + A B C = 5*10*10 + 10*5*10 + 10*10*5-5*5*10-5*10*5-10*5*5 + 5*5*5 = 500 + 500 + 500-250 - 250-250 + 125 = 875. It s not easier than the first method we saw, but it is correct! You ll never need to use 4+ set Inclusion-Exclusion in this course. If it comes to that, try subtracting the illegal set size instead.
Dealing With Overcounting Due to Symmetry There s going to be a dinner party of 8 guests sitting around a round table. How many ways are there to seat everyone around the table, assuming that we don t consider rotations of the table different? A G H B F H G C = E A F E D D C B
Dealing With Overcounting Due to Symmetry We can think of this as a permutation problem to start. There are 8! possible strings ABCDEFGH. Now we can mentally rotate through the equivalent positions to that. How many are there? One for each position for A; 8. G H F A E B D C ABCDEFGH H A Divide by this number, because every ordering will have this same issue. 8!/8 = 7!. B HABCDEFG
Dealing With Overcounting Due to Symmetry Another approach is to anchor the problem so that a choice that shouldn t matter is arbitrarily fixed. Imagine we tell A to just sit down somewhere; we ll think of how everyone will sit in relation to A. Now every possibility is genuinely different. There are 7! ways to rearrange the remaining guests, once we think of wherever A sits as the head of the table. A _ A _
Dealing With Overcounting Due to Symmetry If we also don t care about the difference between a seating arrangement and its mirror image, how many possibilities do we have? H A B B A H G C = C G F E D D E F
Dealing With Overcounting Due to Symmetry If we also don t care about the difference between a seating arrangement and its mirror image, how many possibilities do we have? Flipping the items to go clockwise instead of counterclockwise is just one counterpart for each ordering, if we also don t care about rotation. We divide the previous answer by two: 7!/2 H A B B A H G C = C G F E D D E F
Practicing a Choice of Approach: Counting Diagonals Let a diagonal be a line from a corner of an n-sided polygon to a non-adjacent corner. How many of these are there in general? The two diagonals from this corner
Practicing a Choice of Approach: Counting Diagonals One approach to this problem is a good - bad approach. There are C(N,2) = N(N-1)/2 ways to choose two corners. But N of these are bad, because they re actually sides. So we could say N(N-1)/2 - N.
Practicing a Choice of Approach: Counting Diagonals Another approach could be to think about our individual choices. N choices for our starting vertex, then I can pick any vertex that isn t adjacent. N(N-3) possibilities. Am I done, or have I overcounted?
Practicing a Choice of Approach: Counting Diagonals Another approach could be to think about our individual choices. N choices for our starting vertex, then I can pick any vertex that isn t adjacent. N(N-3) possibilities. I have overcounted, because I could choose vertex A then B, or B then A, and that would be the same diagonal. N(N-3)/2. This is the same result as the other approach; N(N-1)/2 - N = (N 2 - N)/2-2N/2 = (N 2-3N)/2 = N(N-3)/2.
Considering Resources to be Interchangeable: Balls-in-Bins Intro There are a variety of problems in which there s some interchangeable resource, like money or points, that is being allocated (handed out) You have 3 $20 bills to hand out to ten people, possibly handing more than one to the same person. How many ways are there to do that? Suppose you said 10 3 for the problem above, because we could encode the allocation of as a string like (Bob, Bob, Alice). 10*10*10 possibilities. But if that s the same result as (Alice, Bob, Bob), we ve overcounted. How do we handle this kind of problem?
Deriving Balls-in-Bins Let s solve an easier problem to illustrate. How many ways can I put 3 balls in 5 bins, if I don t consider the balls different? Thus the counts are all that matters, like (0,2,0,0,1) Imagine the setup for one of these allocations.
Deriving Balls-in-Bins We re going to try to turn this setup into a string representation that we can easily count. First, the bottom isn t doing anything useful.
Deriving Balls-in-Bins Next, we re going to drop the outer sides. I ll still be able to tell there s nothing in the leftmost bin, and a ball in the rightmost bin.
Deriving Balls-in-Bins Now I can change this to a string. I ll write for a divider and O for a ball. Notice that the number of dividers is one less than the original number of bins. I don t really need to record empty spaces. I can still tell when nothing is in a bin. Every character will be either a ball or a divider. OO O
Deriving Balls-in-Bins If you think about it, these strings are different exactly when the allocation to bins is different. OO O OO O
Deriving Balls-in-Bins If there s exactly one of these strings for each allocation, then we just need to count the number of strings with a valid number of balls and bin dividers. We can rephrase the problem as, How many strings have exactly 3 O s and 4 characters? We ve reduced the problem to one we can solve! How many strings do have 3 O s and 4 characters? OO O OO O
Deriving Balls-in-Bins The answer was that there are C(7,3) = 35 ways of choosing 3 characters to be O, and the rest. Equivalently, there are 4 ways to choose which characters are the. That s C(7,4) = C(7,3). Recall that these two ways of counting will give the same answer. OO O OO O Choose 3 characters to be O (or 4 to be )
The Balls-in-Bins Equation (Sometimes Called Stars and Bars ) If you re trying to find the number of ways you could distribute N indistinguishable items to R receptacles, the answer is C(N+R-1, N). The first number is the number of characters in the encoding - balls (N) plus bin dividers (R-1). The second number is the number of balls (N). You can equivalently choose where the bin dividers go: C(N+R-1, R-1).
Back to the First Example You have 3 $20 bills to hand out to 20 people any way you want, as long as each bill is given out. How many ways are there to do that? Here, the people are the bins - we care about differences between people - and the $20 bills are the balls - we don t care about differences between $20 bills. 19 dividers, 3 bills. C(22,3) = 1540. OO O A string that could encode $40 and $20 for persons 2 and 4
Slightly More Abstract Balls and Bins You re planning on buying 6 bags of Halloween candy, and there are 5 different brands at the store. How many ways are there to buy 6 bags? We could give a particular solution as a tuple of counts, like (3, 2, 1, 0, 0), where each count corresponds to a different candy brand. We should count the number of such tuples that add to 6. That makes this a balls-in-bins problem. You can think of the balls here as your $5 bills (the price) - indistinguishable - and the bins as the candy manufacturers. 6 balls, 4 dividers = C(6+4,4) = 10*9*8*7/(4*3*2) = 210
Minimum Allocations I m dividing $5000 among 3 people, in increments of $1000, but I also need to give everyone at least $1000. What should I do about that?
Minimum Allocations I m dividing $5000 among 3 people, in increments of $1000, but I also need to give everyone at least $1000. What should I do about that? Essentially, $3000 is spoken for, leaving only $2000 to divide up. I can just count the number of ways to distribute that. 3 people = 2 bins, 2 balls of $1000 each. C(4,2) = 6 ways to do it. OO O O O O OO O O OO (3,1,1) (2,2,1) (2,1,2) (1,3,1) (1,2,2) (1,1,3)
Trying to Decide How To Divide Up a Plane I have 30 rows of an airplane I m designing, and I m trying to decide how many of them should be first class, economy plus, or economy. Assuming these sections are always in the same order on the plane, and just differ in their number of rows (which can be 0), how many ways to do this?
Noticing Similarities to Our Encoding I have 30 rows of an airplane I m designing, and I m trying to decide how many of them should be first class, economy plus, or economy. How many ways to do this? This is an instance where the problem should just directly remind us of our balls-in-bins encoding, even if thinking of rows as balls (etc) is a little weird. 2 dividers and 30 rows: C(32,2) = 496 OOOO O
Practice Balls-in-Bins Suppose there s a video game where, every time a player gets an experience point, there are 3 scores that can be improved with it; so after 10 such points, the player might have Strength 3, Trapmaking 3, and Survival 4. How many possible builds (allocations of points) are there with 10 points?
Practice Balls-in-Bins Suppose there s a video game where, every time a player gets an experience point, there are 3 scores that can be improved with it; so after 10 such points, the player might have Strength 3, Trapmaking 3, and Survival 4. How many possible builds are there with 10 points? The scores to improve are the 3 bins; so 2 bin dividers. C(10+2, 2) = C(12,2) = 12*11/2 = 66.
A Mix of Practice Problems How many 6-character passwords have exactly 2 digits, 2 letters (lower or upper case), and 2 special characters (12 possible)? How many possible deals are there of 2 cards to each of 4 players, if I don t care about order in the hand, but I do care who got what cards? What if I don t care who got what cards? If I flip a coin ten times, how many possibilities have exactly the same number of heads and tails? How many don t have the same number? How many of those have more heads than tails? How many ways are there to interleave ABCDE and 12345 while keeping the letters in order and the numbers in order?
A Mix of Practice Problems How many 6-character passwords have exactly 2 digits, 2 letters (lower or upper case), and 2 special characters (12 possible)? Hint: Choose where everything goes, then fill things in. How many possible deals are there of 2 cards to each of 4 players, if I don t care about order in the hand, but I do care who got what cards? What if I don t care who got what cards? Hint (2nd part): How many ways could you rearrange the same 4 hands? If I flip a coin ten times, how many possibilities have exactly the same number of heads and tails? How many don t have the same number? How many of those have more heads than tails? Hint: Choose where the heads are in the sequence; then, think about how to avoid doing anything tedious. How many ways are there to interleave ABCDE and 12345 while keeping the letters in order and the numbers in order? Hint: After your first choice, what s left to choose?
The Password Problem How many 6-character passwords have exactly 2 digits, 2 letters (lower or upper case), and 2 special characters (12 possible)? Choose where digits go: C(6,2) Choose where letters go: C(4,2) Nothing left to choose for character positions Choose the digits: 10*10 Choose the letters: 52*52 Choose the special characters: 12*12 C(6,2)*C(4,2)*10 2 *52 2 *12 2
The Card Problem How many possible deals are there of 2 cards to each of 4 players, if I don t care about order in the hand, but I do care who got what cards? What if I don t care who got what cards? Choose the 4 hands: C(52,2)*C(50,2)*C(48,2)*C(46,2) (other strategies are possible) Don t care about who got them: Divide by 4!
The Coin Problem If I flip a coin ten times, how many possibilities have exactly the same number of heads and tails? How many don t have the same number? How many of those have more heads than tails? Think of the flips as encoded as a string, like TTTTTHHHHH There are C(10,5) ways to pick which ones are heads, if we have a tie (so exactly 5 heads) Since there are 2 10 possible flips in general, there are 2 10 - C(10,5) other possibilities By symmetry, half of those possibilities have heads ahead
The Interleaving Problem How many ways are there to interleave ABCDE and 12345 while keeping the letters in order and the numbers in order? Choose where the letters go among the 10 characters: C(10,5) The order of both sequences is determined, so there s nothing left to choose.