University of Toronto Department of Electrical and Computer Engineering Final Examination ECE 345 Algorithms and Data Structures Fall 2016 Print your first name, last name, UTORid, and student number neatly in the space provided below; print your name at the upper right corner of every page. The exam is thirteen (13) pages including the cover page; if not, report it to the instructor or TA. First Name: Last Name: UTORid: Student Number: This is an open book exam. You are permitted to use the textbook for the course, but nothing else is permissible. Non-native English speakers may use a dictionary. Do all five problems in this booklet. Try not to spend too much time on one problem. Use terminology from the textbook. You must define any different terms before you use them. Write clearly and only in the space provided. Ask the proctor if you need more paper. Do not write on the back of the page. You have 150 minutes for this exam. Raise your hand if you have a question. Do not give C code! Write pseudocode, if asked, and explain your algorithm in English! Analyze time/memory requirements of your algorithms when asked to receive full credit. Happy holidays! Question Points Score Grader 1 20 2 20 3 20 4 20 5 20 Total 100
ECE 345 Final Fall 2016 2 Name: 1. (Multiple Choice and True/False, 20 points). Each question is worth 2 points and it has only one correct answer. Write clearly! If we cannot understand your answer, you will receive no credit. 1) Using the Master Theorem, T (n) = 3T ( n 2 ) + n = Θ(nlog 2 3 ) Solution: True. 2) Given an array of n integers, each belonging to { π, 0, π}, we can sort the array in O(n) time in the worst case. Solution: True. We can simply map π to index 2, 0 to index 1 and π to index 0, then run counting sort. 3) An inorder traversal of a Min-Heap will output the values in sorted order. Solution: False. 4) A cycle in a graph G can be detected in O( V ) time. Solution: True. Depth-first search can detect cycles in O( V ) time. Simply stop, once it finds a back edge for the fist time. 5) Prim s algorithm for finding a minimum spanning tree uses dynamic programming. Solution: False. Greedy. 6) All minimum-weight edges in a graph must be in every minimum spanning tree. Solution: False. This only applies to uniquely-minimum-weight edges. 7) The regular expression (a + b) will reject the string aabab. Solution: False. It will match. 8) For any flow network and any maximum flow on G, there exists an edge such that increasing its capacity increases the maximum flow of the network. Solution: False. A counterexample is a graph with two unit capacity edges in a chain. Increasing the capacity of a single edge will not increase the max flow, since the other edge is at capacity. 9) Proving NP-hardness of a decision problem A is done by showing that A can be reduced to a known NP-hard problem in polynomial time. Solution: False. The known problem is reduced to A. 10) Some NPC problems cannot be solved in exponential time. Solution: False. NPC NP-hard.
ECE 345 Final Fall 2016 3 Name: 2. (Short Answers, 5+5+5+5 points). Answer only in the space provided. There are four independent questions. (a) Given a sorted array of n elements, write a recursive version of the binary search algorithm modified so that it locates the leftmost occurrence of the search key x found in the array (in the event that there are several occurrences). For example, for the following array A[0... 8], a search on key 4 should return index 2: A = [1, 3, 4, 4, 4, 7, 11, 15, 30]. Solution: Check if x A[mid] and go left or right accordingly, rather than just checking if x = A[mid] and then going left or right if it is strictly bigger or smaller. (b) Let G(V, E) be a connected weighted undirected graph that is not a clique, and let T be a minimum spanning tree in G. Choose u, v V such that u and v have no edge between them. Add edge (u, v) to construct a new graph G (V, E + {(u, v)}), where the weight of (u, v), denoted w(u, v), is set to be strictly larger than all the weights of the edges in E. Prove that T is also a minimum spanning tree of G. Solution: T is a spanning tree in G. Adding any other edge e T from G to T will create a cycle. For every cycle created, the heaviest edge should be removed. We prove that this procedure will not change T. There are two cases: If e = (u, v), then e will be the heaviest edge in the cycle and should be the one to remove because otherwise, the resulting tree will be heavier than T. If e (u, v), then the cycle will be part of G, and hence e must be the heaviest edge in the cycle, because otherwise, one can remove the edge e in the cycle that is heavier than e and obtain a better spanning tree than T in G. Therefore, e must be the edge to remove in G.
ECE 345 Final Fall 2016 4 Name: (c) We want to find a feasible solution or determine that no solution exists for the following system of difference constraints using the Bellman-Ford algorithm: x 1 x 4 1 x 1 x 5 4 x 2 x 3 = 9 x 3 x 1 5 x 3 x 5 2 x 4 x 3 3 x 5 x 1 5 x 5 x 4 1 i. Observe that one of the constraints is an equality. Transform the system so that it becomes a set of difference constraints such as the one described in class. Solution: x 1 x 4 1 x 1 x 5 4 x 2 x 3 9 x 3 x 2 9 x 3 x 1 5 x 3 x 5 2 x 4 x 3 3 x 5 x 1 5 x 5 x 4 1 ii. Draw a directed graph representing the system of difference constraints above. Once done, label the source vertex v 0, and other vertices v 1, v 2, v 3, v 4, and v 5 representing variables x 1, x 2, x 3, x 4, and x 5, respectively. Solution: Straightforward. Concerning the equality constraint, it can be replaced by two inequalities: x 2 x 3 9 and x 2 x 3 9, which correspond to the two edges (v 3, v 2 ) and (v 2, v 3 ) of weights 9 and -9 respectively.
ECE 345 Final Fall 2016 5 Name: iii. Use the resulting graph to check if there exists an assignment of the variables x 1, x 2, x 3, x 4, and x 5 that satisfies all the constraints. If such assignment exists, find it and give the integer values for each of x 1, x 2, x 3, x 4, and x 5. Otherwise, prove that no solution exists. Solution: The resulting graph has a negative cycle: v 1 v 3 v 4 v 5 v 1 with weight 5 3 + 1 4 = 1 < 0. Therefore, no feasible solution exists.
ECE 345 Final Fall 2016 6 Name: (d) i. Compute the max flow for the graph below. Use Edmonds-Karp algorithm. For each iteration of the algorithm, show the residual graphs with residual capacities on each edge, the augmenting path that the algorithm selects, and how much flow is pushed along that augmenting path. Solution: 3 iterations of Edmonds-Karp suffice. Max flow is 5. ii. Draw the min-cut corresponding to the max flow you found in part (i) below. Solution:
ECE 345 Final Fall 2016 7 Name: 3. (Greedy Algorithms, 10+10 points). Alice is studying Electrical and Computer Engineering at the University of Otnorot. She is taking several classes, each of which have assignments and tests. Given the large workload, Alice must manage her time wisely. At any given day during the semester, she can either work on a single task (i.e. work on an assignment or study for a test) or do nothing. Each task has a release date and due date. In order for Alice to get full marks on a particular task, she must spend every day from the task s release date to its due date working on it. If she puts in part work, she will get part marks: for example, if her ECE345 midterm is due five (5) days after being announced and she studies two (2) days, she will get 40% on it. Some tasks have more weight than others (exams are worth more than assignments). Alice s goal is to maximize her final (weighted) mark. Let T = {t 1, t 2,..., t n } be the set of tasks Alice is assigned throughout the semester. Let B = {b 1, b 2,..., b n } be the release dates (begin dates) of tasks T. Let E = {e 1, e 2,..., e n } be the due dates (end dates) of tasks T. Let W = {w 1, w 2,..., w n } be the weights of tasks T. Assume that for each task t i, 1 i n, b i < e i. Let D be the number of days in the semester (days are units of time). Note: If needed, you can let task 0 be a procrastination task that lasts all semester long (i.e. w 0 = 0, b 0 = 0, e 0 = D). (Of course, choosing to procrastinate for the whole semester will have Alice fail every class!) (a) Describe a greedy algorithm to solve this problem. Solution: This is a variation of the fractional knapsack problem, disguised as a scheduling problem. Start by getting each task s weight/day. At each day throughout the semester, work on the task that has the most weight/day, for tasks that can be worked on (current day is between start and end). For all t i T, v ti = w ti /(e ti b ti )
ECE 345 Final Fall 2016 8 Name: (b) Prove that your algorithm yields an optimal solution. Solution: Greedy choice: Let O = {o 1, o 2,..., o D } be an optimal solution. Let G = {g 1, g 2,..., g D } be the greedy solution. If o 1 = g 1, we are done. Otherwise, we know what a) g 1 can replace o 1 since G will pick g 1 among tasks that are scheduled on day 1, and b) v g1 v o1. Swap o 1 with g 1 to get O. value(o ) = value(o) v o1 +v g1. By (b) above, value(o ) value(o), but since O is optimal, value(o ) = value(o) Optimal substructure: Copy-paste argument as in factional knapsack.
ECE 345 Final Fall 2016 9 Name: 4. (Shortest Paths, 20 points). Consider the following generalization of the SP problem: Given a directed graph G = (V, E), suppose that in addition to having edge weights {w e : e E}, the graph also has vertex costs {c v : v V }. Now define the cost of a path to be the sum of its edge weights plus the costs of all vertices on the path (including the endpoints). Give a O(( V + E ) log V ) algorithm for the following problem. Input: A directed graph G = (V, E) with positive edge weights w e, positive vertex costs c v, and a source node s. Output: An array cost[ ], such that for every vertex u, cost[u] is the least cost of any path from s to u, based on the definition above. Assume that cost[s] = c s. Solution: For each edge e = (u, v), increase w e by c v. Then run Dijkstra s. The final shortest path has length equal to the sum of the length found by Dijkstra s plus c s (cost of the source).
ECE 345 Final Fall 2016 10 Name: 5. (NP-Completeness, 6+14 points). Consider the Minimum Leaf Spanning Tree problem: MIN-LEAF-TREE: Given an undirected graph G, and an integer k is a there a spanning tree in G that contains at most k leaves? (a) Show that MIN-LEAF-TREE is in NP. Clearly explain the form of the certificate, as well as, how to verify it in polynomial time. Solution: Ensure it is spanning tree and it has at most k leaves. It s poly-time to do so. Note: many students assumed the certificate is a tree. This is not correct. We need to verify it is a tree (connected, acyclic). Or equivalently, that it is connected and E = V 1, which implies a tree. (b) Consider the Hamiltionian path problem: HAM-PATH: Given an undirected graph G = (V, E) and two vertices u and v, is a there a simple path between u and v that visits each vertex in V exactly once? Note: This is not the HAM-CYCLE problem seen in class. You may assume that HAM-PATH is NP-Complete (no need to prove it). Show that MIN-LEAF-TREE is NP-hard by reducing it from HAM-PATH. In other words, show that HAM-PATH p MIN-LEAF-TREE. Make sure to show that your reduction takes polynomial time. Solution A (partial marks 16/20): Given an instance of Hamiltonian Path on a graph G = (V, E) we create an instance of the minimum leaf spanning tree problem G, k as follows. We use exactly the same graph, i.e G = G but now we set k = 2. A tree with 2 leaves is a path, so a spanning tree with two leaves is a Hamiltonian path in the graph. So there exists a Hamiltonian path iff there exists a spanning tree with 2 leaves. Note: some students set k = 1. Strictly speaking this is not correct, but we did not penalize, if the student specifically stated that one of the nodes plays the role of the root. Solution B (full marks 20/20): This solution corresponds to the proper interpretation of the HAM-PATH problem as given in this context. That is, u and v are GIVEN as parameters to the problem, and MUST be the endpoints of the hamiltonian path in G. Given an instance of Hamiltonian Path on a graph G = (V, E) we create an instance of the minimum leaf spanning tree problem G, k as follows. Create G by adding one node attached to u (call it u ), and one node attached to v (call it v ). Also set k = 2. If u v hamiltonian path in G, then a spanning tree with at most 2 leaves in G.
ECE 345 Final Fall 2016 11 Name: Proof: clearly the hamiltonian path has u and v as endpoints, has no cycles and it is a path that spans all vertices in G apart from u and v. But, if the path is extended to include u and v it remains acyclic, it spans all vertices and still has at most 2 leaves. Thus it is a spanning tree with at most 2 leaves in G. Q.E.D If a spanning tree with at most 2 leaves in G, then u v hamiltonian path in G. Proof: The spanning tree in G has to visit u and v. But then, these two nodes are necessarily leaves in the tree. Thus, no other nodes can be leaves in the tree, since this is a spanning tree with at most 2 leaves. This implies that u and v are included in a hamiltonian path of G that has u and v as endpoints in G. Assume towards a contradiction that u has degree 3 in that hamiltonian path. Consider any of the two edges, but not (u, u ). This edge must connect to a non-leaf node in the spanning tree, unless it is v, or the tree would have more than 2 leaves. In turn, the non-leaf node must connect to another non-leaf node, unless it is v, or the tree would have more than 2 leaves, e.t.c. This implies, that the path from u using the edge we picked must end at v. Similarly, if we consider the other edge, but not (u, u ) (remember we assumed u has degree 3, so it has 3 edges), we conclude that there is another path from u to v. But then, we have a cycle in the spanning tree; a contradiction. Thus u has degree 2 in the spanning tree, and thus it has degree 2 in the hamiltonian path of G. Symmetrically, we prove that v also has degree 2. Therefore, both u and v have degree 2 in the hamiltonian path of G. Now, in G, if we exclude u and v, since they are not part of G, we get a hamiltonian path where u and v have degree 2 1 = 1. Thus, we get a hamiltonian path in G with u and v as its endpoints. Q.E.D Note: the proof is of course much harder here. We had a few students that got it completely right with very very similar proofs. For example, in the second half of the left-to-right direction (the contradiction part), if you just say that the hamiltonian path is acyclic and this implies all non-leaf nodes, like u and v, have degree 2, then the proof gets full marks.
ECE 345 Final Fall 2016 12 Name: (this page left intentionally blank)
ECE 345 Final Fall 2016 13 Name: (this page left intentionally blank)
ECE 345 Final Fall 2016 14 Name: (this page left intentionally blank)