CMPSCI 311: Introduction to Algorithms Second Midterm Exam

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

CSE 421 Introduction to Algorithms Final Exam Winter 2005

CSE101: Design and Analysis of Algorithms. Ragesh Jaiswal, CSE, UCSD

Midterm Exam 2 Solutions

Lecture 2: Divide and conquer and Dynamic programming

CMPSCI 240: Reasoning Under Uncertainty First Midterm Exam

CMPSCI 240: Reasoning Under Uncertainty First Midterm Exam

CSCE 750 Final Exam Answer Key Wednesday December 7, 2005

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

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

COMPSCI 611 Advanced Algorithms Second Midterm Exam Fall 2017

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, 2013

COMPSCI 611 Advanced Algorithms Second Midterm Exam Fall 2017

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

/633 Introduction to Algorithms Lecturer: Michael Dinitz Topic: Asymptotic Analysis, recurrences Date: 9/7/17

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

directed weighted graphs as flow networks the Ford-Fulkerson algorithm termination and running time

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):

Fall 2016 Test 1 with Solutions

CSI 4105 MIDTERM SOLUTION

University of Washington March 21, 2013 Department of Computer Science and Engineering CSEP 521, Winter Exam Solution, Monday, March 18, 2013

CMPSCI 611 Advanced Algorithms Midterm Exam Fall 2015

Question Paper Code :

Lecture 4: September 19

Introduction to Divide and Conquer

Analysis of Algorithms I: Asymptotic Notation, Induction, and MergeSort

/633 Introduction to Algorithms Lecturer: Michael Dinitz Topic: Dynamic Programming II Date: 10/12/17

Quiz 1 Date: Monday, October 17, 2016

Midterm Exam. CS 3110: Design and Analysis of Algorithms. June 20, Group 1 Group 2 Group 3

Algorithms Exam TIN093 /DIT602

Algorithms, Design and Analysis. Order of growth. Table 2.1. Big-oh. Asymptotic growth rate. Types of formulas for basic operation count

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:

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

CS173 Running Time and Big-O. Tandy Warnow

b + O(n d ) where a 1, b > 1, then O(n d log n) if a = b d d ) if a < b d O(n log b a ) if a > b d

Limitations of Algorithm Power

COMP 355 Advanced Algorithms

data structures and algorithms lecture 2

CPS 616 DIVIDE-AND-CONQUER 6-1

Analysis of Algorithm Efficiency. Dr. Yingwu Zhu

Essential facts about NP-completeness:

Midterm 2. 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:

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

Review Questions, Final Exam

Midterm 1 for CS 170

CS Analysis of Recursive Algorithms and Brute Force

CSE 202 Homework 4 Matthias Springer, A

CS1800 Discrete Structures Fall 2016 Profs. Gold & Schnyder April 25, CS1800 Discrete Structures Final

Divide and Conquer. CSE21 Winter 2017, Day 9 (B00), Day 6 (A00) January 30,

COMP 355 Advanced Algorithms Algorithm Design Review: Mathematical Background

More Dynamic Programming

COE428 Notes Week 4 (Week of Jan 30, 2017)

CS 170 Algorithms Fall 2014 David Wagner MT2

1 Closest Pair of Points on the Plane

PRAM lower bounds. 1 Overview. 2 Definitions. 3 Monotone Circuit Value Problem

Asymptotic Analysis and Recurrences

More Dynamic Programming

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

CS 580: Algorithm Design and Analysis

MA008/MIIZ01 Design and Analysis of Algorithms Lecture Notes 3

CS 170, Fall 1997 Second Midterm Professor Papadimitriou

CS325: Analysis of Algorithms, Fall Final Exam

CMPSCI611: The Matroid Theorem Lecture 5

Divide and Conquer. Recurrence Relations

Math 391: Midterm 1.0 Spring 2016

MATH 243E Test #3 Solutions

Algorithm Design CS 515 Fall 2015 Sample Final Exam Solutions

The maximum-subarray problem. Given an array of integers, find a contiguous subarray with the maximum sum. Very naïve algorithm:

Theory of Computation

Lecture 17: Trees and Merge Sort 10:00 AM, Oct 15, 2018

CSE 355 Test 2, Fall 2016

CS 170 Algorithms Spring 2009 David Wagner Final

Midterm Exam 2 CS 341: Foundations of Computer Science II Fall 2018, face-to-face day section Prof. Marvin K. Nakayama

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

CS383, Algorithms Spring 2009 HW1 Solutions

Hon.Algorithms (CSCI-GA 3520), Professor Yap Fall 2012 MIDTERM EXAM. Oct 11, 2012 SOLUTION

Midterm 2 for CS 170

CS103 Handout 47 Spring 2018 Practice CS103 Final Exam V

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

Algorithm Design and Analysis

