Homework 2 Solutions

Similar documents
Homework 3 Solutions

Homework 4 Solutions

Question Points Score TOTAL 50

CMPSCI 311: Introduction to Algorithms Practice Final Exam

CMSC351 - Fall 2014, Homework #6

Analysis of Algorithms, I

CS261: Problem Set #2

1 Shortest Paths. 1.1 Breadth First Search (BFS) CS 124 Section #3 Shortest Paths and MSTs 2/13/2018

Problem set 2. Problem 1. Problem 2. Problem 3. CS261, Winter Instructor: Ashish Goel.

Graph Algorithms (part 3 of CSC 282),

1 Shortest Paths. 1.1 Breadth First Search (BFS) CS 124 Section #3 Shortest Paths and MSTs 2/13/2018

Question Points Score TOTAL 55 TOTAL (Calibrated) 50

CS261: Problem Set #1

Algorithms for Data Science

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

Questions Total Points Score

Algorithms (IX) Yijia Chen Shanghai Jiaotong University

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

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

Final Exam May 8, 2018

7 th Asia-Pacific Informatics Olympiad

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

Solving problems on graph algorithms

CSE 21 Spring 2016 Homework 5. Instructions

Homework 2. Sample Solution. Due Date: Thursday, May 31, 11:59 pm

22 Elementary Graph Algorithms. There are two standard ways to represent a

(Refer Slide Time: 00:18)

Notes for Recitation 9

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

Graph Algorithms (part 3 of CSC 282),

CS490 Quiz 1. This is the written part of Quiz 1. The quiz is closed book; in particular, no notes, calculators and cell phones are allowed.

Quiz 2 CS 3510 October 22, 2004

Theorem 2.9: nearest addition algorithm

CSC 373 Lecture # 3 Instructor: Milad Eftekhar

Unit-5 Dynamic Programming 2016

CIS 121 Data Structures and Algorithms Minimum Spanning Trees

Dist(Vertex u, Vertex v, Graph preprocessed) return u.dist v.dist

Introduction to Algorithms April 21, 2004 Massachusetts Institute of Technology. Quiz 2 Solutions

Greedy algorithms is another useful way for solving optimization problems.

Efficient Algorithms and Intractable Problems Spring 2016 Alessandro Chiesa and Umesh Vazirani MT 1 Solution

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

Shortest Path Problem

LECTURES 3 and 4: Flows and Matchings

CSE 21 Spring 2016 Homework 5. Instructions

2018 Pummill Relay problem statement

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

Dijkstra s algorithm for shortest paths when no edges have negative weight.

UML CS Algorithms Qualifying Exam Fall, 2003 ALGORITHMS QUALIFYING EXAM

CS161 - Minimum Spanning Trees and Single Source Shortest Paths

Cs445 Homework #1. Due 9/9/ :59 pm DRAFT

Shortest Paths and Minimum Spanning Trees

CS264: Homework #1. Due by midnight on Thursday, January 19, 2017

Description of The Algorithm

Midterm 1 Solutions. (i) False. One possible counterexample is the following: n n 3

CHENNAI MATHEMATICAL INSTITUTE M.Sc. / Ph.D. Programme in Computer Science

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.

Solution for Homework set 3

22 Elementary Graph Algorithms. There are two standard ways to represent a

SUGGESTION : read all the questions and their values before you start.

CS 161 Fall 2015 Final Exam

Data Structure and Algorithm Homework #5 Due: 2:00pm, Thursday, May 31, 2012 TA === Homework submission instructions ===

Introduction to Algorithms November 17, 2011 Massachusetts Institute of Technology Fall 2011 Professors Erik Demaine and Srini Devadas Quiz 2

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

Algorithms for Minimum Spanning Trees

Minimum Spanning Tree

Homework 6: Higher-Order Procedures Due: 11:59 PM, Oct 16, 2018

CS521 \ Notes for the Final Exam

