# Third Midterm Exam Solutions

#### 24 April 2005

Actual scale was A=92, C=62. Question text is in black, solutions in blue.

```  Q1: 25 points
Q2: 30 points
Q3: 20 points plus 10 extra credit
Q4: 20 points
Total: 95 points plus 10 extra credit
```

• Question 1 (25): Five customers, whom we'll call A, B, C, D, and E, enter a small video rental shop. The shop has only one copy of each of the three Lord of the Rings movies, which we'll call movies 1, 2, and 3.

• (a,5) In how many ways may the three movies each be assigned to one of the customers? (An example assignment might be "A gets movie 1, D gets movie 2, A gets movie 3".)

First counting problem, count sequences of three customers, answer 53 = 125.

• (b,5) In how many ways could the above assignment be made if we only consider how many movies each customer gets? (So the above example becomes "A gets two movies, D gets one, and the others get none".)

Fourth counting problem, count multisets of three customers, answer (3+5-1 choose 3) = (7 choose 3) = (7*6*5)/(1*2*3) = 35.

Some people got this by adding the five ways for all three movies to go the same customer, plus the 5*4 = 20 ways to give two movies to one and one to another, plus the ten ways to give one each to three different customers counted in (d). This is 5 + 20 + 10 = 35, which is correct though more work than using the fourth counting problem.

• (c,5) How many of the assignments in part (a) are possible if no customer may get more than one movie?

Second counting problem, count sequences of three customers with no repeated element, answer 53 = 5*4*3 = 60.

• (d,5) How many of the assignments in part (b) are possible if no customer may get more than one movie? Note: In parts (a)-(d) every movie must be assigned to a customer.

Third counting problem, count sets of three customers, answer (5 choose 3) = (5*4*3)/(1*2*3) = 10.

• (e,5) Now suppose that the store has at least five copies of each movie, and that each customer may take some of the movies, all of them, or none. (But no customer may take more than one copy of the same movie.) (Example: "A takes 1 and 3, B takes none, C takes all, D takes 3 only, E takes 2 and 3") In how many ways can the assignment of movies to customers be made?

Each customer picks a subset of the three movies. There are 23 = 8 such subsets (also computable as (3 choose 0) + (3 choose 1) + (3 choose 2) + (3 choose 3)). So we are counting sequences of five subsets, which is the first counting problem -- answer 85 = 215 = 32768.

• Question 2 (30): This problem involves strings over the alphabet {a,b,c}. A palindrome is a string that is equal to its own reversal, such as λ, aba, or cabbac.

• (a,5) List all the palindromes of length 0, 1, and 2.

Length 0 has λ only, length 1 has a, b, and c, and length 2 has aa, bb, and cc.

• (b,10) Let n be an arbitrary natural. Explain why the number of palindromes of length n+2 is exactly three times the number of palindromes of length n.

A palindrome of length n+2 must have its first and last letters equal, so it must be of the form dud where d is a letter and u is a string of length n. Since (dud)R = duRd, we see that u = uR and u is itself a palindrome. Any choice of a letter d and a palindrome u leads to a palindrome dud, and all palindromes of length n+2 are of this form. (I took off two points for not arguing that you had covered all possible palindromes.) The number of choices of d is 3, so the number of choices of dud is three times the number of choices of u. So the number of length n+2 palindromes is exactly three times the number of length n palindromes, as desired.

• (c,15) Using the result of (b), whether you proved it or not, prove by induction for all naturals n that the number of palindromes of length n is 3(n+1)/2, where the slash represents Java integer division. (Hint: You may use either string induction of separate inductions for odd and even numbers. Remember that if k is a natural, (k+2)/2 is always equal to (k/2) + 1 for Java integer division.)

Let P(n) be "the number of palindromes of length n is 3(n+1)/2".

Odd-even induction, odd case: The base case is n=1, and P(1) is true because there are three palindromes and 3(1+1)/2 = 31 = 3. For the inductive case, assume P(n) and prove P(n+2). P(n) says that the number of length n palindromes is 3(n+1)/2, and (b) then tells us that the number of length n+2 palindromes is three times this number, or 3((n+1)/2)+1. By the reminder in the hint, this number is 3(n+3)/2 which is 3((n+2)+1)/2 as desired. (The reminder makes it unnecessary to deal separately with odd and even n in calculating with the exponent.) So P(n) implies P(n+2) and the entire odd case follows.

Odd-even induction, even case: The base case is n=0, and P(0) is true because there is one palindrome of length 0 and 3(0+1)/2 = 30 = 1. The inductive case is to assume P(n) and prove P(n+2), and this is identical to the inductive step of the odd case above because the inductive step never used the fact that n was odd.

Strong induction: We prove two base cases P(0) and P(1) as above. Then from the argument above that P(n) implies P(n+2), we can conclude that P(n-1) implies P(n+1). If n is at least 1, then, the strong inductive hypthesis (that P(i) is true for all i with i ≤ n) gives us P(n-1) and thus gives us P(n+1). This completes the strong induction and proves P(n) for all naturals n.

• Question 3 (20+10): In the card game of Blackjack or twenty-one the player is dealt two cards. (For the purposes of this problem we will say that they are dealt from a standard 52-card deck, with four cards in each of the thirteen ranks {A,2,3,4,5,6,7,8,9,10,J,Q,K}.)

