# Solutions to Final Exam

#### 21 December 2007

The scale was A = 112, C = 70. The distribution of scores is given at the main CMPSCI 250 web page.

```  Q1: 45 points
Q2: 15 points
Q3: 25 points
Q4: 40+5 points
Total: 125+5 points
```

• Question 1 (45): This problem deals with the following recursively defined sequence of compound proposiations P0, P1, P2,..., which use the atomic propositions a1, a2, a3,...:
• P0 is the proposition "0", meaning "false".
• For all positive natural numbers i, Pi is defined to be "ai → Pi-1".

Thus P1 is "a1 → 0", P2 is "a2 → (a1 → 0)", P3 is "a3 → (a2 → (a1 → 0))", and so forth. Don't forget that for any propositions x and y, "x → y" is defined to be true unless both x is true and y is false.

• (a,15) Prove that for any positive natural i, (¬a1) → Pi. (Hint: Use ordinary induction starting with i=1.) (Also, not that it's "a1", not "ai".

Base case, with i=1: We must prove ¬(a1) → (a1 → 0). Assume that a1 is false, then the implication is true by the rule of vacuous proof, as its antecedent is false.

Inductive step: Assume ¬(a1) → Pi. We prove "¬(a1) → Pi+1" by direct proof. Assume ¬(a1). Then by the IH and Modus Ponens, Pi is true. Since Pi+1 is defined to be "ai+1 → Pi", Pi+1 is true by the rule of trivial proof.

• (b,10) Suppose that we choose a truth value for each atomic proposition ai independently, with each ai equally likely to be true or false. Calculate the probability that P1 is true, the probability that P2 is true, and the probability that P3 is true. (These are three separate questions.)

P1 is false only if a1 is true and 0 is false -- this has a 1/2 probability because a1 is equally likely to be true or false.

P2 is false only if a2 is true and P1 is false. Since a2 does not occur in P1, these two propositions are independent. So the probability that both are false is the product of the probabilities that either is false, or (1/2)(1/2) = 1/4. Thus the probability that P2 is true is 1 - (1/4) or 3/4.

P3 is false only if a3 is true and P2 is false, and again these two propositions are independent. So the probability that P3 is false is (1/2)(1/4) = 1/8, and the probability that it is true is 1 - (1/8) or 7/8.

• (c,20) Under the assumption of part (b), find a formula that gives the probability that Pi is true as a function of the natural number i. Prove that your formula is correct, using (ordinary) induction and the laws of probability.

From the examples, the rule appears to be Prob(Pi) = 1 - 2-i, so we will prove this statement by ordinary induction for all naturals i.

For i=0, Prof(P0 is 0 by the definition of P0 as 0, and 1 - 2-0 = 1 - 1 = 0, so the base case is proved.

For general positive i, Prob(Pi) = 1 - Prob(Pi is false) = 1 - Prob(ai is true and Pi-1 is false) = 1 - Prob(ai)Prob(Pi-1) because of independence, as the atomic proposition ai does not appear in Pi-1.

This is 1 - (1/2)(2-(i-1) by the IH, which is 1 - 2-i by arithmetic.

• Question 2 (15): Let R be a binary relation on a set X. You are given that R is an equivalence relation.
• (a,5) Write down the three properties of an equivalence relation as quantified statements involving R.

Reflexive property: ∀a: R(a,a)

Symmetric property: ∀a:∀b: R(a,b) → R(b,a)

Transitive property: ∀a:∀b:∀c: (R(a,b) ∧ R(b,c)) → R(a,c)

• (b,10) Prove the following quantified statement using the quantifier proof rules and the statements from part (a). All variables represent elements of X:

∀a:∀b: R(a,b) → [∀c:∀d: (R(c,a) ∧ R(d,b)) → R(c,d)]

1. Let a and b be arbitrary elements of X.
2. Assume R(a,b). We want to prove ∀c:∀d: (R(c,a) ∧ R(d,b)) → R(c,d).
3. Let c and d be arbitrary elements of X.
4. Assume R(c,a) ∧ R(d,b).
5. R(c,a) by left separation.
6. R(a,b) by assumption in line 2.
7. R(c,b) by transitivity specified to c, a, and b, on lines 5 and 6.
8. R(d,b) by right separation on line 4.
9. R(b,d) by symmetry, specified to d and b, on line 8.
10. R(c,d) by transitivity, specified to c, b, and d, on lines 7 and 9.
11. (R(c,a) ∧ R(d,b)) → R(c,d), by direct proof on lines 4-10.
12. ∀c:∀d: (R(c,a) ∧ R(d,b)) → R(c,d) by generalization on lines 3-11.
13. R(a,b) → (line 12), by direct proof.
14. Desired conclusion by generalization on lines 1-13.

• Question 3 (25): This question deals with the following labeled undirected graph. The vertex set is {A,B,C,D,E,F,G}. There are edges (B,E), (D,F), and (E,F) of length 1, edges (A,B), (C,G), and (F,G) of length 2, edges (A,C) and (C,D) of length 3, and an edge (B,D) of length 4.
• (a,5) Write down the single-step distance matrix for this graph, suitable as input for Floyd's Algorithm. Breifly describe how you would use this algorithm to find the shortest-path distance matrix, but do not do the calculation!

Here the symbol I means "infinity":

``````    A  B  C  D  E  F  G
---------------------
A| 0  2  3  I  I  I  I
B| 2  0  I  4  1  I  I
C| 3  I  0  3  I  I  2
D| I  4  3  0  I  1  I
E| I  1  I  I  0  0  2
F| I  I  I  1  1  0  2
G| I  I  2  I  I  2  0
``````

We take this matrix X and compute the matrix X6 using min-plus matrix multiplication, with the minimum operation as "addition" and the ordinary addition operation as "multiplication". We compute the sixth power because 6 = n-1, where n is the number of nodes in the graph.

• (b,10) Ignoring the weights of the edges for this part only, conduct a breadth-first search and a depth-first search of this graph, each time starting from node A. Assume that you are able to recognize previously seen nodes, as is usual for such searches of graphs. Draw the two search trees, indicating the tree edges and non-tree edges.

In the BFS, we find B and C from A, find D and E from B, find G (and the non-tree edge to D) from C, find F from D, and find non-tree edges to F from both E and G. The tree looks like this, where stars denote non-tree edges:

``````
A
/ \
/   \
B     C
/ \   * \
/   \ *   \
E     D     G
*    |    *
*   |   *
*  |  *
* | *
*|*
F
``````

In the DFS, we find B from A, D from B, and C from D, find the non-tree edge from C to A, then find G from C, F from G, the non-tree edge to D from F, E from F, and the non-tree edge to B from E. The DFS tree is a straight line through the nodes A, B, D, C, G, F, and E, with the three non-tree back edges as indicated. Other valid trees are possible given different choices of which neighbor of a node to visit first. But note that the DFS cannot finish processing a node without looking at all of that node's neighbors.

• (c,10) Carry out a uniform-cost search on the weighted graph to find the shortest path from node A to node D. Indicate what entries go on and off the priority queue before the final answer is found.

• A goes on the priority queue with label 0.
• (A,0) comes off, (B,2) and (C,3) go on.
• (B.2) comes off, (D,6) and (E,3) go on.
• (C,3) comes off, (D,6) and (G,5) go on.
• (E,3) comes off, (F,4) goes on.
• (F,4) comes off, (D,5) and (G,6) go on.
• (G,5) comes off, nothing new goes on
• (D,5) comes off, and we have found the length-5 path from A through B, E, and F to D.

• Question 4 (40+5): This question deals with a preschool class of eight students: four girls named Abigail, Beatrice, Caroline, and Daisy, and four boys named Edward, Frank, George, and Henry.

• (a,5) When the eight students line up to go to recess, each possible order of the students is equally likely. How many such orders are there?

There are 8! or 88 = 40320 orders.

• (b,10) When the students line up, what is the probability that they alternate between boys and girls, with the first student a boy?

Solution 1: There are 4! orders of the boys, and 4! orders of the girls, so there are (4!)2 = 576 possible orders where the boy-girl arrangement holds. So the probability is 576/40320 = 1/70.

Solution 2: There are (8 choose 4) = 70 ways to pick four of the eight positions for the boys, and all are equally likely, so the probability is 1/70.

Solution 3: There is 4/8 chance of placing a boy first, then a 4/7 chance of a girl second, a 3/6 chance of a boy third, and so on for a total probability of (4/8)(4/7)(3/6)(3/5)(2/4)(2/3)(1/2)(1/1) = 1/70 as in solution 1.

• (c,5) When the students line up, what is the probability that Abigail is somewhere after Edward and somewhere before Frank, possibly with other students in between?

Solution 1: The 3! = 6 possible relative orders of Abigail, Edward, and Frank are each equally likely, so there is a 1/6 chance they will occur in the given order.

Solution 2: There are eight equally likely positions for Abigail, so each has 1/8 probability. In each, we can calculate the probability that Edward is ahead of Abigail and Frank behind her. In each case, there are 72 = 42 ways to place Edward and Frank in two of the seven remaining positions. If Abigail is in position 1 or 8, none of these have Edward ahead of Abigail and Frank behind. If she is in position 2 or 7, six of the 42 choices meet this condition. (For example, if Abigail is in position 2 we can put Edward in position 1 and Frank in 3, 4, 5, 6, 7, or 8.) If Abigail is in position 3 or 6 there are 2*5 = 10 ways to place Edward and Frank and meet the conditions. If Abigail is in position 4 or 5 there are 3*4 = 12 ways to do it. Overall our probability is (1/8)(1/42)(0+6+10+12+12+10+6+0) = 56/(8*42) = 1/6.

• (d,5XC) When the students line up, what is the probability that Edward, Abigail, and Frank, in that order, are three consecutive students in the line? (That is, Edward is immediately in front of Abigail, who is immediately in front of Frank.)

Solution 1: Think of Edward-Abigail-Frank as a single unit -- there are 6! ways to order this unit and the five other students, so the probability that we get one of these orders is 6!/8! = 1/56.

Solution 2: There are (8 choose 3) possible sets of three positions for Edward, Abigail, and Frank, and six of these are consecutive: 123, 234, 345, 456, 567, and 678. So there is a 6/56 chance they will be consecutive, and a 1/6 chance they will be in the right order, so (6/56)(1/6) = 1/56 probability overall.

Solution 3: To find an order with Edward, Abigail, and Frank consecutive in that order, we place those three in any of those six sets of positions, then place the other five students in 5! possible ways. So there are 6*5! orders with them consecutive, and a probability of 6*5!/8! = 1/56.

• (e,5) Let Y be the set consisting of the first three students in the line. When the students line up, what is the probability that Y is the set {Abigail, Edward, Frank}?

Solution 1: There are (8 choose 3) choices of this set, and each is equally likely, making the probability 1/56.

Solution 2: There is a 3/8 chance that one of those three comes first, then a 2/7 chance that one of the other two comes second, then a 1/6 chance that the last of the three comes third, for (3/8)(2/7)(1/6) = 1/56.

• (f,10) During the day, the teacher awards three gold stars for good behavior. It is possible, though not required, that more than one star goes to the same student. How many possibilities are there for the way the stars are awarded (for example, one way is "two to Abigail, one to George")?

Solution 1: This is the Fourth Counting Problem with k=3 and n=8, as we are choosing a multiset of size three from the eight-element set. The number of such multisets is (k+n-1 choose k) = (10 choose 3), or (k+n-1 choose n-1) = (10 choose 7) = 120.

Solution 2: There are 8 ways to give all three stars to the same person, 2(8 choose 2) ways to give two to one student and the third to another, and (8 choose 3) ways to pick a set of three to get one star each. The total is 8 + 2(8 choose 2) + (8 choose 3) = 8 + 56 + 56 = 120.

• (g,5) If the teacher awards each of the three gold stars randomly and independently, with each student equally likely to get each star, what is the probability that three different students get stars?

Solution 1: There are 83 sequences of three students, and 83 sequences with no repeats. Each such sequence is equally likely, so the probability is 8*7*6/8*8*8 = 42/64 = 21/32.

Solution 2: After the first star goes to some student, there is a 7/8 chance that the second star does not go to the same student. If this happens there is then a 6/8 chance that the third star goes to a third student, so the total probability is (7/8)(6/8) = 21/32.

Many people who used Solution 2 for part (f) thought that these 120 ways of awarding the stars were equally likely and thus answered 56/120 for part (g). This is incorrect -- of the 512 equally likely ways to award the three stars in a sequence, six result in each of the 56 sets of three separate winners, three result in each of the 56 ways to give two to one and one to another, and one results in each of the eight ways to give all three to the same student.