# Solutions to Second Midterm Exam Fall 2017

### Directions:

• Answer the problems on the exam pages.
• There are four problems, each with multiple parts, for 100 total points plus 10 extra credit. Actual scale was A = 93, C = 63.
• Some useful definitions precede the questions below.
• No books, notes, calculators, or collaboration.
• In case of a numerical answer, an arithmetic expression like "217 - 4" need not be reduced to a single integer.

Question text is in black, solutions in blue.

```  Q1: 20+10 points
Q2: 30 points
Q3: 30 points
Q4: 30 points
Total: 100+10 points
```

Here are definitions of some terms, sets, predicates, and statements used on this exam.

Remember that a natural is a non-negative integer, so that the set N or all naturals is {0, 1, 2, 3,...}.

The Fibonacci numbers are a sequence of naturals defined by the rules F(0) = 0, F(1) = 1, and, for all n with n ≥ 1, F(n+1) = F(n) + F(n-1).

For every natural n, we define two graphs, a directed graph Dn and an undirected graph Un. Each has vertex set {0, 1,..., n}. The directed edges of Dn are (1) (i, i+1) for every i with 0 ≤ i ≤ n-1, (2) (i+2, i) for every even i with 0 ≤ i ≤ n-2, and (3) (i, i+2) for every odd with 1 ≤ i ≤ n-2.

We also view Dn as a labeled graph where each edge of type (1) above has cost 2, and the other edges have cost 3.

The undirected graph Un has the same vertex set as Dn, and an undirected edge whenever Dn has a directed edge. We will not have occasion to use edge costs in Un.

``````
Directed graph D_5 with edge costs.  The graph U_5 has the
same nodes and edges but all the edges are undirected.

___________        _____________
/      3      \     /      3      \
V               \   V               \
(0) ----> (1) ----> (2) ----> (3) ----> (4) ----> (5)
2      \  2         2   ^  \  2         2   ^
\______________/    \_____________ /
3                   3

``````

• Question 1 (20+10): Pig Floyd is weighed at the beginning of every month. Let W(n), a real number, be his wieight in kilograms for month n. We will assume that for all n with n ≥ 1, his weight follows the rule W(n+1) = √2W(n) - W(n-1) + 700 - 350√2. Note that in our calculations we will not approximate √2. So if W(0) = 350 and W(1) = 400, for example, we calculate W(2) as 400√2 - 350 + 700 - 350√2 = 350 + 50√2.

• (a, 5) Assume for this part only that W(0) and W(1) are both 350. Prove by (strong) induction on all naturals n that W(n) = 350.

Base case: W(0) and W(1) are given to be 350. Note that two base cases are needed for the strong induction.

Strong inductive hypothesis: W(i) = 350 for all i with i ≤ n

Inductive goal: W(n+1) = 350

Proof: W(n+1), by the given rule, is W(n)√2 - W(n-1) + 700 - 350√n, which by the SIH is 350√2 - 350 + 700 - 350√2 = 350.

• (b, 5) Now assume, for the rest of the problem, that W(0) = 350 and W(1) = 400. Calculate W(n) for all naturals n with n ≤ 10. Put your answers in the form a + b√2 where a and b are integers.

W(0) = 350

W(1) = 400

W(2) = 400√2 - 350 + 700 - 350√2 = 350 + 50√2

W(3) = (350 + 50√2)√2 - 400 + 700 - 350√2 = 400

W(4) = 400√2 - (350 + 50√2) + 700 - 350√2 = 350

W(5) = 350√2 - 400 + 700 - 350√2 = 300

W(6) = 300√2 - 350 + 700 - 350√2 = 350 - 50√2

W(7) = (350 - 50√2)√2 - 300 + 700 - 350√2 = 300

W(8) = 300√2 - (350 - 50√2) + 700 - 350√2 = 350

W(9) = 350√2 - 300 + 700 - 350√2 = 400

W(10) = 350 + 50√2 by the same calculation as W(2)

