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
The correct number of leaves is bd. 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 bd", 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
b0 = 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 bd = bd+1, P(d+1) is proved
and we have completed the induction.
We again proceed by ordinary induction, with P(d) being the statement
that any full b-ary tree of depth d has (bd - 1)/(b -
1) total nodes.
The base case is d = 0. We are told that a tree of depth 0 has
one node, and (b0+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(bd+1 - 1)/(b - 1) + 1 = (bd+2 -
b)/(b-1) + (b - 1)/(b - 1) = (bd+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.
On Day 0, a Thursday, we opened a new case of food for his first meal.
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.
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).
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.
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.
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.
Let P(n) be "Black can win if we start with n2 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 k2 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/p2 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.
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:
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).
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);}
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.
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.
FALSE. As described above, Montpelier was discovered from Concord, not Albany. The edge from Abany to Montpelier becomes a forward edge.
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.
FALSE. As shown above, the edges from Boston to Hartford, Augusta, and Providence become tree edges -- the ones to Amherst and Concord do not.
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.
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.
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.
FALSE. Any such connected graph is a tree, but having no cycles in itself only means that the graph is a forest.
TRUE. This is exactly the definition of induction on strings.
Last modified 14 April 2015