Minimum Spanning Tree Today’s announcements: ◮ PA3 due 29 Nov 23:59 ◮ Final Exam, 10 Dec 12:00, OSBO A Today’s Plan: ◮ Kruskal’s algorithm Spanning tree: a subgraph of a connected graph G that ◮ contains all vertices of G (spans G ) and ◮ is connected and acyclic (is a tree). A 8 4 2 7 1 B C D 2 3 9 5 E F Minimum-weight spanning tree? 1 / 6
Depth-First Search code B A F C H DFS(G) DFS(G, v) D E G unmark all vertices mark v unmark all edges for each edge (v,w) for each vertex v if w is unmarked if v is unmarked DFS(G,v) mark (v,w) TREE DFS(G,w) else # calls to DFS . mark (v,w) BACK for loop Total running time: Adj.Matrix? 2 / 6
Kruskal’s Algorithm for Minimum Spanning Trees ( a , d ) ( e , h ) 15 c ( f , g ) b 16 10 5 5 ( a , b ) 2 d 13 ( b , d ) 11 a e ( g , e ) 17 12 12 16 2 8 ( g , h ) f 4 h g 9 ( e , c ) ( c , h ) ( e , f ) ( f , c ) ( d , e ) ( b , c ) ( c , d ) ( a , f ) ( d , f ) 3 / 6
Kruskal’s Algorithm for Minimum Spanning Trees ( a , d ) ( e , h ) 15 c ( f , g ) b 16 10 5 5 ( a , b ) g a c e f b d h 2 d 13 ( b , d ) 11 a e ( g , e ) 17 12 12 16 2 8 ( g , h ) f 4 h g 9 ( e , c ) ( c , h ) ◮ Start with a spanning subgraph T with no edges. ( e , f ) ◮ Start with each vertex in its own set. ( f , c ) ( d , e ) ◮ For all edges ( u , v ) in order of weight ( b , c ) Add ( u , v ) to T unless it forms a cycle in T ( c , d ) (i.e. u and v in the same set) ( a , f ) Union( u , v ) ( d , f ) 4 / 6
Running time of Kruskal’s Algorithm sort edges first OR buildHeap | E | times: Pick the lowest cost edge. remove from sorted array OR removeMin | E | times If u and v are not already connected, connect them. Disjoint set find Disjoint set union 5 / 6
Proof of Correctness Part I: Kruskal finds a spanning tree. Why? Part II: Kruskal finds a minimum one. Loop invariant: The set of edges K i chosen by Kruskal before iteration i is in some MST of G . Base i = 1: True since K 1 = ∅ . IH: Assume true for K i − 1 . (We’ll show it’s true for K i .) ◮ Let T be an MST that contains K i − 1 . ◮ If edge e added at iteration i is in T or no edge is added, there’s nothing to prove. ◮ Otherwise, since T is a spanning tree, T + e has one cycle C . ◮ Let f be an edge in C but not in K i − 1 . ◮ Since T is an MST and T − f + e is a spanning tree, w ( e ) ≥ w ( f ). ◮ Since K i − 1 + f ⊆ T , K i − 1 + f does not contain a cycle so Kruskal must not have considered f yet, implying that w ( e ) ≤ w ( f ). ◮ Thus, T − f + e is an MST containing K i = K i − 1 + e . 6 / 6
Recommend
More recommend