CMPSCI 311: Introduction to Algorithms Practice Final Exam

Similar documents
COMPSCI 311: Introduction to Algorithms First Midterm Exam, October 3, 2018

UML CS Algorithms Qualifying Exam Fall, 2003 ALGORITHMS QUALIFYING EXAM

Solution for Homework set 3

Introduction to Algorithms May 14, 2003 Massachusetts Institute of Technology Professors Erik Demaine and Shafi Goldwasser.

Exam 3 Practice Problems

Homework 4 Solutions

Jessica Su (some parts copied from CLRS / last quarter s notes)

CMSC Theory of Algorithms Second Midterm

Homework 3 Solutions

Solutions for the Exam 6 January 2014

FINAL EXAM SOLUTIONS

CIS 121 Data Structures and Algorithms Midterm 3 Review Solution Sketches Fall 2018

ALGORITHMS EXAMINATION Department of Computer Science New York University December 17, 2007

Questions Total Points Score

U.C. Berkeley CS170 : Algorithms, Fall 2013 Midterm 1 Professor: Satish Rao October 10, Midterm 1 Solutions

1 Format. 2 Topics Covered. 2.1 Minimal Spanning Trees. 2.2 Union Find. 2.3 Greedy. CS 124 Quiz 2 Review 3/25/18

MC 302 GRAPH THEORY 10/1/13 Solutions to HW #2 50 points + 6 XC points

Week 11: Minimum Spanning trees

CMSC Introduction to Algorithms Spring 2012 Lecture 16

Final Exam Math 38 Graph Theory Spring 2017 Due on Friday, June 2, at 12:50 pm. Good Luck!!

Multiple-choice (35 pt.)

Problem Score Maximum MC 34 (25/17) = 50 Total 100

CS521 \ Notes for the Final Exam

COMP 251 Winter 2017 Online quizzes with answers

Chapter 9 Graph Algorithms

11/22/2016. Chapter 9 Graph Algorithms. Introduction. Definitions. Definitions. Definitions. Definitions

U.C. Berkeley CS170 : Algorithms Midterm 1 Lecturers: Alessandro Chiesa and Umesh Vazirani February 13, Midterm 1

Homework 2 Solutions

Framework for Design of Dynamic Programming Algorithms

Theorem 2.9: nearest addition algorithm

Basic Combinatorics. Math 40210, Section 01 Fall Homework 4 Solutions

U.C. Berkeley CS170 : Algorithms Midterm 1 Lecturers: Alessandro Chiesa and Satish Rao September 18, Midterm 1

Final. Name: TA: Section Time: Course Login: Person on Left: Person on Right: U.C. Berkeley CS170 : Algorithms, Fall 2013

CPS 102: Discrete Mathematics. Quiz 3 Date: Wednesday November 30, Instructor: Bruce Maggs NAME: Prob # Score. Total 60

Graphs and Network Flows IE411. Lecture 13. Dr. Ted Ralphs

Subset sum problem and dynamic programming

Source. Sink. Chapter 10: Iterative Programming Maximum Flow Problem. CmSc250 Intro to Algorithms

Solving problems on graph algorithms

These are not polished as solutions, but ought to give a correct idea of solutions that work. Note that most problems have multiple good solutions.

CS261: Problem Set #1

Traveling Salesman Problem (TSP) Input: undirected graph G=(V,E), c: E R + Goal: find a tour (Hamiltonian cycle) of minimum cost

Chapter 5 Graph Algorithms Algorithm Theory WS 2012/13 Fabian Kuhn

Lecture 3: Graphs and flows

Name: Lirong TAN 1. (15 pts) (a) Define what is a shortest s-t path in a weighted, connected graph G.

6. Lecture notes on matroid intersection

Chapter 6. Dynamic Programming

