CSE 2331/5331 Topic 11: Minimum Spanning Tree CSE 2331 / 5331 ๏ฎ 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. 1 3 a 1 d 2 5 b c 2 1 1 e 4 f CSE 2331 / 5331 A Tree ๏ฎ An undirected graph G = (V, E) is a tree if: ๏ฎ ๏ฎ Equivalently, an undirected graph G = (V, E) is a tree if: ๏ฎ ๏ฎ G is connected, and there is no cycle 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. If G is not connected, then no spanning tree exists. CSE 2331 / 5331 Examples 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 CSE 2331 / 5331 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: ๏ฎ ๏ฎ Output: ๏ฎ ๏ฎ ๏ฎ An undirected graph G = (V, E) with weight ๐ค: ๐ธ โ ๐ A minimum spanning tree MST(G) of G. 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 CSE 2331 / 5331 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 ๏ฎ Input: ๏ฎ ๏ฎ Output: ๏ฎ ๏ฎ An undirected graph G = (V, E) with weight ๐ค: ๐ธ โ ๐ A minimum spanning tree MST(G) of G. 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 ๐โฒ = ๐ โช ๐ข โฒ is still a sub-tree of some MST of G ๏ฎ Such that ๐ ๐ CSE 2331 / 5331 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 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. ๏ฎ 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 subtree of some minimum spanning tree of input graph G. ๏ฎ Termination: when all nodes are connected, we obtain a MST of G. ๏ฎ CSE 2331 / 5331 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 weightedge 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 + ๏ฎ = ๐ ๐2 + ๐ธ ๐ฃ๐ โ๐ deg ๐ฃ๐ 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 ! Use a min-priority queue! CSE 2331 / 5331 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? ๏ฎ ๏ฎ # iterations of each call of the inner for-loop? ๏ฎ ๏ฎ V 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.DecreaseKey ๏ฎ ๏ฎ ๏ฎ Total #: 2E Total cost: ๐(๐ธ lg ๐) Q.Contains and Q.Key ๏ฎ ๏ฎ Total #: 2E Total cost: ๐(๐ธ) Q.DeleteMin ๏ฎ ๏ฎ Total #: V Total cost: ๐(๐ lg ๐) Total time complexity: ๐ ๐ + ๐ธ lg ๐ = ๐ ๐ธ lg ๐ CSE 2331 / 5331 Remarks ๏ฎ Primโs algorithm is a greedy algorithm ๏ฎ ๏ฎ One can use even more efficient Priority-Queue implementation, based on the Fibonacci Heap ๏ฎ ๏ฎ The choice of the minimum-weight edge at each iteration ๐(๐ lg ๐ + ๐ธ) There are many possible MST algorithms ๏ฎ Another popular one is Kruskal Algorithm, which also runs in ๐ ๐ธ lg ๐ time CSE 2331 / 5331
© Copyright 2026 Paperzz