private readonly bool[] _marked; // marked[v] = is there an s->v path? /// <summary> /// Computes the vertices reachable from the source vertex <tt>s</tt> in the digraph <tt>G</tt>. /// </summary> /// <param name="g">g the digraph</param> /// <param name="s">s the source vertex</param> public NonrecursiveDirectedDFS(Digraph g, int s) { _marked = new bool[g.V]; // to be able to iterate over each adjacency list, keeping track of which // vertex in each adjacency list needs to be explored next var adj = new IEnumerator <Integer> [g.V]; for (var v = 0; v < g.V; v++) { adj[v] = g.Adj(v).GetEnumerator(); } // depth-first search using an explicit stack var stack = new Collections.Stack <Integer>(); _marked[s] = true; stack.Push(s); while (!stack.IsEmpty()) { int v = stack.Peek(); if (adj[v].MoveNext()) { int w = adj[v].Current; // StdOut.printf("check %d\n", w); if (!_marked[w]) { // discovered vertex w for the first time _marked[w] = true; // edgeTo[w] = v; stack.Push(w); // StdOut.printf("dfs(%d)\n", w); } } else { // StdOut.printf("%d done\n", v); stack.Pop(); } } }
private readonly bool[] _marked; // marked[v] = is there an s->v path? #endregion Fields #region Constructors /// <summary> /// Computes the vertices reachable from the source vertex <tt>s</tt> in the digraph <tt>G</tt>. /// </summary> /// <param name="g">g the digraph</param> /// <param name="s">s the source vertex</param> public NonrecursiveDirectedDFS(Digraph g, int s) { _marked = new bool[g.V]; // to be able to iterate over each adjacency list, keeping track of which // vertex in each adjacency list needs to be explored next var adj = new IEnumerator<Integer>[g.V]; for (var v = 0; v < g.V; v++) adj[v] = g.Adj(v).GetEnumerator(); // depth-first search using an explicit stack var stack = new Collections.Stack<Integer>(); _marked[s] = true; stack.Push(s); while (!stack.IsEmpty()) { int v = stack.Peek(); if (adj[v].MoveNext()) { int w = adj[v].Current; // StdOut.printf("check %d\n", w); if (!_marked[w]) { // discovered vertex w for the first time _marked[w] = true; // edgeTo[w] = v; stack.Push(w); // StdOut.printf("dfs(%d)\n", w); } } else { // StdOut.printf("%d done\n", v); stack.Pop(); } } }