コード例 #1
0
        public LazyPrim(EdgeWeightedGraph graph) : base(graph)
        {
            marked        = new bool[graph.VertexCount];
            mst           = new Queue <UndirectedEdge>();
            priorityQueue = new HeapPriorityQueue <UndirectedEdge>(graph.EdgeCount);

            Visit(0);
            while (!priorityQueue.IsEmpty)
            {
                UndirectedEdge edge = priorityQueue.DeleteMax();
                if (marked[edge.V] && marked[edge.W])
                {
                    continue;
                }

                mst.Enqueue(edge);
                if (!marked[edge.V])
                {
                    Visit(edge.V);
                }
                if (!marked[edge.W])
                {
                    Visit(edge.W);
                }
            }

            foreach (var edge in mst)
            {
                weight += edge.Weight;
            }
        }
コード例 #2
0
ファイル: Kruskal.cs プロジェクト: XieZehuai/Algorithm
        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);
            }
        }
コード例 #3
0
 public void AddEdge(UndirectedEdge edge)
 {
     adjacents[edge.V].Add(edge);
     adjacents[edge.W].Add(edge);
     edgeCount++;
 }