# Solutions to Second Midterm Exam Spring 2018

### 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 = 90, C = 60.
• 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.

Exam text is in black, solutions in blue.

```  Q1: 20 points
Q2: 20+10 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,...}.

For every natural n, we define a directed graph Gn as follows. G0 has a single node with no edges, and we will call that one node its root. For any natural n, Gn+1 consists of two copies of Gn, one new node which is the root of Gn+1, and three new edges -- one from the new root to the root of each of the Gn's, and one from one of the Gn roots to the other.

``````
Directed graphs G_0, G_1, G_2, G_3.  "V" and ">" are arrowheads.

G_0   a     G_1     a      G_2          a
/ \                 / \
V   V               V   V
b --> c             b --> c
/ \   / \
V   V V   V
d -> e f -> g

G_3                a
/ \
/   \
/     \
/       \
V         V
b --------> c
/ \         / \
V   V       V   V
d --> e     f --> g
/ \   / \   / \   / \
V   V V   V V   V V   V
h -> i j ->k l-> m n -> o

``````

The labeled undirected graph OC represents an imagined orienteering course, with seventeen nodes and 25 edges. Each edge is labeled "run", "walk", or "fight", indicating that it takes two, four, or six minutes respectively for an ordinary human competitor to traverse the edge.

``````
Map of orienteering course -- edges are run (r), walk (w), or fight
(f). Normal human competitors take 2 minutes to run, 4 to walk,
6 to fight.  The Hulk takes 1 minute to traverse any edge.

(a)--r--(d)--w--(i)--f--(l)--w--(o)
/            \             |       |
r                f           f       w
/                   \         |       |
(s)--w--(b)--w--(e)--f--(j)--w--(m)--w--(g)
\                   / |     /         |
r               f   f   f           r
\           /     | /             |
(c)--f--(f)--f--(k)--r--(n)--f--(p)
\     |     /
w   w   w
\ | /
(h)

``````

• Question 1 (20+10): I need a new plan to allocate treats to my dogs, Cardie and Duncan. On day i, starting with day 1, I will give ci treats to Cardie and di treats to Duncan. For any natural n, I need to calculate the sum sn = ∑i=in (ci + di), the total number of treats I will need to supply both dogs for the first n days of the plan.

• (a, 10) Duncan proposes that he gets one treat on day 1, and twice as many each succeeding day as on the day before, so that di+1 = 2di for all positive n. Cardie proposes that she get one more treat than Duncan on each day, so that ci = di + 1 for all positive i. Prove by induction for all positive naturals n that under this plan, sn = 2n+1 + n - 2.

For the base case of n = 1, s1 is 3 because Duncan gets one treat and Cardie gets two. This matches the given formula because 21+1 + 1 - 2 = 3. Note that to prove the base case, you need to determine both s1 and the value given by the formula, and show that they are the same.

For the inductive case, we have that sn+1 = sn + cn+1 + dn+1. We know that sn = 2n+1 + n - 2 from the inductive hypothesis, so what we need is cn+1 and dn+1. We are not given these quantities directly, but we are given recurrence information that lets us determine them. For full credit, I wanted a proof that the numbers you used for these two quantities were correct. Since we are given that d1 = 1 and dn+1 = 2dn, we can prove that di = 2i-1 for any i: for the base case 21-1 = 1, and given that dn = 2n-1 from the IH, dn+1 = 2n follows from the recurrence for di.

Once we know that dn+1 = 2n, and consequently cn+1 = 2n + 1, we can calculate sn+1 = sn + cn+1 + dn+1 = 2n+1 + n - 2 + 2n + 1 + 2n = 2n+2 + (n+1) - 2, matching the formula for sn+1. This completes the induction and the proof.

• (b, 10) My counterproposal is that Duncan gets one treat on day 1 and one more on each succeeding day, so that di+1 = di + 1 for all positive i. Cardie again gets one more treat than Duncan each day, so that ci = di + 1 for all positive i. Prove by induction for all positive naturals n that under this plan, sn = n2 + 2n.

Again the base case of n = 1 has one treat for Duncan and two for Cardie, and 12 + 2(1) = 3 so this matches the formula.

For the inductive case, we need a formula for di, and we are given that d1 = 1 and dn+1 = dn + 1. This lets us prove di = i by induction. The base case is clear, and dn+1 = n+1 follows from the IH of dn = n by the rule for d. And of course we now know that ci = i + 1 for any i.

Now we can calculate sn+1 = sn + cn+1 + dn+1 = n2 + 2n + (n+1) + n = n2 + 4n + 3 = (n+1)2 + 2(n+1). This completes the induction and the proof.

• Question 2 (20): The directed graph family Gn is defined above, whith diagrams of Gi for i ≤ 3. G0 has a single node with no edges, and we will call the one node its root. For any natural n, Gn+1 consists of two copies of Gn, one new node which is the root of Gn+1, and three new edges -- one from the new root to the root of each of the Gn's, and one from one of the Gn roots to the other.

• (a, 5) Carry out a depth-first search, recognizing previously seen nodes, for the directed graph G2 with the nodes labeled {a, b, c, d, e, f, g} as above, with start node a and no goal node. When two nodes go on the open list at the same time, they should come out in alphabetical order, with the node earlier in the alphabet coming out first. Show the contents of the stack at each step of the search.

We begin with a on the stack. We pop a and push its neighbors b and c, making the stack (b, c). We pop b and push its neighbors c, d, and e, making the stack (c, d, e, c). We pop c and push its neighbors f and g, making the stack (f, g, d, e, c). We pop f and push its neighbor g, making the stack (g, g, d, e, c). We pop g and push nothing. We pop g again, discovering the forward edge from c to g. We pop d and push its neighbor e, making the stack (e, e, c). We pop e and push nothing. We pop e again, discovering the forward edge from b to e. Finally we pop c, discovering the forward edge from a to c.

In our depth-first search, a node is marked as closed, or put on the closed list, once it has come off the open list. So it is important that c, for example, is put on the open list a second time when it is found from b, and that this entry for c is the one that is processed later. Thus the edge from b to c becomes a tree and not a cross edge.

Many people had a different conception of the stack behavior in which node a was put on at the beginning and not popped to the end. This corresponds to the method stack in a recursive implementation of DFS, but not to the DFS from this course, which is defined in terms of our generic search.

• (b, 5) Draw the depth-first search tree of the search in part (a). Your tree should contain all nine edges of the original graph, with some marked as tree edges and the others marked as back, forward, or cross edges as appropriate.

Root node a has only child b, which has children c and d. Node c has only child f and grandchild g, while node e has child e. That gives all six tree edges -- as noted above, the three forward edges are (a, c), (b, e), and (c, g). There are no back or cross edges in this search.

If you did the search without putting nodes on the open list for the second time, the search tree has node a with children b and c, and grandchildren d, e, f, and g, with cross edges (b, c), (d, e), and (f, g). I gave full credit for this tree if you did the search that way, though of course in that case you did not get full credit in part (a).

• (c, 10) Prove by induction on all naturals n that the graph Gn contains exactly 3(2n - 1) edges.

For the base case, G0 has no edges, and the formula gives 3(20 - 1) = 0. Again, a correct base case states both these numbers and notes that they are the same.

By the definition of Gn+1, its number of edges is 3, plus twice the number of edges in Gn. This latter number is 3(2n - 1) by the IH, so the new number is 3 + 2(3(2n - 1) = 3(2n+1 - 1) as the formula says. This completes the induction and the proof.

There are other relationships between Gn and Gn+1 besides the one given by the definition. For example, Gn+1 can be made from Gn by adding three-edge triangles to each of its 2n leaves, and this is the basis of an induction proof that many of you gave for this question. The problem with that proof is that the inductive step depends on properties of the Gn's that you have not proved from the definition, in contrast to the proof above that uses only the definition. How do you know that there are exactly 2n leaves in Gn? Or that the first n levels of Gn+1 are the same graph as Gn?

• (d, 10XC) Prove by induction on all naturals n that the longest path in the graph Gn has exactly 2n edges. You must show both that there is a path of that length, and that there is no longer path. It might be useful to make the endpoints of this path part of your statement P(n), so that you may use facts about those endpoints in your inductive hypothesis.

This was a challenging problem in part because the fact it proves is almost obvious. The question is why, given the definition of the graph family, it has to be true.

The base case of the induction is simple. With n = 0, there are no edges, and so the longest path is the empty path from the root to itself. This path has length 0, which is what we want since 2n here is 0.

For the inductive case, though, proceeding in the simplest way runs into problems. Suppose we take P(n) to be just "the longest path is of length 2n". The IH tells us that there is a path of length 2n in Gn, and no longer path. But it doesn't tell us where that path goes, so we cannot yet say that there is a longer path using the three new edges.

A better P(n) is "there is a path from the root of Gn of length 2n, and no path anywhere in Gn of length longer than 2n". If we have this statement as our IH, we can say first that we can make a path of length 2n+2 from the root of Gn+1, by taking the edge from the new root to one old root, then the edge to the other old root, then following the assumed path inside of that copy of Gn. This path exists, so we have only to show that there is no longer path. Once a path in Gn+1 takes an edge other than one of the three new edges, it cannot leave the copy of Gn containing that edge. So any path in Gn+1 must be in one of those copies, except perhaps for some of the three new edges. At most two of those can be put into any path, and by assumption we can have at most 2n edges within the Gn, so the path can have at most 2n+2 edges. This completes the induction and the proof.

Question 3 (30): In the sport of orienteering, competitors run from one point to another in a forest, using a map to determine their best choice of route. Above we have an imagined orienteering course, the labeled undirected graph OC. There are seventeen nodes representing points in the forest, and edges indicating where a competitor might travel from one point to another. For ordinary human competitors, an edge labeled "r" (for run) can be traversed in two minutes, an edge labeled "w" (walk) in four, and an edge labeled "f" (fight) in six. Our eventual goal is to determine the optimal route from point s to point g.

The last competitor will be Marvel superhero The Incredible Hulk, who can traverse any edge in one minute. (We will have him run last since he will alter the terrain as he passes through it.)

• (a, 10) For any node x in the graph OC, we are going to define h(x) to be the time that the Hulk would require to run from x to g, Conduct a breadth-first search of OC with start node g and no goal node. Draw the resulting BFS tree, showing all the edges as either tree edges or non-tree edges. Explain how this search gives you the value of h(x) for every node x, and list those values.

We'll assume here that nodes entering the queue at the same time come off in alphabetical order, though that was not a condition of the problem.

• Put g on the queue.
• Take off g, put on m, o, and p.
• Take off m, put on j, k, and l. (g is on the closed list.)
• Take off o, put on l, queue is now (p, j, k, l, l).
• Take off p, put on n. (j, k, l, l, n)
• Take off j, put on d, e, f, k.
• Take off k, put on f, h, (l, l, n, d, e, f, k, f, h)
• Take off l, put on i. Discard the second l.
• Take off n, put on k. (d, e, f, k, f, h, i, k)
• Take off d, put on a.
• Take off e, put on b.
• Take off f, put on c and h.
• Discard k and f. (h, i, k, a, b, c, h)
• Take off h, put on c.
• Take off i, put on nothing (d is on the closed list).
• Discard k. (a, b, c)
• Take off a, put on s. Don't stop until s comes off!
• Take off b and c, put on s twice more.
• Take off s and declare victory, with the final queue (s, s)

The tree has sixteen tree edges and nine non-tree edges. We can find them from the trace above by seeing where the first copy of each node on the queue comes from: (g, m), (g, o), (g, p), (m, j), (m. k), (m, l), (p, n), (j, d), (j, e), (j, f), (k, h), (l, i), (d, a), (e, b), (f, c), (a, s). The nine other edges are non-tree: (o, l), (n, k), (j, k), (k, f), (k, h), (i, d), (h, c), (b, s), (c, s).

The h values are given by the levels of this tree, since it finds the path with the fewest edges between g and each node x, and this path is an optimal one for the Hulk since all edges are the same cost for him. We have h(x) = 0 for x = g, h(x) = 1 for x = m, o, or p, h(x) = 2 for x = j, k, l, or n, h(x) = 3 for x = d, e, f, h, or i, h(x) = 4 for x = a, b, or c, and h(s) = 5.

• (b, 10) Begin a uniform-cost search of OC with start node s and goal node g, using the human times for the edge costs. Stop your search after you have taken six nodes off of the open list.

• Put s0 on the PQ.
• Take off s0, put on a2, b4, and c2. (a2, c2, b4)
• Take off a2, put on d4. (c2, b4, d4)
• Take off c2, put on f8, h6. (b4, d4, h6, f8)
• Take off b4, put on e8. (d4, h6, f8, e8)
• Take off d4, put on i8, j10. (h6, f8, e8, i8, j10).
• Take off h6, put on f10, k10. (f8, e8, i8, j10, f10, k10)

Note that there is no particular reason one node with minimum value should come off the PQ before another, so there is more than one correct sequence of moves. But nodes must come off the PQ in order of smallest distance from the start node.

• (c, 10) Conduct a complete A* search of OC with start node s and goal node g, using the human times for the edge costs and the Hulk's function h(x), computed in part (a), as a heuristic. Indicate which nodes are on the priority queue at each stage of the search.

• Put s0+5 on the PQ.
• Take off s0+5, put on a2+4, b4+4, c2+4.
• Take off a2+4, put on d4+3.
• Take off c2+4, put on f8+3, h6+3.
• Take off d4+3, put on i8+3, j10+2.
• Take off b4+4, put on e8+3. (f11, h9, i11, j12, e11)
• Take off h6+3, put on f10+3, k10+2.
• Take off f8+3, put on j14+2, k14+2.
• Take off i8+3, put on l14+2.
• Take off e8+3, put on j14+2. (f12, f13, k12, j16, k16, l16)
• Take off j10+2, put on k16+2, m14+1.
• Take off k10+2, put on n12+2, j16+2, m16+1.
• Discard f10+2, f11+2. (j16, k16, l16, n14, j18, m15, m17, k18)
• Take off n12+2, put on p18+1.
• Take off m14+1, put on g18+0. Don't stop yet!
• Take off l14+2, put on o18+1, m20+1. (g18, j18, k18, m17, p19, o19)
• Take off g18+0 and declare victory, path is s-a-d-j-m-g.

• 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 both the inductions of Question 1, the statements P(0) are false.

FALSE. Both are true, since 20+1 + 0 - 2 and 02 + 2(0) both evaluate to 0, and the sums also evaluate to 0 when n = 0 because there are no terms to be added.

• (b) If ∀n:P(n) can be proved by strong induction, then there exists a statement Q(n) such that ∀n:Q(n) can be proved by ordinary induction, and the statement ∀n:Q(n) → P(n) is true.

TRUE. Q(n) can be taken to be the statement ∀i: (i ≤ n) → P(i). The inductive step of an ordinary induction proof of ∀n:Q(n) is just the strong inductive step for the strong induction proof of ∀n:P(n). And clearly Q(n) implies P(n) for any n, since we can specify the universal quantifier in Q(n) to i = n to get "(n ≤ n) → P(n)", which clearly implies P(n).

• (c) Define the function f from binary strings to binary strings by the rules f(λ) = 0, f(w0) = f(w), and f(w1) = f(w)1. Then f(10010) = 011.

TRUE. This function deletes all the zeroes in w, and places a single 0 at the start. This creates 011 from 10010.

• (d) The statement ∀x:S(0) + x = S(x), where S is the successor function, is not part of the definition of addition but can be proved from that definition using induction.

TRUE. The similar statement in the definition of addition is ∀x:∀y:x + S(y) = S(x + y).

• (e) It is possible to conduct a generic search of the undirected graph OC, with start node s and goal node g, such that g is the fifth node to come off the open list. (Here s will be the first node to come off the open list.)

FALSE. A node can go on the open list only when a node with an edge to it is processed. Thus the n'th node to come off the list must be at the end of a path from s, of length at most n-1. But the shortest path in OC from s to g has length 5, so that g must be the sixth or later node off of the list.

• (f) The undirected graph OC is bipartite.

FALSE. It has lots of triangles (sets of three nodes with edges between each pair). A triangle is a 3-cycle, and a bipartite graph can not have any cycles of odd length.

• (g) If we create a DFS tree for a search of the undirected graph OC with start node s, every edge will become either a tree edge or a back edge.

TRUE. Any connected undirected graph has this property. Cross and forward edges only occur in a DFS of a directed graph.

• (h) If we conduct a breadth-first search of the directed graph G2, with start node a and no goal node, we will get the same tree edges as in the DFS search of question 2 part (a).

FALSE. The correct DFS search from 2(a) reaches node c from node b, while the BFS finds c from node a.

• (i) For every natural n, the graph Gn is a directed acyclic graph.

TRUE. By induction on n, it is easy to show that Gn has no directed cycles. The base for n = 0 is clear, and once any path in Gn+1 enters one of the copies of Gn, it cannot leave, and by the IH it cannot revisit any node within that copy. And clearly there is no cycle that does not enter one of the two Gn's.

• (j) For every natural n, the graph Gn is not a tree.

FALSE. G0 is a rooted tree, though none of the others are.

• (k) For any natural n, any generic search of the graph Gn from any start node will eventually terminate, even if previously seen nodes are not recognized.

TRUE. An infinite search can only occur when the directed graph has a directed cycle, which none of the Gn's do.

• (l) The function 2h(x) would be an admissible and consistent heuristic for an A* of graph OC with goal node g.

TRUE. Any function based on a distance will be consistent. It is admissible because for any node x, the human time from x to g must be at least twice the Hulk's time, as the human takes at least two minutes for each edge.

• (m) The function 2h(x) would be an admissible and consistent heuristic for an A* of graph OC with goal node g.

FALSE. It will be consistent but not admissible. A human can reach g from p in two minutes, but 4h(p) is 4.

• (n) Consider a game where White chooses "left" or "right", then Black chooses "left" or "right", then the game ends. The four leaves of the game tree are labeled 1, 2, 3, and 4 in some order. Then for each number x from 1 through 4, it is possible to label the game tree to make x the value of the game, where White wants a large number and Black a small one.

FALSE. Each player can avoid the worst outcome for them with their single choice, and their optimal strategy is certainly to do this. So the result of the game under optimal play by both cannot be 1 or 4, only 2 or 3.

• (o) Consider a game where White and Black alternately name bits (0 or 1) until each has named three bits and they have formed a binary string of length six. White wins the game if the resulting string represents a prime number (possibly with leading zeroes). Then White has a winning strategy for this game.

FALSE. Black has a winning strategy. If he moves 1 on his first move and 0 on his third move, the string will be *1***0, with some bit replacing each of the four stars. This is an even number greater than 2, and cannot be prime.