void Dfs(DirectedGraph g, int v) { m_onStack[v] = true; m_marked[v] = true; foreach (var w in g.Adj(v)) { if (HasCycle()) { return; } else if (!m_marked[w]) { //Dfs(g, w); m_edgeTo[w] = v; Dfs(g, w); } else if (m_onStack[w]) { m_cycle = new Stack <int>(); for (int x = v; x != w; x = m_edgeTo[x]) { m_cycle.Push(x); } m_cycle.Push(w); m_cycle.Push(v); } } m_onStack[v] = false; }
void Dfs(DirectedGraph g, int v) { m_marked[v] = true; foreach (var w in g.Adj(v)) { if (!m_marked[w]) { Dfs(g, w); } } }
void bfs(DirectedGraph g, int s) { Queue <int> queue = new Queue <int>(); m_marked[s] = true; queue.Enqueue(s); while (queue.Count != 0) { int v = queue.Dequeue(); foreach (var w in g.Adj(v)) { m_edgeTo[w] = v; m_marked[w] = true; queue.Enqueue(w); } } }
void Dfs(DirectedGraph g, int v) { m_pre.Enqueue(v); m_marked[v] = true; foreach (var w in g.Adj(v)) { if (!m_marked[w]) { Dfs(g, w); } } m_post.Enqueue(v); m_reversePost.Push(v); }