undirected connected weighted graph g w
play

Undirected connected weighted graph ( G , W ) Input: An MST of G - PowerPoint PPT Presentation

Minimum Spanning Trees ( G , W ) undirected connected weighted graph Definition 11.1 A minimum spanning tree (MST) of G is a connected spanning subgraph T of G of minimum weight. The minimum spanning tree problem : Undirected connected weighted


  1. Minimum Spanning Trees ( G , W ) undirected connected weighted graph Definition 11.1 A minimum spanning tree (MST) of G is a connected spanning subgraph T of G of minimum weight. The minimum spanning tree problem : Undirected connected weighted graph ( G , W ) Input: An MST of G Output: A&DS Lecture 11 1 Mary Cryan

  2. Kruskal’s Algorithm A different approach to computing MSTs. A forest is a graph whose connected components are trees. Idea Starting from the spanning forest without any edges, repeatedly add edges of minimum weight until the forest becomes a tree. Algorithm K RUSKAL ( G , W ) 1. F ← ∅ 2. for all e ∈ E in the order of increasing weight do if the endpoints of e belong to different connected 3. components of ( V, F ) then F ← F ∪ { e } 4. 5. return tree with edge set F A&DS Lecture 11 2 Mary Cryan

  3. Correctness of Kruskal’s algorithm 1. Throughout the execution of K RUSKAL , ( V, F ) remains a spanning forest. Proof: ( V, F ) is a spanning subgraph because the vertex set is V . It always remains a forest because edges with endpoints in different connected components never induce a cycle. 2. Eventually, ( V, F ) will be connected and thus a spanning tree. Proof: Suppose that after the complete execution of the loop, ( V, F ) has a connected component ( V 1 , F 1 ) with V 1 � = V . Since G is connected, there is an edge e ∈ E with exactly one endpoint in V 1 . This edge would have been added to F when being processed in the loop, so this can never happen. 3. Throughout the execution of K RUSKAL , ( V, F ) is contained in some MST of G . Proof: Similar to the proof of the corresponding statement for Prim’s algorithm. A&DS Lecture 11 3 Mary Cryan

  4. Data Structures for Disjoint Sets • A disjoint set data structure maintains a collection S = { S 1 , . . . , S k } of disjoint sets . • The sets are dynamic , i.e., they may change over time. • Each set S i is identified by some representative , which is some member of that set. Operations: • M AKE -S ET ( x ) : Creates new set whose only member is x . The representative is x . • U NION ( x, y ) : Unites set S x containing x and set S y containing y into a new set S and removes S x and S y from the collection. • F IND -S ET ( x ) : Returns representative of the set holding x . A&DS Lecture 11 4 Mary Cryan

  5. Implementation of Kruskal’s Algorithm Algorithm K RUSKAL ( G , W ) 1. F ← 0 2. for all vertices v of G do M AKE -S ET ( v ) 3. 4. sort edges of G into non-decreasing order by weight 5. for all edges ( u, v ) of G in non-decreasing order by weight do if F IND -S ET ( u ) � = F IND -S ET ( v ) then 6. F ← F ∪ { ( u, v ) } 7. U NION ( u, v ) 8. 9. return F A&DS Lecture 11 5 Mary Cryan

  6. Analysis of K RUSKAL Let n be the number of vertices and m the number of edges of the input graph • Line 1, line 9: Θ ( 1 ) • Loop in Lines 2–3: Θ ( n · T M AKE -S ET ( n )) • Line 4 : Θ ( m lg m ) � � • Loop in Lines 5–8: Θ m · T F IND -S ET ( n ) + n · T U NION ( n ) . Overall: � �� � � � Θ n T M AKE -S ET ( n ) + T U NION ( n ) + m lg m + T F IND -S ET ( n ) With efficient implementation of disjoint set this amounts to T ( n, m ) = Θ ( m lg m ) . A&DS Lecture 11 6 Mary Cryan

  7. Amortized Analysis We want to analyse the following: Given a sequence of m “Disjoint Set” operations (M AKE -S ET , U NION , F IND -S ET ), bound the total running time to perform a sequence of m operations . • Usually parametrize in terms of n (no. of M AKE -S ET operations), not just m . • (Of course) the bound we get will depend on the particular data structure/implementation we design for this problem. A&DS Lecture 11 7 Mary Cryan

  8. Linked List Implementation of Disjoint Sets Each element represented by a pointer to a cell: x Use a linked list for each set. Representative of the set is at the head of the list. Each cell has a pointer direct to the representative (head of the list). A&DS Lecture 11 8 Mary Cryan

  9. Example Linked list representation of { a, f } , { b } , { g, c, e } , { d } : a f b g c e d The representatives are a , b , g and d . A&DS Lecture 11 9 Mary Cryan

  10. Analysis of Linked List Implementation M AKE -S ET : constant time. F IND -S ET : constant time. U NION : Naive implementation of U NION ( x, y ) appends x ’s list onto end of y ’s list. Assumption: Representative y of each set has attribute last [ y ] : a pointer to last cell of y ’s list. (Not shown in diagrams.) Snag: have to update “representative pointer” in each cell of x ’s list to point to the representative (head) of y ’s list. Cost is: Θ ( length of x ’s list ) . A&DS Lecture 11 10 Mary Cryan

  11. Example (cont’d) a f b g c e d U NION ( g, b ) A&DS Lecture 11 11 Mary Cryan

  12. Conventions for Further Analysis Express running time in terms of: n : the number of M AKE -S ET operations, m : the number of M AKE -S ET , U NION and F IND -S ET operations. Note 1. After n − 1 U NION operations only one set remains. 2. m ≥ n . A&DS Lecture 11 12 Mary Cryan

  13. A nasty example Take: n = ⌈ m/2 ⌉ + 1 , q = m − n = ⌊ m/2 ⌋ − 1 . Elements: x 1 ,x 2 ,...,x n . Operation Number of objects updated M AKE -S ET ( x 1 ) 1 M AKE -S ET ( x 2 ) 1 . . . . . . M AKE -S ET ( x n ) 1 U NION ( x 1 , x 2 ) 1 U NION ( x 2 , x 3 ) 2 U NION ( x 3 , x 4 ) 3 . . . . . . U NION ( x q − 1 , x q ) q − 1 Θ ( m 2 ) Total A&DS Lecture 11 13 Mary Cryan

  14. The Weighted-Union Heuristics Idea Record length of each list. To execute U NION ( x, y ) append shorter list to longer one (breaking ties arbitrarily). Theorem 11.2 Using the linked-list representation of disjoint sets and the weighted-union heuristic, a sequence of m M AKE -S ET , U NION & F IND -S ET operations, n of which are M AKE -S ET operations, takes O ( m + n lg n ) time. Crucial Proof Idea: Each element can appear at most lg n times in the shorter list of a U NION . (because if x is in the shortest list, the UNION operation doubles the size of x ’s list). A&DS Lecture 11 14 Mary Cryan

  15. The Forest Implementation of Disjoint-Sets Each set represented by a rooted tree: g b i e a f h c d A&DS Lecture 11 15 Mary Cryan

  16. Basic Operations M AKE -S ET : Constant time. F IND -S ET : Follow pointers to root. (Path followed is called the find path .) Cost proportional to height of tree. U NION : Naive strategy: root of tree of x made to point to that of y . Cost proportional to height of x ’s tree plus the height of y ’s tree. Not faster than linked list implementation. A&DS Lecture 11 16 Mary Cryan

  17. Improving the Running Time General Strategy Keep trees low. Two Heuristics 1. Union-by-Rank: Attach lower tree to the root of higher tree in U NION 2. Path Compression: Update tree during F IND -S ET operations. A&DS Lecture 11 17 Mary Cryan

  18. The Union-by-Rank Heuristic At each node x maintain a variable rank [ x ] , such that if x is the root of its tree, rank [ x ] is the height of this tree. In executing U NION ( x, y ) make the tree of smaller rank point to the one with larger rank. (Break ties arbitrarily.) A&DS Lecture 11 18 Mary Cryan

  19. g f b d rank [ f ] = 0 rank [ d ] = 0 a e c U NION ( f, d ) rank [ g ] = 1 rank [ d ] = 1 g b d e c a f U NION ( f, g ) rank [ g ] = 2 g b e c a d f A&DS Lecture 11 19 Mary Cryan

  20. The Height of the Trees Lemma 11.3 The height of a tree (in our datastructure) is at most lg ( size of the tree ) . Proof By induction on the number of U NION operations it is proved that a tree of height h has size at least 2 h . • As long as there are no U NION s, all trees have height 0 and contain 1 = 2 0 node. • Suppose U NION ( x,y ) is executed. Let r x and r y be the roots of the trees of x and y , resp. Case 1: rank ( r x ) < rank ( r y ) . Then r x becomes child of r y , and the height remains unchanged, but the number of nodes increases. Case 2: rank ( r x ) > rank ( r y ) . Analogously. Case 3: rank ( r x ) = rank ( r y ) . Then height increase by one and the size of the new tree is size ( r x ) + size ( r y ) ≥ 2 rank ( r x ) + 2 rank ( r y ) = 2 rank ( r x )+ 1 , which is the height of the new tree.

  21. Running Time with Union-by-Rank Only M AKE -S ET : constant time. F IND -S ET : Bounded by rank: O ( log n ) . U NION : Bounded by rank: O ( log n ) . Bottom line ( m operations, of which n are M AKE -S ET ): O ( m log n ) . NOT any better than Linked-Lists with Weighted union heuristic. A&DS Lecture 11 21 Mary Cryan

  22. The Path-Compression Heuristics Idea When performing F IND -S ET ( x ) make each vertex on find path point to root. Implementation Algorithm F IND -S ET ( x ) 1. if x � = π ( x ) then π ( x ) ← F IND -S ET ( π ( x )) 2. 3. return π ( x ) A&DS Lecture 11 22 Mary Cryan

  23. Example f e d f c c a d b e b a F IND -S ET ( a ) A&DS Lecture 11 23 Mary Cryan

  24. The Ackermann Function Ackerman’s Function Function A : N × N → N defined by 2 j for j ≥ 1 A ( 1, j ) = for i ≥ 2 A ( i, 1 ) = A ( i − 1, 2 ) A ( i, j ) = A ( i − 1, A ( i, j − 1 )) for i, j ≥ 2 Other variants exist—last line of definition is crucial common point. Inverse Not true mathematical inverse—grows as slowly as A ( i, j ) grows fast : α ( m, n ) = min { i ≥ 1 | A ( i, ⌊ m/n ⌋ ) > lg n } . A&DS Lecture 11 24 Mary Cryan

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend