- Answer the problems on the exam pages.
- There are five problems, each with multiple parts, for 100 total points plus 10 extra credit. Actual scale A = 90, C = 62.
- 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: 15 points Q3: 25 points Q4: 20+10 points Q5: 20 points Total: 100+10 points

Here are definitions of sets, predicates, and statements used in Questions 1-5 on this exam.

Remember that a **natural** is a non-negative integer, so that
the set **N** of all naturals is {0, 1, 2, 3,...}.

Questions 3 and 5 refer to the labeled undirected graph below. There are eight nodes, each representing a city in the northeast United States. The edge from x to y, if it exists, is labeled with the driving distance from x to y as given by Google Maps. If there is no edge from x to y, we assume that it is only possible to drive from x to y along edges of the graph, through intermediate cities as necessary. Since the edges are undirected, the distance from x to y always equals the distance from y to x.

```
Montpelier ----180---- Augusta
/ | \ / |
/ | \ / |
/ | 117 163 |
/ | \ / |
158 153 \ / 166
/ | Concord |
/ | / | |
/ | 97 68 |
/ | | \ /
Albany --101-- Amherst --95-- Boston
\ | / |
113 53 _102_/ 51
\ | / |
\-- Hartford --74-- Providence
```

**Question 1 (20):**A**full b-ary tree of depth d**is defined as follows. A tree of depth 0 has a single node which is its root. A tree of depth d + 1 is defined to be b separate trees of depth d, with a new node that is the root, and a new edge from the new root to the root of each of the b separate trees.- (a, 10)
Determine how many
**leaves**a full b-ary tree of depth d has, as a function of b and d. Prove your claim, for arbitrary positive b, by ordinary induction on all naturals d.The correct number of leaves is b

^{d}. We let b be an arbitrary positive number and prove P(d), "the number of leaves in a full b-ary tree of depth d is b^{d}", by ordinary induction on all naturals d.The base case is d = 0. We are told that a tree of depth 0 has one node, which is a leaf because it has no children. Since b

^{0}= 1 for any b, the statement P(0) is true.For the inductive step, we assume P(d) as our IH. We are told that a tree of depth d + 1 is made up of b trees of depth d, and therefore it has b times as many leaves as a tree of depth d. Since b times b

^{d}= b^{d+1}, P(d+1) is proved and we have completed the induction. - (b, 10)
Prove that if b > 1, any full b-ary tree of depth d has exactly
(b
^{d+1}- 1)/(b - 1)**total nodes**. Again let b be an arbitrary positive integer with b > 1 and use ordinary induction on all naturals d.We again proceed by ordinary induction, with P(d) being the statement that any full b-ary tree of depth d has (b

^{d}- 1)/(b - 1) total nodes.The base case is d = 0. We are told that a tree of depth 0 has one node, and (b

^{0+1}- 1)/(b - 1) = (b - 1)/(b - 1) = 1 (assuming that b > 1), so P(0) is proved.Our IH is P(d), and we are told that the nodes of a full b-ary tree of depth d + 1 consist of the nodes of b trees of depth d, plus one new node. Using the IH, we have that the total number of nodes is b(b

^{d+1}- 1)/(b - 1) + 1 = (b^{d+2}- b)/(b-1) + (b - 1)/(b - 1) = (b^{d+2}- 1)/(b - 1), just as the statement P(d+1) says it should be. So P(d+1) is proved and the induction is complete.

- (a, 10)
Determine how many
**Question 2 (15):**My dog Duncan gets four servings of food a day. His food comes in cases of twelve cans, and each can has exactly five servings. (My other dog Cardie eats different food.)On Day 0, a Thursday, we opened a new case of food for his first meal.

- (a, 5) Find a positive natural k so that for every natural
n, Day kn is a Thursday and we open a new case of food with his
first meal on that day. (There are infinitely many values of k
with this property, and any one of them will get full credit.)
We need a number divisible by 7 to make Day kn a Thursday for any n, and we need a number divisible by 15 because Duncan needs a new case of food every fifteen days. (He eats 60 servings in 15 days, exactly the content of 12 cans with five servings each.) Any number divisible by 7 × 15 = 105 will be divisible by both 7 and 15, and in fact the Chinese Remainder Theorem tells us that only numbers divisible by 105 have this property, since 7 and 15 are relatively prime.