Analysis of Algorithms - Using Asymptotic Bounds -

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)).

ECS 20: Discrete Mathematics for Computer Science UC Davis Phillip Rogaway June 12, Final Exam

1 Functions, relations, and infinite cardinality

Qualifier: CS 6375 Machine Learning Spring 2015

CS2223 Algorithms D Term 2009 Exam 3 Solutions

10. The GNFA method is used to show that

CSCI3390-Assignment 2 Solutions

UCSD CSE 21, Spring 2014 [Section B00] Mathematics for Algorithm and System Analysis

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

Solving recurrences. Frequently showing up when analysing divide&conquer algorithms or, more generally, recursive algorithms.

Mergesort and Recurrences (CLRS 2.3, 4.4)

CS60007 Algorithm Design and Analysis 2018 Assignment 1

Week 5: Quicksort, Lower bound, Greedy

Algorithms Design & Analysis. Analysis of Algorithm

V. Adamchik 1. Recurrences. Victor Adamchik Fall of 2005

Name: Student ID: Instructions:

Econ 172A, Fall 2012: Final Examination (I) 1. The examination has seven questions. Answer them all.

CS154 Final Examination

Transcription:

CMPSCI 311: Introduction to Algorithms Second Midterm Exam April 11, 2018. 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 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. Hint: To make sure you get as many points as possible, remember that it is often possible to answer later parts of a question without being able to answer earlier parts of a question. Question Value Points Earned 1 10 2 10 3 10 4 10 5 10 Total 50 1

Question 1. (10 points) Indicate whether each of the following statements is TRUE or FALSE. No justification required. 1.1 (2 points): The recurrence T (n) 3T (n/2) + cn with T (2) c solves to T (n) = Θ(n log 2 3 ). Solution: True, just apply the formula we saw in class. 1.2 (2 points): Let f be some flow in a flow network and let S be some cut. Then it may be the case that v(f) > capacity(s), i.e. the value of the flow f is larger than the capacity of the cut S. Solution: False, by Max Flow-Min Cut Theorem 1.3 (2 points): Given a collection of k-bit integers w 1,..., w n and a k-bit integer budget W, the dynamic programming algorithm can find the maximum subset sum in running time that is polynomial in n and k. Solution: False, the running time is O(n2 k ). 1.4 (2 points): Let G be a weighted directed graph possibly with negative edge weights, but with no negative cycles. Then there exists a polynomial time algorithm to find the shortest s t path in G. Solution: True, we can use Ford-Fulkerson algorithm. 1.5 (2 points): If there exists an s t path in the residual graph G f for some flow f, then f is not the maximum flow. Solution: True, we can increase the flow by pushing along the found s-t path. 2

Question 2. (10 points) In this problem we consider the sequence alignment problem. Consider two strings A L G O T E S T 2.1 (3 points): Suppose the gap penalty δ = 1 and the alignment costs are C(x, y) = 1{x y}. 1. (2 points) What is the optimal alignment? Solution: (A-T), (L-E), (G-S), (O-T) 2. (1 point) What is the cost of the optimal alignment? Solution: 4 2.2 (3 points): Suppose the gap penalty δ = 0.5 and the alignment costs C(x, y) is 0 if x = y, 1 if x and y are both vowels, 1 if x and y are both consonants, and 2 otherwise. 1. (2 points) What is the optimal alignment? Solution: There are many, e.g., (A-E), (L-S), (G-T) 2. (1 point) What is the cost of the optimal alignment? Solution: 4. Now suppose we are given two strings x, y and would like to find a substring y of y that minimizes the alignment cost with the entire string x. Formally, we would like to find a substring y that minimizes SeqAlign(x, y, C, δ) among all substrings y of y, where SeqAlign is the standard sequence alignment algorithm, C is a cost matrix, and δ is a gap penalty. Recall that a substring y is a contiguous sequence of characters y i, y i+1,..., y j of y where 1 i j n. Suppose that x is length n and y is length m, also assume δ 0 and C[a, b] 0 for all characters, a, b. 2.3 (2 points): Let F [i, j] denote the cost of the optimal alignment between all of x 1,..., x i and the best suffix of y 1,..., y j. How should we set F [0, j] to handle the base case? Solution: We should set F [0, j] = 0 for all j, since when x is the empty string, the alignment cost with the empty suffix of y is zero. In other words, we don t have to pay for putting gaps at the beginning of x. 2.4 (2 points): After changing the initialization, we run the standard forward program for sequence alignment to compute F [i, j] for all i, j. From this matrix, what is the cost of the optimal substring alignment? (Note you do not have to recover the alignment) Solution: The cost is max 0 j m F [n, j], which is the optimal cost for aligning all of x, with the best suffix of y 1,..., y j, for the best prefix of y. This is precisely the cost of aligning all of x with the best substring of y. 3

