On finding and updating spanning trees and shortest paths Cam chattfree
is an edge that connects a vertex in one set with a vertex in the other. The following properties lead to a number of MST algorithms.We recall For simplicity, we assume all edge weights are distinct. Given any cut in an edge-weighted graph (with all edge weights distinct), the crossing edge of minimum weight is in the MST of the graph. The following method colors black all edges in the the MST of any connected edge-weighted graph with V vertices: Starting with all edges colored gray, find a cut with no black edges, color its minimum-weight edge black, and continue until V-1 edges have been colored black.But we must do more: any edge connecting the vertex just added to a tree vertex that is already on the priority queue now becomes (it is no longer a crossing edge because it connects two tree vertices).The lazy implementation leaves such edges on the priority queue, deferring the ineligibility test to when we remove them.In short, we do not need to keep on the priority queue all of the edges from w to vertices tree—we just need to keep track of the minimum-weight edge and check whether the addition of v to the tree necessitates that we update that minimum (because of an edge v-w that has lower weight), which we can do as we process each edge in s adjacency list.In other words, we maintain on the priority queue just one edge for each non-tree vertex: the shortest edge that connects it to the tree.
The lazy version of Prim's algorithm uses space proportional to E and time proportional to E log E (in the worst case) to compute the MST of a connected edge-weighted graph with E edges and V vertices; the eager version uses space proportional to V and time proportional to E log V (in the worst case).If a graph is not connected, we can adapt our algorithms to compute the MSTs of each of its connected components, collectively known as a If edges can have equal weights, the minimum spanning tree may not be unique.Making this assumption simplifies some of our proofs, but all of our our algorithms work properly even in the presence of equal weights.Prim's algorithm works by attaching a new edge to a single growing tree at each step: Start with any vertex as a single-vertex tree; then add V-1 edges to it, always taking next (coloring black) the minimum-weight edge that connects a vertex on the tree to a vertex not yet on the tree (a crossing edge for the cut defined by tree vertices).We use a priority queue to hold the crossing edges and find one of minimal weight.