University of Toronto Department of Electrical and Computer Engineering. Final Examination. ECE 345 Algorithms and Data Structures Fall 2016

Similar documents
FINAL EXAM PRACTICE PROBLEMS CMSC 451 (Spring 2016)

University of New Mexico Department of Computer Science. Final Examination. CS 561 Data Structures and Algorithms Fall, 2006

CSI 4105 MIDTERM SOLUTION

CMPSCI 311: Introduction to Algorithms Second Midterm Exam

Examination paper for TDT4120 Algorithms and Data Structures

NATIONAL UNIVERSITY OF SINGAPORE CS3230 DESIGN AND ANALYSIS OF ALGORITHMS SEMESTER II: Time Allowed 2 Hours

CS 320, Fall Dr. Geri Georg, Instructor 320 NP 1

CSCE 750 Final Exam Answer Key Wednesday December 7, 2005

CSE 202 Homework 4 Matthias Springer, A

Essential facts about NP-completeness:

CS 170 Algorithms Spring 2009 David Wagner Final

CSE 421 Introduction to Algorithms Final Exam Winter 2005

More on NP and Reductions

CPSC 320 (Intermediate Algorithm Design and Analysis). Summer Instructor: Dr. Lior Malka Final Examination, July 24th, 2009

NP Completeness and Approximation Algorithms

1. (a) Explain the asymptotic notations used in algorithm analysis. (b) Prove that f(n)=0(h(n)) where f(n)=0(g(n)) and g(n)=0(h(n)).

1. Introduction Recap

Discrete Optimization 2010 Lecture 2 Matroids & Shortest Paths

Summer School on Introduction to Algorithms and Optimization Techniques July 4-12, 2017 Organized by ACMU, ISI and IEEE CEDA.

University of New Mexico Department of Computer Science. Final Examination. CS 362 Data Structures and Algorithms Spring, 2007

Combinatorial Optimization

Design and Analysis of Algorithms April 16, 2015 Massachusetts Institute of Technology Profs. Erik Demaine, Srini Devadas, and Nancy Lynch Quiz 2

Limitations of Algorithm Power

Algorithms Exam TIN093 /DIT602

COMP Analysis of Algorithms & Data Structures

CS60007 Algorithm Design and Analysis 2018 Assignment 1

IS 709/809: Computational Methods in IS Research Fall Exam Review

University of New Mexico Department of Computer Science. Midterm Examination. CS 361 Data Structures and Algorithms Spring, 2003

NP-Complete Reductions 2

Greedy Algorithms My T. UF

CS 70 Discrete Mathematics and Probability Theory Fall 2016 Seshia and Walrand Midterm 1 Solutions

CSEP 521 Applied Algorithms. Richard Anderson Winter 2013 Lecture 1

COMPSCI 611 Advanced Algorithms Second Midterm Exam Fall 2017

Design and Analysis of Algorithms May 12, 2011 Massachusetts Institute of Technology. Practice Final Exam. Problem Title Points Parts Grade Initials

NAME (1 pt): SID (1 pt): TA (1 pt): Name of Neighbor to your left (1 pt): Name of Neighbor to your right (1 pt):

CS 170 Algorithms Fall 2014 David Wagner MT2

Midterm 1. Your Exam Room: Name of Person Sitting on Your Left: Name of Person Sitting on Your Right: Name of Person Sitting in Front of You:

Data Structures in Java

University of New Mexico Department of Computer Science. Final Examination. CS 561 Data Structures and Algorithms Fall, 2013

NP-Completeness. Until now we have been designing algorithms for specific problems

Lecture 18: P & NP. Revised, May 1, CLRS, pp

CS 6901 (Applied Algorithms) Lecture 3

VIII. NP-completeness

CS325: Analysis of Algorithms, Fall Final Exam

CS 6783 (Applied Algorithms) Lecture 3

Advanced Analysis of Algorithms - Midterm (Solutions)

Unit 1A: Computational Complexity

