You are on page 1of 12

# Introduction to Algorithms Massachusetts Institute of Technology Professors Shaﬁ Goldwasser and Silvio Micali

November 17, 2003 6.046J/18.410J Handout 25

Solutions to Practice Problems
Problem -1. True or False, and Justify Circle T or F for each of the following statements to indicate whether the statement is true or false, respectively. If the statement is correct, brieﬂy state why. If the statement is wrong, explain why. Your justiﬁcation is worth more points than your true-or-false designation. T F To determine if two binary search trees are identical trees, one could perform an inorder tree walk on both and compare the output lists. Solution: False. Take the two search trees on the elements list ½ ¾, but the trees are different.

½¾

: both trees produce the

T F Constructing a binary search tree on Ò elements takes ª´Ò ÐÓ the comparison model).

Ò

µ time in the worst case (in

Solution: True. Suppose we could always construct a binary search tree in Ó´Ò ÐÓ Òµ time; then we could sort in Ó´Ò ÐÓ Òµ time by putting the elements in a BST and performing an Ç ´Òµ-time tree walk. This contradicts our comparison-based sorting lower bound of ª´Ò ÐÓ Òµ.

Handout 25: Solutions to Practice Problems T F A greedy algorithm for a problem can never give an optimal solution on all inputs. Consider sorting the edges by weight. If we make a new graph by doubling the weight of every edge in the original graph. so the MST is unchanged. 2 Solution: False. . Therefore the weight of the new tree is simply twice the weight of the old tree and can be computed in constant time if the original MST and weight are known. They are the case when Ø ¾. we still need ª´ µ time to compute the cost of the MST of the new graph. Solution: False. but it gives an optimal solution on all inputs. Solution: True. Prim’s algorithm for minimum spanning trees is a counter-example: it greedily picks edges to cross cuts. But this means that Kruskal’s and Prim’s algorithm will do the same thing. T F Suppose we have computed a minimum spanning tree of a graph and its weight. T F 2-3-4 trees are a special case of B-trees. Doubling the weights does not change the sorted order.

so Ù and Ú were separated when we deleted . (For each node. This information is easily maintained on rotation. Consider any MST. Do we still have a spanning tree? We broke the tree in two by deleting an edge. The best algorithm for ﬁnding the rank of a given element runs in linear time.Handout 25: Solutions to Practice Problems 3 T F You have Ò distinct elements stored in an augmented red-black tree with an extra ﬁeld per node containing the number of elements in the subtree rooted at that node (including itself). or else we’re done. Let ´Ù Üµ be the ﬁrst edge on this path. so as long as we still have a spanning tree. and delete. . Since it is a spanning tree. there is only one path between a given two nodes in a tree. keep track of the number of nodes in it’s subtree. Delete ´Ù Üµ from the tree and add ´Ù Ú µ. that is. Thus our added edge does in fact give us back a spanning tree. Solution: We can augment a red-black tree to support order-statistic queries in Ç ´ÐÓ Òµ time. or add an edge in a useless place? Well. undirected graph with edge-weight function Û to reals. we still have a minimum spanning tree. there must be a path from Ù to Ú . Assume it doesn’t contain ´Ù Ú µ. we did not increase the cost. Let Ù ¾ Î be an arbitrary vertex. and is good enough to search for an order-statistic. and let ´Ù Ú µ ¾ be the least-weight edge incident on Ù. The rank of an element is the number of elements with value less than or equal to it (including itself). ´Ù Úµ belongs to some minimum spanning tree of . Since ´Ù Ú µ is a least weight-edge from Ù. Û ´Ù Ú µ Ñ Ò Û´Ù Ú µ ´Ù Ú µ ¾ . insert.) T F Let ´Î µ be a connected. So did we reconnect it. ¼ ¼ Solution: True.

Assuming we pick large enough. È . The starting potential is ¼ and after that it is non-negative. The amortized cost of insertion is Ç ´ÐÓ Òµ. Ç ´ÐÓ Òµ actual cost minus the ÐÓ Ò decrease in potential. Solution: Deﬁne a potential function ¨ Ò ½ ÐÓ when there are Ò items in the heap.Handout 25: Solutions to Practice Problems 4 T F It is possible to do an amortized analysis of a heap (plain old stored-in-an-array-discussedat-the-time-of-heapsort heap) to argue that the amortized time for insert is Ç ´ÐÓ Òµ (where Ò is the number of items in the heap at the time of insertion) and the amortized time for extract-max is Ç ´½µ. so that’s it. Ç ´ÐÓ Òµ for the actual insert and ÐÓ Ò for the increase in potential. the amortized cost of extractmax is Ç ´½µ.

Give an algorithm to ﬁnd the maximum spanning tree. 5 (b) Consider an undirected. but it is not true that every combination of two sub-MSTs (by a light edge) is an MST. it is their converse that we would need to prove correctness. it is not safe to divide the vertices arbitrarily. as can be seen by the counterexample below. Û´ µ ½¼. This is not an MST. for a total weight of 21. Û´ µ ½. However. then the actual maximum weight spanning tree of corresponds to a minimum spanning tree with less weight than Å Ë Ì ´ µ. Minimum and Maximum Spanning Trees (a) It can be shown that in any minimum spanning tree (of a connected. weighted graph . Suppose you want to ﬁnd the maximum spanning tree. then the two remaining trees are each MSTs on their respective sets of nodes. the algorithm might divide the vertices into sets and . A cycle. These facts inspire the 6.046 Staff to suggest the following algorithm for ﬁnding an ´Î µ: split the nodes arbitrarily into two (nearly) equal-sized MST on a graph sets. ¼ ¼ ¼ . Û ´Ù Ú µ becomes  Û ´Ù Ú µ) and compute the minimum spanning tree Å Ë Ì ´ µ of the resulting graph using Kruskal’s algorithm (or any other MST algorithm). In other words. weighted graph). it is true that every MST is the combination of two sub-MSTs (by a light edge). are connected in a A concrete counterexample is the following: vertices µ ½. which is a contradiction. The resulting tree corresponds to a maximum weight spanning tree for the original graph . and recursively ﬁnd MSTs on those sets.e. and a light edge crossing between the two sets has weight 1. and Û´ µ ½¼. Then connect the two trees with a least-cost edge (which is found by iterating over ). The facts we recalled are essentially irrelevant. connected. The MST of each subgraph has weight 10.Handout 25: Solutions to Practice Problems Problem -2. Speciﬁcally. in which the weight of every edge is Solution: You can make a copy of called replaced with its negation (i. Would you want to use this algorithm? Why or why not? Solution: This algorithm is actually not correct. if we remove an edge ´Ù Ú µ. and the edge ´Ù Ú µ is a least-weight edge crossing between those two sets. Note that if it were not a maximum weight spanning tree. where Û ´ minimum spanning tree consists of the ﬁrst three edges and has weight 12.

the single source shortest paths problem in each of the following graphs.Handout 25: Solutions to Practice Problems Problem -3. state which algorithm (from among those we have studied in class and section) would be best to use. Solution: First Topological Sort and then dynamic programming (Refer to DAG -S HORTEST-PATHS on Page 536 in CLR) . (a) A weighted directed acyclic graph.Ç ´ Ð Î µ . give a brief description of it. and give its running time. (Recall that in this problem. choose an arbitrary data structure. the graph contains a directed cycle. If the running time depends on which data structures are used in the implementation. as efﬁciently as possible.) For each graph. Solution: Dijkstra’ Algorithm with Fibonacci Heaps .Ç ´Î · µ (b) A weighted directed graph where all edge weights are non-negative. we must ﬁnd a shortest path from a designated source vertex to every vertex in the graph. Finding Shortest Paths 6 We would like solve. If you don’t know the name of the algorithm.

of the edges have negative weights. but not all.Ç ´Î µ 7 . the graph contains a directed cycle. Solution: Bellman-Ford’s Algorithm .Handout 25: Solutions to Practice Problems (c) A weighted directed graph in which some.

Thus proved.) If the graph has a root.e. Roots of a graph A root of a directed graph from Ö . Solution: Refer Figure 1 Figure 1: Problem 4-(a) (b) [10 points] Prove the following claim.Handout 25: Solutions to Practice Problems Problem -4. Let Ì be the last tree constructed by DFS in this forest and let Ö be the root of this tree (i. DFS constructed Ì starting from the node Ö . If any node in Ì is a root of the graph. . then Ö is a root of . Solution: None of the nodes in the trees other than Ì of the forest constructed by DFS reach Ö (or Ö would not be the root of a new tree). so is Ö . Consider the forest constructed by running depth-ﬁrst-search (DFS) on the graph . 8 ´ Î µ is a node Ö such that every other node Ú ¾ Î is reachable (a) [3 points] Give an example of a graph which does not have a root.

F IND -O NE -ROOT ´ µ for each vertex Ù ¾ Î ℄ do color Ù℄ WHITE for each vertex Ù ¾ Î ℄ do if color Ù℄ WHITE then last-root Ù £ store current root DFS-V ISIT ´Ùµ r last-root T EST-ROOT ´Ö µ We test whether Ö is a root by recoloring all nodes white. ﬁnds a root of the graph if one exists. Give an Ç ´ Î · algorithm to ﬁnd all the roots of the graph. give an Ç ´ Î · µ-algorithm which when given a graph . Thus. Solution: From above. then the root of the last tree output by DFS must be a root. and outputs N IL otherwise. Clearly. µ time. F IND -A LL -ROOTS ´ µ R EVERSE -G RAPH ´ µ for each vertex Ù ¾ Î ℄ do color Ù℄ WHITE DFS-V ISIT ´Ö µ for each vertex Ù ¾ Î ℄ do if color Ù℄ BLACK then O UTPUT ´Ùµ £ Ù is a root It is easy to see that the procedure R EVERSE -G RAPH ´ µ takes Ç ´Î · fore. (Hint: Ù is a root of iff Ö is reachable from Ù). this algorithm takes Ç ´ Î · µ time. the algorithm to ﬁnd all roots takes Ç ´Î · µ as well.Handout 25: Solutions to Practice Problems (c) [7 points] Using the result proved in (b). We can ﬁnd all nodes that reach Ö by reversing the edges of and then starting a DFS from Ö in the reversed graph. we know that if has a root. we ﬁrst run DFS and then check whether the last root Ö reaches out to all nodes. There- . A node Ù is a root iff Ù can reach Ö . 9 µ(d) [5 points] Suppose you are a given a root Ö of a graph . This will show that every node is reachable from Ö . This is because if Ù is a root then it reaches Ö and conversely. calling DFS-V ISIT ´Ö µ and then verifying that every node in is black. Solution: We now ﬁnd all other roots as follows. if Ù reaches Ö then it reaches every other node as well. This can be done by doing DFS again starting with Ö and checking if the forest has only one root.

. Å ´¼ Ú µ ½. Give a recurrence expression for Å ´ Ú µ. . We shall do the base cases for you: for all . ÑÒ . If we do the problem. for Ú ¼. you might use this algorithm to determine how quickly you can get an A on the quiz. . For example. ÚÒ . If we don’t do the problem. Your goal is to come up with an algorithm which. computes the minimum number of minutes required to earn at least Î points on the quiz. . Suppose further that no partial credit is awarded (unlike this quiz). question has integral point value Ú ¼ and requires Ñ ¼ minutes to solve. we can only choose to either do. . it costs Ñ minutes. Ú¾ . we must choose an optimal way to earn Ú points from the remaining problems. This yields the recurrence: Å ´ µ ÑÒ Ú Ñ · ´  ½   µ Å Ú Ú Å ´  ½ µ Ú . . Solution: Because there is no partial credit. Ñ½ . Ñ¾ . and we should choose an optimal way to earn the remaining Ú   Ú points from among the other problems. and Ú ¼. Test-Taking Strategies 10 Consider (if you haven’t already!) a quiz with Ò questions. Å ´ Úµ ¼. .Handout 25: Solutions to Practice Problems Problem -5. given Ú½ . The faster choice is optimal. and Î . (a) Let Å ´ Ú µ denote the minimum number of minutes needed to earn Ú points when you are restricted to selecting from questions ½ through . or not do. For each ½ Ò. problem . .

by deﬁnition. we keep an additional bit corresponding to whether the minimum was Ñ · Å ´   ½ Ú   Ú½ µ or Å ´   ½ Ú µ. The process terminates when we reach the cell for Å ´ Ú µ for some Ú ¼.Handout 25: Solutions to Practice Problems (b) Give pseudocode for an Ç ´ÒÎ µ-time dynamic programming algorithm to compute the minimum number of minutes required to earn Î points on the quiz. i. whether it is fastest to do problem or not. ¾ ¾ ¼ ¼ ℄ ÑÒ  ½ ℄ Ú È È . Solution: Ñ Îµ FASTEST ´ Ú £ ﬁll in Ò ¢ Î array for Å . (c) Explain how to extend your solution from the previous part to output a list Ë of the questions to solve. we start from the cell for Å ´Ò Î µ and trace backwards in the following way: if the bit for Å ´ Ú µ is set. Ë Ú and Solution: In each cell of the table containing value Å ´ Ú µ. such that Î Ë Ñ is minimized. we include in Ë and go to the cell for Å ´   ½ Ú   Ú µ. After the table has been ﬁlled out. otherwise we exclude from Ë and go to the cell for Å ´   ½ Ú µ. The entry Å Ò Î ℄ is. This is the quantity we desire. the minimum number of minutes needed to earn Î points. It is also possible to do this without keeping the extra bit (just by looking at both possibilities and tracing back to the smallest one). when all Ò problems are available to be answered. base case ﬁrst for Ú ½ to Î 11 ¼ ℄ ½ £ now ﬁll rest of table Å Ú for for Ú £ compute ﬁrst term of recurrence if   ¼ Ú Ú Ñ ½ to ½ to Ò Î else Ñ £ ﬁll table entry Å Ú · Å  ½   ℄ Ú Ú Ú Å ℄ return Å Ò Î Filling in each cell takes Ç ´½µ time. for a total running time of Ç ´ÒÎ µ.e.

” or “bang for the buck”). where Ú Ñ Ú Ñ .e. we sort the problems in descending value of Ú Ñ (i.. Then for some of the points gained on problem . Give an Ç ´Ò ÐÓ Òµ-time algorithm to determine which questions to solve (and how much time to devote to them) in order to receive Î points the fastest.Handout 25: Solutions to Practice Problems (d) Suppose partial credit is given. which is Ç ´Ò ÐÓ Òµ. This contradicts the optimality of the choice that we assumed. That is. Correctness follows by the following argument: suppose an optimal choice of problems only did part of problem and some of problem . so that the number of points you receive on a question is proportional to the number of minutes you spend working on it. choosing to do each corresponding problem until Î points are accumulated (so only the last problem chosen might be left partially completed). there is a faster way to gain them from problem (because hasn’t been completed). The running time is dominated by the sort. decreasing “rate. although the problems are not identical). 12 . Therefore it is safe to greedily choose to do as much of a remaining problem having highest “rate” as needed. Speciﬁcally. and you can work for fractions of minutes. you earn Ú Ñ points per minute on question (up to a total of Ú points). Solution: A greedy approach works here (as it does in the fractional knapsack problem. We then iterate over the list.