COMPUTER SCIENCE 20, SPRING 2014 Homework Problems Recursive Definitions, Structural Induction, States and Invariants Due Wednesday March 12, 2014. CS 20 students should bring a hard copy to class. CSCI E-120 students should submit an electronic copy. Note: Use the following definition of the set of valid integer binary trees T. Base Cases: ɛ T Constructor Case: If x Z, l T, r T, then (x, l, r) T A node of a tree is any element in T that is a three-tuple, that is, it is of the form (x, l, r). This means that ɛ T is not a node. If z = (x, l, r) is a node, then l and r are child nodes of z, if they are non-empty. 1. The nodes in a tree obey the heap property if, for every node z in the tree, the value in z, that is x, is at least as big as the value in each of z s children (the tree in module 15 obeys the heap property). Prove that if a binary tree has the heap property, then the value in the root of the tree is at least as large as the value in any node of the tree. Proof by induction: Base Case: A tree of size 1, for which this property is trivially true. (It is also possible to induct on the empty tree) Inductive Hypothesis: For all trees with n nodes in it, the claim in question holds. Now we want to show that the claim holds for a tree T of size n+1 that has the heap property. Any such tree can be written as (x, l, r) by definition, with x being the root node. For the rest of this proof, we are assuming l and r are non-empty (if one or both is empty, we may ignore them as then there are no values to compare our root node to). Since T has the heap property, the trees l and r must also have the heap property. We also know that l and r are both at most size n, since T itself is only size n + 1. 1
By the inductive hypothesis, we know that the root nodes l 0 and r 0 are at least as large as any other nodes in l and r respectively. Then by the heap property, x l 0 and x r 0, so x must also be at least as large as any other nodes in l and r. Thus the root of the tree T is at least as large as any value in any of its nodes. 2. A complete binary tree (example below) is a binary tree in which every level, except possibly the last level, is completely filled, and all nodes are as far left as possible. Also, remember that an internal node is a node that has children. Prove that the number of internal nodes in a complete binary tree with n nodes is n/2 where x equals the largest integer that is not greater than x. Base case: A tree of size 1, for which there are 1/2 = 0 internal nodes. Inductive Hypothesis: A complete binary tree with n nodes has n/2 internal nodes. We will consider two cases: n is odd and n is even. First we shall prove that n is odd if and only if every node has either 0 or 2 children, and n is even iff there is one node with one child. Note that in a complete binary tree, there can be at most one node with only one child. Then, proof: Suppose our tree s nodes each have either 0 or 2 children. We can count the number of nodes in a tree by starting at the root and adding in its number of children, and then proceeding as such with each of those children. However, since every node can only have 2 or 0 children, we will only ever add 2 or 0 to our initial root count of 1, and so n is odd. If the tree has one node with one child, then we can apply the same process, but at one point we will only add 1 node, and so n will be even. Now consider the two cases n odd and n even: n is odd. Since every node in the tree with n nodes has 0 or 2 children, the complete tree of size n + 1 will create a node with 1 child, creating 2
one more internal node. Since by the inductive hypothesis a tree of size n has n/2 = (n 1)/2 internal nodes for odd n, the tree of size n + 1 will have (n 1)/2 + 1 = (n + 1)/2 = (n + 1)/2 internal nodes, as n + 1 is even. n is even. Then the complete tree of size n + 1 will add on another child to the node that only has 1 child, so no new internal nodes are created. Then it will still have n/2 internal nodes by the inductive hypothesis. However, this is equal to (n + 1)/2 for even n, which is our desired result. Since we have showed the tree of size n + 1 has (n + 1)/2 internal nodes, we see by induction that the claim is true for all n. 3. (From Meyer, problem 6.6) Let m, n Z where m 0 and n 0. Then, let s define a set of integers, L m,n, recursively as follows: Base cases: m, n L m,n Constructor cases: If j, k L m,n, then (a) j L m,n. and (b) j + k L m,n Let L be an abbreviation for L m,n for the rest of this problem. (a) Prove by structural induction that every common divisor of m and n also divides every member of L. (b) Prove that any integer multiple of an element of L is also in L. (c) Show that if j, k L and k 0 then the remainder of j divided by k is also in L; that is, that rem(j, k) L. (a) Proof by induction. Base case: m, n L m,n. m and n are obviously both divisible by all of their common divisors. For the induction, assume all current members of L m,n are divisible by all common divisors of m and n, and then show that for any new element we can add, this property still holds. The constructor cases are then: j for j L m,n. j is divisible by anything j is. By the inductive hypothesis, j is divisible by all common divisors of m and n, and so j is as well. 3
j + k for j, k L m,n. For any common divisor x of m and n, we can write j = ax, k = bx for some integers a, b, since j and k are both divisible by x. Then j +k = x(a+b), and so j +k is divisible by x as well. This holds for any common divisor x, so j + k is divisible by any common divisor of m and n Thus any element of L m,n is divisible by all common divisors of m and n. (b) Consider any element x L. We wish to show that for any integer a, ax L. We can do a proof by induction here: Base case: x, x L. This can also be written as 1 x and 1 x are in L. Inductive hypothesis: nx L for some integer n. We then see (n 1)x = nx + ( x) and (n + 1)x = nx + x. Since nx, x, x L by the inductive hypothesis and base case, it follows that (n 1)x, (n + 1)x L. We can reach every integer multiple of x in this fashion, and so the claim holds. (c) The value rem(j, k) is defined such that j = nk + rem(j, k) for some integer n such that 0 < rem(j, k) < k. We already showed in part b that nk L if k L, and so j nk L by the second constructor case. This is equal to rem(j, k), and so rem(j, k) L. 4. (From Sipser, exercise 1.6) Draw state machines that only accept strings in the following set. Assume that the alphabet is Σ = {0, 1}; that is, that all strings s Σ. (Bonus point(s) if you draw your raw state machines in LaTeX; check out the TikZ package to do this.) {w : w contains the substring 0101, i.e. w = x0101y for some x and y} 4
Note: this is a nondeterministic machine, meaning that it accepts a string as long as there exists a path from the start state to the accept state. The arrow indicates the start state, and the double-circles indicate the accept state. 5. (From Meyer, problem 5.29) A robot named Wall-E wanders around a twodimensional grid. He starts at (0,0) and is allowed to take four different types of steps: (a) (+2,-1) (b) (+1, -2) (c) (+1, +1) (d) (-3, 0) For example, Wall-E might take the following stroll. The types of his steps are denoted by each arrow s subscript: (0, 0) a (2, 1) c (3, 0) b (4, 2) d (1, 2)... Wall-E s true love, the fashionable and high-powered robot, Eve, awaits at (0,2). (a) Describe a state machine model of this problem. (b) Will Wall-E ever find his true love? Either find a path from Wall-E to Eve or use the Invariant Principle to prove that no such path exists. (a) In our state machine model, every state takes the form (x, y). The start state is (0, 0), and the possible transitions out of a state (x, y) are to (x + 2, y 1), (x + 1, y 2), (x + 1, y + 1), (x 3, y). (b) He will not. Consider if we take all of the possible states and take the x and y coordinates mod 3. Then the possible transitions are equivalent to: i. (+2, 1) (+2, +2) (mod 3) ii. (+1, 2) (+1, +1) (mod 3) iii. (+1, +1) (+1, +1) (mod 3) iv. ( 3, +0) (+0, +0) (mod 3) We then propose the following invariant: for any state (x, y) x mod 3 = y mod 3 We see that it is true for the start state (0, 0), and all of the transitions preserve this invariant, as they add the same amount to both the x and y coordinates mod 3. However, our desired state (0, 2) does not satisfy this invariant, and so it must be impossible to reach. 5