ps9sol - Introduction to Algorithms Massachusetts Institute...

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

View Full Document Right Arrow Icon
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 Singapore-MIT Alliance Professors Erik Demaine, Lee Wee Sun, and Charles E. Leiserson Day 32 6.046J/18.410J SMA5503 Handout 32 Problem Set 9 Solutions MIT students: This problem set is due in lecture on Day 32. Reading: Chapters 22 and 24. Both exercises and problems should be solved, but only the problems should be turned in. Exercises are intended to help you master the course material. Even though you should not turn in the exercise solutions, you are responsible for material covered by the exercises. Mark the top of each sheet with your name, the course number, the problem number, your recitation instructor and time, the date, and the names of any students with whom you collaborated. MIT students: Each problem should be done on a separate sheet (or sheets) of three-hole punched paper. You will often be called upon to "give an algorithm" to solve a certain problem. Your write-up should take the form of a short essay. A topic paragraph should summarize the problem you are solving and what your results are. The body of your essay should provide the following: 1. A description of the algorithm in English and, if helpful, pseudocode. 2. At least one worked example or diagram to show more precisely how your algorithm works. 3. A proof (or indication) of the correctness of the algorithm. 4. An analysis of the running time of the algorithm. Remember, your goal is to communicate. Graders will be instructed to take off points for convoluted and obtuse descriptions. 2 Exercise 9-1. Do exercise 22.2-7 on page 539 of CLRS. Solution: Handout 32: Problem Set 9 Solutions Correctness: Let and be any two nodes such that is the diameter of the tree. There is a unique path from to . Let be the first node on that path discovered by BFS. tree. Exercise 9-2. Do exercise 22.3-12 on page 549 of CLRS. Solution: Run DFS once from each vertex. The graph is singly connected iff all edges are tree or back. Time is . Exercise 9-3. Do exercise 22.4-3 on page 552 of CLRS. Alternatively, one could run a modified DFS which only allows an edge to be examined once. i.e., once has been examined, eliminate the edge or (the same edge) and continue looking for the gray node in the DFS. Essentially, this is the same method as the first procedure. Exercise 9-4. Do exercise 24.1-4 on page 591 of CLRS. Solution: There is a simpler answer than this one . . . R E E Q0 F If the graph does contain a cycle then a cycle, it will be found after at most time is always . and the running time is . If does contain edges and vertices have been examined. Thus the running [email protected] Solution: Run a modified version of DFS where one tests to see if the edge node which is not 's parent. F G #C F G H $ D and so all three are equal. Thus is the diameter of the leads to a gray #" 7 5 664 0 A" @" $9&" If the paths and do share edges, then is on . Since is the longest path, . 32 0 1 ! # Since , . . Since was the last node found by . Thus , and (&' &" & # $%#' $%#" $%#" $ %#" ! F S # $ D 3C 0 B &" 8#" $ $ ) E If the paths so from to and from to do not share edges, then the path from to includes Run BFS on any node remembering the node in the graph, remembering the node discovered last. Run BFS from discovered last. is the diameter of the tree. [email protected] H F IG E Handout 32: Problem Set 9 Solutions 3 and to run a B ELLMAN -F ORD -N EGATIVE -C YCLE -I NFINITY 1 I NITIALIZE -S INGLE -S OURCE 2 for to 3 do for each edge 4 do R ELAX 5 for each edge 6 do if 7 then 8 BFS' The BFS' procedure is the same as the ordinary BFS except that whenever a node is placed in the queue, it also has its value set to . Exercise 9-5. Do exercise 24.3-6 on page 600 of CLRS. Solution: Maintain an array indexed from to . The are doubly linked lists. If then contains the vertices with distance from . contains vertices not reachable from . Extract the minimum element from by searching the elements of in turn for a nonempty list and extracting an element from that list. time. Relaxing an edge can be done in constant time. The algorithm runs in time. Exercise 9-6. Do exercise 24.5-7 on page 614 of CLRS. Solution: The appropriate sequence of steps relaxes the edges of the predecessor subgraph in the order of a DFS or BFS. The proof is by contradiction. If this sequence of relaxations does not result in , then there is a shorter path from to . However, this path must be of length , because there are no negative-weight cycles. Thus, the predecessor subgraph must i R c T h&d d F XR g C Vy e D IJKSTRA' 1 I NITIALIZE -S INGLE -S OURCE 2 3 4 5 while 6 do E XTRACT-M IN 7 8 for each vertex Adj 9 do R ELAX ` U VT The idea is to find a node in the negative-weight cycle, to set its weight to BFS-like procedure on that node setting the values of reachable nodes to . U WT y g y g ` y e e 6XR Y 7 ` E XR y g DR H e sH v U VT XR U VT #t a g xwC t Y v g e u g e e g DR q r # e [email protected]@2 H f q r d c [email protected]@2 q f F S y E T g e CR sF c T d g pihDR a igef7 d7 a 0 a g # y a g e y 97 ae d d kw F j 0 g e 6XR Y e fF a b` g ` y e 7 4 Handout 32: Problem Set 9 Solutions include this path which is doesn't because then the relaxations on the graph would have given the optimal solution. Problem 9-1. Running in Boston To get in shape, you have decided to start running to work. You want a route that goes entirely uphill and then entirely downhill so that you can work up a sweat going uphill and then get a nice breeze at the end of your run as you run faster downhill. Your run will start at home and end at work and you have a map detailing the roads with road segments (any existing road between two intersections) and intersections. Each road segment has a positive length, and each intersection has a distinct elevation. (a) Assuming that every road segment is either uphill or downhill, give an efficient algorithm to find the shortest route that meets your specifications. Solution: Dijkstra's algorithm solves the single source shortest-paths problem on a time. In this problem general graph with non-negative edge weights in we can actually do better and solve it in time. The difference is that we must go uphill before we go downhill. With this constraint we know we have somewhere along the optimal path there will be a highest point. Call it . A consequence of this path being optimal is that there exist no other points for which the length of the best uphill path from home to the point plus the length of the best downhill path from the point to work is shorter than the best paths to and from . So if we could find the best uphill path to each point and the best downhill path from each point, we can do a linear scan through the points to find the one with the smallest sum and this tells us the optimal path. Now we just have two subproblems of finding the single source shortest uphill paths to each point and the single goal shortest paths downhill from each point. Consider the uphill problem. We can solve this by throwing away all downhill edges, then because the path always moves uphill there can no longer be any cycles (since there are no level edges). Therefore we are dealing with a DAG and we discussed in class how to find the single source shortest paths in a DAG in linear time. Basically this involves putting a topological ordering on the verticies and then just computing the best paths in order. We can similarly solve the same downhill problem. The total run time is to produce each DAG. Then we solve two single source shortest path in a DAG problems which each run in time. Finally, traversing the verticies to find the optimal peak vertex takes time. Thus the total time is . (b) Give an efficient algorithm to solve the problem if some roads may be level (i.e., both intersections at the end of the road segments are at the same elevation) and therefore can be taken at any point. v m q on m v rbpDfxl rm E m v rxl E m v utsl E E l wl E m m v uxyl v E Handout 32: Problem Set 9 Solutions Solution: In this case, we can no longer replace each edge with a directed edge that assures us that no cycles exist. Therefore our best method in this general case is to use Dijkstra's algorithm after transforming the graphs as above replacing each level road with two directed arrows. Problem 9-2. Karp's minimum mean-weight cycle algorithm 5 Let , where ranges over all directed cycles in . A cycle for which is called a minimum mean-weight cycle. This problem investigates an efficient algorithm for computing . Assume without loss of generality that every vertex is reachable from a source vertex . Let be the weight of a shortest path from to , and let be the weight of a shortest path from to consisting of exactly edges. If there is no path from to with exactly edges, then . Solution: If there were a negative-weight cycle, then because the minimum would have to be negative, therefore there are no negative weight cycles. Given that there are no negative weight cycles, then the shortest path will not take any cycles and can only be at most edges long. (b) Show that if , then is strictly positive because . Then Solution: First, we know because the shortest path when no negative weight cycles exist is going to cost more with nodes than the shortest path with fewer nodes that is the actual shortest path. (c) Let be a -weight cycle, and let and be any two vertices on . Suppose that the weight of the path from to along the cycle is . Prove that . (Hint: The weight of the path from to along the cycle is .) Solution: We know because the shortest path from to might use . Alternatively we also know that because the shortest path to from might go through and around the zero weight cycle for a cost of . Therefore with these two inequalities we know . T w v # y) j j 0 T "j c T xm tvr# B j j 0 T x T 8u# j j v ! j @ j T m m q for all vertices . (Hint: Use both properties from part (a).) h u T m w 3 &@ $ t j j T F q s R c T pm (a) Show that if , then contains no negative-weight cycles and for all vertices . 0 u F q 0 p j [email protected] j R zH {Y Let be a directed graph with weight function the mean weight of a cycle of edges in , and let to be d ~fm d 0 F } | yH (1 Y 0 B3 c F q s ( ( ( 0 3'"Gs F w3&@ j 0 u 0 3 &0 u $ ~ j UB 3j 0 0 R H G F j . We define 6 Handout 32: Problem Set 9 Solutions (Hint: Show that a shortest path to any vertex on the minimum mean-weight cycle can be extended along the cycle to make a shortest path to the next vertex on the cycle.) Solution: Get to the cycle along some shortest path and then extend the path along the cycle to make a shortest path of length . If is the vertex we end up at, then . Then since for we took the shortest possible path to the cycle, there cannot exist any shorter path to the node with fewer steps, only equal path lengths. (e) Show that if , then Solution: We know that there exists some vertex with a maximum difference of 0, and all differences are greater than 0, so the minimum must be 0. Solution: Adding to each edge increases by . It also increases by and decreases by . Manipulate, and both sides increase by and the equation is maintained. Thus, by picking , we can use the previous part. F S E E Solution: Compute for recurrence mum of the maximum of the fraction. in . In H F IG r# j 0 Y v 3bp1 #3j m ( ( ( c P0 j E (g) Give an -time algorithm to compute . time by evaluating the time, determine the mini- #m [email protected] "j R (f) Show that if we add a constant to the weight of each edge of by . Use this to show that , then T m w 3 &@ p 0 ( [email protected] t j j T T xm w 3 &@ (I0 [email protected] b j j T T m w 3 &@ (60 j T [email protected] t j u m T 0 u 1 (d) Show that if such that 0 0 u , then there exists a vertex on the minimum mean-weight cycle WT j [email protected] [email protected] "j j 0 H F S is increased ...
View Full Document

{[ snackBarMessage ]}

Ask a homework question - tutors are online