pset6

# pset6 - Introduction to Algorithms Massachusetts Institute...

This preview shows page 1. Sign up to view the full content.

This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Introduction to Algorithms Massachusetts Institute of Technology Professors Shaﬁ Goldwasser and Silvio Micali October 31, 2003 6.046J/18.410J Handout 19 Problem Set 6 This problem set is due in recitation on Friday, November 14. Reading: Chapters 17.1-17.4, 22.1-22.5, 23.1-23.2, 24.1-24.5. There are four problems and one exercise. You should only hand in Problems 6.1 through 6.4. The ﬁrst exercise is for review purposes and will not be graded. Each problem is to be done on a separate sheet (or sheets) of three-hole punched paper. Mark the top of each sheet with your name, the course number, the problem number, your recitation section, the date, and the names of any students with whom you collaborated. Exercise 6-1. Graphs: Terminology and Deﬁnitions Note: This problem is not to be handed in. A graph is a set of vertices, , and edges, . In this problem, we will review some basic properties of graphs. For each question, answer True or False or ﬁll in the blank where indicated. Justify your answers. (It may be useful to refer to Section B.4 in CLRS.) (a) An undirected acyclic graph contains (b) A directed acyclic graph contains (c) A dense graph contains (d) Let edges. edges. edges. be an undirected graph. If , then (e) If a graph is sparse, then it is a tree. (f) For every directed graph, there is a unique topological sort. (g) A graph is connected iff it contains a cycle. (h) Every undirected graph has a minimum spanning tree. (i) A directed acyclic graph is not strongly connected. (j) A directed acyclic graph contains a vertex (k) If there is a directed path from to connected component. such that . in a directed graph, then vertices and (l) Suppose we have a directed graph that is strongly connected. For any depth-ﬁrst search of , if all the forward edges of (with respect to the depth-ﬁrst forest) are removed from , the resulting graph is still strongly connected. UT ¡ S ¢¤ G©D%ERQPBIC  ¤ F ¢ ¤C 8 4 \$#"HG©D%ED2BA@97653 ¦¦  ¡ ¨¤¢ ©§¦WV¡ E ¤ \$%"! ¤ \$#"! ¦ UT &&&&&&& '''''''& ¡ ¡ ¨¤¢ 210¦)(¡ ¨¤¢ ©§¦¥£¡ is dense. form a 2 Problem 6-1. Cycles Handout 19: Problem Set 6 (a) Give an algorithm to ﬁnd a minimum weight cycle in a given weighted, undirected, connected graph in which all edge weights are non-negative. Prove your algorithm is correct and analyze its running time. (Optional Extra Credit Problem: Can you ﬁnd a more efﬁcient algorithm for this problem?) (b) Give an algorithm to determine if a given directed, connected graph contains an edge that is in every cycle. Prove your algorithm is correct and analyze its running time. (Optional Extra Credit Problem: Can you ﬁnd a more efﬁcient algorithm for this problem?) (c) Suppose that a weighted, directed graph has a negative-weight cycle. Give an efﬁcient algorithm to list the vertices of one such cycle. Prove your algorithm is correct and analyze its running time. Problem 6-2. Minimum Spanning Trees (a) Consider the following algorithm, N EW-MST, for computing a minimum spanning tree. The algorithm takes as input an undirected, weighted, connected graph . It sorts the edges in non-increasing order according to edge weight. It then goes through each edge in , in the sorted order, and determines if removing that edge disconnects the graph. If not, then the edge is removed permanently, otherwise the edge remains. Below, we provide pseudocode for the N EW-MST algorithm. Prove that the N EW-MST algorithm outputs a minimum spanning tree of the input graph . (b) Give an efﬁcient algorithm to ﬁnd a spanning tree for a connected, weighted, undirected graph such that the weight of the maximum-weight edge in the spanning tree is minimized. Prove your algorithm is correct. Problem 6-3. Labeled Graphs and Longest Paths You are given a directed graph , in which each vertex has a unique label a cost function assigning a weight to each edge, and a source . Additionally, has the property that . Design an algorithm to construct an output array every edge such that is the length of the longest path from to in .  ¦  ¤ D#Eg pqep¨piBiBi¨ X eh¨ge f N EW-MST( ) 1 Sort edges according to edge weight in non-increasing order: 2 3 While : 4 If is connected 5 6 7 Output . ¡ ¦d fe ¡ rE   ¨ d10¦¤ ¢ ¡ ¤ l ¤ iUajnmk%Tj  ( %qiT fg¨hUp o#T¤   ¨¤¢ d10¦¡ t¡T sT r pew¡ r x p(¡ y¡ e x vu w1T  ¦ YX ¤ ba`'2%HF ¡ ¡ ¡  ¤ c§¦HF t sT r ¡ ¨   o Handout 19: Problem Set 6 Problem 6-4. Amortized Weight-Balanced Trees 3 Consider an ordinary binary search tree augmented by adding to each node the ﬁeld giving the number of keys stored in the subtree rooted at . Let be a constant in the range . We say that a given node is -balanced if and The tree as a whole is -balanced if every node in the tree is -balanced. We will study the following amortized approach to maintaining weight-balanced trees. (a) Given a node in an arbitrary binary search tree, show how to rebuild the subtree rooted at so that it becomes -balanced. You algorithm should run in time time and can use auxiliary storage. (b) Show that performing a search in an worst-case time. -node -balanced binary search tree takes For the remainder of this problem, assume that the constant is strictly greater than . Suppose that I NSERT and D ELETE are implemented as usual for an -node binary search tree, except that after every such operation, if any node in the tree is no longer -balanced, then the subtree rooted at the highest such node in the tree is “rebuilt” so that it becomes -balanced. We shall analyze this rebuilding scheme using the potential method. For a node in a binary search tree , we deﬁne and we deﬁne the potential of as where is a sufﬁciently large constant that depends on . (c) Argue that any binary search tree has nonnegative potential and that a -balanced tree has potential 0. (d) Suppose that units of potential can pay for rebuilding an -node subtree. How large must be in terms of in order for it to take amortize time to rebuild a subtree that is not -balanced. (e) Show that inserting a node into or deleting a node from an -node -balanced tree costs amortized time. qe 's p jktBT f X s s f X u u X  ¢ ¨#s¤  }  h s h@  1#¤    ¨Qq's p 990T yp rktBTaq's p {ze xp rktBTy1s¤  q | e   q | e  ¢ " ¤ v {QtF u X f u" u 0iq's p j§tBT7~vsq's p 9%T p jktBT e } u u q | e q's p jktBT7~vsaq's p {ze yxp jktBT e }uuq | e us s u u " q'X s j§tT0HF e¤ fp u u u ss v "` ¤ ©ja %YHF " ¤ ©`%YF 's p j§tBT§! qe¤ tlu Gwvu fX   ...
View Full Document

## This note was uploaded on 01/01/2011 for the course CS 5503 taught by Professor Charlese.leiserson during the Fall '01 term at MIT.

Ask a homework question - tutors are online