q2-s2009-sol

# q2-s2009-sol - Introduction to Algorithms Massachusetts...

This preview shows pages 1–3. Sign up to view the full content.

Introduction to Algorithms April 15, 2009 Massachusetts Institute of Technology 6.006 Spring 2009 Professors Sivan Toledo and Alan Edelman Quiz 2 Solutions Quiz 2 Solutions Problem 1. True or False [20 points] (4 parts) For each of the following questions, circle either T (True) or F (False). Explain your choice. (No credit if no explanation given.) (a) T F Topological sort requires Ω( V lg V ) if the edge weights are unbounded. Explain: Solution: False. This statement is almost complete nonsense; topological sort has nothing to do with weights. It takes Θ( V + E ) time on any (unweighted) DAG. (b) T F A set of n integers whose values are in the range [0 ,n 8 ) can be sorted in O ( n ) time. Explain: Solution: True. Use radix sort with a radix of size n . Then each invocation of counting sort takes O ( n + n ) = O ( n ) time. Each element has 8 “digits”, so the total time for radix sort is O (8 n ) = O ( n ) . (c) T F If a depth-ﬁrst analysis of a graph contains at least one back edge, any other depth-ﬁrst analysis of the same graph will also contain at least one back edge. Explain: Solution: True. This follows from the fact that an analysis contains a back edge if and only if the graph contains a cycle. (d) T F Any DFS forest of an undirected graph contains the same number of trees. Explain: Solution: True. In an undirected graph, each connected component of the graph will be a single tree in a DFS.

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

View Full Document
6.006 Quiz 2 Solutions Name 2 Problem 2. Short Answer [32 points] (4 parts) (a) In the graph of 2x2x2 Rubik’s cube positions (as in Problem Set 4), there are exactly 6 edges incident on each vertex. We want to use a bi-directional BFS to ﬁnd a shortest path between two vertices s and t . If the shortest path from s to t contains d edges, approximately how many vertices must be explored to ﬁnd a shortest path, in the worst case? How does this compare to the number of vertices required for a single BFS starting from s ? Solution: Each vertex has 5 children (the sixth edge leads to its predecessor). Ex- ploring n levels deep then requires visiting about 5 n vertices. We must explore d/ 2 levels deep from each side, so we visit approximately 2 · 5 d/ 2 +1 vertices. This is approximately the square root of the number required for a single BFS. (b) Each edge in a connected, unweighted graph G is colored either red or blue. Present an algorithm to compute a path between s and t that traverses the fewest number of red edges. Analyze its running time. Solution: Assign each blue edge a weight of 0 , and each red edge a weight of 1 . Then run Dijkstra’s algorithm, for a total time of either O (( V + E )lg V ) or O ( E + V lg V ) . This solution received full credit. It is possible to achieve
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### Page1 / 7

q2-s2009-sol - Introduction to Algorithms Massachusetts...

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

View Full Document
Ask a homework question - tutors are online