minimum spanning trees
play

Minimum spanning trees One of the most famous greedy algorithms - PowerPoint PPT Presentation

Minimum spanning trees One of the most famous greedy algorithms (actually rather family of greedy algorithms). Given undirected graph G = ( V, E ) , connected Weight function w : E I R For simplicity, all edge weights distinct


  1. Minimum spanning trees One of the most famous greedy algorithms (actually rather family of greedy algorithms). • Given undirected graph G = ( V, E ) , connected • Weight function w : E → I R • For simplicity, all edge weights distinct • Spanning tree: tree that connects all vertices, hence n = | V | ver- tices and n − 1 edges • MST T : w ( T ) = � ( u,v ) ∈ T w ( u, v ) minimized What for? • Chip design • Communication infrastructure in networks Minimum Spanning Trees 1

  2. 6 4 5 9 2 14 10 15 8 3 Minimum Spanning Trees 2

  3. Growing a minimum spanning tree First, “generic” algorithm. It manages set of edges A , maintains in- variant: Prior to each iteration, A is subset of some MST. At each step, determine edge ( u, v ) that can be added to A , i.e. with- out violating invariant , i.e., A ∪ { ( u, v ) } is also subset of some MST. We then call ( u, v ) a safe edge . 1: A ← ∅ 2: while A does not form a spanning tree do find an edge ( u, v ) that is safe for A 3: A ← A ∪ { ( u, v ) } 4: 5: end while Minimum Spanning Trees 3

  4. We use invariant as follows: Initialization. After line 1, A triv. satisfies invariant. Maintenance. Loop in lines 2–5 maintains invariant by adding only safe edges. Termination. All edges added to A are in a MST, so A must be MST. Minimum Spanning Trees 4

  5. How to recognize safe edges? Definitions 1. A cut ( S, V − S ) of an undir. graph G = ( V, E ) is a partition of V . 2. An edge ( u, v ) crosses cut ( S, V − S ) if one endpoint is in S , the other one in V − S . 3. A cut respects a set A ⊆ E if no edge in A crosses the cut. 4. An edge is a light edge crossing a cut if its weight is the minimum of any edge crossing the cut. Theorem 1. Let A be a subset of E that is included in some MST for G , let ( S, V − S ) be any cut of G that respects A , let ( u, v ) be a light edge crossing ( S, V − S ) . Then, ( u, v ) is safe for A . Minimum Spanning Trees 5

  6. Proof. • let T be a MST that includes A and assume T does not include ( u, v ) • Goal: construct MST T ′ that includes A ∪ { ( u, v ) } . This shows that ( u, v ) is safe (by def.) • ( u, v ) �∈ T , so there must be path p = ( u = w 1 → w 2 → · · · → w k = v ) with ( w i , w i +1 ) ∈ T for 1 ≤ i < k • u and v are on opposite sides of cut ( S, V − S ) , so there must be at least one edge ( x, y ) of T crossing cut • ( x, y ) is not in A because A respects cut • ( x, y ) is on unique path from u to v , so removing ( x, y ) breaks T into two components Minimum Spanning Trees 6

  7. • adding ( u, v ) reconnects them to form new spanning tree T ′ = T − { ( x, y ) } ∪ { ( u, v ) } • ( u, v ) is light edge crossing ( S, V − S ) , and ( x, y ) also crosses this cut, therefore w ( u, v ) ≤ w ( x, y ) and W ( T ′ ) = w ( T ) − w ( x, y ) + w ( u, v ) ≤ W ( T ) Hence T ′ is MST. • A ⊆ T and ( x, y ) �∈ A (this was because ( x, y ) crosses cut but A respects cut), so A ⊆ T ′ • Since ( u, v ) ∈ T ′ , we have A ∪ { ( u, v ) } ⊆ T ′ and ( u, v ) is safe for A q.e.d. Minimum Spanning Trees 7

  8. We see: • at any point, graph G A = ( V, A ) is a forest with components being trees • Any safe edge ( u, v ) for A connects distinct components of G A , since A ∪ { ( u, v ) } must be acyclic • main loop is executed | V | − 1 times (one iteration for every edge of the resulting MST) We’ll see Kruskal’s and Prim’s algorithms, they differ in how they specify rules to determine safe edges. In Kruskal’s, A is a forest ; in Prim’s, A is a single tree . Minimum Spanning Trees 8

  9. The following is going to be used later on. Corollary. Let A be subset of E that is included in some MST for G , let C = ( V C , E C ) be a connected component (tree) in forest G A = ( V, A ) . If ( u, v ) is a light edge connecting C to some other component in G A , then ( u, v ) is safe for A . Proof. The cut ( V C , V − V C ) respects A ( A defines the components of G A ), and ( u, v ) is a light edge for this cut. Therefore, ( u, v ) is safe for A . Minimum Spanning Trees 9

  10. Kruskal’s algorithm Kruskal’s adds in each step an edge of least possible weight that con- nects two different trees. If C 1 , C 2 denote the two trees that are connected by ( u, v ) , then since ( u, v ) must be light edge connecting C 1 to some other tree, the corol- lary implies that ( u, v ) is safe for C 1 . Minimum Spanning Trees 10

  11. Implementation This particular implementation uses Disjoint-Set data structure. Each set contains vertices in a tree of the current forest. • Make-Set ( u ) initializes a new set containing just vertex u . • Find-Set ( u ) returns representative element from set that contains u (so we can check whether two vertices u, v belong to same tree). • Union ( u, v ) combines two trees (the one containing U with the one containing v ). Minimum Spanning Trees 11

  12. The Algorithm Given: graph G = ( V, E ) , weight function w on E 1: A ← ∅ 2: for each vertex v ∈ V [ G ] do Make-Set ( u ) 3: 4: end for 5: sort edges of E into nondecr. order by weight w 6: for each edge ( u, v ) ∈ E , taken in nondecreasing order by weight w do if Find-Set ( u ) � = Find-Set ( v ) then 7: A ← A ∪ { ( u, v ) } 8: Union ( u, v ) 9: end if 10: 11: end for 12: return A Minimum Spanning Trees 12

  13. • initializing A takes O (1) • sorting edges takes O ( E log E ) • main for loop performs O ( E ) Find-Set and Union operations; along with | V | Make-Set operation, this takes O (( V + E )) · O (log E ) = O ( E log V ) (see Section 21.4 in book) • Disjoint-Set operations take O ( E log V ) • Total running time of Kruskal’s is O ( E log V ) Minimum Spanning Trees 13

  14. 6 4 6 4 5 9 5 9 2 2 14 14 10 10 15 15 8 8 3 3 6 4 6 4 5 9 5 9 2 2 14 14 10 10 15 15 8 8 3 3 6 4 6 4 5 9 5 9 2 2 14 14 10 10 15 15 8 8 3 3 6 4 6 4 5 9 5 9 2 2 14 14 10 10 15 15 8 8 3 3 6 4 6 4 5 9 5 9 2 2 14 14 10 10 15 15 8 8 3 3

  15. Prim’s algorithm • A always forms a single tree (as opposed to a forest like in Kruskal’s). • Tree start from single (arbitrary) vertex r (root) and grows until it spans all of V . • At each step, a light edge is added to tree A that connects A to isolated vertex of G A = ( V, A ) . • By corollary, this adds only edges safe for A , hence on termination, A is MST. Minimum Spanning Trees 15

  16. Implementation Key is efficiently selecting new edges . In this implementation, ver- tices not in the tree reside in min-priority queue Q based on a key field: For v ∈ V , key [ v ] is minimum weight of any edge connecting v to a vertex in tree A ; key [ v ] = ∞ if there is no such edge. Field π [ v ] names parent of v in tree. During algorithm, A is kept implicitly as A = { ( v, π [ v ]) : v ∈ V − { r } − Q } When algorithm terminates, min-priority queue Q is empty, MST A for G is thus A = { ( v, π [ v ]) : v ∈ V − { r }} Minimum Spanning Trees 16

  17. Given: graph G = ( V, E ) , weight function w , root vertex r ∈ V 1: for each u ∈ V do key [ u ] ← ∞ 2: π [ u ] ← NIL 3: 4: end for 5: key [ r ] ← 0 6: Q ← V 7: while Q � = ∅ do u ← Extract-Min ( Q ) {w.r.t. key} 8: for each v ∈ adj [ u ] do 9: if v ∈ Q and w ( u, v ) < key [ v ] then 10: π [ v ] ← u 11: key [ v ] ← w ( u, v ) 12: end if 13: end for 14: 15: end while Minimum Spanning Trees 17

  18. Lines 1–6 • set key of each vertex to ∞ (except root r whose key is set to 0 so that it will be processed first) • set parent of each vertex to NIL • initialize min-priority queue Q Algorithm maintains three-part loop invariant : 1. A = { ( v, π [ v ]) : v ∈ V − { r } − Q } 2. Vertices already placed into MST are those in V − Q 3. For all v ∈ Q , if π [ v ] � = NIL, then key [ v ] < ∞ and key [ v ] is weight of a light edge ( v, π [ v ]) connecting v to some vertex already placed into MST Minimum Spanning Trees 18

  19. Line 8 identifies u ∈ Q incident on a light edge crossing cut ( V − Q, Q ) , expect in first iteration, in which u = r due to line 5. Removing u from Q adds it to set V − Q of vertices in the tree, adding ( u, π [ u ]) to A . The for loop of lines 9–14 updates the key and π fields of every vertex v adjacent to u but not in the tree. This maintains third part of loop invariant. Minimum Spanning Trees 19

  20. Running time Depends on how min-priority queue Q is implemented. If as binary min-heap (Chapter 6 in book), then • can use Build-Min-Heap for initialization in lines 1–6, time O ( V ) • body of while loop is executed O ( V ) times, each Extract-Min takes O (log V ) , hence total time for all calls to Extract-Min is O ( V log V ) • for loop in lines 9–14 is executed O ( E ) times altogether, since sum of lengths of all adjacency lists is 2 | E | . Minimum Spanning Trees 20

  21. 6 4 6 4 5 9 5 9 2 2 14 14 10 10 root 15 15 8 8 3 3 6 4 6 4 5 9 5 9 2 2 14 14 10 10 15 15 8 8 3 3 6 4 6 4 5 9 5 9 2 2 14 14 10 10 15 15 8 8 3 3 6 4 5 9 2 14 10 15 8 3 Minimum Spanning Trees 21

Recommend


More recommend