public DirectedDFS(DiGraph G, IEnumerable<int> sources) { marked = new bool[G.V]; foreach (int s in sources) if (!marked[s]) dfs(G, s); }
private void dfs(DiGraph G, int v) { marked[v] = true; foreach (int w in G.Adj(v)) if (!marked[w]) dfs(G, w); }
static void Main(string[] args) { DiGraph dg = new DiGraph(5); dg.addEdge(0, 1); dg.addEdge(1, 2); dg.addEdge(1, 3); dg.addEdge(2, 4); //dg.addEdge(4, 0); //Comment it for topological dg.addEdge(4, 3); Console.WriteLine(dg); dg.ShowMarked(); dg.ShowCycle(); dg.ShowTopological(); Console.WriteLine("{0}-{1} is strongly connected : {2}", 0, 1, dg.isStonglyConnected(0, 1)); Console.WriteLine("{0}-{1} is reachable : {2}", 0, 3, dg.Reachable(0, 3)); Console.ReadKey(); }
public DirectedDFS(DiGraph G, int s) { if (s < 0 || s > G.V-1) s = G.V-1; marked = new bool[G.V]; dfs(G, s); }
static void Main(string[] args) { DiGraph dg=new DiGraph(5); dg.addEdge(0, 1); dg.addEdge(1, 2); dg.addEdge(1, 3); dg.addEdge(2, 4); //dg.addEdge(4, 0); //Comment it for topological dg.addEdge(4, 3); Console.WriteLine(dg); dg.ShowMarked(); dg.ShowCycle(); dg.ShowTopological(); Console.WriteLine("{0}-{1} is strongly connected : {2}", 0, 1, dg.isStonglyConnected(0, 1)); Console.WriteLine("{0}-{1} is reachable : {2}", 0, 3, dg.Reachable(0, 3)); Console.ReadKey(); }
public DiGraph reverse() { DiGraph R = new DiGraph(V); for (int v = 0; v < V; v++) foreach (int w in Adj(v)) R.addEdge(w, v); return R; }
private IEnumerable<int> order; // topological order #endregion Fields #region Constructors public Topological(DiGraph G) { DirectedCycle cyclefinder = new DirectedCycle(G); if (!cyclefinder.hasCycle()) { DepthFirstOrder dfs = new DepthFirstOrder(G); order = dfs.ReversePost(); } }
private bool[] onStack; // vertices on recursive call stack #endregion Fields #region Constructors public DirectedCycle(DiGraph G) { onStack = new bool[G.V]; edgeTo = new int[G.V]; marked = new bool[G.V]; for (int v = 0; v < G.V; v++) if (!marked[v]) dfs(G, v); }
private void dfs(DiGraph G, int v) { pre.Enqueue(v); marked[v] = true; foreach (int w in G.Adj(v)) if (!marked[w]) dfs(G, w); post.Enqueue(v); reversePost.Push(v); }
private Stack<int> reversePost; // vertices in reverse postorder #endregion Fields #region Constructors public DepthFirstOrder(DiGraph G) { pre = new Queue<int>(); post = new Queue<int>(); reversePost = new Stack<int>(); marked = new bool[G.V]; for (int v = 0; v < G.V; v++) if (!marked[v]) dfs(G, v); }
private IEnumerable <int> order; // topological order public Topological(DiGraph G) { DirectedCycle cyclefinder = new DirectedCycle(G); if (!cyclefinder.hasCycle()) { DepthFirstOrder dfs = new DepthFirstOrder(G); order = dfs.ReversePost(); } }
public static void ShowCycle(this DiGraph dg) { DirectedCycle Cycle = new DirectedCycle(dg); if (Cycle.hasCycle()) { Console.Write("Directed Graph Cycled in : "); foreach (var e in Cycle.Cycle()) { Console.Write("{0} ", e); } Console.WriteLine(); } }
public static void ShowMarked(this DiGraph dg) { Console.Write("Marked: "); DirectedDFS reachable = new DirectedDFS(dg, dg.V); for (int v = 0; v < dg.V; v++) { if (reachable.Marked(v)) { Console.Write(v + " "); } } Console.WriteLine(); }
public static void ShowTopological(this DiGraph dg) { Topological top = new Topological(dg); if (top.isDAG()) { return; } Console.Write("Topological: "); foreach (int v in top.Order) { Console.Write("{0} ", v); } Console.WriteLine(); }
private void dfs(DiGraph G, int v) { onStack[v] = true; marked[v] = true; foreach (int w in G.Adj(v)) if (this.hasCycle()) return; else if (!marked[w]) { edgeTo[w] = v; dfs(G, w); } else if (onStack[w]) { cycle = new Stack<int>(); for (int x = v; x != w; x = edgeTo[x]) cycle.Push(x); cycle.Push(w); cycle.Push(v); } onStack[v] = false; }