private void Dfs(Digraph g, int v) { this.onStack[v] = true; this.marked[v] = true; foreach (int w in g.GetAdjacencyList(v)) { if (this.HasCycle) { return; } else if (!this.marked[w]) { this.edgeTo[w] = v; this.Dfs(g, w); } else if (onStack[w]) { this.cycle = new Stack <int>(); for (int x = v; x != w; x = edgeTo[x]) { this.cycle.Push(x); } this.cycle.Push(w); this.cycle.Push(v); } onStack[v] = false; } }
private void Dfs(Digraph g, int s) { this.marked[s] = true; var adjEnumerators = new IEnumerator <int> [g.V]; for (int v = 0; v < g.V; v++) { adjEnumerators[v] = g.GetAdjacencyList(v).GetEnumerator(); } Stack <int> stack = new Stack <int>(); marked[s] = true; stack.Push(s); while (stack.Count != 0) { int current = stack.Peek(); if (adjEnumerators[current].MoveNext()) { int w = adjEnumerators[current].Current; if (!marked[w]) { marked[w] = true; stack.Push(w); } } else { stack.Pop(); } } }
private void Dfs(Digraph g, int s) { this.marked[s] = true; foreach (int v in g.GetAdjacencyList(s)) { if (!marked[v]) { Dfs(g, v); } } }
private void Dfs(Digraph g, int s) { this.marked[s] = true; this.Count++; foreach (int v in g.GetAdjacencyList(s)) { if (!marked[v]) { this.edgeTo[v] = s; Dfs(g, v); } } }
private void Bfs(Digraph g, int s) { Queue <int> queue = new Queue <int>(); this.marked[s] = true; this.distTo[s] = 0; queue.Enqueue(s); while (queue.Count != 0) { int v = queue.Dequeue(); foreach (int i in g.GetAdjacencyList(v)) { if (!marked[i]) { this.edgeTo[i] = v; this.marked[i] = true; this.distTo[i] = this.distTo[v] + 1; queue.Enqueue(i); } } } }