Minimum Spanning Tree Todays announcements: PA3 due 29 Nov 23:59 - - PowerPoint PPT Presentation

minimum spanning tree
SMART_READER_LITE
LIVE PREVIEW

Minimum Spanning Tree Todays announcements: PA3 due 29 Nov 23:59 - - PowerPoint PPT Presentation

Minimum Spanning Tree Todays announcements: PA3 due 29 Nov 23:59 Final Exam, 10 Dec 12:00, OSBO A Todays Plan: Kruskals algorithm Spanning tree: a subgraph of a connected graph G that contains all vertices of G (spans G )


slide-1
SLIDE 1

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 E C D B F 8 4 2 7 1 2 5 9 3

Minimum-weight spanning tree?

1 / 6

slide-2
SLIDE 2

Depth-First Search code

DFS(G) unmark all vertices unmark all edges for each vertex v if v is unmarked DFS(G,v) # calls to DFS. for loop Total running time: Adj.Matrix? DFS(G, v) mark v for each edge (v,w) if w is unmarked mark (v,w) TREE DFS(G,w) else mark (v,w) BACK

2 / 6

A E C D B H F G

slide-3
SLIDE 3

Kruskal’s Algorithm for Minimum Spanning Trees

a b c d e f g h 5 15 5 2 16 17 12 13 16 10 11 2 8 9 4 12

(a, d) (e, h) (f , g) (a, b) (b, d) (g, e) (g, h) (e, c) (c, h) (e, f ) (f , c) (d, e) (b, c) (c, d) (a, f ) (d, f )

3 / 6

slide-4
SLIDE 4

Kruskal’s Algorithm for Minimum Spanning Trees

a b c d e f g h 5 15 5 2 16 17 12 13 16 10 11 2 8 9 4 12

◮ Start with a spanning subgraph T with no edges. ◮ Start with each vertex in its own set. ◮ For all edges (u, v) in order of weight

Add (u, v) to T unless it forms a cycle in T (i.e. u and v in the same set) Union(u, v) (a, d) (e, h) (f , g) (a, b) (b, d) (g, e) (g, h) (e, c) (c, h) (e, f ) (f , c) (d, e) (b, c) (c, d) (a, f ) (d, f )

4 / 6

a b c d e f g h

slide-5
SLIDE 5

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

slide-6
SLIDE 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 Ki chosen by Kruskal before iteration i is in some MST of G. Base i = 1: True since K1 = ∅. IH: Assume true for Ki−1. (We’ll show it’s true for Ki.)

◮ Let T be an MST that contains Ki−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 Ki−1. ◮ Since T is an MST and T − f + e is a spanning tree, w(e) ≥ w(f ). ◮ Since Ki−1 + f ⊆ T, Ki−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 Ki = Ki−1 + e.

6 / 6