- (b, 10) Prove your assertion in part (a) by ordinary
induction on all naturals n, using the value of k you proposed
there.
We can carry out this proof if k is equal to 105m for any positive natural m. For the base case of n = 0, Day kn is Day 0 and we are told that it is a Thursday and that a new case is opened. The IH is the statement P(n), that Day kn was a Thursday on which a new case was opened. We want to prove P(n+1), the same statement about Day k(n+1). This day is k days after Day kn. Since k is divisible by 7, Day k(n+1) is also a Thursday. And since k = 105m, Duncan ate exactly 7m cases of food during the k days since Day kn, and we open a new case on Day k(n+1).

- (a, 5) Find a positive natural k so that for every natural
n, Day kn is a Thursday and we open a new case of food with his
first meal on that day. (There are infinitely many values of k
with this property, and any one of them will get full credit.)
**Question 3 (25):**Above is a labeled undirected graph, where the eight nodes represent cities and the edges represent driving distance between certain pairs of cities.- (a, 5) For any two cities x and y, define d(x, y) to be
the
**minimum number of edges**for any path in the graph from x to y. Using a breadth-first search, find the value of d(x, Providence) for each of the eight cities x in the graph.Starting a BFS from Providence, we first put Hartford and Boston on the queue, marking them as distance 1. It doesn't matter in which order we explore Hartford and Boston, as either way we will place Albany, Amherst, Concord, and Augusta on the queue and mark than as distance 2. From one of those cities we will explore Montpelier and mark it as distance 3. I took off one point if you did not indicate how the distances cam from a BFS. I did not take off any points if you found each distance by doing a separate BFS staring from x with Providence as the goal node, but this took you more time that you could perhaps have used elsewhere. Finally, some of you didn't read the definition of d and found the driving distance rather than the number of edges.

- (b, 10) Trace a uniform-cost search with start node
Amherst and goal node Providence.
- Start: PQ contains only (Amherst, 0)
- Explore Amherst: add (Albany, 101), (Montpelier, 153), (Concord, 97), (Boston, 95), and (Hartford, 53).
- Explore Hartford: add (Albany, 166), (Boston, 155), (Providence,
127). Note that you have now discovered the optimal path, but the
UCS algorithm
*does not yet know that it is optimal*and you must continue. - Explore Boston: add (Providence, 146), (Concord, 163), and (Augusta, 261).
- Explore Concord: add (Montpelier, 214) and (Augusta, 260).
- Explore Albany: add (Montpelier, 259).
- Find (Providence, 127) in the PQ and report this as the optimal path length from Amherst to Providence.

- (c, 10) Trace an A
^{*}search with start node Amherst and goal node Providence, where the heuristic h(x) for any city x is given by h(x) = 50d(x, Providence). (Here d is the function defined in part (a) of this problem.)- Start: PQ contains only (Amherst, 0 + 100).
- Explore Amherst: add (Albany, 101 + 100), (Montpelier, 153 + 150), (Concord, 97 + 100), (Boston, 95 + 50), and (Hartford, 53 + 50).
- Explore Hartford: add (Albany, 166 + 100), (Boston, 155 + 100), and (Providence, 127 + 0).
- Find (Providence, 127) in the PQ and report this as the optimal path length from Amherst to Providence.
- Note that with the help of the heuristic, the algorithm found this optimal path without ever considering the path from Amherst through Boston to Providence. Many of you incorrectly got 177 for the priority of the Amherst-Hartford-Providence path, and thus explored Boston -- I gave such answers 6/10 points if they were otherwise correct.

