- 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 = 90, C = 58.
- Some useful definitions precede the questions below.
- No books, notes, calculators, or collaboration.
- In case of a numerical answer, an arithmetic expression like
"2
^{17}- 4" need not be reduced to a single integer.

Question text is in black, solutions in blue.

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

Question 3 uses the following real-Java class, whose objects are
binary search trees. Every node, leaf or not, contains a
`String`

data item.

```
public class BST {
boolean isLeaf;
String data;
BST left, right;
public BST (String w) {isLeaf = true; data = w;}
public BST (String w, BST lTree, BST rTree) {
isLeaf = false; data = w; left = lTree; right = rTree;}
// getters and setters
// instance methods added in Question 3
}
```

A `BST`

object is defined to be **valid** if for
every non-leaf node v, every item in the left subtree of v is strictly
less than the data item of v itself, and every data item in the right
subtree of v is strictly greater than that of v itself.

You will probably want the following two methods of the real Java
`String`

class. If x and y are `String`

objects, `x.equals(y)`

returns `true`

if and
only if x and y have the same letters in the same order. And
`x.compareTo(y)`

returns an `int`

that is
negative if x comes before y in alphabetical order, 0 if they are
equal, and positive if y comes before x.

Question 4 uses the undirected graph G, and the implicit directed graph D, given below. Each graph has the same six nodes, each representing a town in the Pioneer Valley. In G, there is an edge from x to y if it is possible to drive directly from x to y. Each node in G is also labeled with the value h(x), which is the driving time (in minutes) from x to Northampton (Ntn) with no traffic.

The directed graph D has two directed edges for each edge in G. The edge from x to y, if it exists, is labeled with the driving time from x to y during rush hour. This value is given in the row for x and the column for y in the matrix below. A "--" entry in the matrix indicates a pair of towns for which there is no direct connection.

```
Undirected graph G representing road connections in the Pioneer Valley:
18 Sun -------------
/ \ \
/ \ \
/ \ \
/ \ 16 \ 25
| NHa --------- NAm
| | |
| | |
| | |
| 0 | 10 | 18
Ntn -------- Had --------- Amh
(Numbers by towns represent h(x), driving time to NO without traffic.)
Labels for directed graph D (single-step cost matrix for rush hour):
Amh Had NAm NHa Ntn Sun
Amh 0 15 15 -- -- --
Had 12 0 -- 8 30 --
NAm 19 -- 0 10 -- 14
NHa -- 10 9 0 -- 12
Ntn -- 15 -- -- 0 20
Sun -- -- 11 12 22 0
```

**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)
Let P(n) be a predicate on naturals. If P(0) and P(1) are true, and
∀n: P(n) → (P(2n) ∧ P(2n+1)) is true, then
∀n:P(n) must be true.
TRUE. By strong induction, we have P(0) and P(1), and then for every n with n ≥ 2, if we have P(k) for each k ≤ n we also have P(n+1) because we have P((n+1)/2).

- (b)
Let P(n) be a predicate on naturals. If P(0) and P(1) are true, and
∀n: P(n) → (P(2n) ∧ P(3n)) is true, then
∀n:P(n) must be true.
FALSE. There is no reason that we should have P(5), for example. If P(n) were "n ≠ 5", for example, the given statements would all be true but ∀n: P(n) would be false.

- (c) Let P(w) be a predicate on binary strings. If P(0)
and P(1) are true, and ∀w: P(w) → (P(w0) ∧ P(w1))
is true, then ∀w: P(w) must be true. (Here "0" and "1"
represent one-letter strings.)
FALSE. This is close to the Law of String Induction but we are not given P(λ). If P(w) were "w ≠ λ", all the given statements would be true, but ∀w: P(w) would be false.

- (d) The operation e(x, y) = x
^{y}on naturals is neither commutative nor associative.TRUE. In general x

^{y}and y^{x}are different numbers (e.g., 2^{3}and 3^{2}), and x raised to the y^{z}power is different from x^{y}raised to the z power. (If x = y = z = 3, the former is 3^{27}and the latter is 27^{3}= 3^{9}.) - (e) Let
`natural f (natural n)`

be a recursive pseudo-Java method. If`f(0)`

terminates without recursion, and`f(n)`

for positive n terminates except for calls to`f(k)`

with k ≤ n, then`f(n)`