Your favorite blog : (popularly known as VIJAY JOTANI S BLOG..now in facebook.join ON FB VIJAY

Practice Problems for the Final

CS 373: Combinatorial Algorithms, Spring 1999

Introduction to Graph Theory

Problem 1. Which of the following is true of functions =100 +log and = + log? Problem 2. Which of the following is true of functions = 2 and =3?

Tutorial 7: Advanced Algorithms. (e) Give an example in which fixed-parameter tractability is useful.

Mathematical Tools for Engineering and Management

Algorithm Design Methods. Some Methods Not Covered

Analyze the obvious algorithm, 5 points Here is the most obvious algorithm for this problem: (LastLargerElement[A[1..n]:

CS 125 Section #5 Graph Traversal and Linear Programs October 6, 2016

L.J. Institute of Engineering & Technology Semester: VIII (2016)

MATH10001 Mathematical Workshop. Graphs, Trees and Algorithms Part 2. Trees. From Trees to Prüfer Codes

CS 341: Algorithms. Douglas R. Stinson. David R. Cheriton School of Computer Science University of Waterloo. February 26, 2019

Homework #5 Algorithms I Spring 2017

Introduction to Algorithms I

Sankalchand Patel College of Engineering - Visnagar Department of Computer Engineering and Information Technology. Assignment

Solutions to relevant spring 2000 exam problems

Dynamic Programming. CSE 101: Design and Analysis of Algorithms Lecture 19

CISC 320 Midterm Exam

Algorithms Assignment 3 Solutions

Chapter 9 Graph Algorithms

8 Matroid Intersection

Lecturers: Sanjam Garg and Prasad Raghavendra March 20, Midterm 2 Solutions

CSci 231 Final Review

END-TERM EXAMINATION

LECTURES 3 and 4: Flows and Matchings

Assignment 1 Introduction to Graph Theory CO342

Maximum flows & Maximum Matchings

CS161 - Final Exam Computer Science Department, Stanford University August 16, 2008

1 Minimum Cut Problem

The Nonsense Prerequisites [2.5]

1 i n (p i + r n i ) (Note that by allowing i to be n, we handle the case where the rod is not cut at all.)

Graph Definitions. In a directed graph the edges have directions (ordered pairs). A weighted graph includes a weight function.

6.006 Final Exam Name 2. Problem 1. True or False [30 points] (10 parts) For each of the following questions, circle either True, False or Unknown.

CME 305: Discrete Mathematics and Algorithms Instructor: Reza Zadeh HW#3 Due at the beginning of class Thursday 02/26/15

CIS 121 Data Structures and Algorithms Minimum Spanning Trees

CMPSCI611: The SUBSET-SUM Problem Lecture 18

Important separators and parameterized algorithms

Problem Set 1. Solution. CS4234: Optimization Algorithms. Solution Sketches

Lecture 4: Graph Algorithms

Chapter 9 Graph Algorithms

Lecture 8: The Traveling Salesman Problem

Homework 4 Even numbered problem solutions cs161 Summer 2009

Lecture 10,11: General Matching Polytope, Maximum Flow. 1 Perfect Matching and Matching Polytope on General Graphs

DO NOT RE-DISTRIBUTE THIS SOLUTION FILE

ICS 161 Algorithms Winter 1998 Final Exam. 1: out of 15. 2: out of 15. 3: out of 20. 4: out of 15. 5: out of 20. 6: out of 15.

Faster parameterized algorithms for Minimum Fill-In

Algorithms: Lecture 7. Chalmers University of Technology

Midterm 2. Replace by cover page.

Scan and Quicksort. 1 Scan. 1.1 Contraction CSE341T 09/20/2017. Lecture 7

Exam problems for the course Combinatorial Optimization I (DM208)

NP Completeness. Andreas Klappenecker [partially based on slides by Jennifer Welch]

Definition For vertices u, v V (G), the distance from u to v, denoted d(u, v), in G is the length of a shortest u, v-path. 1

Graph Algorithms. Chromatic Polynomials. Graph Algorithms

Transcription:

CMPSCI 311: Introduction to Algorithms Practice Final Exam Name: ID: Instructions: Answer the questions directly on the exam pages. Show all your work for each question. Providing more detail including comments and explanations can help with assignment of partial credit. If the answer to a question is a number, unless the problem says otherwise, you may give your answer using arithmetic operations, such as addition, multiplication, choose notation and factorials (e.g., 9 35! + 2 or 0.5 0.3/(0.2 0.5 + 0.9 0.1) is fine). If you need extra space, use the back of a page. No books, notes, calculators or other electronic devices are allowed. Any cheating will result in a grade of 0. If you have questions during the exam, raise your hand. Question Value Points Earned 1 10 2 20 3 30 4 10 5 20 6 10 Total 100 1

Question 1. (10 points) True of False? Indicate whether each of the following statements is TRUE or FALSE. No justification required. 1.1 (2 points): 3-coloring can be solved by breadth first search and therefore is in P. False 1.2 (2 points): True n i=1 2i = Θ(2 n ). 1.3 (2 points): A dynamic program that implements the following recursive form can be used to solve the subset sum problem, which asks to find a subset S of numbers from x 1,..., x n (all non-negative) with maximum weight subject to not exceeding a given number W. Val(i, w) = max{val(i 1, w), x i + Val(i 1, w x i )} True 1.4 (2 points): For any flow network, and any two vertices s, t there is always a flow of at least 1 from source s to target t. False 1.5 (2 points): The recurrence T (n) = 2T (n 1) + O(1) solves to Θ(n 2 ). False 2

Question 2. (20 points) Short Answer. Answer each of the following questions in at most two sentences. 2.1 (4 points): In a weighted graph G where all edges have weight 1, how can we use Djikstra s algorithm to find a minimum spanning tree? All trees are minimum spanning trees when the edges have weight 1. So as you run Djikstra s just record the parent of each node, and this is an MST. 2.2 (4 points): Solve the recurrence T (n) = 3T (n/2) + O(n). T (n) = log 2 n 1 i=0 3 i n 2 i = O(n (3/2) log 2 n 1 ) = O(n log 2 3 ). 2.3 (4 points): Suppose a dynamic programming algorithm creates an n m table and to compute each entry of the table it takes a minimum over at most m (previously computed) other entries. What would the running time of this algorithm be, assuming there is no other computations. We compute n m numbers and each computation requires O(m) time, so the total is O(nm 2 ). 3

2.4 (4 points): Why is MaxFlow in N P co-n P? We can verify both positive and negative instances via the Max-Flow Min-Cut theorem. 2.5 (4 points): Suppose A is a randomized algorithm that finds the optimal solution to some minimization problem with probability at least p (0, 1). More precisely, if we run A o some input, it returns a candidate solution O along with the cost of O, and with probability at least p, we are guaranteed that O minimizes the cost function. For another parameter δ > 0, how can we use A to find an optimal solution with probability at least 1 δ and what is the running time of this new algorithm? If we run the algorithm m times, then the probability that all m runs fail to find the optimum is at most (1 p) m = [(1 p) 1/p ] pm = [(1 1 1/p )1/p ] pm e pm. We want this to be at most δ, which requires we set m = p 1 log(1/δ) so our running time is O(p 1 log(1/δ) runtime for A). 4

Question 3. (30 points) Consider the longest increasing subsequence problem defined as follows. Given a list of numbers a 1,..., a n an increasing subsequence is a list of indices i 1,..., i k {1,..., n} such that i 1 < i 2 <..., i k and a i1 a i2..., a ik. The longest increasing subsequence is the longest list of indices with this property. 3.1 (2 points): What is the longest increasing subsequence of the list 5, 3, 4, 8, 7, 10? There are two, 3, 4, 8, 10 or 3, 4, 7, 10. 3.2 (4 points): Consider the greedy algorithm that chooses the first element of the list, and then repeatedly chooses the next element that is larger. Is this a correct algorithm? Either prove its correctness or provide a counter example. The above input is a counter example as the greedy algorithm would choose 5, but 5 is not in either of the optimal solutions. 3.3 (4 points): Consider the greedy algorithm that chooses the smallest element of the list, and then repeatedly chooses the smallest element that comes after this chosen one. Is this a correct algorithm? Either prove its correctness or provide a counterexample. The input 2, 3, 4, 1 is a counter example, since this greedy algorithm would choose 1 first, but 1 is not in the optimal solution. 5

3.4 (5 points): Consider a divide and conquer strategy that splits the list into the first half and second half, recursively computes L = (l 1,..., l kl ), R = (r 1,..., r kr ) the longest increasing subsequences in each half, and then, if the last chosen element in the first half is less than the first chosen index in the second half (i.e. a lkl a r1 ) returns L R, otherwise it returns the longer of L and R. Is this a correct algorithm? either prove its correctness of provide a counterexample. Consider the input 2, 3, 4, 5, 0, 1. The optimal is 2, 3, 4, 5 but if we do divide and conquer we find 2, 3, 4 and 0, 1 so we output 2, 3, 4. 3.5 (15 points): Design a dynamic programming algorithm for longest increasing subsequence. Prove its correctness and analyze its running time. Let the input have size n, we build a size n table A by, j (i) = argmax{a[j] such that j < i and a j a i } A[i] = 1 + A[j (i)] j (i) is set to zero if no choice for j exists (for example on the first element of the list). The proof is based on the fact that A[j] holds the length of the longest increasing subsequence that ends on a j. This is proved by induction, where the base case is easy. For the inductive step, consider some index i, we set A[i] = 1 + A[j (i)], and we are guaranteed that j (i) maximizes A[j] among all possible positions j that could come before i in an increasing subsequence. Thus we are ensured that A[i] holds the proper value. The running time is O(n 2 ). 6

Question 4. (10 points) In this problem we investigate the feedback arc-set problem which generalizes the topological ordering. Given a directed graph (which may contain cycles), the goal in feedback arc-set is to find an ordering of the vertices that minimizes the number of back edges. More precisely, if G = (V, E) is a directed graph, and (a 1,..., a n ) with a i a j is an ordering of the vertices, we define the cost as cost(a 1,..., a n ) = i<j 1[(a j, a i ) E] Here 1[ ] is a function that is 1 if the argument is true and zero otherwise. This is the number of edges going from right to left (backward) if we ordered the vertices with a 1 on the left and a n on the right. v 2 v 3 v 1 v 4 v 6 v 5 4.1 (1 points): In the above graph, what is the cost of (v 1, v 2, v 3, v 4, v 5, v 6 )? 3 + 1 + 1 + 0 + 0 + 0 = 5 4.2 (1 points): In the above graph, what is the cost of (v 6, v 5, v 4, v 3, v 2, v 1 )? 2 + 2 + 1 + 1 + 1 + 0 = 7 4.3 (2 points): True or False. a directed acyclic graph always has an ordering O with cost(o) = 0. True, any topological ordering has no feedback arcs. 4.4 (6 points): Prove that the decision version of feedback arc set is NP-complete. That is given a directed graph and an integer k, decide whether the graph has an ordering with at most k back-edges. The proof is by reduction from vertex cover. Given an instance G = (V, E) of vertex cover, we will define a instance of feedback arc set. Let G = (V, E ) be a new directed graph where for each v V we have two vertices v (1), v (2) V connected with the directed edge (v (1) v (2) ). For every undirected edge (u, v) E, define two directed edges (u (2) v (1) ) and (v (2) u (1) ). If G has n vertices and m edges, this new graph has 2n vertices and n + 2m edges. We claim that G has a vertex cover of size at most k if and only if G has an ordering with at most k back edges. The idea for the proof is that each edge (u, v) E forms a 4-cycle in G. If S is a vertex cover, we can remove the edges (v (1) v (2) ) for each v S and break all of the 4-cycles, since we remove an edge from each cycle. More precisely, if S is a vertex cover, we construct an ordering as follows: the first S nodes are v (2) for each v S, followed by each u (1), u (2) pair for u / S, and finally followed by v (1) for each v S. The order in each group does not matter. Since S is a vertex cover, all edges from into u (1) for u / S originate from some v (2) for v S and similarly all edges from u (2) point to v (1) for some v S. Thus the only back edges in this ordering are from v (1) v (2) for v S, which is k back edges. 7

The other direction is by a counting argument. If no subset of k vertices covers all edges, then there is no way to break all m cycles in G. Observe that if you break and edge of the form (v (2) u (1) ) you can break at most one cycle, so it is always better to break edges of the form (v (1) v (2) ). However, since there is no vertex cover of size at most k, choosing k of these edges cannot break all the cycles. 8

Question 5. (20 points) In this problem we investigate vertex-capacitated flow networks. We are given a directed graph G = (V, E) with source s and sink t and a capacity c v for each v V. We want an s t flow f that satisfies the usual conservation of flow constraints, but instead of satisfying edge-capacity constraints, satisfies the vertex capacity constraints f(v) c v. Here f(v) = (u,v) E f u,v is the total flow entering the node v. The goal is to design an algorithm for computing a maximum s t flow in a vertex-capacitated network. 5.1 (5 points): Draw a directed graph G with clearly labeled source s and sink t, where if we consider the usual edge-capacitated version of the problem (with edge capacities c e = 1) we get a maximum flow with a different value than if we consider the vertex capacitated version of the problem (with vertex capacities c v = 1). s t In this graph the maximum flow in the edge-capacitated version has value 2, while the vertex capacitated version has value 1. 5.2 (15 points): Design a polynomial time algorithm for computing the maximum flow in a nodecapacitated network. Prove that the algorithm is correct and analyze its running time. In the flow network, replace each vertex v with two vertices v (1) and v (2) with an edge e v = (v (1) v (2) ) of capacity c(e v ) = c v. All incoming edges to v in the original network point to v (1) and have capacity (no constraint). All outgoing edges from v in the original network now start from v (2) and have capacity. The edge capacitated maximum flow in this network is precisely the vertex capacitated maximum flow in the original network. The only capacity constraints are on the e v edges, and these precisely encode the fact that we must satisfy the vertex capacities. The running time is the same as the max flow problem O(( V + E )F ) where F is the value of the maximum flow and E is the number of edges in the original network and V is the number of vertices. 9

Additional space. 10

Question 6. (10 points) Consider a variant of the subset sum problem where we are given a set of numbers x 1,..., x n and need to partition them numbers into sets S 1,..., S K such that for each k {1,..., K}, i S k x i W for some target W. The goal is to minimize K, the number of sets in the partition. We will study a simple approximation algorithm for this problem. The algorithm considers the items in order, and forms the first set S 1 by repeatedly adding the numbers x 1, x 2,... until the next number would exceed the target W. Then it proceed to construct the next set. 6.1 (2 points): Give an example input where this algorithm does not use the minimum number of sets. Let W = 5 and consider the sequence 4, 2, 3, 1. The optimal solution uses two sets {4, 1} and {2, 3} but our approximation algorithm tries to group 2 with 4 and fails, so it forms {4}, {2, 3}, {1}. 6.2 (2 points): Derive a lower bound on K the smallest possible number of sets in the partition in terms of the target W and the total weight X = n i=1 x i. K X/W since all the weight must be included and at most W can be included in each set. 6.3 (6 points): Use this lower bound to prove that this greedy algorithm always produces a number of sets K that is at most 2K. Consider sets S 1, S 2. We claim i S 1 S 2 x i W. This is clear since S 2 was only formed because adding the next element to S 1 would exceed the weight constraint, and this item was added to S 2. Similarly, for every adjacent pair of sets s, s+1, we have i S s S s+1 x i W by the same argument. Thus, letting K be the number of sets produced by the algorithm, K X/W = 1 W This proves that K 2K. K s=1 i S s x i 1 W K/2 s=1 i S 2s 1 S 2s x i 1 W (K/2 W ) = K/2 11