- (a, 5) For any two cities x and y, define d(x, y) to be
the
**Question 4 (20+10):**Here is a rather silly two-player game. We have a single pile of n stones, where n is a positive integer. On any player's move, she may divide the pile into p equal subpiles, where p is any**prime number**, and then remove all but one pile. (So, for example, if n = 24, the player to move could make two piles and leave 12 stones, or make three piles and leave 8 stones. Don't forget that 1 is not a prime number.)The two players alternate moves until there is only one stone left, whereupon the player who cannot move loses the game. White moves first. So if n = 1, White cannot move and Black wins. If n = 2, White can make two piles of one stone each and remove one of them, leaving Black with one stone, so that Black than cannot move and White wins.

- (a, 5) Determine the winner, assuming optimal play by
each player, for each initial value of n from n = 3 through
n = 9.
For n = 3, 5, and 7, White's only move is to leave one stone, and thus win the game.

For n = 4 or 9, White's only move is to leave two or three stones respectively, whereupon Black leaves one stone and wins.

For n = 6, White has two possible moves, leaving either two or three stones. In either case Black's only move is a winning one, and Black wins the game. I deducted a point if your analysis did not take account of White's having a choice here.

For n = 8, each player can only halve the number of stones on each turn, so that White wins on her second move as Black has no second move.

- (b, 15) Prove, by strong induction on all positive
numbers n, that Black has a winning strategy in this game
when we start with n
^{2}stones.Let P(n) be "Black can win if we start with n

^{2}stones".For the base of our induction on all positive numbers, we prove P(1), which says that Black wins if we start with one stone. We already observed above that this is true.

A strong inductive hypothesis says that Black can win the game starting from k

^{2}stones, where k is any number with 1 ≤ k ≤ n. We want to prove P(n+1), which says that Black wins when we start with (n+1)^{2}stones.White's first move must be to leave (n+1)

^{2}/p stones, where p is some prime that divides n+1. (By the Atomicity Lemma, it cannot divide (n+1)^{2}without dividing n+1 itself.) Black can reply by dividing the pile by p again, leaving (n+1)^{2}/p^{2}stones. Since this number is the square of (n+1)/p, a number in the range from 1 to n, the SIH tells us that Black wins the resulting game. - (c, 10XC) Describe a boolean method that takes a
positive integer n and returns true if and only if White
wins the game starting with n stones, again assuming optimal
play by both players. You may use real Java, the book's
pseudo-Java, or other pseudocode as long as your algorithm
is clear.
For full credit, argue convincingly that your algorithm gives the correct answer for all n. (This might or might not involve an induction proof.)

As it happens, whenever a player has a choice of moves, that choice

*does not matter*for the eventual outcome of the game. The result depends only on the number of primes in the factorization of n, counting multiple copies of the same prime as separate. If this number is odd, White will win, and if it is even, Black will win. (I said that it is a rather silly game.)Thus a correct algorithm to determine the winner is to count the prime factors and test the number of them for oddness. This can be done either iteratively or recursively. One simple recursive version is as follows:

`public boolean ww (int n) { // returns whether White wins game on n stones, assuming n > 0 if (n == 1) return false; int d = 2; while (n % d != 0) d++; return !ww (n/d);}`

There are of course various ways to do this wrongly, such as omitting the base case or omitting the "!" in the recursive call that switches the roles of the first and second player after a move is made. There are also a number of ways to do it correctly.

The interesting question is whether you have proved that this algorithm is correct. The code above represents one possible play of the game, where each player always chooses the smallest prime dividing n. To prove that neither player can improve their lot be moving differently, we need some sort of argument.

Many of you claimed without further proof that White wins if and only if there are an odd number of prime factors. If you gave some explanation, I had to make a subjective judgement about how convincing your argument was. (If you said, for example, "If n has k prime factors, the game will always proceed for exactly k turns before ending, because each turn removes one prime factor," I was pretty sympathetic.)

The induction proof is actually pretty simple. Let P(n) be the statement "White wins with n stones if and only if n has an odd number of prime factors" and prove P(n) for all positive n by strong induction. The base of n = 1 follows directly from the definition of the game, since 1 has an even number of prime factors and Black wins with 1 stone. Now assume as SIH that P(k) is true for all k with k ≤ n. We will use this assumption to prove P(n+1). If White starts the game with n+1 stone, she must choose a prime p and leave Black as the

