Beispiel #1
0
        public Kruskal(EdgeWeightedGraph graph) : base(graph)
        {
            mst = new Queue <UndirectedEdge>();
            IPriorityQueue <UndirectedEdge> priorityQueue = new HeapPriorityQueue <UndirectedEdge>(graph.Edges());
            UnionFind unionFind = new UnionFind(graph.VertexCount);

            while (!priorityQueue.IsEmpty && mst.Count < graph.VertexCount - 1)
            {
                UndirectedEdge edge = priorityQueue.DeleteMax();
                if (unionFind.IsConnected(edge.V, edge.W))
                {
                    continue;
                }

                unionFind.Union(edge.V, edge.W);
                mst.Enqueue(edge);
            }
        }