- Answer the problems on the exam pages.
- There are three problems, each with multiple parts, for 100 total points plus 10 extra credit. Actual scale was A = 93, 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
"2
^{17}- 4" need not be reduced to a single integer.

Exam text is in black, solutions in blue.

Q1: 30+10 points Q2: 40 points Q3: 30 points Total: 100+10 points

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

The **Fibonacci numbers** are a sequence of naturals defined
by the rules F(0) = 0, F(1) = 1, and, for all n with n ≥ 1, F(n+1) =
F(n) + F(n-1).

A **binary string** is a string over the alphabet {0, 1}.

A ** winning strategy** for one player in a two-person, zero-sum game
is a way for that player to choose moves such that they are guaranteed a
positive payoff at the end of the game no matter how their opponent moves.

The two-player game of **Tic-Tac-Toe** is played on a three-by-three
grid of squares. Each player in turn places one of their markers in a vacant
square, until either (1) one player gets three markers in a row, and thus
wins, or (2) there are no vacant squares left, and the game is a draw.

**Cardie's game** begins with a single stick whose length in
centimeters is a positive natural. A move in the game is for Cardie to
choose a stick whose length is even and break it into two sticks of equal
length. The game ends when there are no sticks remaining with even length.

The directed graph below represents an imagined neighborhood of San Francisco, where our three commuters live at node s and want to travel to their workplace at node g. Two-way edges in this graph represent level roads, and one-way edges represent sloped roads, with the edge pointing downhill. Beatriz can traverse a downhill edge in one minute, a level edge in two minutes, or an uphill edge in five minutes.

```
Directed graph for an imagined neighborhood of San Francisco:
(a) <---------> (b) <---------> (c)
^ ^ ^
| | |
| | |
| | |
v | v
(d) <---------- (e) ----------> (g)
^ ^ ^
| | |
| | |
| | |
| | |
(s) <---------- (f) ----------> (h)
```

**Question 1 (30+10):**My dog Cardie loves to break large sticks into smaller pieces. She has invented a one-player game where she starts with one stich whose length in centimeters is a positive integer. Her move in the game is to take a stick whose length is some even integer and break it into two equal-length pieces. She continues the game until there are no even-length pieces left. For example, given a stick of length 12, she breaks it into two pieces of length 6, then breaks each of these into two pieces of length 3, and then stops.You are asked below to prove various facts about this game

*by induction*. Non-inductive proofs of the given results will get only partial credit.- (a, 5) Prove by ordinary induction on all naturals k that if
Cardie starts with one stick of length 2
^{k}, she will finish the game and have only sticks of length 1.Let P(k) be the statement "Starting with one stick of size 2

^{k}, Cardie finishes with only sticks of length 1".Base case: P(0) says that starting with one stick of size 2

^{0}= 1, she finishes with only sticks of lemgth one. This is true because the game ends immediately, leaving only a stick of length 1.Inductive step: Assume that the game on a stick of size 2

^{k}ends with sticks of size 1. Given a stick of size 2^{k+1}, Cardie breaks it in half because 2^{k+1}is an even number. This leaves two sticks each of length 2^{k}, and by the IH she breaks each of these into sticks of length 1. The game then finishes, and there are only sticks of length 1 left. - (b, 10) Prove by ordinary induction on all naturals k that
if Cardie starts with one stick of length 2
^{k}, she will break exactly 2^{k}- 1 sticks before finishing.P(k) says that starting with a stick of length 2

^{k}, Cardie finishes the game after breaking exactly 2^{k}- 1 sticks.Base case: P(0) says that starting with a stick of length 2

^{0}= 1, she finishes after breaking 2^{0}- 1 = 0 sticks. This is true because 1 is an odd number, and the game ends without any sticks being broken.Inductive step: Assume that Cardie can finish the game from a stick of length 2

^{k}by breaking exactly 2^{k}- 1 sticks. Given a stick of length 2^{k+1}, her first move is to break it into two sticks each of length 2^{k}, since 2^{k+1}is an even number. By the IH, she can finish the game on each of these sticks by breaking 2^{k}- 1 sticks for each. This means that she finishes her original game by breaking the initial stick and then making 2(2^{k}- 1) more breaks, for 2^{k+1}- 1 breaks in all. - (c, 10) Prove by strong induction on all positive naturals
n that if Cardie starts the game with a single stick of length,
she finishes the game with only odd-length sticks. (Of course she
can't finish the game in any other way, but we need to prove that
the game doesn't continue forever. This should be similar to (but
simpler than) the proof that our factoring algorithm always
produces a prime factorization.)
Here P(n) is the statement that Cardie breaks a stick of length n into odd-size pieces, and the strong inductive hypothesis Q(n) is that P(i) is true for all i such that 1 ≤ i ≤ n.

