q2-f2010-sol

q2-f2010-sol - Introduction to Algorithms November 17, 2010...

Info iconThis preview shows pages 1–4. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the 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 month-day-year 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 negative-weight 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

Page1 / 10

q2-f2010-sol - Introduction to Algorithms November 17, 2010...

This preview shows document pages 1 - 4. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online