# Second Midterm Exam Spring 2017

### Directions:

• 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 "217 - 4" need not be reduced to a single integer.

```  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 2k, she will finish the game and have only sticks of length 1.

• (b, 10) Prove by ordinary induction on all naturals k that if Cardie starts with one stick of length 2k, she will break exactly 2k - 1 sticks before finishing.

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

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

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

• 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?

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

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

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

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

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

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

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

• (d) The exponentiation operation xy on naturals is neither commutative nor associative.

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

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

• (g) There are fewer than 1000 Fibonacci numbers with 100 or fewer digits.

• (h) Let G be an undirected graph with exactly five nodes and exactly seven edges. Then G must be a connected graph.

• (i) Let G be an undirected graph with exactly thirteen nodes and exactly thirteen edges. Then it is possible that G is a tree.

• (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 3k leaves and exactly 3k+1 - 1 total nodes.

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

• (l) The game tree for Tic-Tac-Toe (described above) has depth exactly nine.

• (m) There exists a zero-sum deterministic two-player game of perfect information where both players have winning strategies.

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

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