/// <summary> /// Determines whether the edge-weighted digraph <c>G</c> has a topological /// order and, if so, finds such an order.</summary> /// <param name="G">the edge-weighted digraph</param> /// public Topological(EdgeWeightedDigraph G) { EdgeWeightedDirectedCycle finder = new EdgeWeightedDirectedCycle(G); if (!finder.HasCycle) { DepthFirstOrder dfs = new DepthFirstOrder(G); order = dfs.ReversePost(); } }
private int[] rank; // rank[v] = position of vertex v in topological order /// <summary> /// Determines whether the digraph <c>G</c> has a topological order and, if so, /// finds such a topological order.</summary> /// <param name="G">the digraph</param> /// 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; foreach (int v in order) { rank[v] = i++; } } }
private int count; // number of strongly-connected components /// <summary>Computes the strong components of the digraph <c>G</c>.</summary> /// <param name="G">the digraph</param> /// public KosarajuSharirSCC(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 bool[G.V]; id = new int[G.V]; foreach (int v in dfs.ReversePost()) { if (!marked[v]) { this.dfs(G, v); count++; } } // check that id[] gives strong components Debug.Assert(check(G)); }
public static void MainTest(string[] args) { // read in digraph from command-line argument TextInput input = new TextInput(args[0]); Digraph G = new Digraph(input); DepthFirstOrder dfs = new DepthFirstOrder(G); Console.WriteLine(" v pre post"); Console.WriteLine("--------------"); for (int v = 0; v < G.V; v++) { Console.Write("{0,4} {1,4:} {2,4}\n", v, dfs.Pre(v), dfs.Post(v)); } Console.Write("Preorder: "); foreach (int v in dfs.Pre()) { Console.Write(v + " "); } Console.WriteLine(); Console.Write("Postorder: "); foreach (int v in dfs.Post()) { Console.Write(v + " "); } Console.WriteLine(); Console.Write("Reverse postorder: "); foreach (int v in dfs.ReversePost()) { Console.Write(v + " "); } Console.WriteLine(); }