# Solutions to Second Midterm Exam Spring 2016

### Directions:

• Answer the problems on the exam pages.
• There are four problems, some with multiple parts, for 100 total points plus 10 extra credit. Actual scale A = 93, C = 66.
• 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.

```  Q1: 20 points
Q2: 15 points
Q3: 40 points
Q4: 25+10 points
Total: 100+10 points
```

Question text is in black, solutions in blue.

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

Remember that the scope of any quantifier is always to the end of the statement it is in.

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

A binary string is a string over the alphabet {0, 1}, and the reversal wR of a binary string w is defined by the rules λR = λ and (wa)R = awR.

An undirected graph is said to be bipartite if it is possible to partition its nodes into two sets R and B, such that every edge of the graph has one endpoint in R and one in B. Equivalently, the nodes may be colored red and blue such that no edge connects red to red or blue to blue.

On a two-dimensional grid, the Manhattan distance between two points (a, b) and (c, d) is the minimum length of any path from one point to the other that follows the edges of the grid, that is, |a - c| + |b - d|.

The following scenario is used in Question 4. On a shelf in our house there are two containers of dog treats, one with large treats for Cardie and one with small treats for Duncan. Every day Cardie gets five large treats and Duncan gets seven small treats. If there are fewer than 20 large treats in Cardie's container at the beginning of the day, we add 48 more. If there are fewer than 20 small treats in Duncan's container at the beginning of the day, we add 90 more. (For example, suppose there are 23 treats in Cardie's container at the start of Day 0. Then there are 23 - 5 = 18 there at the start of Day 1, and 18 + 48 - 5 = 61 at the start of Day 2.

The function c(n) denotes the number of treats in Cardie's container at the start of Day n, and the function d(n) denotes the number of treats in Duncan's container at the start of Day n.

The n by n knight graph has an n by n square array of nodes, labeled as in chess positions with rows 1, 2,..., n and columns a, b,..., so the for example node d3 is in the third row and the fourth column. There is an undirected edge between two nodes whenever a chess knight can move from one to the other. (Chess knights move either two squares horizontally and one vertically, or one horizontally and two vertically.

This page shows pictures of the 3 by 3, 4 by 4, 5 by 5, and 6 by 6 knight graphs. (Note that neither the 1 by 1 nor the 2 by 2 knight graphs have any edges at all.) The 3 by 3 knight graph has 8 edges, and the 4 by 4 knight graph has 24.

Thus if f(n) is the number of edges in the n by n knight graph, we have all the values for n ≤ 4. By an analysis we won't explain here, for all n with n ≥ 4, f(n+1) is equal to f(n) + 20 + 4(2n - 7). You will need this formula for Question 2.

This page has a 4 by 4 knight graph with labels, which are not exactly the labels we are using. We are calling the nodes a1, a2,..., d4, and we will normally put the a nodes in a column rather than a row, but the 4 by 4 kn1ght graph you will search in Question 3 otherwise looks the same as this one.

• Question 1 (20): The following are ten true/false questions, with no explanation needed or wanted, no partial credit for wrong answers, and no penalty for guessing. Each one counts two points.

• (a) If a rooted tree has only one node, its depth is 1.

FALSE. The depth of a rooted binary tree is the length of the longest path from the root to a leaf. If there is only one node, the root is a leaf and the only such path has length 0, so the depth is 0.

• (b) Define a binary predicate X(i, j) on the naturals as follows. X(0, 0) is false, x(0, k) is true whenever k ≠ 0, and X(i+1, j+1) always has the same value as X(i, j). Then for any i and j, it must be true that X(i, j) is true if and only if i ≠ j.

FALSE. If i = j, we know that X(i, j) is false, and if i < j, we know that it is true. But the definition does not tell us whether X(1, 0), for example, is true. If it is true, then the claimed characterization of X(i, j) is false.

• (c) If an undirected graph with n nodes has fewer than n edges, it cannot have a cycle.

FALSE. A connected graph needs as many edges as vertices in order to have a cycle. But a graph consisting of a triangle and a separate isolated node has four nodes and three edges, and yet has a cycle.

• (d) For any binary strings u and v, (uRvR)R = uv.

FALSE. The left-hand side is equal to vu, and in general vu is not equal to uv.

• (e) A generic search may continue after a goal node has been put on its open list.

TRUE. The generic search ends when a goal node comes off the open list.

• (f) Let G be a two-player deterministic game with perfect information and a finite game tree with real-number values at its leaves. It is possible that G has value x, but that none of the leaves of the tree has value x.

FALSE. The value of a finite game of this kind is the value of the leaf reached when both players play optimally. (If there are multiple leaves of this value, it might be possible for optimal moves to reach more than one of them, but the Determinacy Theorem means that there is at least one such leaf.) Thus the value of such a game is the value of some leaf.

• (g) Let G be a game as in part (f), where White (who wants the game result to be as large as possible) has the first move, and every leaf in the left subtree of the root node has value at least x. Then the value of the game is at least x.

TRUE. White can guarantee a payoff of at least x by moving to the left subtree, since then the result of the game must be one of the leaves in that subtree. She will thus make this move unless she is assured of an equal or greater payoff by moving right -- in either case her payoff will be at least x.

• (h) There exists a rooted binary tree T, with a positive even number of nodes, such that every internal node of T has exactly two children. (Recall that a node of such a tree is called an internal node if and only if it is not a leaf.

FALSE. In such a tree every leaf has a sibling leaf, so the number of leaves must be even. The number of internal nodes of an n-node binary tree is always n-1, so in such a tree the number of internal nodes is odd and thus the total number of nodes is odd.

This is also easy to prove by induction. The base case is a one-node tree, which of course has an odd number of nodes. The inductive step is to have a tree with a root and two subtrees, and the IH tells us that each of these subtrees has an odd number of nodes. The sum of 1 (for the root) and two odd numbers is necessarily odd.

• (i) Let P(w) be a predicate with one free variable w of type "binary string". It is possible that P(λ) is true and that P(w) implies ((P(w0) ∧ P(w1)) for any string w, but that there exists a string v sich that P(v) is false.

FALSE. The Law of Induction for binary strings says that if this base case and this inductive step are both true, then P(v) must be true for all binary strings v.

• (j) For every positive natural n, there is an arithmetic expression En such that (1) the only operators of En are + and ×, (2) the only atomic values of En are prime numbers, and (3) En evaluates to n. (Note that the empty string λ is not an arithmetic expression.)

FALSE. For n ≥ 2 this is true, since any such natural can be factored into primes. But there is no such arithmetic expression E1, because by induction it is easy to see that every arithmetic expression of this kind has a value greater than 1. (For the base case, all primes are greater than 1, and for the inductive case the sum and product of two numbers greater than 1 is itself greater than 1.)

• Question 2 (15): Let f(n) be the number of edges in the n by n knight graph, as defined above. Prove that for all naturals n with n ≥ 1, f(n) = 4(n - 1)(n - 2). (Hint: Check enough base cases until you can use the formula given above for your inductive step.)

We are told that f(1) = 0, f(2) = 0, f(3) = 8, and f(4) = 24. Each of these four cases matches the formula f(n) = 4(n - 1)(n - 2) because 0 = 4(0)(-1), 0 = 4(1)(0), 8 = 4(2)(1), and 24 = 4(3)(2). We must do all four of these base cases because the formula we are given for f(n+1) is only valid for n ≥ 4. Thus our inductive step will prove P(4) → P(5) but not P(3) → P(4), and we need P(4) as a base case.

For the inductive step, we assume that f(n) = 4(n-1)(n-2) and use the formula that tells us f(n+1) = f(n) + 20 + 4(2n - 7). Using the IH to substitute for f(n), we get f(n) = 4(n-1)(n-2) + 20 + 4(2n-7) = 4[(n-1)(n-2) + 5 + 2n - 7] = 4(n-1)[(n-2) + 2] = 4(n+1-1)(n+1-2). This matches the formula for f(n+1) and we have completed the inductive step and the proof.

• Question 3 (40): These questions all involve searches of the 4 by 4 knight graph, defined above. Its sixteen nodes are labeled a1, a2,..., d3, d4.

• (a, 5) Carry out a depth-first search of the graph with start node a1 and goal node c3. Assume that once a node has been put on the open list, it is recognized and not put on the open list again.

There are various ways this could go, depending on the choice of which node to take off the open list when there are ties -- I allowed any choice that respected the definition of the stack. I also allowed either recognizing nodes on the list (as I said) or the usual method of putting nodes on a closed list when they come off the open list. Here is one solution:

• Put a1 on the list.

• Take a1 off, put b3 and c2 on, stack = (b3, c2).

• Take b3 off, put c1, d2, d4 on, stack = (c1, d2, d4, c2).

• Take c1 off, put a2, d3 on, stack = (a2, d3, d2, d4, c2).

• Take a2 off, put b4, c3 on, stack = (b4, c3, d3, d2, d4, c2).

• Take b4 off, recognize c2 and d3, stack = (c3, d3, d2, d4, c2)

• Take c3 off and declare victory.

• (b, 5) Carry out a breadth-first search of the graph with start node a1 and goal node c3. As in part (a), assume that once a node has ben put on the open list, it is recognized and not put on the open list again.

Here I choose to enqueue neighbors in alphabetical order -- other choices were allowed.

• Put a1 on the queue.

• Take a1 off, put b3, c2 on, queue = (b3, c2). Note that the queue now holds the two nodes at distance exactly one from a1.

• Take b3 off, put c1, d2, d4 on, queue = (c2, c1, d2, d4).

• Take c2 off, put a3, b4 on, recognize d4, queue = (c1, d2, d4, a3, b4). Note that the queue now holds the five nodes at distance exactly 2 from a1.

• Take c1 off, put a2, d3 on, queue = (d2, d4, a3, b4, a2, d3).

• Take d2 off, put b1, c4 on, queue = (d4, a3, b4, a2, d3, b1, c4).

• Take d4 off, recognize b3, c2, queue = (a3, b4, a2, d3, b1, c4).

• Take a3 off, recognize b1, c4, queue = (b4, a2, d3, b1, c4).

• Take b4 off, recognize a2, d3, queue = (a2, d3, b1, c4). Note that the queue now holds the four nodes at distance exactly three from a1.

• Take a2 off, put c3 on, recognize b4, c1, queue = (d3, b1, c4, c3).

• Take d3 off, put b2 on, recognize c1, b4, queue = (b1, c4, c3, b2).

• Take b1 off, recognize a3, c3, d2, queue = (c4, c3, b2).

• Take c4 off, recognize a3, b2, d2, queue = (c3, b2). Note that the queue now holds the two nodes at distance exactly four from a1.

• Take c3 off and declare victory.

• (c, 15) Carry out another breadth-first search of the graph with start node a1, but this time with no goal node. (You may refer to your work in part (b) and use it without copying it.) Redraw the graph as a BFS tree, indicating both tree and non-tree edges.

Continuing from part (b):

• Take c3 off, put a4, d1 on, recognize a2, b1, queue = (b2, a4, d1).

• Take b2 off, recognize a4, c4, d1, d3, queue = (a4, d1). Note that the queue now holds the two nodes at distance exactly five from a1.

• Take a4 off, recognize b2 and c3, queue = (d1).

• Take d1 off, recognize b2 and c3, queue = (empty).

• Declare defeat.

The sixteen nodes and 24 edges (15 tree, 9 non-tree) appear in the BFS tree as follows:

• Level 0: Node a1 with tree edges to b3 and c2.

• Level 1: Node b3 with tree edges to c1, d2, d4; Node c2 with tree edges to a3 and b4, non-tree edge to d4.

• Level 2: Node c1 with tree edges to a3, d3; Node d2 with tree edges to b1, c4; node d4 with no more edges, Node a3 with non-tree edges to b1 and c4; Node b4 with non-tree edges to a2 and d3.

• Level 3: Node a2 with tree edge to c3; Node d3 with tree edge to b2, node b1 with non-tree edge to c3; Node c4 with non-tree edge to b2.

• Level 4: Node c3 with tree edges to a4 and d1; Node b2 with non-tree edges to a4 and d1.

• Level 5: Leaf nodes a4 and d1.

• (d, 5) Recall the definition of a bipartite graph given above. Is the 4 by 4 knight graph bipartite? How can you tell that it is our isn't, given the BFS tree from part (c)?

This graph is bipartite. All edges, tree or non-tree, in the BFS tree go from some level k to level k+1. Thus we can make all nodes in even-numbered levels red, for example, and all nodes in odd-numbered levels blue, and every edge will have an endpoint of each color.

• (e, 10) Label the edges of the 4 by 4 knight graph such that edges that go two steps vertically, such as from a1 to b3, are labeled 3, and edges that go two steps horizontally, such as from a1 to c2, are labeled 5. Conduct a uniform-cost search of the weighted graph, with start node a1 and goal node c3.

Here entries in the priority queue will be node number and distance found from a1:

• Put a1-0 on.

• Take a1-0 off, put b3-3 and c2-5 on.

• Take b3-3 off, put c1-6, d2-8, d4-8 on.

• Take c2-5 off, put a3-10, b4-8, d4-8 on.

• Take c1-6 off, put a2-11, d3-9 on.

• Take d2-8 off, put b1-13, c4-11 on.

• Take d4-8 off, put nothing on.

• Take b4-8 off, put a2-11 on.

• Discard second copy of d4-8.

• Take d3-9 off, put b2-14 on. (b4 is on closed list)

• Take a3-10 off, put b1-13, c4-15 on.

• Take c4-11 off, put b2-16 on.

• Take a2-11 off, put c3-16 on.

• Take b1-11 off, put c3-16 on.

• Take b2-14 off, put a4-17, d1-a9 on.

• Take c3-16 off and declare victory. The only remaining nodes on the priority queue are a4-17 and d1-19.

• (f, 5) Suppose we want to convert our uniform-cost search of the labeled graph of part (e) into an A* search. Would Manhattan distance (as defined above) from c3 be a suitable heuristic? Why or why not?

The heuristic will be admissible and consistent because the Manhattan length of a knight's move is always 3 and these edges are given length either 3 or 5. For any path of knight's moves from a node to the goal, there must be a corresponding path of grid edges of equal or shorter cost, and so the Manhattan distance will always be between 0 and the distance in the weighted graph. To check consistency, consider any knight move from node x to node y. Because there is a path of grid edges from y to x of cost less than that of the knight move, we must have h(x) ≤ h(y) + c(x, y). (Proving this formally would involve the triangle inequality that says that for any three nodes x, y, and z, MD(x, z) ≤ MD(x, y) + MD (y, z), where MD is Manhattan distance.)

Many people argued that this heuristic would not be useful, without considering whether it met the conditions to be used in an A* search. It doesn't appear to be very helpful on this small graph, but on a larger version of the graph it would do some good by delaying consideration of paths that go a long way in the wrong direction.

• Question 4 (25+10): These questions involve the dog treat scenario defined above.

• (a, 5) As above, let c(n) and d(n) be the number of treats in Cardie's and Duncan's containers, respectively, at the start of Day n. Write down rules explaining how c(n+1) and d(n+1) depend on c(n) and d(n), respectively. (Hint: Use a definition by cases or Java `if` statements.)

If c(n) < 20, c(n+1) = c(n) + 48 - 5, else c(n+1) = c(n) - 5.

If d(n) < 20, d(n+1) = d(n) + 90 - 7, else d(n+1) = d(n) - 7.

Some people did not give the dogs their deserved treats on days their container was restocked, and others checked for 20 treats after giving the dogs their treats, rather than before as indicated by the definition and the example.

• (b, 5) Assume that c(0) = 34 and that d(0) = 46. Find the values of c(7) and d(7).

The successive values of c(n) are 34, 29, 24, 19, 62, 57, 52, 47, so that c(7) = 47. Checking for 20 treats at the end of the day gave the same answer and I did not penalize this. The values of d(n) are 46, 39, 32, 25, 18, 101, 94, 87, so that d(7) = 87.

• (c, 15) With the assumptions of part (b), prove by induction that for every natural n, c(n) + d(n) (the total number of treats in the two containers at the start of Day n) is not divisible by 6. (Hint: Use ordinary induction, referring to the rules in part (a) for your inductive step.

P(n) says that c(n) + d(n) is not divisible by 6.

For the base case, we compute c(0) + d(0) = 34 + 46 = 80 and note that 80 is not divisible by 6.

We then assume that c(n) + d(n) is not divisible by 6, and compute c(n+1) + d(n+1) from c(n) + d(n). There are four cases depending on which containers, if any, are restocked:

• If neither is restocked, c(n+1) + d(n+1) = c(n) - 5 + d(n) - 7 = c(n) + d(n) - 12.

• If only Cardie's container is restocked, c(n+1) + d(n+1) = c(n) + 43 + d(n) - 7 = c(n) + d(n) + 36.

• If only Duncan's container is restocked, c(n+1) + d(n+1) = c(n) - 5 + d(n) + 83 = c(n) + d(n) + 78.

• If both containers are restocked, c(n+1) + d(n+1) = c(n) + 43 + d(n) + 83 = c(n) + d(n) + 126.

In each of the four cases, the sum changes by a multiple of 6 (-12, 36, 78, and 90 are each divisible by 6). Thus if the former sum is not divisible by 6 as we assume, the new sum has the same remainder modulo 6 and is thus also not divisible by 6. This is the inductive step of an ordinary induction proving P(n) is true for all naturals n.

• (d, 10XC) Using the assumptions of parts (b) and (c), prove the following statement:

∃x: ∀y: ∃n: (n > y) ∧ (c(n) + d(n) = x)

(Hint: One way to do this is by contradiction. Your proof may be informal if it is convincing.)

The most common argument I got for this was essentially "let y be arbitrary, let n = y + 1, and then c(n) + d(n) is some integer x because we never run out of treats". This ignores the order of quantifiers, proving the true but irrelevant statement "∀y: ∃n: ∃x: (n > y) ∧ (c(n) + d(n) = x)". I gave this answer two of ten points -- be warned that order of multiple quantifiers is now sure to come up on the final exam.

The first step in the actual proof is to decide what value x should have so that we can prove the remainder of the statement for that value and then use the Rule of Existence. What does the statement actually mean? It says "there is a value of x such that for any natural y, c(n) + d(n) takes on the value x after step y", or more simply "there is a value of x such that infinitely often, c(n) + d(n) = x".

Could this fail to be the case? Let's look at the negation of the given statement: "∀x: ∃y: ∀n: (n > y) → c(n) + d(n) ≠ x". (This translation uses the Definition of Implication.) This says that for every value of x, there is a y that gives the last time c(n) + d(n) takes on the value y. But there are only finitely many possible values of x, since c(n) is always between 15 and 62, and d(n) is always between 13 and 102. If we take the finitely many values of y given by the negation of the statement, and take the largest of these values, then for n larger than that, c(n) + d(n) cannot take on any of the possible x values, and this is a contradiction.

We can also prove this without contradiction. Because there are only finitely many choices of c(n) and d(n), we must eventually have two different values m and n such that c(m) = c(n) and d(m) = d(n). But since the process of changing c's and d's is deterministic, if the pair of value occurs twice it will keep occurring periodically forever. For that particular value of c(n) + d(n), then, we will have that value occur infinitely many often.

Reasoning similar to the Chinese Remainder Theorem can give us an explicit answer to the question. In the first 48 days, Cardie will eat 5*48 = 240 treats, and her container will be restocked with some multiple of 48 treats. This multiple must be 5 because c(48) could not possibly be 48 greater than or 48 less than c(0) = 34. Thus c(48) = 34, and similarly d(90) = 46. Therefore every 48*90 days, we have the value of c(n) + d(n) = 90 recurring, and we can take x to be 80 in the given statement. (Since the least common multiple of 48 and 90 is actually 720, we will also have a value of 80 every 720 days.)