private void dfs(Digraph g, int v) { marked[v] = true; foreach (var w in g.getAdj(v)) { if (!marked[w]) dfs(g, w); } order.Push(v); }
private void dfs(Digraph g, int v) { marked[v] = true; onStack[v] = true; foreach (int w in g.getAdj(v)) { if(hasCycle()) return; else if (!marked[w]) { edgeTo[w] = v; dfs(g, w); } else if (onStack[w]) { for (int x = v; x != w; x = edgeTo[x]) cycle.Push(x); cycle.Push(w); cycle.Push(v); } } onStack[v] = false; }