Minimum Spanning Trees
Last updated
Last updated
Given an undirected graph, a spanning tree T is a subgraph of G, which has the following properties.
Is connected.
Is acyclic.
Includes all of the vertices.
A minimum spanning tree is a spanning tree of minimum total weight. A shortest paths tree depends on the start vertex, while there is no source for a minimum spanning tree.
A cut is an assignment of a graph’s nodes to two non-empty sets.
A crossing edge is an edge which connects a node from one set to a node from the other set.
Given any cut, minimum weight crossing edge is in the MST.
Start with no edges in the MST.
Find a cut that has no crossing edges in the MST.
Add smallest crossing edge to the MST.
Repeat until V-1 edges.
Start from some arbitrary start node.
Repeatedly add shortest edge that has one node inside the MST under construction.
Repeat until V-1 edges.
Prim’s and Dijkstra’s algorithms are similar, however:
Dijkstra's considers "distance from the source".
Prim's considers "distance from the tree."
Priority Queue operation count, assuming binary heap based PQ.
Insertion: V, each costing O(log V)
time.
Delete-min: V, each costing O(log V)
time.
Decrease priority: O(E)
, each costing O(log V)
time.
Overall runtime: O(V * log(V) + V * log(V) + E * logV)
. Assuming E > V, this is just O(E log V)
.
Consider edges in increasing order of weight.
Add edge to MST unless doing so creates a cycle.
Repeat until V-1 edges.
Adding an edge to MST means that add the edge to the ArrayList and connect the two vertices in the WeightedQuickUnion
. If the two vertices are already connected, the edge should be ingored since adding it will create a cycle.
Insertion: E, each costing O(log E)
time.
Delete-min: O(E)
, each costing O(log E)
time.
Union: O(V)
, each costing O(log V)
time.
IsConnected: O(E)
, each costing O(log V)
time.
Overall runtime: O(E + V log * V + E log * V)
.
Assuming E > V, this is just O(E log V)
.