Minimum Spanning Trees Chapter 23 1 CPTR 430 Algorithms Minimum Spanning Trees
� Motivation ■ Electonic circuitry design ■ Make the pins of multiple components electrically equivalent ■ “Wire” them together: use n 1 wires to connect n pins, each wire connecting two pins ■ Use the shortest amount of wire as possible 2 CPTR 430 Algorithms Minimum Spanning Trees
� ✄ ✄ ✞ ✁ ✁ ✂ ✄ ✆ ✄ ✂ ✁ ✝ ☎ ✂ ✄ � ✁ ✁ ✂ Model of Solution ■ Use an undirected, connected graph G V E , where ❚ V is the set of pins ❚ E is the set of possible interconnections between pairs of pins ❚ For each edge u v E , w u v is the cost (length of wire) needed to connect pin u to pin v ■ Task: find an acyclic subset T E that connects all the vertices such that ∑ w T w u v ✟✡✠ u v T is minimized 3 CPTR 430 Algorithms Minimum Spanning Trees
� ✁ � ✁ Minimum Spanning Tree ■ T is acyclic and T connects all vertices T is a tree that spans G ■ The problem of finding T is called the minimum spanning tree problem ❚ “Minimum” means minimum weight; all spanning trees have the same number of edges: V 1 8 7 1 2 3 9 4 2 8 14 4 11 0 4 6 7 8 10 7 6 5 1 2 4 CPTR 430 Algorithms Minimum Spanning Trees
MSTs are Not Unique 8 7 1 2 3 9 4 2 8 14 4 11 0 w(T) = 37 4 6 7 8 10 7 6 5 1 2 8 7 1 2 3 9 4 2 8 14 w(T ) = 37 4 11 0 4 6 7 8 10 7 6 5 1 2 5 CPTR 430 Algorithms Minimum Spanning Trees
✁ ✄ Popular MST Algorithms ■ Kruskal’s algorithm ■ Prim’s algorithm ■ Both are greedy algorithms ■ Both commonly run in O E lg V time 6 CPTR 430 Algorithms Minimum Spanning Trees
✄ ☎✆ ✝ ✁✂ � ✁ ✄ ✂ Generic Greedy Algorithm ■ Both Kruskal’s and Prim’s algorithms are variations of this basic idea ■ Loop invariant: Prior to each iteration, A is a subset of some minimum spanning tree ■ Each step determines an edge u v that can be added to A without violating this invariant I.e., A u v some MST ■ Such an edge is called a safe edge for A 7 CPTR 430 Algorithms Minimum Spanning Trees
✂ � ✝ ✝ ✂ ✁ � Generic Greedy Algorithm public static Graph genericMST(Graph G) { Graph A = G.copyWithoutEdges(); while ( A is not a spanning tree for G ) { A.addEdge(an edge in G that is safe for A); } return A; } Loop invariant: Prior to each iteration, A is a subset of T , some minimum spanning tree ■ Initialization : Before the loop is executed once, A A T ■ Maintenance : The loop maintains the invariant by adding only safe edges ■ Termination : A A T a MST 8 CPTR 430 Algorithms Minimum Spanning Trees
✁ ✄ ✄ ☎ ✆ ✄ ✂ � ✁ � ✁ ☎ ✁ ✂ ✂ ✂ Safe Edge ■ The trick is finding the safe edge ■ Within the while loop a safe edge exists because for some MST T : ❚ The invariant requires that A T ❚ Entry into the body of the while is granted only when A T u v u v T and u v A edge , where public static Graph genericMST(Graph G) { Graph A = G.copyWithoutEdges(); while ( A does not span G ) { A.addEdge(an edge in G that is safe for A); } return A; } 9 CPTR 430 Algorithms Minimum Spanning Trees
✂ ✄ ✁ ✄ ✂ ✁ � � Cut S V S of an undirected graph G V E partitions V ■ A cut 8 7 1 2 3 S 9 S 4 2 8 14 4 11 0 4 6 7 8 10 7 6 5 1 2 V − S V − S 10 CPTR 430 Algorithms Minimum Spanning Trees
✂ ✁ � ✂ ✄ ✄ ☎ � ✁ Crossing a Cut ■ An edge u v E crosses cut S V S if one of its endpoints is in S and the other is in V S ■ A cut respects a set A of edges if no edge in A crosses the cut ■ A light edge crossing a cut has the minimum weight of all edges crossing the cut There can be multiple light edges in case of ties 8 7 1 2 3 S 9 S 4 2 8 14 4 11 0 4 6 7 8 10 7 6 5 1 2 V − S V − S 11 CPTR 430 Algorithms Minimum Spanning Trees
✄ ✄ � � ✂ ✁ ✁ ✂ ✂ ✁ ✄ ✄ ✁ ✂ � ✁ � ✂ ✄ Safe Edge, Formally (Theorem 23.1) Let ■ G V E be a connected, undirected, weighted graph ■ A be a subset of E such that A is a subset of some minimum spanning tree for G S V S be any cut of G that respects A ■ u v S V S be a light edge crossing ■ Edge u v is safe for A 12 CPTR 430 Algorithms Minimum Spanning Trees
✂ ✂ ✁ ✁ ✂ � ✄ ✄ ✁ ✄ Proof ■ Let T be a MST that includes A ■ Assume T does not include u v (if it does, then u v is trivially safe) u v would form a cycle with the edges on path p from u to v in ■ Edge T (such a path exists because T spans G ) ■ Edges of T are shown, not edges in G x u ■ Edges of A are highlighted p y ■ Navy blue vertices are in S ; v yellow vertices are in V S 13 CPTR 430 Algorithms Minimum Spanning Trees
✁ ✂ ✂ ☎ ✁ ✂ ✄ ✄ � ✂ ✁ ✁ ✄ ✄ ✁ ✁ � ✄ ✂ � ✄ ✂ ✁ ✁ ✂ Proof ■ u and v are on opposite sides of the cut S V S x u at least one edge in T on path p that p y also crosses the cut v ■ Let x y be such an edge x y A , since the cut respects A ■ x y ■ Since is found on a unique path u p v , removing x y and adding u v creates a new spanning tree, T 14 CPTR 430 Algorithms Minimum Spanning Trees
✆ ✄ ✁ ✁ ✄ ✁ ✁ ✁ � ✄ ✄ ✁ ✁ ✂ � ✄ ✆ ✁ ✁ ✄ ✄ ✂ ✁ ☎ ✄ ✂ ✁ � ✄ ✁ ✁ ✄ ✆ ✄ ✁ ✁ ✂ ✄ ✁ ✂ � ✄ ☎ ✁ � � ✄ ✁ ✆ ✁ � ✄ ✄ ✁ ✂ ✁ � ✄ ✄ ✁ ✁ ✂ ✄ ✄ ✁ ✁ ✁ � ✁ � ✁ ✁ ✁ � ✄ ✁ ✂ ✂ ✂ ✄ ✁ ✁ ✄ ✂ ✂ ✄ ✁ ✄ ✁ � ✄ ✁ � ✂ � � ✁ ✂ ✂ ✄ ✄ ✁ ✄ ✂ � � Proof T T x y u v Is T a MST? u v is a light edge crossing cut S V S and ■ x y also crosses cut S V S w u v w x y w T w T w x y w u v w T ■ T is a MST w T w T ■ w T w T and w T w T w T w T T is also a MST ■ A T and x y A A T ■ A u v T and T is a MST u v is safe for A 15 CPTR 430 Algorithms Minimum Spanning Trees
✁ ✆ ✂ ✂ ✄ ✁ � ✁ ✂ ✄ � � ✄ ✁ ✂ ✂ ✁ � ✄ ✄ Corollary 23.2 Let ■ G V E be a connected, undirected, weighted graph ■ A E be included in some MST for G ■ C V C E C be a connected component (tree) in the forest G A V A u v is a light edge connecting C to some other component in G A u v is safe for A How does this follow from Theorem 23.1? 16 CPTR 430 Algorithms Minimum Spanning Trees
✁ ✂ ✁ � ✂ ✄ � ✄ ✄ ✂ ✆ ✁ Proof ■ The cut V C V V C respects A u v is a light edge for this cut ■ u v is safe for A Both Kruskal’s and Prim’s algorithms rely on Corollary 23.2 17 CPTR 430 Algorithms Minimum Spanning Trees
Kruskal vs. Prim ■ Both provide an efficient way of selecting the safe edge ■ Kruskal’s algorithm: ❚ A is a forest ❚ The safe edge is the least weight edge in the graph that connects two distinct components ■ Prim’s algorithm: ❚ A is a single tree ❚ The safe edge is the least weight edge in the graph that connects the tree A to a vertex not in the tree 18 CPTR 430 Algorithms Minimum Spanning Trees
✂ ✁ ✄ ✂ ✁ ✁ � ✂ ✄ ✄ Kruskal’s Algorithm ■ Based on the genericMST() algorithm ■ Safe edge = the minimum weight edge that connects any two trees in the coalescing forest of trees, A ■ Suppose C 1 and C 2 are the two trees, and that u v is the minimum weight edge that connects them u v has to be a lightweight edge connecting C 1 to some other tree ■ (Why?) u v is safe for C 1 (by Corollary 23.2) 19 CPTR 430 Algorithms Minimum Spanning Trees
� ✁ � ✁ Kruskal’s Algorithm, Take Two Said another way: ■ Kruskal’s algorithm begins with a forest of V trees ❚ Each tree contains Every vertex in G one vertex forms exactly one tree ❚ Each tree contains no edges ■ At each step, Kruskal’s algorithm adds the lowest weight edge from E that does not form a cycle ❚ Two trees coalesce into a single tree 20 CPTR 430 Algorithms Minimum Spanning Trees
Kruskal’s Algorithm is Greedy Kruskal’s algorithm is greedy, since at each step it adds the edge of the least possible weight The current optimum turns out to be a part of the overall optimum solution 21 CPTR 430 Algorithms Minimum Spanning Trees
� ✁ ✂ ✄ ✄ ✂ ✁ � ✄ ✁ ✂ ✄ � ✂ ✁ Implementation Details ■ The edges in E must be sorted in nondecreasing order by weight ■ Consider each edge u v in order: ❚ Both endpoints u and v belong to the same tree u v would form a cycle u v do not add to the forming MST ❚ Otherwise, u and v belong to the same tree add u v to the forming MST 22 CPTR 430 Algorithms Minimum Spanning Trees
� ✁ � ✁ ✂ � ☎ Implementation Details ■ Define the relation over V , u v V : u v u and v belong to the same tree partitions V into equivalance classes ■ ■ The disjoint-set data structure is ideal for implementing the equivalance classes (See Chapter 21) 23 CPTR 430 Algorithms Minimum Spanning Trees
Recommend
More recommend