must terminate for all naturals n.FALSE. If it said "k < n" instead of "k ≤ n" it would be true. But if f(1) called f(1), this would be consistent with the given property and would cause an infinite recursion.

- (f) If x and y are any two positive numbers, each with
three decimal digits, and x > y, then the Euclidean Algorithm
on x and y will terminate in 20 or fewer steps.
TRUE. We proved in lecture that two steps of the EA yields two numbers that are at most half of the original numbers. Thus 20 steps will reduce any numbers less than 1024 to 1 each. In fact on two numbers bounded by (1.61)

^{k}the EA will terminate in at most k steps. And 1000 is less than 1.61^{18}because 1.61^{6}is greater than 10. (On 987 and 610 it takes 16 steps.) - (g) If m and n are positive integers, any 3m by 3n
rectangle can be tiled with size-3 L-shaped tiles like those we
used to tile chessboards with one square missing.
FALSE. We observed in a homework problem that we cannot tile a 3 by 3 square.

- (h) The path relation on a directed graph may fail to be
transitive.
FALSE. We proved that the path relation is always transitive -- if there are paths from x to y and from y to z, there is a path from x to z.

- (i) Without a closed list or marking of nodes, a generic
search of a finite directed acyclic graph is not guaranteed to
terminate.
("Acyclic" means "has no directed cycles".)
FALSE. We can prove by induction on n that every node at distance n from the start node will eventually come off the open list. Since there are no directed cycles, the distance to every node is bounded by the number of edges. Once all paths up to this maximum distance have been explored, the search terminates.

- (j) Consider a two-move game where the final payoff is
paid by Black to White. White moves first. If she moves left,
Black can choose the payoff to be either -1 or 3. If she moves
right, Black can choose either -2 or -3. Then the value of the
entire game is -2.
FALSE. White's best move is to move left, whereupon Black can choose -1. (If she moves right, Black will choose -3.) So -1 is the value of the game under optimal play.

