private int[] rank; // rank[v] = rank of vertex v in order /** * Determines whether the digraph {@code G} has a topological order and, if so, * finds such a topological order. * @param G the digraph */ public Topological(Digraph G) { DirectedCycle finder = new DirectedCycle(G); if (!finder.hasCycle()) { DepthFirstOrder dfs = new DepthFirstOrder(G); order = dfs.reversePost(); rank = new int[G.V()]; int i = 0; for (int v : order) rank[v] = i++; } }
/** * Unit tests the {@code DirectedCycle} 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); DirectedCycle finder = new DirectedCycle(G); if (finder.hasCycle()) { StdOut.print("Directed cycle: "); for (int v : finder.cycle()) { StdOut.print(v + " "); } StdOut.println(); } else { StdOut.println("No directed cycle"); } StdOut.println(); }