/// <summary> /// topological sort in an edge-weighted digraph /// </summary> /// <param name="g"></param> public Topological(EdgeWeightedDigraph g) { EdgeWeightedDirectedCycle finder = new EdgeWeightedDirectedCycle(g); if (!finder.HasCycle()) { DepthFirstOrder dfs = new DepthFirstOrder(g); order = dfs.ReversePost(); } }
/// <summary> /// by finding a cycle in predecessor graph /// </summary> private void findNegativeCycle() { int v = edgeTo.Length; EdgeWeightedDigraph spt = new EdgeWeightedDigraph(v); for (int w = 0; w < v; w++) { if (edgeTo[v] != null) { spt.AddEdge(edgeTo[v]); } } EdgeWeightedDirectedCycle finder = new EdgeWeightedDirectedCycle(spt); cycle = finder.Cycle(); }