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
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).
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.
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.
TRUE. In general xy and yx are different numbers (e.g., 23 and 32), and x raised to the yz power is different from xy raised to the z power. (If x = y = z = 3, the former is 327 and the latter is 273 = 39.)
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.
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.6118 because 1.616 is greater than 10. (On 987 and 610 it takes 16 steps.)
FALSE. We observed in a homework problem that we cannot tile a 3 by 3 square.
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.
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.
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.
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).
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.
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.
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.)
Some people had a correct though much less efficient search by
returning There was no need here to check the calling BST for validity,
since I said you could assume it. It was important to check
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);}
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.
isLeaf
before calling any methods from
left
or right
, to avoid null pointer exceptions.
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.
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.
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.)
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
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( );}
isValid( )
are necesary so that
the left/right condition is verified at every non-leaf node, not
just at the root.
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.
Last modified 23 November 2015