Base case: P(1) is the statement that if Cardie starts with a stick of length 1, she will finish with only odd-size pieces. This is true because the game finishes immediately, and the length of the only remaining stick is 1, which is an odd number.

Inductive step: Assume Q(n), and consider the game starting with one stick of size n+1. If n+1 is an odd number, the game ends immediately leaving a single odd-length stick, and so P(n+1) is true. If n+1 is even, Cardie will break the stick into two pieces each of size m = (n+1)/2. Since n is positive, we know that 1 ≤ m ≤ n, and so the SIH implies that P(m) is true. So Cardie will break each of the sticks of length m into odd-length pieces, finishing the original game with only odd-length pieces remaining.

- (d, 5) Write a recursive pseudo-Java method
`natural pieces (natural n)`

that takes a positive natural as input and returns the number of sticks that are left after Cardie's game if she starts with one stick of length n. (So, in the example above,`pieces (12)`

should return 4, as she finishes with four sticks each of length 3.) You may use the Java integer division operators`/`

and`%`

. Don't worry about exception handling, just assume that the input is a positive natural.`natural pieces (natural n) { if (n % 2 == 0) return 2 * pieces(n/2); return 1;}`

We don't need an explicit base case for n = 1 because the odd-length case includes it.

- (e, 10) Prove, by strong induction on all positive
naturals n, that your method from part (d) returns the correct
answer on input n, according to the definition of Cardie's game.
P(n) is that

`pieces(n)`

terminates with the correct number of pieces that Cardie makes from an initial stick of length n. Again, the strong inductive hypothesis Q(n) is that P(i) is true for all i such that 1 ≤ i ≤ n.Base case: P(1) says that the game on a stick of size 1 ends with one stick, since

`pieces(1)`

returns 1. This is true because the game ends with no sticks being broken, and thus one stick left.Inductive step: We assume Q(n) and consider the game starting with one stick of length n+1. If n+1 is odd, the game ends with one stick left, and the method is correct because

`pieces(n+1)`

returns 1. If n+1 is even, Cardie's first move is to break the stick of size n+1 into two sticks each of size m = (n+1)/2. Since n is positive, we know that 1 ≤ m ≤ n. So the SIH says that`pieces(m)`

returns the correct number of pieces that Cardie makes from an initial stick of length m. The call to`pieces(n+1)`

twice the value of`pieces(m)`

, and this is correct because the number of sticks left from an initial stick of length n+1 is the total number in the two piles from the length-m sticks, and each of these has`pieces(m)`

sticks.

- (a, 5) Prove by ordinary induction on all naturals k that if
Cardie starts with one stick of length 2
**Question 2 (40):**These questions all involve searches of graphs representing the imagined San Francisco neighborhood where Ahmed, Beatriz, and Chandra live and work. This graph can be found above. Edges represent roads that are either level or sloped. The directed graph shown above has a two-way edge for each level road, and a directed edge pointing in the downhill direction of each sloped road. Thre three people all live at node s and work at node g, and we want to advise then how to get to work in the morning. In all cases, the search should recognize nodes that have been seen and treat them as appropriate for the kind of search being done.- (a, 10) Ahmed is a novice bicyclist and cannot travel up
hills. Thus his route if it exists, must be a path in the given
directed graph, so that he takes only level or downhill edges.
(We are not concerned with how he gets his bike back home in the
evening.) Carry out a depth-first search of this graph, with
start node s and goal node g. If two nodes go onto the stack at
the same time, they should come off in alphabetical order. Is the
node g reachable from s? If so, what path did you find?
The start node s begins on the stack. Then s comes off, and d goes on. Then d comes off, and a goes on. Then a comes off, b goes on, and we discover a back edge from a to d. Then b comes off, c goes on, and we discover a back edge from b to a. Then c comes off, g goes on, and we discover a back edge from c to b. Finally, g comes off, and we declare victory. We have found the path s-d-a-b-c-g.

- (b, 5) Continue the DFS from part (a) after you reach the
goal node, until all the nodes are in your DFS forest. (If your
stack empties, start a new tree with the alphabetically-first
remaining node.) Draw the enture DFS forest and identify each
directed edge in the graph as a tree, back, forward, or cross edge.
When we take g off, we discover a back edge to c but the stack is empty. We restart the search from e, since it is alphabetically before the other unseen nodes f and h. From e we find cross edges to b, d, and g, since these nodes are already seen and are neither ancestors nor descendents of e. The stack is empty so we must start a third tree by putting f on the stack (as it comes before h alphabetically). We take f off, put h on, and discover cross edges to s and e. Finally, we take h off and discover a cross edge to g.