1. Let n and m be positive integers with n m. a. Write the inclusion/exclusion formula for the number S(n, m) of surjections from {1, 2,...

Dynamic Programming: 1D Optimization. Dynamic Programming: 2D Optimization. Fibonacci Sequence. Crazy 8 s. Edit Distance

CS 561, Lecture 10. Jared Saia University of New Mexico

Minimum Spanning Trees

CS 4349 Lecture October 18th, 2017

Solutions to Problem 1 of Homework 3 (10 (+6) Points)

Notes and Answers to Homework Exam 1, Geometric Algorithms, 2017

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

Minimum Spanning Trees

Exercises Optimal binary search trees root

CMPSCI611: The SUBSET-SUM Problem Lecture 18

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

FINAL EXAM SOLUTIONS

Algorithms (V) Path in Graphs. Guoqiang Li. School of Software, Shanghai Jiao Tong University

Title. Ferienakademie im Sarntal Course 2 Distance Problems: Theory and Praxis. Nesrine Damak. Fakultät für Informatik TU München. 20.

CS446: Machine Learning Fall Problem Set 4. Handed Out: October 17, 2013 Due: October 31 th, w T x i w

CS 170 Second Midterm ANSWERS 7 April NAME (1 pt): SID (1 pt): TA (1 pt): Name of Neighbor to your left (1 pt):

0.1 Welcome. 0.2 Insertion sort. Jessica Su (some portions copied from CLRS)

CS 1803 Pair Homework 4 Greedy Scheduler (Part I) Due: Wednesday, September 29th, before 6 PM Out of 100 points

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

CS483 Design and Analysis of Algorithms

CS 4349 Lecture October 23rd, 2017

Notes for Lecture 24

CSC 1700 Analysis of Algorithms: Minimum Spanning Tree

CS420/520 Algorithm Analysis Spring 2009 Lecture 14

Problem Set 2 Solutions

UML CS Algorithms Qualifying Exam Fall, 2004 ALGORITHMS QUALIFYING EXAM

G205 Fundamentals of Computer Engineering. CLASS 21, Mon. Nov Stefano Basagni Fall 2004 M-W, 1:30pm-3:10pm

CSE331 Introduction to Algorithms Lecture 15 Minimum Spanning Trees

Quiz 2 Solutions. (a) T F Topological sort requires Ω(V lg V ) if the edge weights are unbounded. Explain:

Design and Analysis of Algorithms

Parallel Graph Algorithms

1 Non greedy algorithms (which we should have covered

Transcription:

CS3510 Design & Analysis of Algorithms Section A Homework 2 Solutions Released: 3pm, Friday, Oct 13, 2017 This homework has a total of 4 problems on 3 pages. Solutions should be submitted to GradeScope before 3:00pm on Wednesday Oct 11. The problem set is marked out of 20, you can earn up to 21 = 1 + 5 + 4 + 5 + 6 points. If you choose not to submit a typed write-up, please write neat and legibly. Collaboration is allowed/encouraged on problems, however each student must independently complete their own write-up, and list all collaborators. No credit will be given to solutions obtained verbatim from the Internet or other sources. 0. [1 point, only if all parts are completed] (a) Submit your homework to Gradescope. (b) Student id is the same as on T-square: it s the one with alphabet + digits, NOT the 9 digit number from student card. (c) Pages for each question are separated correctly. (d) Words on the scan are clearly readable. Updates to this homework since version 0 are: Question 1, changed in v1 to explicitly specify that you only need to find the length of the subsequence, not the subsequence itself. 1. (5 points) Longest alternating subsequence. Recall the longest increasing subsequence problem. Now instead we want to find the maximum length subsequence that alternates increasing/decreasing, starting with increasing. That is, we want to find a sequence of indices i 1 < i 2 <... < i k such that For each 1 j < k with j odd, A[i j ] < A[i j+1 ], and For each 1 j < k with j even, A[i j ] > A[i j+1 ]. (a) (3 points) Given an O(n 2 ) time algorithm for finding the length of the longest alternating subsequence using dynamic programming. States: two states per index, 1

LongestUp[i] longest subsequence ending at i where the last entry is bigger than the preceding. LongestDown[i] longest subsequence ending at i where the last entry is smaller than the preceding. Base case: LongestU p[i], LongestDown[i] 1. Transition: LongestU p[i] = LongestDown[i] = max LongestDown[j] + 1 j<i,a[j]<a[i] max LongestUp[j] + 1 j<i,a[j]>a[i] These values can also be viewed as breaking down the states by the parity of subsequence lengths. LongestDown[i] is the length of the longest odd lengthed alternating subsequence ending at i, and LongestU p[i] is the length of the longest even lengthed alternating subsequence ending at i. It s also possible to do this by having 1 state per index. We can directly transit on LongestDown[i] by doing LongestDown[i] = max LongestDown[j] + 2, j<k<i,a[j]<a[k],a[k]>a[i] but this takes O(n 3 ) time, and one also need to take the even total length one into account by also outputting the solution max LongestDown[j] + 1. i,j<i,a[j]<a[i] As it turns out, greedy even works, and runs in O(n) time! Consider maintaining an alternating subsequence. If the length is even, and we have an element that s less than the last one, we append it to the sequence. Otherwise, we have an element that s bigger or equal. In that case we simply replace the last element with it. This works because the even length means we have A [ i j ] > A [ i j 1 ], and increasing the value of A[i j ] would still meet this condition. Another way to view the greedy solution is that it essentially goes through the array A and removes all non-alternating triples i such that A[i 1] A[i] A[i + 1] 2

or A[i 1] A[i] A[i + 1]. Due to the above solution, it s also possible to prove the following seemingly incorrect dynamic program that just stores the values of LAS[i] is correct: State: LAS[i] is the max length of an alternating subsequence ending at i. Base case: LAS[i] 1. Transition: LAS[i] = max LAS[j] + 1. j<i,(las[j] is odd AND A[j]<A[i]) OR (LAS[j] is even AND A[j]>A[i]) GRADING: 1 point for justification of algorithm. 0.5 point for an O(n 3 ) time algorithm. 2 points for a correct algorithm (any of the ones listed above), even if the justification are iffy. (b) (2 points) Show that the problem of finding the longest alternating subsequence can be formulated and solved as a shortest path on 2n vertices. We can create two vertices per index, corresponding to the two states from the first solution to part a). Then for any j < i with A[j] < A[i], we add an edge from LongestDown[j] to LongestUp[i] with weight 1, and for any j < i with A[j] > A[i], we add an edge from LongestUp[j] to LongestDown[i] with weight 1. We also add a super source with edges of weight 0 to all vertices, and a super sink where all vertices have edges of weight 0 going to. Then the shortest path from the super source to super sink gives the negation of the length of the longest alternating sequence. GRADING: The requirement of exactly 2n vertices was not necessary: allow O(1) discrepancies in vertex counts. 2. (4 points) Minimum Spanning Tree Consider the graph below. 3

A C 10 4 3 5 B D 2 4 4 E G 5 7 6 F (a) (2 points) Give a cut that shows edge e = {B, D} is in an MST. Be sure to specify the cut via a set of vertices. As {B, D} is the minimum edge in the graph, any valid cut for this edge is a correct answer. (b) (2 points) Show using the cut rule that if we increase the weights of some edges in G to form H, then an edge e in the minimum spanning tree in G is in some minimum spanning tree of H if its weights were not adjusted. The proof is by cut property. If edge e was placed in the original MST, there must exist some cut C for which e had the minimum weight. When some of the non-tree edges are modified, e must still be the lowest weight edge across the cut. 3. (5 points) Bellman-Ford algorithm for finding shortest paths. In class, we discussed the following variant of the Bellman-Ford algorithm, whose pseudocode is below. While true { For all edges e = u --> v { dist[v] = min(dist[v], dist[u] + l(u --> v)); } if no vertex s dist value is changed, stop } The text book s version (in Section 4.6.) instead fixes the iteration count at Θ(n). We also showed in class that if we do indeed get to such an iteration count, there must be a negative cycle. In this question, we will discuss how many times the outer loop of Bellman-Ford algorithm actually needs to run. For such a discussion, it is important to fix an ordering by which we consider the edges, that is we view the edges as a fixed list, e 1... e m. (a) (1 point) For any n, exhibit a graph and an ordering of the edges where the Bellman-Ford algorithm takes more than n/2 steps. 4

Consider the following directed graph with 4 vertices (n = 4), starting node as 1 and distances to all the other nodes as infinity: 1 2 3 4 With the following edge order, it will take 3 (greater than 2 = n/2) iterations before the algorithm converges: (3) (4) (2) (3) (1) (2) GRADING: There is some ambiguity about whether this needs to work for every n (above some threshold), or just some particular n. The more restrictive version of for some n is considered acceptable. Providing a graph with a negative cycle is also acceptable, since this version didn t specify positive weighted edges. (b) (2 points) Show that for any graph G without negative cycles and any s-t pair, there exists an ordering of edges so that dist[t] (as defined in the pseudocode above) becomes the value of the true s to t distance in 1 step. For any s-t pair, consider the shortest path from s to t and process the edges in that order. That way, in the first iteration itself, dist[t] will be the true s-t distance. (c) (2 points) Consider the k k square grid with two-way edges of positive lengths. We can label each vertex by its row/column number, with the top left corner being (1, 1), which is also our source vertex. Then each edge is a 4 tuple, (r 1, c 1 ) (r 2, c 2 ), and we can produce edge ordering by sorting lexicographically via these tuples. For example, on a 2 2 grid, this would order the edges 5

as: (1, 1) (1, 2) (1, 1) (2, 1) (1, 2) (1, 1) (1, 2) (2, 2) (2, 1) (1, 1) (2, 1) (2, 2) (2, 2) (1, 2) (2, 2) (2, 1) Show that for any k, there exists a set of positive weights for these edges of the k k square grid so that the Bellman-Ford algorithm with (1, 1) as the starting point takes at least k/10 iterations (the 10 was chosen as an attempt to remove the need of getting constructions with good constants). Consider a graph where all the vertical edges have length 0, but all horizontal edges have length (which if we want actual numbers, can be set to 10k 2 ) except for the bottom ones in odd indexed columns, and top ones in even indexed columns having weight 1. Specifically, the edges with weight 0 are (k, 1) (k, 2) (1, 2) (1, 3) (k, 3) (k, 4) (1, 4) (1, 5)... have weight 0. Then the shortest path consists of moving downwards, rightwards, upwards, rightwards, downwards, and etc. Each sequence of upwards propagation takes k iterations of the Bellman-Ford algorithm because the edges are updated in reverse ordering. As there are at least k/2 of these upward segments, the total number of iterations required is at least k 2 /2. GRADING: edge weights of 0 and are acceptable. 6

4. (6 points) Coloring a graph The minimum coloring problem asks to label the vertices with the fewest number of distinct colors so that no edge has two endpoints having the same color. (a) (2 points) Consider the following greedy algorithm: find a maximum independent set, color it with color 1, remove it from the graph, and repeat (using colors 2). Show that this algorithm is suboptimal by providing a graph of size at most 5 where it does not produce the optimal coloring. A B E F D One possible max independent set is A and F. Use one color for this. We now have B, D, and E left. This is a complete graph, so we d need 3 colors, totaling in 4 colors. However, this graph can be colored in 3 colors, if A and E are colored, then B and F are colored, then finally D. (b) (3 points) Give an O(4 n ) time algorithm for finding the fewest number of colors needed to color a graph. One way that this can be done is to define a state to be the minimum number of colors needed to color a subset of the vertices, and the transition can then be an entire subset of vertices that can be colored using the same color. We define a subproblem C(S) as the minimum number of colors needed, where S is a subset of V. We construct a table C and enumerate all 2 n independent subsets of V in increasing order of size. The base case is C( ) = 0. At each subset S i where 1 i 2 n, C (S) = min {C (S \ T )} + 1, T S where T is a non-empty independent subset of S. This iterates over all 2 n subsets of V, and on each iteration, we select minimum over all 2 S possible independent subsets of S. So the upper bound is O(4 n ). (c) (1 points): Improve the running time to O(3 n ). 7

The transitoin cost from Part (b) can be expressed as follows: n k=0 ( ) n 2 k = k n k=0 ( ) n 2 k 1 n k = (2 + 1) n (1) k 8