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; } }
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); } }
public void AddEdge(UndirectedEdge edge) { adjacents[edge.V].Add(edge); adjacents[edge.W].Add(edge); edgeCount++; }