This preview shows pages 1–2. Sign up to view the full content.
This preview has intentionally blurred sections. Sign up to view the full version.
View Full Document
Unformatted text preview: Solutions to Homework Three CSE 101 3.24. Given: a directed acyclic graph G = ( V,E ) Topologically sort G (Gives an ordering v 1 ,v 2 ,...,v n of the nodes V , where n =  V  ) For i = 1 to n 1 : if ( v i ,v i +1 ) negationslash E : output no Output yes A path touching all vertices is sometimes called a Hamilton path . Suppose G has a Hamilton path. Any topological ordering must respect all the edges on this path, and must therefore put the vertices in exactly the same order as they appear on the path. The algorithm above will find this unique topological sort, and confirm the existence of the corresponding Hamilton path. On the other hand, if G does not have a Hamilton path , the algorithm will find some topological ordering v 1 ,...,v n , but one of the edges ( v i ,v i +1 ) will not be in E . The running time is the same as that of topological sort: O (  V  +  E  ). 3.25. (a) Give a lineartime algorithm which works for directed acyclic graphs. Suppose u has edges to nodes w 1 ,...,w k . Then the nodes reachable from u are precisely: u itself, and the nodes reachable from all the w i . This gives us a simple recursive formula for cost values: cost[ u ] = min { p u , min ( u,w ) E cost[ w ] } . To make it iterative, it would help if we could make sure to compute all the cost[ w ] values before we get to cost[ u ]. Well, for a dag this is easy: just handle vertices in reverse topological order!...
View
Full
Document
 Summer '10
 Paturi
 Algorithms, Sort

Click to edit the document details