Problem set 1. (c) Is the Ford-Fulkerson algorithm guaranteed to produce an acyclic maximum flow?

Midterm Exam 2 Solutions

P is the class of problems for which there are algorithms that solve the problem in time O(n k ) for some constant k.

Computer Science 385 Analysis of Algorithms Siena College Spring Topic Notes: Limitations of Algorithms

10.3 Matroids and approximation

Algorithms Re-Exam TIN093/DIT600

Lecture 4: NP and computational intractability

CS 241 Analysis of Algorithms

ECS122A Handout on NP-Completeness March 12, 2018

NP-completeness. Chapter 34. Sergey Bereg

Chapter 34: NP-Completeness

Lecture 13, Fall 04/05

Lecture 2: Divide and conquer and Dynamic programming

Algorithm Design and Analysis

Complexity, P and NP

NP-Completeness. CptS 223 Advanced Data Structures. Larry Holder School of Electrical Engineering and Computer Science Washington State University

Algorithm Design and Analysis

CMPSCI 611 Advanced Algorithms Midterm Exam Fall 2015

NP-Complete Problems. More reductions

CS 583: Algorithms. NP Completeness Ch 34. Intractability

Easy Problems vs. Hard Problems. CSE 421 Introduction to Algorithms Winter Is P a good definition of efficient? The class P

Quiz 1 Solutions. Problem 2. Asymptotics & Recurrences [20 points] (3 parts)

NP and Computational Intractability

NP-Completeness. Andreas Klappenecker. [based on slides by Prof. Welch]

COMPSCI 611 Advanced Algorithms Second Midterm Exam Fall 2017

8. INTRACTABILITY I. Lecture slides by Kevin Wayne Copyright 2005 Pearson-Addison Wesley. Last updated on 2/6/18 2:16 AM

CMPSCI611: The Matroid Theorem Lecture 5

Algorithms: Lecture 12. Chalmers University of Technology

CS 598RM: Algorithmic Game Theory, Spring Practice Exam Solutions

NP-Completeness. Algorithmique Fall semester 2011/12

Computational Complexity and Intractability: An Introduction to the Theory of NP. Chapter 9

Class Note #20. In today s class, the following four concepts were introduced: decision

NP-complete problems. CSE 101: Design and Analysis of Algorithms Lecture 20

CSE 431/531: Analysis of Algorithms. Dynamic Programming. Lecturer: Shi Li. Department of Computer Science and Engineering University at Buffalo

Algorithm Design Strategies V

Efficient Algorithms and Intractable Problems Spring 2016 Alessandro Chiesa and Umesh Vazirani Midterm 2

Design and Analysis of Algorithms

Dynamic Programming: Shortest Paths and DFA to Reg Exps

Greedy Algorithms. Kleinberg and Tardos, Chapter 4

Algorithms. NP -Complete Problems. Dong Kyue Kim Hanyang University

1 T 1 = where 1 is the all-ones vector. For the upper bound, let v 1 be the eigenvector corresponding. u:(u,v) E v 1(u)

CS1800 Discrete Structures Spring 2018 February CS1800 Discrete Structures Midterm Version A

CS 350 Algorithms and Complexity

ICS 252 Introduction to Computer Design

CS675: Convex and Combinatorial Optimization Fall 2014 Combinatorial Problems as Linear Programs. Instructor: Shaddin Dughmi

CHAPTER 3 FUNDAMENTALS OF COMPUTATIONAL COMPLEXITY. E. Amaldi Foundations of Operations Research Politecnico di Milano 1

Outline. 1 NP-Completeness Theory. 2 Limitation of Computation. 3 Examples. 4 Decision Problems. 5 Verification Algorithm

Algorithms and Data Structures (COMP 251) Midterm Solutions

CSCI3390-Second Test with Solutions

NAME: Be clear and concise. You may use the number of points assigned toeach problem as a rough

Dynamic Programming. Data Structures and Algorithms Andrei Bulatov

Morning Time: 1 hour 30 minutes

Transcription:

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)