- (a)
Let P(n) be a predicate on naturals. If P(0) and P(1) are true, and
∀n: P(n) → (P(2n) ∧ P(2n+1)) is true, then
∀n:P(n) must be true.
**Question 2 (20):**I have decided on a new plan for my daily walks with my dogs Cardie and Duncan. On Day 1 we will walk one mile, and on Day 2 we will walk three miles. On subsequent days we will walk the average of the distances on the previous two days. There is thus a function W, from positive integers to reals, where W(n) is the number of miles we walk on Day n. This function is defined recursively by the rules W(1) = 1, W(2) = 3, and (for all n with n ≥ 3) W(n) = (W(n-1) + W(n-2))/2.- (a, 15) Prove that for all positive integers n, W(n) = (7 +
8(-1/2)
^{n})/3. (Hint: Use strong induction, as in proofs about the Fibonacci sequence.) (Note, by the way, that (-1/2)^{n}is not the same thing as -(1/2)^{n}.)Let f(n) be (7 + 8(-1/2)

^{n})/3. We must show that for all positive integers n, W(n) = f(n). We have two base cases, for n = 1 and n = 2. We compute that f(1) = (7 + 8(-1/2))/3 = (7-4)/3 = 1, matching the given value for W(1). Then f(2) = (7 + 8(1/4))/3 = (7 + 2)/3 = 4, matching W(2).For the inductive case, we use the rule W(n+1) = (W(n) + W(n-1))/2 and apply the strong inductive hypothesis to n and n - 1 to get W(n+1) = f(n) + f(n-1) = [(7 + 8(-1/2)

^{n})/3 + (7 + 8(-1/2)^{n-1})/3]/2 = (1/6)(7 + 7 + 8(-1/2)^{n}(1 + (-1/2)^{-1})) = (1/6)(14 + 8(-1/2)^{n}(1 - 2)) = (7 - 4(-1/2)^{n})/3 = (7 + 8(-1/2)^{n+1})/3 = f(n+1). So assuming W(k) = f(k) for all k with k ≤ n, we have proved W(n+1) = f(n+1). - (b, 10) Let S(n) be the total number of miles we walk in the
first n days of this plan, so that S(0) = 0. Prove that
*for all naturals*n, S(n) = (21n - 8(1 - (-1/2)^{n}))/9. (Hint: Use ordinary induction (starting from n = 0) and the result of part (a), whether you proved that result or not.)Let g(n) be the proposed formula for S(n). For the base case, we must verify that g(0) = 0 to match S(0), which is true because (21(0) - 8(1 - (-1/2)

^{0}) = 0 - 8(1 - 1) = 0.For the inductive case, we assume that S(n) = g(n) and compute S(n+1) = S(n) + W(n+1), since the miles we walk on Day n+1 are added to S(n) to get S(n+1).

S(n) + W(n+1) = (21n - 8(1 - (-1/2)

^{n}))/9 + (7 + 8(-1/2)^{n+1})/3 = (1/9)(21n - 8 + 8(-1/2)^{n}+ 21 + 24(-1/2)^{n+1}) = (1/9)(21(n+1) - 8 + (8 - 12)(-1/2)^{n}) = ((21(n+1) - 8(1 + 4(-1/2)^{n}))/9 = (21(n+1) - 8(1 - (-1/2)^{n+1})) = g(n+1).We have completed the ordinary induction and shown that S(n) = g(n) for all natural numbers n.

- (a, 15) Prove that for all positive integers n, W(n) = (7 +
8(-1/2)
**Question 3 (25+10):**Above we defined a real Java class`BST`

whose objects are rooted binary search trees somewhat similar to the boolean expression trees of Discussion #7. In a`BST`

object every node, leaf or not, has a`String`

data item. A`BST`

object is**valid**if for every non-leaf node v, every data item in v's left subtree is strictly less then v's data item, and and every data item in v's right subtree is strictly greater than v's data item. (Thus a valide`BST`

cannot have two nodes with equal data items.) definitions, and predicates above, and the statements from Question 1.- (a, 10) Write an instance method
`public boolean search (String w)`

for this class.*If the calling object is valid*, this method returns`true`

if and only if one of the data items in the calling object is equal to w. (If the object is not valid, I don't care what your method does.)`public boolean search (String w) { if (data.equals(w)) return true; if (isLeaf) return false; if (data.compareTo(w) > 0) return left.search(w); return right.search(w);}`

Some people had a correct though much less efficient search by returning

`left.search(w) || right.search(w)`

in place of the last two lines. This means that every node of the tree will be searched unless w is found, since there is no longer a binary search.There was no need here to check the calling BST for validity, since I said you could assume it. It was important to check

`isLeaf`

before calling any methods from`left`

or`right`

, to avoid null pointer exceptions. - (b, 5) Write a recursive definition of valid
`BST`

objects, with a base case for one-node trees and an inductive case for trees with left and right subtrees.A recursive definition is not an induction proof and not a method to test validity. You are asked here for a definition of what it means for a BST to be valid, with a base case for one-node trees and an inductive case for how larger valid BST's are built from smaller ones.

For the base case, any one-node BST is valid since there is no way for the validity condition to be false.

For the inductive case, we can make a valid BST from a new root and two subtrees if (1) both subtrees are valid, (2) all the data items in the left subtree are less than the root's data item, and (3) all the data items in the right subtree are greater than the root's data item.

A BST is valid only if made up from one-node trees following the inductive rule.

- (c, 10) Let w be an arbitrary
`String`

. Prove, by induction on valid`BST`

objects, using your definition from part (b), that your`search`

method is correct when called with parameter w.For the base case, assume our tree has one node. If its data item is w, we return true, and if it is not w, we return false. This is correct.

For the inductive case, assume that our tree is made from left and right subtrees according to the rule, and that our search is correct if applied to the left or the right subtree. Then if w is the root's data item we correctly return true. If w is less than the root's data item, by the validity condition w is either in the left subtree or not in the tree at all, and by the IH our search of the left subtree will get the correct answer. Similarly if w is greater than the root's data item, we will search the right subtree and find w if it is there.

I did not give full credit for this proof if, like most of you, you checked validity by comparing the root's data item to the data items of its

*children*rather than all of its*descendants*. Unless, that is, you did the non-binary search mentioned above, and proved it correct without using the validity condition. - (d, 10XC) Write an instance method
`public boolean isValid( )`

for this class. Your method should return`true`

if and only if the calling object is valid. You may want to write helper instance methods. (Hint: Use recursion.)`public boolean isValid( ) { if (isLeaf) return true; if (data.compare(left.max( )) <= 0) return false; if (data.compare(right.min( )) >= 0) return false; return (left.isValid( ) && right.isValid( ));} public String max( ) { if (isLeaf) return data; return right.max( );} public String min( ) { if (isLeaf) return data; return left.min( );}`

The comment "max/min" on these answers refers to comparing the root's data item against the items from the roots of the subtrees, instead of against the appropriate max and min.

The recursive calls to

`isValid( )`

are necesary so that the left/right condition is verified at every non-leaf node, not just at the root.

- (a, 10) Write an instance method
**Question 4 (30):**Above is an undirected graph G, where the six nodes represent towns in the Pioneer Valley (Amherst, Hadley, North Amherst, North Hadley, Northampton, and Sunderland) and the edges represent direct road connections between towns. Each town x is labeled by the time (in minutes) h(x) to drive from x to Northampton when there is no traffic.Also above is the single-step cost matrix for a labeled

*directed*graph D with the same six nodes as G, and two directed edges for each edge in G. The label on a directed edge (x, y) represents the time (in minutes) to drive directly from x to y during rush hour.For example, at rush hour it takes 15 minutes to drive from Amherst to eithe Hadley or North Amherst. Since one cannot drive

*directly*from Amherst to any of the other three towns, the matrix has a "--" entry in the other locations in the first row. The third row indicates that from North Amherst, it takes 19 minutes to drive to Amherst, 10 to drive to North Hadley, or 14 to drive to Sunderland. Note that driving time between towns depends on which direction you are going, which is why we need a directed graph.- (a, 5)
Trace a depth-first search of G with start node North
Amherst and goal node Northampton. When two items go on the open
list at the same time, they come off in alphabetical order of town name. Use a
closed list.
- NAm goes on the stack.
- NAm comes off, Amh, NHa, and Sun go on.
- Amh comes off, Had goes on, stack is (Had, NHa, Sun).
- Had comes off, NHa and NtN go on, stack is (NHa, Ntn, Nha, Sun).
- NHa comes off, Sun goes on, stack is (Sun, Ntn, Nha, Sun).
- Sun comes off, Ntn goes on, stack is (Ntn, Ntn, Nha, Sun).
- Ntn comes off and victory is declared.

- (b, 5) Trace a breadth-first search of G with start node North
Amherst and goal node Northampton. When two items go on the open
list at the same time, they come off in alphabetical order of town name. Use a
closed list.
- NAm goes on the queue.
- NAm comes off, Amh, NHa, and Sun go on.
- Amh comes off, Had goes on, queue is (NHa, Sun, Had).
- NHa comes off, Had and Sun go on, queue is (Sun, Had, Had, Sun).
- Sun comes off, Ntn goes on, queue is (Had, Had, Sun, Ntn).
- Had comes off, Ntn goes on, queu is (Had, Sun, Ntn, Ntn).
- Had is discarded as it is on the closed list.
- Sun is discarded as it is on the closed list.
- Ntn comes off and victory is declared.

- (c, 10) Trace a uniform-cost search with start node North
Amherst and goal node Northampton, using the costs in the directed
graph D. Use a closed list, so that a node does not go onto the
open list once it has come off.
- (NAm, 0) goes on the PQ.
- (NAm, 0) comes off, (Amh, 19), (NHa, 10), and (Sun, 14) go on.
- (NHa, 10) comes off, (Had, 20) and (Sun, 22) go on.
- (Sun, 14) comes off, (Ntn, 36) goes on.
- (Amh, 19) comes off, (Had, 34) goes on.
- (Had, 20) comes off, (Ntn, 50) goes on.
- (Sun, 22) and (Had, 34) each come off and are discarded.
- (Ntn, 36) comes off and victory is declared.

- (d, 10) Trace an A
^{*}search with start node North Amherst and goal node Northampton, where the heuristic h(x) for any town x is given by the no-traffic driving time written next to node x on graph G. Again, a town should not go onto the open list once it has come off.- (NAm, 0+25) goes on the PQ.
- (Nam, 0+25) comes off, (Amh, 19+18), (Nha, 10+16), and (Sun, 14+18) go on.
- (Nha, 10+16) comes off, (Had, 20+10) and (Sun, 22+18) go on.
- (Had, 20+10) comes off, (Amh, 32+18) and (Ntn, 50+0) go on.
- (Sun, 14+18) comes off, (Ntn, 36+0) goes on.
- (Ntn, 36+0) comes off and victory is declared.

- (a, 5)
Trace a depth-first search of G with start node North
Amherst and goal node Northampton. When two items go on the open
list at the same time, they come off in alphabetical order of town name. Use a
closed list.

Last modified 23 November 2015