• (a,10) To get a score of 21, one of the cards must be an ace and the other must have a rank in the set {10,J,Q,K}. If any set of two cards is equally likely, what is the probability that the player's score is 21?

The number of two-card sets is (52 choose 2) = (52*51)/(1*2) = 1326. The number of two-card sets containing an ace and a card from {10,J,Q,K} is 4*16 = 64. So the probability is 64/1326 = a bit under 5%.

It's possible but trickier to do this by counting sequences in both the numerator and denominator. The number of two-card sequences from the deck is 52*51 (not 52*52, because after the first card is chosen it cannot be chosen again). The number of sequences adding to 21 is 4*16 for the sequences of an ace followed by a ten or face card, plus 16*4 for sequences of a ten or face card followed by an ace. The answer is thus 128/52*51, identical to the above. Most people who tried this method forgot to consider both orders and got half the right answer. (Many of these people, perhaps, didn't realize that they were counting sets on the top and sequences on the bottom of the ratio.)

• (b,10) To get a score of 20, both cards must have ranks in the set {10,J,Q,K}. (Note: This isn't actually true because you could have an ace and a nine, but you were told to solve the problem as written.) What is the probability of this happening, if each set of two cards is equally likely?

If we count sets, we again have (52 choose 2) = 1326 in the denominator. In the numerator, we may have any two of the 16 tens and face cards, so there are (16 choose 2) = (16*15)/(1*2) = 120 such sets and the answer is 120/1326 = 60/663 = 20/221, about 9%.

In this problem the sequence counters mostly got the right answer. The number of two-card sequences is 52*51, and the number of two-card sequences of tens or face cards is 16*15. The ratio (16*15)/(52*51) is the same as that above. Some people also got this by computing the chance that the first card is worth ten, 16/52, and multiplying this by the chance that the second card is also worth ten, which is 15/51 because by this point one of the desired cards is out of the deck.

• (c,10 extra credit) Counting an ace as 11, a card with rank in {J,Q,K} as 10, and every other card as its numerical value, what is the probability that the player has a score of 17, 18, 19, 20, or 21 with her two cards?

This is a more complicated application of the same principles. I found it easiest to count sets, and add the numerators over the common denominator of (52 choose 2):

• Score of 17: Ace and six 4*4 = 16, ten and seven 16*4 = 64, nine and eight 4*4 = 16, total 96.
• Score of 18: Ace and seven 4*4 = 16, ten and eight 16*4 = 64, two nines (4 choose 2) = 6, total 86.
• Score of 19: Ace and eight 4*4 = 16, ten and nine 16*4 = 64, total 80.
• Score of 20: Ace and nine 4*4 = 16 (This is the case I forgot in part (b) -- no one was penalized for leaving it out here as well.), two tens (16 choose 2) = 120, total 136.
• Score of 21: Ace and ten 4*16 = 64 as in (a).
• Total probability (96+86+80+136+64)/1326 = 462/1326 = 77/221 or about 35%.

• Question 4 (20): This question deals with the undirected graph in the following picture. Recall that an edge in an undirected graph may be traversed in either direction. (Note: This graph has no loops and exactly four edges.)

```    (2)-------(3)
| \      /
|  \    /
|   \  /
|    \/
|    /\
|   /  \
|  /    \
| /      \
(1)       (4)
```

• (a,5) Write the adjacency matrix of this graph, using the numerical order on the vertices {1,2,3,4}.

```    0 1 1 0
1 0 1 1
1 1 0 0
0 1 0 0
```

Nearly everybody got this.

• (b,15) Determine how many paths of length 0, 1, 2, 3, and 4 there are from vertex 1 to vertex 4 in this graph, and list these paths. (For example, there is exactly one two-step path from 1 to 3, which we may describe as "1 to 2 to 3".) (Hint: If you do one complete matrix multiplication, you can compute the individual entries of the other matrices you need. It is also possible to find all the paths by exhaustive search, but the matrix method is more reliable.)

• There are no paths of length 0 because 1 does not equal 4.
• There are no paths of length 1 because there is no edge from 1 to 4.
• We can count all paths of length two in the graph by sqauring the matrix to get:
```     2 1 1 1
1 3 1 0
1 1 2 1
1 0 1 1
```

The last entry of the first row tells us that there is exactly one path of length 2, and by inspection this is from 1 to 3 to 2.

• We can compute the number of three-step paths by dot-producting the first row of the matrix A from (a) with the fourth column of the matrix A2 just above. Since (0 1 1 0) dot (1 0 1 1) is 1, there is one path and by inspection we find that it goes from 1 to 3 to 2 to 4.
• Now we find the number of paths by dotting the first row of A2 with the fourth column of A2 to get (2 1 1 1) dot (1 0 1 1) or 4. We have to look until we find all four paths: 1 to 2 to 3 to 2 to 4, 1 to 2 to 1 to 2 to 4, 1 to 3 to 1 to 2 to 4, and 1 to 2 to 4 to 2 to 4.

There were two principal mistakes people made on this problem. Some didn't do the matrix multiplication and looked for the paths by hand -- this was fine if you were right but many people found only three of the four four-step paths. (This cost 3 of the 15 points.) A larger number of people misread the question -- they found the number of paths of each length but didn't list the paths. (This cost 5 of the 15 points.)