Data Str & Algorithm HW Solutions 72

Data Str & Algorithm HW Solutions 72 - 72 Chap. 11...

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

View Full Document Right Arrow Icon
72 Chap. 11 Graphs INITIALIZE array Count to 0’s; FOR every edge (v, w) // Similar to BFS topo sort Count[w]++; IF the number of vertices with zero Count is not 1 THEN return "No Root"; ELSE { do DFS search from the vertex with zero Count; Verify that every vertex has been marked; } 11.11 The following algorithm is O ( | V | + | E | ) . It is a minor modi f cation on DFS. Unfortunately, it will not detect a cycle if the input is not a DAG. // V is the root of the DAG. int DAGdepth(Graph& G, int v, int depth) {// DFS int currmax = depth; for (Edge w = G.first(v); G.isEdge(w); w = G.next(w)) currmax = MAX(DAGdepth(G, G.v2(w), depth+1), currmax); return currmax; } 11.12 To solve this problem, simply run the BFS topological sort algorithm. If
Background image of page 1
This is the end of the preview. Sign up to access the rest of the document.
Ask a homework question - tutors are online