Unformatted text preview: directed graph. Assume that the cost adjacency matrix of the digraph is given. The algorithm should run in O(n 3 ) time. A possible solution: Procedure Cycle( G=(V,E)) for each vertex v in V do mark[v]:=unvisited; for each vertex v in V do if mark[v]=unvisited then Cycle1(V,null); Procedure Cycle1(V,p) {p is the parent of v in the DFS tree} mark[v]:=visited; for each vertex w adjacent to v do if mark[w]=unvisited then Cycle1(V,w) e l s e i f w ≠ p then begin print(“cycle exists”); exit end Hence for a matrix representation we have O(n 3 ), however we can do better if G is represented as adjacency list (as in DSF). Then the algorithm checks at most n edges, so time is O(2n)=O(n). 6.[10] 7.[10] 8.[10]...
View
Full
Document
This note was uploaded on 12/10/2009 for the course CAS 2c03 taught by Professor Janicki during the Spring '03 term at McMaster University.
 Spring '03
 janicki

Click to edit the document details