Discrete Structures May 13, 2014 Prof Feighn Final Exam 1. (40 points) Define each of the following terms: (a) (binary) relation: A binary relation from a set X to a set Y is a subset of X Y. (b) function: A function from a set X to a set Y is a relation from X to Y such that each x X is related to exactly one y Y. (c) onto: A function f : X Y is onto if, for every y Y, there is x X such that f(x) = y. (d) one-to-one: A function f : X Y is one-to-one if, for all x, x X, we have that f(x) = f(x ) implies x = x. (e) transitive: A relation R on a set X is transitive if we have that xry and yrz implies xrz. (f) equivalence relation: A relation on a set X is an equivalence relation if it is reflexive, symmetric, and transitive. (g) equivalence class: A subset A of X is an equivalence class of an equivalence relation R on X if there is x X such that A = {y X yrx}. (h) recurrence relation: A recurrence relation for the sequence a 1, a 2,... is an expression for a n in terms of its predecessors.
2. (10 points) Construct the truth table for (p = q) ( q = p) p q p = q q p q p (p = q) ( q = p) T T T F F T T T F F T F F T F T T F T T T F F T T T T T 3. (10 points) For integers x, y Z write x y if y x is a multiple of three. Show that is an equivalence relation on Z. (Recall that Z = {..., 2, 1, 0, 1, 2,... } and that n Z is a multiple of three if and only if n = 3m for some m Z.) Proof. is reflexive: Let x Z. Then x x = 3 0 and so x x. is symmetric: Asssume x, y Z and x y. Then there is m Z so that y x = 3m. Then x y = 3( m) and so y z. is transitive: Assume x, y, z Z, y y and y z. Then there is m, n Z so that y x = 3m and z y = 3n. Hence z x = 3(m + n) and so x z. 4. (10 points) Prove that, for all sets X, Y, and Z, X (Y Z) (X Y ) (X Z) Proof. Let t X (Y Z). Then t X and t (Y Z). Therefore t X and t Y or t X and t Z. In the first case t X Y and in the second case t X Z. In any case, t (X Y ) (X Z). 5. (10 points) Prove by induction that for all positive integers n. 2 + 4 + 6 + + 2n = n(n + 1)
Proof. Let P (n) be the proposition that 2 + 4 + 6 + + 2n = n(n + 1) We will prove by induction that P (n) holds for all positive integers n. Base case (P (1) is true): Suppose n = 1. We have 2 = 1(1 + 1) and P (1) is true. Inductive case (P (n) = P (n + 1) is true): Suppose P (n) is true for the positive integer n. Then 2 + 4 + 6 + + 2n + 2(n + 1) =n(n + 1) + 2(n + 1) and so P (n + 1) is true. =(n + 1)(n + 2) 6. (10 points) Consider f : N N given by f(n) = n 3. (a) Is f 1-1? If so, prove it. Otherwise find a counterexample. f is 1-1. Proof. Let x, x N. Suppose f(x 1 ) = f(x 2 ). Then x 3 1 = x 3 2. It follows that x 3 1 x 3 2 = (x 1 x 2 )(x 2 1 x 1 x 2 + x 2 2) = 0. Since x 2 1 x 1 x 2 +x 2 2 = (x 1 x 2 ) 2 +x 1 x 2 is the sum of a non-negative term and a positive term, this second term is never zero. So x 3 1 x 3 2 = 0 implies that x 1 = x 2. (b) Is f onto? If so, prove it. Otherwise find a counterexample. The function f is not onto. For example, 2 is not the cube of a natural number. 7. (20 points) For A = {a, b}, B = {b, c} and U = {a, b, c, d}, compute each of the following. (a) A B = {b} (b) A B = {a} (c) A A = {(a, a), (a, b), (b, a), (b, b)} (d) B = {a, b}
8. Let R be the relation given by the digraph below. 1 2 3 (a) (5 points) Write the matrix M for R. 1 1 0 M = 1 1 0 0 0 1 (b) (5 points) Compute M 2. M 2 = 2 2 0 2 2 0 0 0 1 (c) (10 points) Prove that R is an equivalence relation. Proof. R is reflexive since all diagonal entries of M are non-zero. R is symmetric since M is symmetric. R is transitive because if an entry of M 2 is non-zero then the corresponding entry of M is also non-zero. 9. (10 points) In each of the following two problems, compute t(n) where t(n) is the number of times the statement x = x + 1 is executed. Be sure to explain your answers. (a) for i = 1 to n for j = 1 to i x = x + 1 t(n) = 1 + 2 + 3 + + n = n(n + 1) n
(b) i = 2 n while (i 1){ x = x + 1 i = i/2 } The while loop is executed log 2 (2 n ) + 1 = n + 1 times and each of these times x = x + 1 is executed once. Therefore t(n) = n + 1 10. (10 points) Consider the relational databases given by the three tables below. Write a sequence of operations to answer the following query. Find all buyers who buy parts from Jones s department together with the numbers of those parts. Temp1:=DEPARTMENT[Manager=Jones] Temp2:=Temp1[Dept=Dept]SUPPLIER Temp3:=Temp2[Part No=Part No]BUYER Answer:=Temp3[Name, Part No]. Table 1: DEPARTMENT Dept Manager 23 Jones 04 Yu 96 Zamora 66 Washington Table 2: BUYER Name Part No United Supplies 2A ABC Unlimited 8C200 United Supplies 1199C JCN Electronics 2A United Supplies 335B2 ABC Unlimited 772 Danny s 900 United Supplies 772 Underhanded Sales 20A8 Danny s 20A8 DePaul University 42C ABC Unlimited 20A8
Table 3: SUPPLIER Dept Part No Amount 04 335B2 220 23 2A 14 04 8C200 302 66 42C 3 04 900 7720 96 20A8 200 96 1199C 296 23 772 39 23 99999999 0 11. (15 points) Solve the following recurrence relations. (a) a n = 5 + a n 1 with initial condition a 0 = 3. a n = 5 + a n 1 = 5 + (5 + a n 1 ) = 2 5 + a n 2 = = k 5 + a n k Setting k = n, we have a n = n 5 + a 0 = 5n + 3. (b) a n = 3a n 1 2a n 2 with initial conditions a 0 = 8, a 1 = 13. The equation x 2 3x + 2 has roots x = 1 and x = 2. Hence a n = b 1 n + d 2 n. The initial conditions give 8 =b + d 13 =b + 2d which has solution b = 3 and d = 5. We conclude a n = 3 + 5 2 n. (c) a n = 6a n 1 9a n 2 with initial conditions a 0 = 1, a 1 = 12. The equation x 2 6x + 9 has the double root x = 3. Hence a n = b3 n + dn3 n. The initial conditions give 1 =b 12 =3b + 3d which has solution b = 1 and d = 3. We conclude a n = 3 n +3n3 n = 3 n (1 + 3n).
12. (20 points) Here is pseudocode for our binary search algorithm. Input: A sequence s i, s i+1,..., s j, i 1 sorted in nodecreasing order, a value key, i, and j. Output: The output is an index k for which s k = key or if key is not in the sequence, the output is the value 0. 1. binary search(s, i, j, key){ 2. if (i > j) //not found 3. return 0 4. k = (i + j)/2 5. if (key == s k ) //found 6. return k 7. if (key < s k ) // search left half 8. j = k 1 9. else // search right half 10. i = k + 1 11. return binary search(s, i, j, key) 12. } (a) Show how this algorithm works for the input s 1 = B, s 2 = D, s 3 = F, s 4 = S and key = S. At the start i = 1 and j = 4. Since 1 4, k = 2. Now key D and key D and so i = 3. We now execute binary search to the data s 3 = F and s 4 = S with the same key. Since 3 4, k = 3. Now key F and key S and so i = 4. We next execute binary search to the data s 4 = S. Since 4 4, k = 4. This time key = S is true and so k = 4 is returned. (b) Let b n be the number of times the algorithm is called in the worst case where the input sequence has n items. Explain why b 1 = 2 and b n = 1 + b n/2
For n 0, binary search is called once at the beginning. In the worst case, key is not present. If n = 1, binary search is called again at line 11, but this time with i > j and so binary search is not called again. We conclude that b 1 = 2. If n > 1, then binary search is called again at line 11, but with j i = n/2. Hence b n = 1 + b n/2. (c) Find b 2, b 3, and b 4. b 2 = 3, b 3 = 3, b 4 = 4 (d) Solve the recurrence relation in part b) for b n in case n is a power of 2, i.e. n = 2 k. We have b 2 k = 1 + b 2 k 1 = 1 + (1 + b 2 k 2) = 2 + b 2 k 2 = = m + b 2 k m Setting m = k, we obtain b 2 k = k + b 2 0 = k + 2 = log 2 (n) + 2.