• (c, 10) Still assuming that W(0) = 350 and W(1) = 400, prove by (strong) induction on all naturals n that each value W(n) can be written in the form an + bn√2, where an and bn are integers depending on n. (You are not asked in this part to determine the integers an and bn, only to prove that they exist.

Base case: a0 = 350, a1 = 400, b0 = b1 = 0. Note that two base cases are needed for the strong induction.

Strong IH: ai and bi exist for all i with 0 ≤ i ≤ n.

Inductive goal: an+1 and bn+1 exist.

Proof: W(n+1) = (an + bn√2)√2 - (an-1 + bn-1√2) + 700 - 350√2 = (2bn - an-1 + 700) + (an + bn-1 - 350)√2. Thus W(n+1) is of the required form, as the integers are closed under addition and multiplication by 2. The integers are not closed under multiplication by √2, so the proof has to note somehow that when we multiply a number of the form a + b√2 by √2, we get some number c + d√2.

A number of you claimed a solution for this problem based on an incomplete or invalid proof of the true fact that the values of W(n) cycle with period 8. I gave substantial credit for this, but not full credit.

• (d, 10XC) Still assuming that W(0) = 350 and W(1) = 400, determine the value of W(84), which is Floyd's weight seven years after the weighings start. Justify your answer. You will probably find an induction proof useful to avoid having to calculate all the intermediate values of W(n).

The key fact, as many of you observed but fewer of you proved, is that the values of W(n) cycle with period 8, so that W(n) = W(n%8) in Java notation. Once we know this, we see that W(84) is equal to W(4), which we computed in part (b) to be 350.

How to prove that the cycle exists? We let our statement P(n) be "W(n) = W(n%8)". We can observe the base cases for n ≤ 10 from part (b). Our SIH is that W(i) = W(i%8) for all i with i ≤ n, and the inductive goal is to prove that W(n+1) = W((n+1)%8). The rule tells us that W(n+1) depends on W(n) and W(n-1), and the SIH lets us replace W(n) and W(n-1) by W(n%8) and W((n-1)%8. Finally the rule lets us take the expression in W(n%8) and W((n-1)%8) and convert it to W((n+1)%8), proving the inductive goal.

It is possible to prove that W(n+8) = W(n) by eight applications of the rule, and this is enough to prove that W(84) = W(4). Easier is to prove that W(n+4) = 700 - W(0), by four applications of the rule.

• Question 2 (20): The graph families Un and Dn, for all naturals n, are defined above.

• (a, 10) Prove by (strong) induction on n that in the graph Un there are exactly F(n+1) different paths from vertex 0 to vertex n such that the vertex number always increases along the path. (Here F(n) refers to the Fibonacci sequence defined above.)

First base: n = 0, there is one empty path from 0 to 0, F(0+1) = 1.

Second base: n = 1, there is one path from 0 to 1 using the single edge (0, 1), F(1+1) = 1. Note that we need two base cases for the strong induction.

SIH: For each i with i ≤ n, there are F(i+1) paths from 0 to i.

Inductive goal: There are F(n+2) paths from 0 to n+1.

Proof: Any path from 0 to n+1 must get to n+1 using either the edge from n or the edge from n-1. There are F(n+1) paths doing the former and F(n) paths doing the latter, by the SIH. By the Fibonacci rule, the total number F(n+1) + F(n) is equal to F(n+2).

• (b, 10) Given the cost function on Dn given above, where edges of the form (i, i+1) have cost 2 and the others have cost 3, prove the following by (strong) induction for all positive naturals n: "The minimum-cost path in Dn from vertex 0 to vertex n has cost (3n+1)/2 if n is odd and has cost 3n/2 + 1 if n is even."

First base: n = 1 (since the induction is on positive naturals) The only path has cost 2, and 2 = (3(1) + 1)/2.

Second base: n = 2, the only path has cost 4, and 4 = (3(2)/2) + 1.

SIH: The best path to each vertex i with i ≤ n has the given cost.

Inductive goal: The best path to vertex n+1 has the given cost.

Proof: If n is odd and n+1 is even, the only edge into n+1 is from n, so any path to n+1 takes a path to n and then an edge of cost 2. Since the best path to n has cost (3n+1)/2, the best path to n+1 has cost (3n+1)/2 + 2 = (3n+5)/2 = 3(n+1)/2 + 1, as given by the formula for even numbers.

If n is even and n+1 is odd, there are two edges into n+1, one from n and one from n-1. Applying the SIH, the cost of the best path to n+1 is the lesser of 3 + (3(n-1) + 1)/2 and 2 + (3n/2 + 1)/2, which is (3n + 4)/2 as opposed to (3n+6)/2. This is equal to the odd-case formula for n+1, which is (3(n+1) + 1)/2. I did not give full credit unless you proved that the path was optimal, by considering the multiple options for a path into an odd-numbered vertex.

Question 3 (30): This question involves various searches of the graphs D5 and U5 defined and pictured above. Note that each of these graphs has six vertices and nine edges. In each case, assume that your search uses a closed list.

• (a, 5) Conduct a breadth-first search of the undirected graph U5, with start node 3 and no goal node. Draw the resulting BFS tree, indicating with edges are tree edges and which are non-tree edges. If two nodes enter the queue at the same time, they should come off in numerical order, lowest number first.

Node 3 goes on the queue. Node 3 comes off, and nodes 1, 2, 4, and 5 go on. Node 1 comes off, and nodes 0 and 2 go on. Node 2 (the first copy, from 3) comes off, and nodes 0 and 4 go on. Node 4 (the first copy, from 3) comes off, and node 5 goes on. Node 5 comes off, and nothing goes on. Node 0 comes off (the copy from 1) and nothing goes on. The remaining nodes are discarded as they are all on the closed list.

In the tree, 3 is the root, with children 1, 2, 4, and 5, and 1 has a child 0. The four non-tree edges are (0, 2), (1, 2), (2, 4), and (4, 5).

• (b, 10) Conduct a depth-first search of the graph D5, with start node 3 and no goal node. Draw the resulting DFS tree, indicating which edges are tree, back, forward, and cross edges respectively. If two nodes enter the stack at the same time, they should come off in numerical order, lowest number first.

Node 3 goes on the stack. Node 3 comes off, and 4 and 5 go on. 4 comes off, and 2 and 5 go on. Note that there are now two copies of 5 on the stack, because 5 is not yet on the closed list. The copy closer to the top is the one from 4.

Node 2 comes off, and node 0 goes on (3 is on the closed list). Node 0 comes off, and 1 goes on. Node 1 comes off, and nothing goes on because 2 and 3 are on the closed list.

Now the copy of 5 from 4 comes off, and nothing goes on. The second copy of 5 is discarded, emptying the stack and finishing the search.

Node 3 is the root of the DFS tree, with a tree edge to 4 and a forward edge to 5. Node 4 has tree edges to its two children, 2 and 5. Node 2 has a tree edge to 0 and a back edge to 3. Node 0 has a tree edge to 1. Node 1 has back edges to 2 and 3.

• (c, 10) Conduct a uniform-cost search of the directed graph D5 using the given edge costs, with start node 3 and only goal node 1. Indicate the order in which nodes enter and leave the priority queue.

We will denote items on the PQ as pairs of the form (node number, priority). (3, 0) goes on the PQ. It comes off, and (4, 2) and (5, 3) go on. (4, 2) comes off, and (2, 5) and (5, 4) go on. (5, 3) comes off, and nothing goes on. (5, 4) is discarded because 5 is on the closed list. (2, 5) comes off, and (0, 8) goes on (3 is on the closed list). (0, 8) comes off and (1, 10) goes on. (1, 10) comes off and we declare victory. We have found the path 3-4-2-0-1, of cost 10.

• (d, 5) Conduct an A* search of the directed graph D5 using the given edge costs, with start node 3 and only goal node 1. The heuristic function h for each node x should give h(x) = |x - 1|. (You may assume without proof that this heuristic is admissible and consistent.) Indicate the order in which nodes enter and leave the priority queue, and the priority assigned to each entry in that queue.

We denote items on the PQ by pairs of the form (node number, cost + heuristic). To begin, (3, 0+2) goes on. It comes off, and (4, 2+3) and (5, 3+4) go on. (4, 2+3) comes off, and (2, 5+1) and (5, 4+4) go on. (2, 5+1) comes off and (0, 8+1) goes on (3 is on the closed list). (5, 3+4) comes off and nothing goes on. (5, 4+4) is discarded as 5 is on the closed list. (0, 8+1) comes off and (1, 10+0) goes on. (1, 10+0) comes off and we declare victory.

We found the same path as in part (c) and actually did the same operations -- the only difference is that we explored 2 before 5 in the A* search because 2's heuristic was smaller.

• Question 4 (30): The following are fifteen true/false questions, with no explanation needed or wanted, no partial credit for wrong answers, and no penalty for guessing. Some of them refer to the scenarios of the other problems, and/or the entities defined above. Each one counts two points.

• (a) In the scenario of Question 1, whenever W(0) = W(1), we can be sure that W(0) = W(n) for all naturals n.

FALSE. If W(0) = W(1) = 0, for example, W(2) = 700 - 350√2 ≠ 0.

• (b) If f(0) = 4 and f(n+1) = f(n) + n - 3 for every natural n, then f(n) = (n2 - 5n + 8)/2 for every natural n.

FALSE. I had intended this to be true, but got the numbers wrong. If the formula were true, f(0) would be 4 but f(1) would be (1 - 5 + 8)/2 = 2, while the recursive definiton gives f(1) = f(0) + 0 - 3 = 1. The correct closed form of this recurrence is (n2 - 7n + 8)/2.

• (c) The set {0, 1, 2, 3, 4, 5, 6}, with the successor function S(i) = (i+1)%7, does not satisfy the fifth Peano axiom for naturals.

FALSE. The fifth axiom says that if you start at 0 and keep taking successors, you will reach all the numbers, and that is true in this system.

• (d) The operation of exponentiation on naturals, with E(x, y) = xy, is neither commutative nor associative.

TRUE. E(2, 3) ≠ E(3, 2), and E(2, E(3, 2)) ≠ E(E(2, 3), 2). I see that I asked this same question in Spring 2017.

• (e) If a binary string has odd length and has a first letter and a last letter that are different, it must contain either 00 or 11 as a substring.

TRUE. If a string does not have either of those two substrings, it must alternate 0's and 1's, and so if it has odd length its first and last letter will be the same.

• (f) The graph Dn is strongly connected if and only if n is even.

TRUE. We can always reach every node from 0, and reach n from any node, so we are strongly connected if and only if we can reach 0 from n. If n is even we can do so with the backwards edges. But if n is odd there is no edge out of node n and thus cannot reach 0 from it.

• (g) The graph Un is bipartite if and only if n ≤ 1.

TRUE. The graphs U0 and U1 are clearly bipartite. But all the other Un's contain a triangle with nodes 0, 1, and 2, and these three nodes cannot be split into two groups without an edge between two nodes in the same group.

• (h) Let Σ be a nonempty finite alphabet with k letters. Then the number of strings in Σn that never have the same letter twice in a row is (k-1)kn-1.

FALSE. The correct number is k(k-1)n-1, because we may choose any of the k letters to start and then have k-1 choices for each subsequent letter. Actually I neglected to specify that n should be positive, so the statement is also false because it gives (k-1)/k instead of 1 for n = 0.

• (i) A rooted binary tree has a number of leaves equal to its number of internal nodes.

FALSE. It has one more leaf than its number of internal nodes.

• (j) A generic search may fail to terminate even if the search space is finite.

TRUE. The generic search does not recognize previously seen nodes unless we say it does, so it could get caught in an infinite loop.

• (k) In part (d) of Problem 3, the heuristic h(x) = 2|x-1| would not have been both admissible and consistent. (It would fail to meet at least one of the conditions.)

TRUE. It turns out to be admissible, actually, but h(2) = 2, h(4) = 6, and there is an edge from 4 to 2 with cost 3, violating the consistency condition.

• (l) The call tree of our recursive `factorial` method has n-1 leaves for any input n with n > 1.

FALSE. It has one leaf, because this method only calls itself at most once per iteration So the call tree has a single branch and a single leaf.

• (m) There exists a boolean expression (as in Discussion #7) with more operators than primitive expressions.

TRUE. This would be false if there were only binary operators, but the expression "NOT NOT 0", for example, has two operators and only one primitive expression.

• (n) Suppose that the first player has two choices for a first move in a finite game, and the optimal move is to choose the left subtree. Then every leaf value in the left subtree must be greater than or equal to every leaf value in the right subtree.

FALSE. There could be a high value in the right subtree that does not figure into the value of the game because the second player can avoid it being the result of the game.

• (o) If a finite game has value v, then v must occur as a leaf value in its game tree.

TRUE. If both players play their optimal strategy, the result will be v, so they must reach a leaf with v as its leaf value.