Question 3. (10 points) In this problem we will study an unfamiliar recurrence, and we will derive an asymptotic upper bound from first principles. Consider an algorithm whose running time on an input of size n satisfies T (n) 4T (n/2) + cn 2, T (2) c. For the following questions, it may be helpful to draw the recursion tree. 3.1 (2 points): Derive an upper bound on T (n) in terms of T (n/4) (and n, c, and any numerical constants, but not in terms of T (m) for m n/4). In other words, unroll the recursion twice. Solution: T (n) 4T (n/2) + cn 2 16T (n/4) + 4c(n/2) 2 + cn 2 = 16T (n/4) + 2cn 2. 3.2 (2 points): For any i 0, how many subproblems of size n/2 i must we solve (express your answer as a function of n and i)? Solution: 4 i. 3.3 (2 points): For any i 0, how much additional work (not including the recursive calls), must we do for all problems of size n/2 i (express your answer as a function of n and i)? Solution: O(n 2 ). 3.4 (2 points): We may write the total running time as??? T (n) total work for problems of size n/2 i i=0 What should we put in for the upper limit of the summation (where??? is)? In other words, what is the largest value of i we must consider? Solution: Acceptable answers are log 2 (n), log 2 (n), log 2 (n), log 2 (n) + 1, log 2 (n) 1. 3.5 (2 points): What is the Big-Oh running time of the algorithm, as a function of n? Solution: O(n 2 log 2 (n)). 4

Question 4. (10 points) Let G = (V, E) be a directed acyclic, unweighted, graph with a source node s and a target node t. There are no incoming edges to s and no outgoing edges from t. Here, we will design an algorithm to count the number of distinct s t paths in the graph. Two paths P, P are distinct if there is some edge e that is in P but not in P, or vice versa (i.e. they are not the exact same sequence of edges). b s a d t 4.1 (2 points): How many distinct s t paths are in the graph above? Solution: 5 c 4.2 (2 points): Consider a greedy algorithm that finds any path P, modifies the graph by removing all edges in P, and repeats, returning the number paths found. Draw a graph with at most 5 nodes (including s, t) where this algorithm returns an incorrect answer. Solution: Remove c in the above graph. 4.3 (2 points): Let ForwardPaths(v) denote the number of distinct paths from s to some vertex v V, with ForwardPaths(s) = 1. Write down a recursive formula for ForwardPaths(v). Solution: ForwardPaths(v) = ForwardPaths(u) (u,v) E 4.4 (2 points): We can also write down a backwards recursion. Let BackwardPaths(u) denote the number of paths from some vertex u V to t, with BackwardPaths(t) = 1. Write down a recursive formula for BackwardPaths(u). Solution: BackwardPaths(u) = BackwardPaths(v) (u,v) E 4.5 (2 points): Fill in the following pseudocode to complete the algorithm. Algorithm 1 CountPaths(G) order = Solution: Topological order of G for v in order do ForwardPaths(v) = Solution: (u,v) E ForwardPaths(u) end for return ForwardPaths(t) {Fill me in} {Fill me in} 5

Question 5. (10 points) Suppose we are given an array of objects A[1... n]. Any object that occurs in strictly more than n/2 positions is called a majority element, and in this problem, we will design an algorithm for finding a majority element, or deciding that no such element exists. Assume that you can test if two objects are equal, but they may not be sorted (i.e. there is no notion of less than for these objects). For example, the list [o 1, o 2, o 1, o 1, o 3 ] has o 1 as the majority, but [o 1, o 2, o 1, o 1, o 3, o 4, o 3 ] has no majority. 5.1 (3 points): In a divide and conquer approach, suppose we recurse on the left and right halves of the array, but both recursive calls report that no majority element exists. What are the remaining possibilities for the majority element of the original array? Briefly explain why. Solution: There are no remaining possibilities for the majority element. This is true because every element appears less than n/4 times in the left and also less than n/ times on the right, so it appears less than n/2 times overall. 5.2 (3 points): Now, suppose that the left recursive call reports that o L is a majority element of the left half, while the right reports that o R is the majority element of the right half. What are the remaining possibilities for the majority element of the original array? Briefly explain why. Solution: The only two possibilities are o L and o R. For any other element, it appears less than n/4 times on the left (since o L appears more than n/4 times on the left), and similarly on the right. So no other element can be a possibility. o L and o R remain possibilities since o L might have appears n/2 times on the left, and 1 time on the right. 5.3 (2 points): Describe how to test if object o is a majority element in a list of length n in O(n) time. Solution: Simply traverse the array and check equality between o and every element, and return true if we find more than n/2 elements that are equal. 5.4 (2 points): The above arguments inspire a divide and conquer algorithm. What is the running time of that algorithm? Solution: O(n log 2 n). The algorithm has the same structure as MergeSort. 6

7