private void Dfs(IDiGraph g, int v) { onStack[v] = true; marked[v] = true; foreach (int w in g.Adj(v)) { if (HasCycle()) { return; } else if (!marked[w]) { edgeTo[w] = v; Dfs(g, w); } else if (onStack[w]) { cycle = new Chapter1.Stack <int>(); for (int x = v; x != w; x = edgeTo[x]) { cycle.Push(x); } cycle.Push(w); cycle.Push(v); } onStack[v] = false; } }
public IEnumerable <IDirectedEdge> PathTo(int v) { if (!HasPathTo(v)) { return(null); } Chapter1.Stack <IDirectedEdge> path = new Chapter1.Stack <IDirectedEdge>(); for (var e = edgeTo[v]; e != null; e = edgeTo[e.From]) { path.Push(e); } return(path); }
public override IEnumerable <int> PathTo(int v) { if (!HasPathTo(v)) { return(null); } Chapter1.Stack <int> path = new Chapter1.Stack <int>(); for (int x = v; x != S; x = edgeTo[x]) { path.Push(x); } path.Push(S); return(path); }