1.3 // Concatenate two strings String strcat(String s1, String s2); // Return the length of a string int length(String s1); // Extract a substring, beginning at start, of length length String extract(String s1, int start, int length); // Get first character char first(String s1); // Compare two strings int strcmp(String s1, String s2); // Copy a string int strcpy(String source, String dest); 1.9 No. There are many problems for which nobody has (yet) been able to find an algorithm. But even more, it can be proven that there are problems for which no solution exists (unsolvable problems). 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. (Find a counterexample.) (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. Therefore 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.11 (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.28 Use strong 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.