示例#1
0
 private void DfsVisit(GraphBase g, int u, int p)
 {
     _marked[u] = true;
     foreach (var v in g.Adj(u))
     {
         if (_hasCycle)
         {
             break;
         }
         if (!_marked[v])
         {
             _parents[v] = u;
             DfsVisit(g, v, u);
         }
         else if (v != p)
         {
             _cycle = new Stack <int>();
             _cycle.Push(v);
             while (u != v)
             {
                 _cycle.Push(u);
                 u = _parents[u];
             }
             _cycle.Push(v);
             _hasCycle = true;
             break;
         }
     }
 }
示例#2
0
 private void DfsVisit(GraphBase g, int u, int p)
 {
     _vertices[u].Color = Color.Gray;
     foreach (var v in g.Adj(u))
     {
         if (_hasCycle)
         {
             break;
         }
         if (_vertices[v].Color != Color.Gray)
         {
             _vertices[v].Parent = _vertices[u];
             DfsVisit(g, v, u);
         }
         else if (v != p)
         {
             _cycle = new Stack <int>();
             _cycle.Push(v);
             while (u != v)
             {
                 _cycle.Push(u);
                 u = _vertices[u].Parent.Val;
             }
             _cycle.Push(v);
             _hasCycle = true;
             break;
         }
     }
 }