public void dfs(DiGraph g, int s) { marked[s] = true; onStack[s] = true; for (int w : g.adj(s)) if (this.hasCycle()) return; else if (!marked(w)) { edgeTo[w] = s; dfs(g, w); } else if (onStack[w]) { cycle = new Stack<Integer>(); for (int x = s; x != w; x = edgeTo[x]) cycle.push(x); cycle.push(w); cycle.push(s); } onStack[s] = false; }