This preview shows page 1. Sign up to view the full content.
Unformatted text preview: MIT OpenCourseWare http://ocw.mit.edu 6.006 Introduction to Algorithms
Spring 2008 For information about citing these materials or our Terms of Use, visit: http://ocw.mit.edu/terms. Introduction to Algorithms: 6.006 Massachusetts Institute of Technology Professors Srini Devadas and Erik Demaine April 2, 2008 Handout 9 Quiz 2 Practice Problems 1 True/False Decide whether these statements are True or False. You must brieﬂy justify all your answers to receive full credit. 1. There exists a comparison sort of 5 numbers that uses at most 6 comparisons in the worst case. True False Explain: Solution: False. The number of leaves of a decision tree which sorts 5 numbers is 5! and the height of the tree is at least lg(5!). Since 5! = 120, 26 = 64, and 27 = 128, we have 6 < lg(5!) < 7. Thus at least 7 comparisons are required. 2. Heapsort can be used as the auxiliary sorting routine in radix sort, because it operates in place. True False Explain: Solution: False. The auxiliary sorting routine in radix sort needs to be stable, meaning that numbers with the same value appear in the output array in the same order as they do appear in the input array. Heapsort is not stable. It does operate in place, meaning that only a constant number of elements of the input array are ever stored outside the array. 2 Handout 9: Quiz 2 Practice Problems 3. If the DFS ﬁnishing time f [u] > f [v ] for two vertices u and v in a directed graph G, and u and v are in the same DFS tree in the DFS forest, then u is an ancestor of v in the depth ﬁrst tree. True False Explain: Solution: False. In a graph with three nodes, r u and v , with edges (r, u) and (r, v ), and r is the starting point for the DFS, u and v are siblings in the DFS tree, neither as the ancestor of the other. 4. Let P be a shortest path from some vertex s to some other vertex t in a graph. If the weight of each edge in the graph is increased by one, P will still be a shortest path from s to t. True False Explain: Solution: False. In a graph where w(s, v1 ) = 1, w(v1 , v2 ) = 1, w(v2 , t) = 1, and w(s, t) = 4, the shortest path would change if 1 was added to every edge weight. 5. If an inplace sorting algorithm is given a sorted array, it will always output an unchanged array. True False Explain: Solution: False. Inplace just means that it only uses a constant amount of extra memory. Stable would mean it would leave the array unchanged. Handout 9: Quiz 2 Practice Problems 6. [5 points] Dijkstra’s algorithm works on any graph without negative weight cycles. True False Explain: 3 Solution: False. A single negative edge makes false the assumption that when you expand a node, you have already found the shortest path to that node. 7. [5 points] The Relax function never increases any shortest path estimate d[v ]. True False Explain: Solution: True. It only changes d[v ] if it can decrease it. 4 Handout 9: Quiz 2 Practice Problems 2 Short Answer
1. What property of the Rubik’s cube graph made 2way BFS more efﬁcient than ordinary BFS? Solution: The number of nodes at most 7 away from any source was signiﬁcantly less than the number of nodes at most 14 away from that source. 2. What is the running time of the most efﬁcient deterministic algorithm you know for ﬁnding the shortest path between two vertices in a directed graph, where the weights of all edges are equal? (Include the name of the algorithm.) Solution: Run BFS, treating it as an unweighted graph. This takes O(V + E ) time. 3 Topological Sort Another way of performing topological sorting on a directed acyclic graph G = (V, E ) is to repeatedly ﬁnd a vertex of indegree 0 (no incoming edges), output it, and remove it and all of its outgoing edges from the graph. Explain how to implement this idea so that it runs in time O(V + E ). What happens to this algorithm if G has cycles? Solution: First, for each vertex, we need to count how many incoming edges it has. We store these counts in a dictionary keyed by the vertices. This takes Θ(V + E ) time. For each vertex with 0 incoming edges, store it in a special dictionary. Call that dictionary zero. We repeatedly do the following: Take a vertex out of zero. For every edge coming out of that vertex, decrement the count of that edge’s target vertex. If you happen to decrement a count to 0, add that vertex to zero. This touches every vertex once and every edge once, so it also takes Θ(V + E ) time. If there is a cycle, at some time when we try to take a vertex out of zero, we won’t ﬁnd any. Handout 9: Quiz 2 Practice Problems 5 4 Shortest Paths Carrie Careful has hired Lazy Lazarus to help her compute singlesource shortest paths on a large graph. Lazy writes a subroutine that, given G = (V, E ), a source vertex s, and a nonnegative edgeweight function w : E → R, outputs a mapping d : V → R such that d[v ] is supposed to be the weight δ (s, v ) of the shortestweight path from s to v (or ∞ if no such s → v path exists) and also a function π : V → (V ∪ {N IL}) such that π [v ] is the penultimate vertex on one such shortest path (or NIL if v = s or v is unreachable from s). Carrie doesn’t trust Lazarus very much, and wants to write a “checker” routine that checks the output of Lazarus’s code (in some way that is more efﬁcient than just recomputing the answer herself). Carrie writes a “checker” routine that checks the following conditions. (No need for her to check that w(u, v ) is always nonnegative, since she creates this herself to pass to Lazarus.) (i) d[s] = 0 (ii) π [s] = N IL (iii) for all edges (u, v ) : d[v ] ≤ d[u] + w(u, v ) (iv) for all vertices v : if π [v ] = N IL, then d[v ] = d[π [v ]] + w(π [v ], v ) � (v) for all vertices v = s : if d[v ] < ∞, then π [v ] = N IL (equivalently: π [v ] = N IL =⇒ d[v ] = ∞) � � 1. Show, by means of an example, that Carrie’s conditions are not sufﬁcient. That is, Lazarus’s code could output some d, π values that satisfy Carrie’s checker but for which d[v ] �= δ (s, v ) for some v . (Hint: cyclic π values; unreachable vertices.) Solution: The following graph is a counterexample to Carrie’s checker: s has no edges, π [s] = N IL, and d[s] = 0 u and v have edges of weight 0 to each other, π [u] = v , π [v ] = u, d[u] = 0, and d[v ] = 0. Instead, it should be the case that d[u] = d[v ] = ∞, and π [u] = π [v ] = N IL. 2. How would you augment Carrie’s checker to ﬁx the problem you identiﬁed in (a)? Solution: In addition to the given checks, Carrie should also do the following: (a) Check that π [v ] �= N IL =⇒ (π [v ], v ) ∈ E . (b) Run DFS on G from source S . For each unreachable vertex v , check that d[v ] = ∞ and π [v ] = N IL. (c) Run DFS on G� = (V, E � ), where E � = {(v, π [v ]) : π [v ] = N IL} to check that G� is � acyclic. These checks take time Θ(V + E ). 6 Handout 9: Quiz 2 Practice Problems 3. You are given a connected weighted undirected graph G = (V, E , w) with no negative weight cycles. The diameter of the graph is deﬁned to be the maximumweight shortest path in the graph, i.e. for every pair of nodes (u, v ) there is some shortest path weight δ (u, v ), and the diameter is deﬁned to be max{δ (u, v )}.
(u,v ) Give a polynomialtime algorithm to ﬁnd the diameter of G. What is its running time? (Your algorithm only needs to have a running time polynomial in E  and V  to receive full credit; don’t worry about optimizing your algorithm.) Solution: Run BellmanFord V  times, once from each node. This will ﬁnd all V 2 short est paths. Then just take the maximum length shortest path. Running BellmanFord V  times takes time O(V 2 E ). 4. You are given a weighted directed graph G = (V, E , w) and the shortest path distances δ (s, u) from a source vertex s to every other vertex in G. However, you are not given π (u) (the predecessor pointers). With this information, give an algorithm to ﬁnd a shortest path from s to a given vertex t in O(V + E ) time. Solution: Start at u. Of the edges that point to u, at least one of them will come from a vertex v that satisﬁs δ (s, v )+ w(v, u) = δ (s, u). Such a v is on the shortest path. Recursively ﬁnd the shortest path from s to v . This algorithm hits every vertex and edge at most once, for a running time of O(V + E ). ...
View
Full
Document
This note was uploaded on 09/24/2010 for the course CS 6.006 taught by Professor Erikdemaine during the Spring '08 term at MIT.
 Spring '08
 ErikDemaine
 Algorithms

Click to edit the document details