- 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.

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.
- (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.
- (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.)
- (d) The operation e(x, y) = x
^{y}on naturals is neither commutative nor associative. - (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 termine for all naturals n. - (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.
- (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.
- (h) The path relation on a directed graph may fail to be transitive.
- (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".)
- (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.

- (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}.) - (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.)

- (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.) - (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. - (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. - (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.)

- (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.
- (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.
- (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.
- (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.

Last modified 22 November 2015