CSE 2331 / 5331
CSE 2331/5331
Topic 10:Minimum Spanning
Tree
Edge-weighted un-directed graph G = (V, E) and edge weight function 𝑤𝑤:𝐸𝐸 → 𝑅𝑅 E.g, road network, where each node is a city, and each
edge is a road, and weight is the length of this road.
CSE 2331 / 5331
3 5ba
f
c
d e
1 1
42
11 2
A Tree
An undirected graph G = (V, E) is a tree if: G is connected, and there is no cycle
Equivalently, an undirected graph G = (V, E) is a tree if: G is connected, and 𝐸𝐸 = 𝑉𝑉 − 1
One key property: Given a tree T on nodes V, if we add any edge 𝑒𝑒 to T, it
will create a cycle. That is, 𝑇𝑇 ∪ 𝑒𝑒 contains a cycle for any 𝑒𝑒 ∉ 𝑇𝑇.
CSE 2331 / 5331
A Spanning Tree
A spanning tree T of an undirected graph G = (V, E) is a tree that includes all vertices V of G 𝑇𝑇 ⊆ 𝐸𝐸
Weight of a spanning tree T: 𝑤𝑤 𝑇𝑇 = ∑ 𝑢𝑢,𝑣𝑣 ∈𝑇𝑇 𝑤𝑤 𝑢𝑢, 𝑣𝑣 where 𝑤𝑤:𝐸𝐸 → 𝑅𝑅 is the weight function on edges of G
A minimum spanning tree of G is a spanning tree with smallest weight.
CSE 2331 / 5331
If G is not connected, then no spanningtree exists.
Examples
CSE 2331 / 5331
Is minimum spanning tree unique?
CSE 2331 / 5331
MST
MST for given G may not be unique
Since MST is a spanning tree: # edges : |V| - 1
If the graph is unweighted: All spanning trees have same weight
Key Property of MST
Given a MST 𝑇𝑇 of 𝐺𝐺 = (𝑉𝑉,𝐸𝐸), let 𝑒𝑒 ∈ 𝐸𝐸 be any edge in 𝐸𝐸 but not in 𝑇𝑇. The following then holds: There is a unique cycle C containing e in 𝑇𝑇 ∪ 𝑒𝑒 . 𝑒𝑒 is an edge with largest weight in C.
Sketch of proof: Why unique? If 𝑒𝑒 does not have largest weight, let 𝑒𝑒′ ∈ 𝐶𝐶 be an edge
with largest weight in C. 𝑇𝑇′ = 𝑇𝑇 − 𝑒𝑒𝑒 + 𝑒𝑒 is a spanning tree of G 𝑤𝑤𝑒𝑒𝑤𝑤𝑤𝑤𝑤𝑤𝑤 𝑇𝑇′ ≤ 𝑤𝑤𝑒𝑒𝑤𝑤𝑤𝑤𝑤𝑤𝑤(𝑇𝑇) ⇒ 𝑇𝑇 cannot be MST. Contradiction ⇒ e must have largest weight in C.
CSE 2331 / 5331
Construct MST
Input: An undirected graph G = (V, E) with weight 𝑤𝑤:𝐸𝐸 → 𝑅𝑅
Output: A minimum spanning tree MST(G) of G.
CSE 2331 / 5331
A MST T is V-1 number of edges that connect all nodes, with no cycle.
Intuitively, we will choose “safe” edges to incrementally build T
Example
How to choose the first edge that is “safe”, namely, it must be in some MST? Note, it is some, not all !
CSE 2331 / 5331
Intuition
Intuitively, Incrementally grow a sub-tree 𝑇𝑇 𝑆𝑆 ⊆ 𝐸𝐸 connecting a subset of
nodes 𝑆𝑆 ⊂ 𝑉𝑉 At the beginning of each iteration, 𝑇𝑇 𝑆𝑆 is a sub-tree of some MST
of G At each iteration, grow 𝑇𝑇 (𝑆𝑆𝑒) to include one more vertex 𝑆𝑆𝑒 = 𝑆𝑆 ∪
𝑢𝑢 Such that 𝑇𝑇 𝑆𝑆′ is still a sub-tree of some MST of G
CSE 2331 / 5331
Input: An undirected graph G = (V, E) with weight 𝑤𝑤:𝐸𝐸 → 𝑅𝑅
Output: A minimum spanning tree MST(G) of G.
Prim’s MST Algorithm: High-level
𝑈𝑈 : unconnected vertices 𝑆𝑆 = 𝑉𝑉 – 𝑈𝑈: vertices connected by current partial tree
CSE 2331 / 5331
Example
Greedy algorithm Break ties arbitrarily
CSE 2331 / 5331
MST Theorem
CSE 2331 / 5331
Key to the correctness of PrimMST algorithm. Invariance:
each time PrimMST() algorithm grows the partial tree (i.e, adds another edge to it), the invariance is that the new tree is still a subtreeof some minimum spanning tree of input graph G. Termination:
when all nodes are connected, we obtain a MST of G.
MST Theorem:Let T be a sub-tree of a minimum spanning tree.
If e is a minimum weight edge connecting T to some vertex not in T, then 𝑇𝑇 ∪ 𝑒𝑒 is a subtree of a minimum spanning tree.
Proof of MST Theorem
By the theorem’s hypothesis, T is a subtree of some MST A of G.
If e is not an edge of A, then 𝐴𝐴 ∪ 𝑒𝑒 contains a cycle. Let 𝐶𝐶 be this cycle. There must exists some edge 𝑒𝑒′ ∈𝐶𝐶 from T to a vertex not in T.
Since e is a minimum weight edge from T to vertices not in T, 𝑤𝑤𝑒𝑒𝑤𝑤𝑤𝑤𝑤𝑤𝑤 𝑒𝑒 ≤ 𝑤𝑤𝑒𝑒𝑤𝑤𝑤𝑤𝑤𝑤𝑤 𝑒𝑒′ .
Replacing 𝑒𝑒′ ∈ 𝐴𝐴 by e gives a new tree 𝐵𝐵 = 𝐴𝐴 − 𝑒𝑒𝑒 +𝑒𝑒 such that 𝑤𝑤𝑒𝑒𝑤𝑤𝑤𝑤𝑤𝑤𝑤 𝐵𝐵 ≤ 𝑤𝑤𝑒𝑒𝑤𝑤𝑤𝑤𝑤𝑤𝑤 𝐴𝐴 .
𝑇𝑇 ∪ 𝑒𝑒 ⊆ 𝐵𝐵. So 𝑇𝑇 ∪ 𝑒𝑒 is also a subtree of some MST. Done.
CSE 2331 / 5331
Naive Implementation of Prim’s MST
Naïve implementation: linear scan all edges to identify min-weight edge 𝑣𝑣𝑖𝑖 ,𝑣𝑣𝑗𝑗 at each iteration
Total time complexity: 𝑂𝑂 𝑉𝑉𝐸𝐸CSE 2331 / 5331
First Improvement of Implementation
Storing costs at vertices Each unvisted vertex in 𝑈𝑈 maintain the smallest weight-
edge to visited vertices
CSE 2331 / 5331
First Improvement: Cost at Vertices
CSE 2331 / 5331
Analysis
Time to update cost at each vertex at each iteration (in the While-loop): Line 6: 𝑂𝑂 𝑉𝑉 Lines 8—13: 𝑂𝑂(deg 𝑣𝑣𝑗𝑗 )
Total time complexity: 𝑂𝑂 𝑉𝑉2 + ∑𝑣𝑣𝑗𝑗∈𝑉𝑉 deg 𝑣𝑣𝑗𝑗 = 𝑂𝑂 𝑉𝑉2 + 𝐸𝐸
CSE 2331 / 5331
Second Improvement: Efficient Update
Consider Line 6 of previous implementation We spend linear time to identify the vertex 𝑣𝑣𝑗𝑗 with
smallest cost from 𝑈𝑈 Can we do better? What operations do we need to support?
extracting the smallest value from a set, updating the cost of some vertices
The cost can only decrease !
CSE 2331 / 5331
Use a min-priority queue!
Second Improvement: Priority Queue
CSE 2331 / 5331
Analysis:
We use min-heap to implement the priority queue The maximum size of Q is V # iterations of While-loop?
V # iterations of each call of the inner for-loop?
deg 𝑣𝑣𝑗𝑗 Total #times lines 7—10 are executed:
∑𝑣𝑣𝑗𝑗∈𝑉𝑉 deg 𝑣𝑣𝑗𝑗
CSE 2331 / 5331
Analysis cont.
Q.insert: Total #: V Total cost: 𝑂𝑂(𝑉𝑉 lg𝑉𝑉)
Q.IsNotEmpty and Q.MinKey: Total #: V Total cost: O(V)
Q.DeleteMin Total #: V Total cost: 𝑂𝑂(𝑉𝑉 lg𝑉𝑉)
CSE 2331 / 5331
Q.DecreaseKey Total #: 2E Total cost: 𝑂𝑂(𝐸𝐸 lg 𝑉𝑉)
Q.Contains and Q.Key Total #: 2E Total cost: 𝑂𝑂(𝐸𝐸)
Total time complexity:𝑂𝑂 𝑉𝑉 + 𝐸𝐸 lg𝑉𝑉 = 𝑂𝑂 𝐸𝐸 lg𝑉𝑉
Remarks
Prim’s algorithm is a greedy algorithm The choice of the minimum-weight edge at each
iteration One can use even more efficient Priority-Queue
implementation, based on the Fibonacci Heap 𝑂𝑂(𝑉𝑉 lg𝑉𝑉 + 𝐸𝐸)
There are many possible MST algorithms Another popular one is Kruskal Algorithm, which also
runs in 𝑂𝑂 𝐸𝐸 lg𝑉𝑉 time
CSE 2331 / 5331