CS231 Algorithms Handout #8 Prof Lyn Turbak September 21, 2001 Wellesley College PROBLEM SET 2 Due: Friday, September 28 Reading: CLRS Chapter 5 & Appendix C; CLR Sections 6.1, 6.2, 6.3, & 6.6; Suggested Problems: CLRS C.1-1, C.1-2,C.1-3,C.1-4; C.2-2, C.2-3,C.2-10; C.3-1, C.3-2, C.3-3; 5.4-2, CLR 6.1-1, 6.1-2, 6.1-3, 6.1-4; 6.2-2, 6.2-3, 6.2-4, 6.2-11; 6.3-1, 6.3-2, 6.3-3 ; 6.6-1; 6-2; Problem 1: Let Me Count the Ways... [30] In the following parts,. please show how you derive your answers: a [5] How many ways can 12 students be assigned to the 15 computers in E101? (Assume each student is assigned to one computer.) b [5] Before we started running out of telephone numbers and changes were made to the system, it used to be that a long distance phone number was an area code followed by a 7-digit local number. An area code was a 3-digit number whose middle digit had to be a 0 or 1, whose first digit could not be 0 or 1, and whose last two digits could not be 11. The first 3 digits of the 7- digit local number could not be an area code. Under this scheme, how many long-distance phone numbers were there? c [10] How many terms are there in the expansion of (w + x + y + z) 7? Each term has the form cw e1 x e2 y e3 z e4, where e1 + e2 + e3 + e4 = 7. d [10] How many ways can 100 students be scheduled into 5 different writing classes so that each class has exactly 20 students and each student takes one class? Problem 2: The Infamous Monty Hall Problem [15] Do CLRS C.2-9, p. 1106 (CLR Exercise 6.2-10, p. 110). Justify your answer with a probability tree. It is important to note that the emcee knows where the prize is hidden, and so will always be able to reveal an empty stage behind one of the curtains you have not picked. (This is a classic problem that got national attention a few years back when it appeared in a popular column by Marilyn vos Savant. Many readers of her column, including those with math PhDs, made fools of themselves by adamantly arguing the wrong answer.) 1
Problem 3: Randomizing Arrays [15] Many array manipulation algorithms assume that the elements of an input array are randomly permuted. An array of n distinct items is randomly permuted if each of the n! permutations is equally likely. In many cases, the array input to such an algorithm is the output of another process that is decidedly non-random. So it is desirable to find an efficient way to randomly permute a given array. Suppose that: A is a length n array containing all the integers from 1 to n. swap(a, i, j) swaps the values in A[i] and A[j]. random(x, y) is a routine that returns a random integer between x and y, inclusive. For each of the following algorithms, indicate whether the algorithm randomly permutes the input array A. Justify your answer in each case. (Hint: try the algorithms for n = 3, and generalize.) a [5] b [5] c [5] for i <- 1 to n do A[i] <- random(1, n) for i <- 1 to n do swap(a, i, random(1, n)) for i <- 1 to n do swap(a, i, random(i, n)) Problem 4: Coin Flipping [25] We all know that a flip of a fair coin can be used to choose one of two possibilities with equal probability. It is not hard to see that a n flips of a fair coin can choose one of 2 n possibilities with equal probability. But did you know that a fair coin can be used to choose among m possibilities, where m is not a power of 2? In this problem, you will explore the case of m = 3, but the strategy generalizes to any m. Suppose you are given a black-box procedure Flip() that returns one of the symbols H or T, each with a probability of 1/2. a [10] Using Flip(), write pseudocode for a procedure One-Third() that returns one of the symbols A, B, or C, each with a probability of 1/3. (Hints: (1) think of the papertearing exercise from class and (2) make One-Third() recursive!) b [5] Demonstrate that your One-Third() procedure from part a is correct by showing that the probability of returning A is 1/3. (Presumably, the argument for B and C will be similar, so you need not show it.) c [10] Determine the expected number of times that your One-Third() procedure will call Flip() before returning a result. 2
Problem 5: Poker Time! [15] There are many variants of poker; here we describe the form most commonly played on video poker machines. A poker hand is a collection of five cards. We will write cards as a value (2 through 10, J (jack), Q (queen), K (king), or A (ace)) followed by a suit (C (clubs), D (diamonds), H (hearts), S (spades)). We will write a poker hand as comma delimited cards within square brackets, e.g. [2H, 5C, 6H, JS, AD]. The order of cards in a hand does not matter, but by convention we will write them in sorted order from low to high. The order of a card is first determined by its value (2 is lowest, ace is highest); two cards with the same value are order by suit, in the sequence (from low to high) clubs, diamonds, hearts, spades. In the following problems, show your work. a [5] How many distinct poker hands are there? b [10] A poker hand can be classified into several categories; here are two of them (see Extra Credit Problem 2 for the rest): Flush: a hand whose cards are all of the same suit. E.g.: [3C, 5C, 8C, 9C, QC], [8S, 9S, 10S, JS, KS] Straight: a sequence of five consecutive values. Note that for the purposes of a straight, an ace can count as the lowest card as well as the highest. E.g.: [AC, 2H, 3H, 4D, 5S], [4D, 5H, 6D, 7C, 8S], [10H, JH, QH, KC, AH] If you are dealt a poker hand from a fresh deck, is it more likely that you will be dealt a flush or a straight? (Note that a single hand can be both a flush and a straight such a hand is called a straight flush.) Justify your answer. If you would like to explore the probabilities of poker in more detail, do Extra Credit Problem 2. 3
Extra Credit Problem 1: Envelopes [20] (The best probability problem I know of! The following wording comes from Uri Wilensky s MIT Phd thesis) There are two envelopes in front of you. One contains x dollars and the other contains 2x dollars. You choose one, open it, and get $100. You are then offered a switch if you want it. You can take the other envelope instead. Here are two arguments: 1. There were 2 envelopes, one contained x dollars and the other 2x. You got either x or 2x. Each was equally likely, so switching doesn t help. 2. The other envelope contains either $200 or $50. These are equally likely. So the expected value of the other envelope is ($200 + $50)/2 = $125. You should switch. Which if any of these arguments do you believe? What is the bug in the other argument? Extra Credit Problem 2: More Poker Time! [30] This problem extends the analysis of poker from Problem 5. A poker hand can be classified into the following categories; in cases where more than one category applies, the one appearing earliest in the list is chosen: Royal Flush: a ten, jack, queen, king, and ace of the same suit. E.g.: [10H, JH, QH, KH, AH] Straight Flush: a sequence of five consecutive values of the same suit. E.g.: [AC, 2C, 3C, 4C, 5C], [8S, 9S, 10S, JS, QS]. Note that for the purposes of a straight, an ace can count as the lowest card as well as the highest. Four of a Kind: a hand that contains four cards with the same value: E.g.: [6C, 6D, 6H, 6S, 9D], [2S, 3C, 3D, 3H, 3S] Full House: a hand that contains three of one value of card and two of another. E.g.: [7C, 7H, JD, JH, JS], [2C, 2H, 2S, 8D, 8H] Flush: a hand whose cards are all of the same suit. E.g.: [3C, 5C, 8C, 9C, QC], [8S, 9S, 10S, JS, KS] Straight: a sequence of five consecutive values. Note that for the purposes of a straight, an ace can count as the lowest card as well as the highest. E.g.: [AC, 2H, 3H, 4D, 5S], [4D, 5H, 6D, 7C, 8S], [10H, JH, QH, KC, AH] Three of a Kind: a hand that contains three cards with the same value. E.g.: [2C, 2D, 2H, 8C, 10H], [4H, 5C, 5H, 5S, QS], [2S, 7H, JD, JH, JS] Two Pairs: a hand that contains two cards of one value and two cards of another value. E.g.: [3H, 3S. 7C, 7H, QD], [8C, 8H, JD, KC, KS], [6S, 9H, 9S, QC, QH] Pair of Jacks or Better: a hand that contains two cards with the same value that is a Jack or higher. E.g: [JC, JD, QS, KC, AH], [7S, QD, QS, KS, AD], [3H, 9S, JC, JH, KD], [4H, 5C, 10H, AH, AS] 4
Pair of Tens or Less: a hand that contains two cards with the same value that is ten or lower. E.g: [8C, 8S, JC, QD, AC], [4S, 5D, 5H, JS, QH], [4H, 6D, 10C, 10H, JD], [2H, 6D, 7S, 8C, 8H] Nothing: a hand that has no pairs, straights, or flushes. Part a: Below is a table for the number of distinct hands in each category and the probability of getting a hand in each category dealt from a shuffled deck. Fill in the second and third columns by carefully counting the number of each kind of hand. Don't forget that each hand should only be classified into one category. That is, you should only count a straight flush as a straight flush and not as a straight or a flush. Show your derivation of each number. Category Number of Disinct Hands in Category Probability Video Poker Payoff Royal Flush 800:1 Straight Flush 50:1 Four of a Kind 25:1 Full House 9:1 Flush 6:1 Straight 4:1 Three of a Kind 3:1 Two Pairs 2:1 Pair of Jacks or Better 1:1 Pair of Tens or Less 0:1 Nothing 0:1 Total Part b: The fourth column shows a typical video poker payoff schedule. The notation n:1 means that for every dollar bet, n dollars will be one if the hand is in the given category. Given this payoff schedule determine the expected value of a dollar bet for five randomly dealt cards. Note: The expected value will be very low, because it does not model the "draw" aspect of draw poker in a real video poker game. In video poker, the player is first dealt a hand of five cards from a full, shuffled deck. The player is then allowed to discard as many of the cards from the hand as desired; all discarded cards are replaced by new cards drawn from the remaining deck. With the above payoff schedule, playing an optimal strategy in the dyields about a 99.5% return. That is, the perfect player on average will lose one half cent for every dollar bet. Of course, most players are far from perfect, so the casinos rake in quite a bit of money on this game. On the other hand, the laws of probability allow players to sometimes beat the house, which keeps them coming back and serves as good advertising to attract others. There are a few video poker machines with a different payoff schedule that has greater than 100% return for perfect play. These machines are coveted by professional gamblers, who will often play at them for stretches of 24 hours or longer. The key to winning on such a machine is to play until hitting a royal flush, which has a high relative payoff. Why would casinos have such machines? Because they attract customers, and few people can play them optimally. Any deviance from optimal play usually leads to an advantage for the casino. 5
Problem Set Header Page Please make this the first page of your hardcopy submission. CS231 Problem Set 2 Due Friday, September 28, 2001 Name: Date & Time Submitted (only if late): Collaborators (anyone you collaborated with in the process of doing the problem set): In the Time column, please estimate the time you spent on the parts of this problem set. Please try to be as accurate as possible; this information will help me to design future problem sets. I will fill out the Score column when grading your problem set. Part Time Score General Reading Problem 1 [30] Problem 2 [15] Problem 3 [15] Problem 4 [25] Problem 5 [15] Extra Credit 1 [20] Extra Credit 2 [30] Total 6