/** * Determines whether the edge-weighted digraph <tt>G</tt> has a topological order and, if so, * finds such an order. * * @param G the edge-weighted digraph */ public Topological(EdgeWeightedDigraph G) { EdgeWeightedDirectedCycle finder = new EdgeWeightedDirectedCycle(G); if (!finder.hasCycle()) { DepthFirstOrder dfs = new DepthFirstOrder(G); order = dfs.reversePost(); } }
public KosarajuSCC(Digraph G) { marked = new boolean[G.V()]; id = new int[G.V()]; DepthFirstOrder order = new DepthFirstOrder(G.reverse()); for (int s : order.reverse()) if (!marked[s]) { dfs(G, s); count++; } }
private KosarajuSharirSCC(Digraph<V> digraph) { ids = new HashMap<>(digraph.vertexCount()); visited = new HashSet<>(digraph.vertexCount()); size = new int[digraph.vertexCount()]; DepthFirstOrder<V> dfo = DepthFirstOrder.from(digraph.reverse()); for (V vertex : dfo.reversePost()) { if (!visited.contains(vertex)) { dfs(digraph, vertex); count++; } } }
public KosarajuSCC(Digraph G) { // compute reverse postorder of reverse graph DepthFirstOrder dfs = new DepthFirstOrder(G.reverse()); // run DFS on G, using reverse postorder to guide calculation marked = new boolean[G.V()]; id = new int[G.V()]; for (int v : dfs.reversePost()) { if (!marked[v]) { dfs(G, v); count++; } } // check that id[] gives strong components assert check(G); }
/** * Unit tests the {@code DepthFirstOrder} data type. * * @param args the command-line arguments */ public static void main(String[] args) { In in = new In(args[0]); Digraph G = new Digraph(in); DepthFirstOrder dfs = new DepthFirstOrder(G); StdOut.println(" v pre post"); StdOut.println("--------------"); for (int v = 0; v < G.V(); v++) { StdOut.printf("%4d %4d %4d\n", v, dfs.pre(v), dfs.post(v)); } StdOut.print("Preorder: "); for (int v : dfs.pre()) { StdOut.print(v + " "); } StdOut.println(); StdOut.print("Postorder: "); for (int v : dfs.post()) { StdOut.print(v + " "); } StdOut.println(); StdOut.print("Reverse postorder: "); for (int v : dfs.reversePost()) { StdOut.print(v + " "); } StdOut.println(); }