private void DFS(Digraph g, int v) { _onStack[v] = true; _marked[v] = true; foreach (var w in g.Adjacent(v)) { if (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; }
public static int DegreeOut(Digraph g, int v) { int degree = 0; foreach (var w in g.Adjacent(v)) { degree++; } return(degree); }
private void DFS(Digraph g, int v) { _marked[v] = true; foreach (var w in g.Adjacent(v)) { if (!_marked[w]) { DFS(g, w); } } }
private void DFS(Digraph g, int v) { _pre.Enqueue(v); _marked[v] = true; foreach (var w in g.Adjacent(v)) { if (!_marked[w]) { DFS(g, w); } } _post.Enqueue(v); _reversePost.Push(v); }