*first*player with (n+1)/p stones. Since (n+1)/p ≤ n, the SIH tells us that Black will win this game if (n+1)/p has an*odd*number of prime factors and lose if it has an even number. Thus White wins the original game if n+1 itself has an odd number of prime factors and loses if it has an even number (since n+1 has exactly one more prime factor than does (n+1)/p).

- (a, 5) Determine the winner, assuming optimal play by
each player, for each initial value of n from n = 3 through
n = 9.
**Question 5 (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.Parts (a)-(e) of this problem deal with a DFS of the labeled undirected graph above,

**using a closed list**, with Amherst as the start node and no goal node. In this DFS, when two cities enter the open list at the same time, the one that comes off first is the one that is**earlier in alphabetical order**. Thus the edge labels have no effect on the DFS' behavior.Parts (f)-(j) of this problem do not refer to the given graph.

- (a) The DFS tree from this search consists of a single path
from the root to a single leaf.
FALSE. The DFS starts with Amherst on the stack, takes it off, and puts on Albany, Boston, Concord, Hartford, and Montpelier. It takes off Albany and puts on new copies of Hartford and Montpelier. It takes off Hartford and puts on Boston and Providence. It takes off Boston and puts on Augusta, Concord, and Providence. It takes off Augusta and puts on Concord and Montpelier. It takes off Concord and puts on Montpelier, then takes off Montpelier. At this point the only node not on the closed list is Providence. The entry for Providence that is highest on the stack is the one put on while exploring Boston. So the DFS tree has seven nodes in a single path, not eight. The eighth node, Providence, is a second child of Boston.

- (b) The edge from Albany to Montpelier becomes a tree edge.
FALSE. As described above, Montpelier was discovered from Concord, not Albany. The edge from Abany to Montpelier becomes a forward edge.

- (c) There is no articulation point in the original
undirected graph.
TRUE. We can see this from the DFS. The root, Amherst, has only one child and every other node has an edge to a proper ancestor of its parent. Thus no edge would disconnect the graph by being removed.

- (d) Of the five edges with Boston as an endpoint, exactly
two become tree edges.
FALSE. As shown above, the edges from Boston to Hartford, Augusta, and Providence become tree edges -- the ones to Amherst and Concord do not.

- (e) Boston comes off of the open list before Hartford does.
- (f) Without a closed list or marking of nodes, a DFS of any
finite directed graph is not guaranteed to find all the nodes
reachable from the start.
TRUE. For example, if we start at one node of a directed triangle, we could repeatedly explore the three edges of that triangle without looking at any other edges out of those nodes.

- (g) Without a closed list or marking of nodes, a BFS of any
finite directed graph is not guaranteed to find all the nodes
reachable from the start.
FALSE. The BFS will find all the nodes at distance 1, then all the nodes at distance 2, and so forth. Since the graph is finite, there are only finitely many nodes at each distance, and each reachable node must be at some finite distance. The cost of not having a closed list or marked nodes is that the BFS will take additional time to explore multiple paths to the same node.

- (h) If a directed graph with more than one node is strongly
connected, each node must have at least one edge going into it and
at least one edge going out of it.
TRUE. Since the graph is strongly connected, there must be a path from every node to every other node. If node x has no edge in and there exists a different node y, there can be no path from y to x. If x has no edge out, there can be no path from to y.

- (i) Any undirected graph with no cycles is a tree.
FALSE. Any such

*connected*graph is a tree, but having no cycles in itself only means that the graph is a forest. - (j) Let Σ be a finite alphabet and let P(w) be a
predicate with one free variable whose type is "string in
Σ
^{*}". If I prove P(λ) and I prove ∀w:∀a: P(w) → P(wa), where the type of a is "letter in Σ", then I may conclude ∀w:P(w).TRUE. This is exactly the definition of induction on strings.

- (a) The DFS tree from this search consists of a single path
from the root to a single leaf.

Last modified 14 April 2015