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)
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.
Let P(k) be the statement "Starting with one stick of size
2k, Cardie finishes with only sticks of length 1".
Base case: P(0) says that starting with one stick of size
20 = 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
2k ends with sticks of size 1. Given a stick of size
2k+1, Cardie breaks it in half because 2k+1
is an even number. This leaves two sticks each of length
2k, 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.
P(k) says that starting with a stick of length 2k,
Cardie finishes the game after breaking exactly 2k - 1
sticks.
Base case: P(0) says that starting with a stick of length
20 = 1, she finishes after breaking 20 - 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 2k by breaking exactly 2k - 1
sticks. Given a stick of length 2k+1, her first move is
to break it into two sticks each of length 2k, since
2k+1 is an even number. By the IH, she can finish the
game on each of these sticks by breaking 2k - 1 sticks
for each. This means that she finishes her original game by
breaking the initial stick and then making 2(2k - 1)
more breaks, for 2k+1 - 1 breaks in all.
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.
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.
We don't need an explicit base case for n = 1 because the
odd-length case includes it.
natural pieces (natural n) {
if (n % 2 == 0) return 2 * pieces(n/2);
return 1;}
P(n) is that Base case: P(1) says that the game on a stick of size 1 ends with one
stick, since 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)
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.
pieces(1)
returns 1. This is true because
the game ends with no sticks being broken, and thus one stick left.
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.
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.
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.
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.
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).
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.
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 xy on naturals
is neither commutative nor associative.
TRUE. To prove this we need a counterexample for each property:
23 ≠ 32, and 2(32) ≠ (23)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 2k 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
220/105 = 10.48576), (1.6)500 is
greater than 10100. 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 3k leaves and exactly
3k+1 - 1 total nodes.
FALSE. The 3k number is correct but the total number of
nodes is 1 + 3 + 32 + ... + 3k =
(3k+1 - 1)/2, not 3k+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.
Last modified 17 April 2017