Exercises from Chapter 2 2.2 2.6 2.10 2.17 2.22 2.23 2.27 Solutions 2.2 (a) For any a, a+a = 2a is even, so the relation is reflexive. a+b = b+a, so the relation is symmetric. If a+b and b+c are even, then (a+b) + (b+c) = a+c+2b is even, which implies a+c is even, so the relation is transitive. (b) Not an equivalence relation because it is not reflexive: a+a is not odd. (c) Equivalence relation. You can check the details. (d) Not an equivalence relation because it is not symmetric. (e) For any a, a-a=0, which is an integer, so the relation is reflexive. If a-b is an integer, then since b-a = -(a-b), it is also an integer, so the relation is symmetric. If a-b and b-c are integers, then since a-c = a-b + b-c, it is also an integer, and the relation is transitive. Therefor the relation is an equivalence relation. (f) The relation is not an equivalence relation because it is not transitive. Here is a counterexample showing it is not transitive: a=3, b=1, c=0. Then |a-b|<=2 and |b-c|<=2, but |a-c|>2. 2.6 void clear(); void insert(int); void remove(int); void sizeof(); bool isEmpty(); // return count of integers with a given value int countInBag(int); // add the integers in another bag to this bag // If an integer occurs a times in this bag and b times in the other // bag, then it will occur a+b times after performing the operation void union(bag); // intersect this bag with another bag. // If an integer occurs a times in this bag and b times in the other // bag, then it will occur min(a,b) times after performing the operation void intersect(bag); // take the integers in another bag from this bag. // If an integer occurs a times in this bag and b times in the other // bag, then it will occur max(a-b,0) times after performing the operation void difference(bag); 2.10 (a) Probably fibr, unless you don't understand recursion. (b) It can be shown that the running time of fibr is 2#(Omega(n)), while the running time of fibi is Omega(n), which is much faster. (2#k means 2 to the power k). It's not hard to show the running time of fibi is Omega(n). To estimate the running time of fibr, let S(n) be its running time. Then S(1) = S(2) = 1, and S(n) = S(n-1) + S(n-2) + 1 for n > 2. (Strictly speaking, I should use constants instead of the 1's, but since we are trying to get an estimate, that's OK.) Then, using induction on n, we can show that S(n) >= 2#(n/2 - 1). (use strong induction with two base cases: n = 1 and n = 2.) Therefore S(n) = 2#(Omega(n)). 2.17 The first sum is just 1+2+...+n. The second sum is (n-1+1)+(n-2+1)+...+(n-n+1). The third sum is (n-0)+(n-1)+...+(n-n+1). Obviously they are all the same. 2.22 (a) Assume 1 + 3 + ... + (2n-1) = n#2. The sum of the first n even numbers is 2 + 4 + ... + 2n = (1+1) + (3+1) + ... + (2n-1+1) = 1 + 3 + ... + (2n-1) + 1 + 1 + ... + 1 (n times) = n#2 + n (b) Base case: n = 1. The sum of the first even number is 2. 1#2 + 1 = 2, so the claim is true for n = 1. Induction step: assume the sum of the first n even numbers is n#2 + n. The sum of the first n+1 even numbers is 2 + 4 + ... + 2n + (2n+2) = n#2 + n + (2n+2) by the induction assumption = n#2 + 2n + 1 + n+1 = (n+1)#2 + (n+1) 2.23 Use induction on n. Base cases: n=1: Fib(1) = 1, and (5/3)#1 = 5/3 > 1. n=2: Fib(2) =1 < (5/3)#2. Induction step: Assume Fib(m) < (5/3)#m for all m2. We will prove Fib(n) < (5/3)#n. By definition, Fib(n) = Fib(n-1) + Fibn(n-2) < (5/3)#(n-1) + (5/3)#(n-2) by induction assumption = (5/3)#(n-2)(5/3 + 1) = (5/3)#(n-2)8/3 < (5/3)#n since 8/3 < 25/9 = (5/3)#2. 2.27 Base case: n=1: T(1) = 1 by defintion, and 1(1+1)/2 = 1. Induction step: assume T(n-1) = (n-1)n/2. To prove T(n) = n(n+1)/2, T(n) = T(n-1) + n by definition = (n-1)n/2 + n by induction assumption = n(n+1)/2.