ch3 4 - Solutions to Homework Three CSE 101 3.24. Given: a...

Info iconThis preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the 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 linear-time 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

Page1 / 3

ch3 4 - Solutions to Homework Three CSE 101 3.24. Given: a...

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online