We have found six tree edges (s, d), (d, a), (a, b), (b, c), (c, g), and (f, h). There were four back edges (a, d), (b, a), (c, b), and (g, c). There were six cross edges (e, b), (e, d), (e, g), (f, e), (f, s), and (h, g). There were no forward edges.

- (c, 10) Beatriz is an experienced cyclist and can cycle
up hills, so her graph of the neighborhood is a directed graph
with edge weights, including the uphill edges for each sloped
road. It takes her one minute to take an edge downhill, two
minutes to take a level edge, and five minutes to take an edge
uphill. Conduct a uniform-cost search of this weighted directed
graph starting from s, with goal node g, and give the fastest-time
path that you discover. If two entries in the priority queue are
tied for minimum priority, the one with the alphabetically earlier
node should come off first. If two entries for the same node with
the same priority are tied for minimum, then the one the went on
first should come off first.
We begin with (s, 0) on the priority queue. We remove it and put on (d, 1) and (f, 5). We remove (d, 1) and put on (a, 3) and (e, 6). We remove (a, 3) and put on (b, 5). We remove (b, 5) next (as it is alphabetically before (f, 5), and put on (c, 7). We next remove (f, 5) and put on (e, 6) and (h, 6). We next remove the first copy of (e, 6) that was put on, the one for the path from d, and put on (g, 7). We discard the second (e, 6). We take off (h, 6) and put on another (g, 7). We take off (c, 7) and put on (g, 9). Finally we take off the first (g, 7) that we put on, the one for the path from e. We declare victory and have found the path s-d-e-g. There were two other paths of length 7, s-f-e-g and s-f-h-g, but for full credit you had to break the ties correctly.

- (d, 5) Give an example of an admissible, consistent
heuristic for an A
^{*}search of Beatriz' weighted directed graph. Do not use the heuristic that is always 0. Explain why your choice meets the conditions of being admissible and consistent.There are many choices, ranging from h(x) = 1 for all nodes except g to the "perfect heuristic" of the actual distance from x to g in Beatriz' graph. A popular choice was the minimum number of edges from x to g, which is also the Manhattan distance if we assume that the length of each edge is one minute. This is admissible because it is non-negative and no greater than the actual distance. It is consistent because if there is an edge from x to y, h(x) cannot be greater than h(y) + 1, which in turn cannot be greater than h(y) plus the cost to Beatriz of taking the edge (x, y).

- (e, 10) Chandra walks to work and thus is not affected by
the hills. He is thus navigating an undirected graph, where the
one-way edges shown have all become two-way, and he wants a path
with the fewest possible edges. Carry out a breadth-first search
of the graph with start node s and goal node g. When entries go
on the queue at the same time, they should come off in
alphabetical order. Once a node has come off the open list, it
should be recognized and never put back on. Identify the path
found by the BFS. Continue the BFS after the goal node has been
found, to include all twelve undirected edges, and draw the BFS
tree indicating which are the tree edges.
We start with s on the queue. We take s off and put on d and f. We take d off because it is first alphabetically, and put a and e on. We than take off f, discover a non-tree edge from f to e (which is already on the queue), and put on h. We next take off a, and put on b. Then we take off e, find a non-tree edge from e to b, and put on g. We take off h, and find a non-tree edge from h to g. We take off b, and put on c. We take off g, declaring victory with the path s-d-e-g, but continue by finding a non-tree edge from g to c. Finally we take off c and finish because the queue is empty and all nodes have been seen.

Our tree has root s with children d and f. Node d has children a and e, and node f has only child h. Node a has only child b and only grandchild c, and node e has only child g. That is all the eight tree edges, and the four non-tree edges are listed above.

- (a, 10) Ahmed is a novice bicyclist and cannot travel up
hills. Thus his route if it exists, must be a path in the given
directed graph, so that he takes only level or downhill edges.
(We are not concerned with how he gets his bike back home in the
evening.) Carry out a depth-first search of this graph, with
start node s and goal node g. If two nodes go onto the stack at
the same time, they should come off in alphabetical order. Is the
node g reachable from s? If so, what path did you find?
**Question 3 (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. Each one counts two points.- (a) The set of non-negative
`values in Java satisfies some but not all of the Peano axioms for the natural numbers.`

TRUE. There is a zero, no two numbers have the same successor, every number except zero is the successor of another, and induction works. But

`MAXINT`

has no successor. - (b) We can define the usual "<" relation on naturals
recursively by saying "0 < x is true for any x" and "for any y,
Sy < x is true if and only if y < x". (Here "Sy" denotes the
successor of y.)
FALSE. With this definition, it is easy to prove by induction that "x < y" is always true.

- (c) Suppose we are using our definitions to prove by
induction that multiplication of naturals is commutative. Then the
base case of our proof should be that for any natural y, y × 0
= 0 × y.
TRUE. This is the P(0) if P(n) is "∀y: y × n = n × y".

- (d) The exponentiation operation x
^{y}on naturals is neither commutative nor associative.TRUE. To prove this we need a counterexample for each property: 2

^{3}≠ 3^{2}, and 2^{(32)}≠ (2^{3})^{2}. - (e) Let P(w) be a property of strings over a nonempty
alphabet Σ. If P(a) holds for every one-letter string a, and
P(w) → P(wa) holds for every string w and every letter a, then
P(w) holds for every string w.
FALSE. These properties hold for P(w) = "w is non-empty", which is false for w = λ.

- (f) Let k be an arbitrary natural. Then there exists a
string w such that every binary string of length k is a substring of w.
TRUE. We can let w be the concatenation of all 2

^{k}strings of length k. If k = 2 we have w = 00011011, and if k = 3 we have w = 000001010011100101110111. - (g) There are fewer than 1000 Fibonacci numbers with 100 or
fewer digits.
TRUE. The n'th Fibonacci number is about (1.6)

^{n}. Since (1.6)^{5}is greater than 10 (it is 2^{20}/10^{5}= 10.48576), (1.6)^{500}is greater than 10^{100}. So the number of such Fibonacci numbers is less than 500, hence also less than 1000. - (h) Let G be an undirected graph with exactly five nodes and
exactly seven edges. Then G must be a connected graph.
TRUE. If G were not connected it must break into at least two connected components. Components of size 1 and 4 could have at most six total edges (all within the size-4 component), and components of size 2 and e could have at most four (one in the size-2 and three in the size-3).

- (i) Let G be an undirected graph with exactly thirteen nodes
and exactly thirteen edges. Then it is possible that G is a tree.
FALSE. A tree of thirteen nodes must have exactly twelve edges.

- (j) Let k be any natural and let T be a rooted tree where
every non-leaf has exactly three children, and every leaf has depth
exactly k. Then T has exactly 3
^{k}leaves and exactly 3^{k+1}- 1 total nodes.FALSE. The 3

^{k}number is correct but the total number of nodes is 1 + 3 + 3^{2}+ ... + 3^{k}= (3^{k+1}- 1)/2, not 3^{k+1}- 1. - (k) Recall that our generic search declares victory when a
goal node first comes
*off*the open list. Suppose we alter it so that it declares victory when a goal node first goes*onto*the open list. Then the revised search will still have the property that it declares victory only if a path exists from the start node to a goal node.TRUE. In the generic search, nodes are only placed on the open list if they have paths to them from the start node.

- (l) The game tree for Tic-Tac-Toe (described above) has
depth exactly nine.
TRUE. There are possible runs of the game (including all the draws) that take nine moves to fill up the entire grid. The depth of the tree is the maximum number of edges in a path from the root to a leaf, which in this case is the maximum number of moves in any run of the game.

- (m) There exists a zero-sum deterministic two-player game of
perfect information where both players have winning strategies.
FALSE. If there were two such strategies, what would happen if the two players used them against one another? The payoff to one player would have to be both positive and negative, which is impossible.

- (n) Suppose it is your move in a two-player game given by a
game tree, where each leaf is labeled by the payoff to you if the
game reaches that leaf. Let m be the largest value of any leaf in
the subtree for the node representing the current position. Then
you shoudl definitely move to a position represented by a child of
the current node whose subtree contains a leaf labeled m.
FALSE. This would be a good idea if your opponent was conspiring with you to maximize your payoff. But if they are playing optimally, there is no guarantee that they will let you reach the maximum possible payoff, and the correct move for you might be to that child or to another.

- (o) Define the function g from the naturals to the naturals
by the rules g(0) = 1, g(1) = 0, and for every n with n > 1, g(n)
= 2g(n-2) + 1. Then for every even natural n, g(n) > g(n+1).
TRUE. Let P(n) be the statement "g(2n) > g(2n+1)". If we prove ∀n:P(n) by induction, we establish that the given statement is true because every even natural is equal to 2n for some natural n.

Base case: g(0) = 1 > g(1) = 0.

Inductive step: Assume that g(2n) > g(2n+1). By the definition, g(2n+2) = 2g(2n) + 1 > (by the IH) 2g(2n+1) + 1 = g(2n+3). This proves P(n+1) and completes the induction.

- (a) The set of non-negative

Last modified 17 April 2017