Пример #1
1
 private void dfs(EdgeWeightedDigraph G, int v)
 {
     pre.Enqueue(v);
     marked[v] = true;
     foreach (var w in G.Adj(v))
         if (!marked[w.from])
             dfs(G, w.from);
     post.Enqueue(v);
     reversePost.Push(v);
 }
 //若 顶点v在一个环中,那么一次深搜可以找到该环
 private void Dfs(EdgeWeightedDigraph g, int v)
 {
     _onStack[v] = true;
     _marked[v]  = true;
     foreach (var e in g.Adj(v))
     {
         if (HasCycle())
         {
             return;
         }
         else if (!_marked[e.To])
         {
             _edgeTo[e.To] = e;
             Dfs(g, e.To);
         }
         //如果遍历到一个已经访问过到点,那么就是有环了
         else if (_onStack[e.To])
         {
             _cycle = new Stack <DirectedEdge>();
             var f = e;
             while (f.From != e.To)
             {
                 _cycle.Push(f);
                 f = _edgeTo[f.From];
             }
             _cycle.Push(f);
         }
     }
     _onStack[v] = false;
 }
Пример #3
0
 private void dfs(EdgeWeightedDigraph G, int v)
 {
     onStack[v] = true;
     marked[v]  = true;
     foreach (var w in G.Adj(v))
     {
         if (this.hasCycle())
         {
             return;
         }
         else if (!marked[w.to])
         {
             edgeTo[w.to] = v;
             dfs(G, w.to);
         }
         else if (onStack[w.to])
         {
             cycle = new Stack <DirectedEdge>();
             for (int x = v; x != w.to; x = edgeTo[x])
             {
                 //cycle.Push(x);
                 cycle.Push(w);
             }
             //cycle.Push(v);
         }
     }
     onStack[v] = false;
 }
Пример #4
0
 private void relax(EdgeWeightedDigraph G, int v)
 {
     foreach (DirectedEdge e in G.Adj(v))
     {
         int w = e.to;
         if (distTo[w] > distTo[v] + e.Weight)
         {
             distTo[w] = distTo[v] + e.Weight;
             edgeTo[w] = e;
         }
     }
 }
Пример #5
0
 private void relax(EdgeWeightedDigraph G, int v)
 {
     foreach (DirectedEdge e in G.Adj(v))
     {
         int w = e.to;
         if (distTo[w] > distTo[v] + e.Weight)
         {
             distTo[w] = distTo[v] + e.Weight;
             edgeTo[w] = e;
         }
     }
 }
Пример #6
0
 private void dfs(EdgeWeightedDigraph G, int v)
 {
     pre.Enqueue(v);
     marked[v] = true;
     foreach (var w in G.Adj(v))
     {
         if (!marked[w.from])
         {
             dfs(G, w.from);
         }
     }
     post.Enqueue(v);
     reversePost.Push(v);
 }
Пример #7
0
        private void Dfs(EdgeWeightedDigraph dg, int v)
        {
            preQueue.Enqueue(v);
            marked[v] = true;

            foreach (var e in dg.Adj(v))
            {
                int w = e.To;
                if (!marked[w])
                {
                    Dfs(dg, w);
                }
            }

            postQueue.Enqueue(v);
            reversePost.Push(v);
        }
Пример #8
0
 public void Relax(EdgeWeightedDigraph g, int v)
 {
     foreach (DirectedEdge e in g.Adj(v))
     {
         int w = e.To;
         if (_distTo[w] > _distTo[v] + e.Weight)
         {
             _distTo[w] = _distTo[v] + e.Weight;
             _edgeTo[w] = e;
             if (_pq.Contains(w))
             {
                 _pq.Change(w, _distTo[w]);
             }
             else
             {
                 _pq.Insert(w, _distTo[w]);
             }
         }
     }
 }
Пример #9
0
 private void relax(EdgeWeightedDigraph G, int v)
 {
     foreach (DirectedEdge e in G.Adj(v))
     {
         int w = e.to;
         if (distTo[w] > distTo[v] + e.Weight)
         {
             distTo[w] = distTo[v] + e.Weight;
             edgeTo[w] = e;
             if (pq.contains(w))
             {
                 pq.change(w, distTo[w]);
             }
             else
             {
                 pq.Insert(w, distTo[w]);
             }
         }
     }
 }
Пример #10
0
 private void relax(EdgeWeightedDigraph G, int v)
 {
     foreach (DirectedEdge e in G.Adj(v))
     {
         int w = e.to;
         if (distTo[w] > distTo[v] + e.Weight)
         {
             distTo[w] = distTo[v] + e.Weight;
             edgeTo[w] = e;
             if (!onQ[w])
             {
                 queue.Enqueue(w);
                 onQ[w] = true;
             }
         }
         if (cost++ % G.V == 0)
         {
             findNegativeCycle();
         }
     }
 }
Пример #11
0
        private void Relax(EdgeWeightedDigraph g, int v)
        {
            foreach (var e in g.Adj(v))
            {
                int w = e.To;
                if (_distTo[w] > _distTo[v] + e.Weight)
                {
                    _distTo[w] = _distTo[v] + e.Weight;
                    _edgeTo[w] = e;
                    if (!_onQueue[w])
                    {
                        _queue.Enqueue(w);
                        _onQueue[w] = true;
                    }
                }

                if (_cost++ % g.V == 0)
                {
                    FindNegativeCycle();
                }
            }
        }
Пример #12
0
 private void dfs(EdgeWeightedDigraph G, int v)
 {
     onStack[v] = true;
     marked[v] = true;
     foreach (var w in G.Adj(v))
         if (this.hasCycle())
             return;
         else if (!marked[w.to])
         {
             edgeTo[w.to] = v;
             dfs(G, w.to);
         }
         else if (onStack[w.to])
         {
             cycle = new Stack<DirectedEdge>();
             for (int x = v; x != w.to; x = edgeTo[x])
                 //cycle.Push(x);
             cycle.Push(w);
             //cycle.Push(v);
         }
     onStack[v] = false;
 }
Пример #13
0
 private void relax(EdgeWeightedDigraph G, int v)
 {
     foreach (DirectedEdge e in G.Adj(v))
     {
         int w = e.to;
         if (distTo[w] > distTo[v] + e.Weight)
         {
             distTo[w] = distTo[v] + e.Weight;
             edgeTo[w] = e;
             if (!onQ[w])
             {
                 queue.Enqueue(w);
                 onQ[w] = true;
             }
         }
         if (cost++%G.V == 0)
             findNegativeCycle();
     }
 }
Пример #14
0
 private void relax(EdgeWeightedDigraph G, int v)
 {
     foreach (DirectedEdge e in G.Adj(v))
     {
         int w = e.to;
         if (distTo[w] > distTo[v] + e.Weight)
         {
             distTo[w] = distTo[v] + e.Weight;
             edgeTo[w] = e;
             if (pq.contains(w)) pq.change(w, distTo[w]);
             else pq.Insert(w, distTo[w]);
         }
     }
 }