Practice Final Solutions 1. Consider the following algorithm. Assume that n 1. line code 1 alg(n) 2 j = 0 3 if (n = 0) 4 return j } 5 else 6 j = 2n+ alg(n 1) 7 return j } } Set up a recurrence relation (with initial conditions) whose solution gives L(n), the number of lines executed when alg(n) runs. (You do not need to solve this recurrence relation.) Also set one up for R(n), the return value for alg(n). Solution: Let L(n) be the number of lines executed when alg(n) runs. If n > 0, then we will execute lines 1, 2, 3, 5, and 6. Next we will call alg(n 1), which will execute L(n 1) lines. Last, we will execute line 7. This gives a total of 6+L(n 1) lines. If, on the other hand, n = 0, then we will execute only lines 1, 2, 3, and 4. Therefore, the recurrence relation with initial conditions is L(n) = 6 + L(n 1) n > 0. L(0) = 4 For the return value, if n > 0, the algorithm returns the value in j, which is 2n plus whatever alg(n 1) returns. If n = 0 then the algorithm also returns j, but this time j = 0. Therefore the recurrence relation with initial conditions is 2. Consider the set X = 1, 2}. R(n) = 2n + R(n 1) n > 0. R(0) = 0 (a) List all relations on this set. Which are reflexive? Transitive? Symmetric? Equivalence relations? Solution: A relation is a subset of the set X X. Therefore we need to know what the elements of X X look like. They are the following: 1
X X = (1, 1), (1, 2), (2, 1), (2, 2)}. Since this set has four elements, it should have 2 4 = 16 subsets. Therefore there are 16 relations on the set X. These relations are size sets 0 } 1 (1, 1)}, (1, 2)}, (2, 1)}, (2, 2)} 2 (1, 1), (1, 2)}, (1, 1), (2, 1)}, (1, 1), (2, 2)}, (1, 2), (2, 1)} (1, 2), (2, 2)}, (2, 1), (2, 2)} 3 (1, 1), (1, 2), (2, 1)}, (1, 1), (1, 2), (2, 2)}, (1, 1), (1, 2), (2, 2)}, (1, 2), (2, 1), (2, 2)} 4 (1, 1), (1, 2), (2, 1), (2, 2)} The reflexive relations are (1, 1), (2, 2)}, (1, 1), (2, 2), (1, 2)}, (1, 1), (2, 2), (2, 1)}, The transitive relations are (1, 1), (1, 2), (2, 1), (2, 2)}. }, (1, 1)}, (1, 2)}, (2, 1)}, (2, 2)} (1, 1), (2, 2)}, (1, 1), (2, 1)}, (1, 2), (2, 2)}, (2, 1), (2, 2)}, (1, 1), (1, 2), (2, 2)}, (1, 1), (2, 1), (2, 2)}, The symmetric relations are (1, 1), (1, 2), (2, 1), (2, 2)}. }, (1, 1)}, (2, 2)}, (1, 1), (2, 2)}, (1, 2), (2, 1)}, (1, 1), (1, 2), (2, 1)}, (1, 2), (2, 1), (2, 2)}, (1, 1), (1, 2), (2, 1), (2, 2)}. Therefore, the equivalence relations are (1, 1), (2, 2)}, (1, 1), (1, 2), (2, 1), (2, 2)}. (b) How many of those relations are functions? One to one functions? Onto functions? Invertible functions? Solution: A function is a relation R which has the property that for each x X there is a unique y X such that (x, y) R. Therefore, the functions are (1, 1), (2, 2)}, (1, 2), (2, 2)}, (1, 1), (2, 1)}, (1, 2), (2, 1)}. The one to one functions are 2
(1, 1), (2, 2)}, (1, 2), (2, 1)}. These are also the onto functions, and the invertible functions. (c) List all partitions of X. Solution: Recall that a partition P of X is a set of subsets of X such that i. for each S, T P such that S T, we have S T = }, and ii. the union of all elements of P is the set X. The above definition allows to include the empty set in a partition. I will let you choose whether or not you want to. Here, we will not. Therefore, the partitions of X are 1}, 2}}, 1, 2}}. 3. Out of 30 students, 10 do not have American citizenship and 20 do not have Canadian citizenship. There are 5 students with both American and Canadian citizenship. How many have neither Canadian nor American citizenship? Solution: You should draw a Venn diagram for this problem (and follow along with my explanation). Let A be the set of students who have American citizenship and let C be the set of students who have Canadian citizenship. Let S be the set of all students. We know that the number of elements of S \ A (which we denote by S \ A ) is 10 and that S \ C = 20. This means that In addition, we are told that A = 20 and C = 10. Since we get A C = 5. A C = A + C A C, A C = 20 + 10 5 = 25. This is the number of students with American or Canadian citizenship. Therefore the number of students without either citizenship is 5. 3
4. (a) Solve the following recurrence relation, writing the term L(n) as a function of n. L(n) = 3L(n 1) + 10L(n 2) L(1) = 2 L(2) = 3 Solution: FIrst we consider the characteristic equation of the recurrence: This gives roots Therefore the general solution is r 2 3r 10 = 0, or (r 5)(r + 2) = 0. r 1 = 2, r 2 = 5. L(n) = c 1 ( 2) n + c 2 (5) n. To solve for the constants, use the initial conditions. 2 = L(1) = c 1 ( 2) 1 + c 2 (5) 1 3 = L(2) = c 1 ( 2) 2 + c 2 (5) 2 2 = 2c 1 + 5c 2 3 = 4c 1 + 25c 2 Multiply the first equation by 2 to get 4 = 4c 1 + 10c 2 3 = 4c 1 + 25c 2 and add the two equations to get 7 = 35c 2, so that c 2 = 1/5. Plug this into the original first equation to get Therefore, the specific solution is 2 = 2c 1 + (1/5)(5) 2 = 2c 1 + 1 c 1 = 1/2. L(n) = ( 1/2)( 2) n + (1/5)(5) n = ( 2) n 1 + 5 n 1. 4
(b) Suppose that your answer L(n) above is the number of lines executed for an algorithm that you wrote. Suppose that I have another algorithm which does the same as your algorithm but has a run-time complexity of θ(n 4 ). Which algorithm is better? Prove your answer is correct. Solution: The algorithm with complexity θ(n 4 ) is better. The reason is that it has a lower complexity than the given algorithm, which has run-time complexity θ(5 n ). We can see this as follows: L(n) = ( 2) n 1 + 5 n 1 5 n 1 + 5 n 1 = (1/5)(5) n + (1/5)(5) n = (2/5)5 n, so that L(n) = O(5 n ). To show the other direction, notice that for n 2, so that 2 n 1 (5/2) n 1 (1/2)(5) n 1 (1/2)5 n, L(n) = ( 2) n 1 + 5 n 1 5 n 1 2 n 1 5 n 1 (1/2)5 n 1 = (1/2)5 n 1 = (1/10)5 n. This gives L(n) = Ω(5 n ). Therefore, L(n) = θ(5 n ), which is incredibly greater than θ(n 4 ). 5. (a) Does the graph below have an Euler cycle? Why or why not? Solution: Yes it does. The reason is that the degree of each vertex is even, and we know that this is equivalent to having an Euler cycle. 5
(b) Solve the traveling salesman problem for the graph below. After that, list the order in which edges are added into a minimal spanning tree using Kruskal s algorithm. Next do the same, but for Prim s algorithm, starting at the lower left vertex. Solution: To solve the traveling salesman problem, we note that there are 5 vertices, so any Hamiltonian cycle must have 5 edges. So we will list combinations of 5 edges, in order of increasing total weight, until we find a Hamiltonian cycle. We will list the edges by their weights since they are all distinct. edge combination Hamiltonian cycle? 12345 no 12346 no 12356 no 12456 yes So a solution is the Hamiltonian cycle consisting of edges labeled 1, 2, 4, 5, and 6. The total weight of this solution is 18. Note that this argument does not rule out the existence of other solutions. For Kruskal s algorithm, we add edges into a growing tree, in order of increasing weight, making sure to omit edges which form cycles in our tree. Therefore we will add edges 1, 2, 3, 4 (in order of our addition). Note we stop at 4 edges because there are 5 vertices in the graph, and consequently any spanning tree will have 4 edges. For Prim s algorithm, we invade from the start vertex at the lower left. The edges we will add (in order) are 1, 2, 3, 4. 6