示例#1
0
        public Kruskal(WeightedEdgeGraph G)
        {
            queue = new Queue <WeightedEdge>();
            pq    = new MinPQ <WeightedEdge>(G.GetE());
            union = new int[G.GetV()];
            for (int x = 0; x < union.Count(); x++)//初始化连通分量
            {
                union[x] = x;
            }
            foreach (var e in G.edges())//初始化优先队列
            {
                pq.insert(e);
            }

            while (!pq.IsEmpty() && queue.Count < G.GetV() - 1)
            {
                var minE = pq.DeletePQHead();//最短边
                int v    = minE.either();
                int w    = minE.other(v);
                if (isConnect(v, w))
                {
                    continue;                 //会形成环
                }
                queue.Enqueue(minE);
                connect(v, w);//加入同一连通分量
            }
        }