This preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full Document
Unformatted text preview: Introduction to Algorithms November 17, 2010 Massachusetts Institute of Technology 6.006 Fall 2010 Professors Konstantinos Daskalakis and Patrick Jaillet Quiz 2 Solutions Quiz 2 Solutions Problem 1. What is Your Name? [2 points] (2 parts) (a) [1 point] Flip back to the cover page. Write your name there. (b) [1 point] Flip back to the cover page. Circle your recitation section. 6.006 Quiz 2 Solutions Name 2 Problem 2. Short Answer [38 points] (4 parts) (a) [9 points] Give an example of a graph such that running Dijkstra on it would give incorrect distances. Solution: Below is one example of such a graph. There needs to be a vertex u such that when it is extracted, the distance to it is not the weight of the shortest path. But this alone is not enough: there needs to be a vertex v adjacent to u whose shortest path is through u . Since the edges from u get relaxed only once, then even though the distance to u could later be updated to the correct shortest distance, the distance to v will not be. Dijkstra will also yield incorrect distances for a graph with a negative weight cycle. Figure 1 : A gets extracted first, after which edges ( A,B ) and ( A,C ) are relaxed, and the distances are d [ A ] = 0 ,d [ B ] = 5 ,d [ C ] = 10 . B is extracted next, leading to edge ( B,D ) being relaxed, and d [ D ] becomes 6 . D is extracted next, but it has no edges to relax. Finally, C is extracted, relaxing edge ( C,B ) and making d [ B ] = 3 . The shortest path to D has weight 4 , but d [ D ] = 6 . (b) [9 points] Give an efficient algorithm to sort n dates (represented as monthdayyear and all from the 20 th century), and analyze the running time. Solution: Use radix sort. First sort by day using counting sort with an array of size 31, then sort by month using counting sort with an array of size 12, and finally sort by year using counting sort with an array of size 100, where the counter in slot i corresponds to year 1900 + i . The running time of radix sort is Θ( d ( n + k )) . In this case, d = 3 and k is maximum 100 , so the running time is Θ( n ) . 6.006 Quiz 2 Solutions Name 3 (c) [10 points] Give an O ( V + E )time algorithm to remove all the cycles in a directed graph G = ( V,E ) . Removing a cycle means removing an edge of the cycle. If there are k cycles in G , the algorithm should only remove O ( k ) edges. Solution: Do a DFS of the graph, and at the end, remove all the back edges. As you traverse the graph, you can check whether the edge you are trying to relax goes to a node that has been seen but is not yet finished, and if so, then it is a back edge and you can store it in a set. After the DFS, remove all the edges that are in this set. (d) [10 points] Let G = ( V,E ) be a weighted, directed graph with exactly one negative weight edge and no negativeweight cycles. Give an algorithm to find the shortest distance from s to all other vertices in V that has the same running time as Dijkstra....
View
Full
Document
This note was uploaded on 01/20/2012 for the course CS 6.006 taught by Professor Erikdemaine during the Fall '08 term at MIT.
 Fall '08
 ErikDemaine
 Algorithms

Click to edit the document details