예제 #1
0
            public DirectedWeightedCycle(DirectedWeightedEdgeGraph G)
            {
                marked  = new bool[G.GetV()];
                onstack = new bool[G.GetV()];
                edgeTo  = new DirectedWeightedEdge[G.GetE()];

                for (int v = 0; v < G.GetV(); v++)
                {
                    if (!marked[v])
                    {
                        dfs(v, G);
                    }
                }
            }
예제 #2
0
        public Dijkstra(DirectedWeightedEdgeGraph G, int s)
        {
            distTo = new double[G.GetV()];
            edgeTo = new DirectedWeightedEdge[G.GetE()];
            dic    = new Dictionary <int, double>();

            for (int i = 0; i < distTo.Length; i++)//到所有顶点的距离初始化为最大值
            {
                distTo[i] = double.MaxValue;
            }

            distTo[s] = 0;            //从0节点开始
            dic.Add(0, 0);
            while (dic.Count() > 0)   //直到所有节点都处理完为止
            {
                var minE = MinEdge(); //取出目前最短的路径的终点
                dic.Remove(minE);
                relax(minE, G);       //开始放松
            }
        }
예제 #3
0
        public BellmanFord(DirectedWeightedEdgeGraph G, int s)
        {
            disTo   = new double[G.GetV()];
            edges   = new DirectedWeightedEdge[G.GetE()];
            inQueue = new bool[G.GetV()];
            queue   = new Queue <int>();

            for (int i = 0; i < G.GetV(); i++)
            {
                disTo[i] = double.MaxValue;
            }

            disTo[s]   = 0;
            inQueue[s] = true;
            queue.Enqueue(s);

            while (queue.Count() > 0 && !hasNegativeCycle())//没有负权重环出现
            {
                int v = queue.Dequeue();
                inQueue[v] = false;
                relax(v, G);
            }
        }