Lecture 11: Kruskal's MST Algorithm: CLRS Chapter 23
Lecture 11: Kruskal's MST Algorithm: CLRS Chapter 23
Lecture 11: Kruskal's MST Algorithm: CLRS Chapter 23
Introduction to UNION-FIND data structure. Used in Kruskals algorithm Will see implementation in next lecture.
Idea of Kruskals Algorithm Build a forest. Initially, trees of the forest are the vertices (no edges). In each step add the cheapest edge that does not create a cycle. Continue until the forest is a single tree. (Why is a single tree created?) This is a minimum spanning tree (we must prove this).
Outline by Example
a 3 e
10 5 7
b 12 d
9 c 2
b c
d MST
original graph edge {d, c} {a, e} {a, d} {e, d} {b, c} {a, b} {b, d} weight 2 3 5 7 9 10 12
A={
Step 1: Choose an edge e in F of minimum weight, and check whether adding e to A creates a cycle. If yes, remove e from F . If no, move e from F to A.
Step 2: If F = , stop and output the minimal spanning tree (V, A). Otherwise go to Step 1.
Remark: Will see later, after each step, (V, A) is a subgraph of a MST.
How to Choose the Edge of Least Weight Question: How does algorithm choose edge e F with minimum weight? Answer: Start by sorting edges in E in order of increasing weight. Walk through the edges in this order.
(Once edge e causes a cycle it will always cause a cycle so it can be thrown away.)
How to Check for Cycles Observation: At each step of the outlined algorithm, (V, A) is acyclic so it is a forest. If u and v are in the same tree, then adding edge {u, v} to A creates a cycle. If u and v are not in the same tree, then adding edge {u, v} to A does not create a cycle.
Question: How to test whether u and v are in the same tree? High-Level Answer: Use a disjoint-set data structure Vertices in a tree are considered to be in same set. Test if Find-Set(u) = Find-Set(v)? Low -Level Answer: The UNION-FIND data structure implements this:
7
The UNION-FIND Data Structure UNION-FIND supports three operations on collections of disjoint sets: Let n be the size of the universe.
Union(u, v): O(log n) Merge the sets respectively containing u and v into a common set.
For now we treat UNION-FIND as a black box. Will see implementation in next lecture.
8
Kruskals Algorithm: the Details Sort E in increasing order by weight w; O(|E| log |E|)
A = { }; for (each u in V ) CREATE-SET(u); for i from 1 to |E| do if (FIND-SET(ui) != FIND-SET(vi) ) { add {ui, vi} to A; UNION(ui, vi); } return(A);
Remark: With a proper implementation of UNION-FIND, Kruskals algorithm has running time O(|E| log |E|).
Correctness of Kruskals Algorithm Lemma: Let (V, A) be a subgraph (part) of a MST of G = (V, E), and let e = {u, v} E \ A be an edge such that (1) (V, A {e}) has no cycle; (2) e has minimum weight among all edges in E \ A such that (1) is satised. Then (V, A {e}) is a subgraph of some MST containing (V, A).
10
if e T there is edge e T A such that W (e) = W (e ) and T = T {e} {e } is a tree. Since W (T ) = W (T ) this implies T is a MST so (V, A{e}) is a subgraph of MST T so lemma is correct.
11
T u {u,v}
u u
path in T: u u v
u v v
12
Correctness of Kruskals Algorithm Continued Proof: Let T be any MST with (V, A) as a subgraph. If e T , we are done. Suppose that e = {u, v} T . There is a unique path from u to v in the MST T , which contains at least one edge e E \ A. e = e (because e T but e T ). (V, A {e }) has no cycles (because (V, A {e }) is included in T .) W (e) W (e ) (because both edges in E \ A and assumption (2) in previous page). Consider the new tree T = (T {e}) \ {e }. If W (e) = W (e ), then T is another minimum spanning tree containing (V, A {e}). If W (e) < W (e ), then W (T ) W (T ) = W (e) W (e ) < 0. Contradiction.
13
T v u v u
14
Original graph G a b c e d
15