minimum spanning trees and kruskal s algorithm
play

Minimum Spanning Trees and Kruskal's Algorithm Steve Tanimoto - PDF document

2/5/2016 Minimum Spanning Trees The minimum-spanning-tree problem Given a weighted undirected graph, compute a spanning tree of minimum weight CSE373: Data Structures and Algorithms Minimum Spanning Trees and Kruskal's Algorithm Steve


  1. 2/5/2016 Minimum Spanning Trees The minimum-spanning-tree problem – Given a weighted undirected graph, compute a spanning tree of minimum weight CSE373: Data Structures and Algorithms Minimum Spanning Trees and Kruskal's Algorithm Steve Tanimoto Winter 2016 This lecture material represents the work of multiple instructors at the University of Washington. Thank you to all who have contributed! Winter 2016 CSE 373: Data Structures & Algorithms 2 Minimum Spanning Tree Algorithms Kruskal’s Algorithm • Kruskal’s Algorithm for Minimum Spanning Tree construction – A greedy algorithm. – Uses a priority queue. – Uses the UNION-FIND technique. • Prim’s Algorithm for Minimum Spanning Tree – Related to Dijkstra’s Algorithm for shortest paths. – Both based on expanding cloud of known vertices (basically using a priority queue instead of a DFS stack) Winter 2016 CSE 373: Data Structures & Algorithms 3 Winter 2016 CSE 373: Data Structures & Algorithms 4 Kruskal’s Algorithm Pseudocode Kruskal’s Example 2 Edges in sorted order: B A 1. Sort edges by weight (better: put in min-heap) 1 1: (A,D), (C,D), (B,E), (D,E) 2. Put each node in its own subset (of a UNION-FIND instance). 1 5 2: (A,B), (C,F), (A,C) 2 E 3. While output size < |V|-1 1 3: (E,G) D 1 – Consider next smallest edge (u,v) 3 5 C 5: (D,G), (B,D) – if find(u) and find(v) indicate u and v are in different 6 G 6: (D,F) 2 10 sets 10: (F,G) F • output (u,v) • Perform union(find(u),find(v)) Output: Recall invariant: u and v in same set if and only if connected in output-so-far Note: At each step, the UNION-FIND subsets correspond to the trees in a forest. Winter 2016 CSE 373: Data Structures & Algorithms 5 Winter 2016 CSE 373: Data Structures & Algorithms 6 1

  2. 2/5/2016 Kruskal’s Example Kruskal’s Example 2 Edges in sorted order: 2 Edges in sorted order: B B A 1 A 1 1: (A,D), (C,D), (B,E), (D,E) 1: (A,D), (C,D), (B,E), (D,E) 1 1 5 2: (A,B), (C,F), (A,C) 5 2: (A,B), (C,F), (A,C) 2 E 2 E 1 1 3: (E,G) 3: (E,G) D D 1 3 1 3 5 5 C 5: (D,G), (B,D) C 5: (D,G), (B,D) 6 6 G 6: (D,F) G 6: (D,F) 2 2 10 10 10: (F,G) 10: (F,G) F F Output: (A,D) Output: (A,D), (C,D) Note: At each step, the union/find sets are the trees in the forest Note: At each step, the union/find sets are the trees in the forest Winter 2016 CSE 373: Data Structures & Algorithms 7 Winter 2016 CSE 373: Data Structures & Algorithms 8 Kruskal’s Example Kruskal’s Example 2 Edges in sorted order: 2 Edges in sorted order: B B A A 1 1 1: (A,D), (C,D), (B,E), (D,E) 1: (A,D), (C,D), (B,E), (D,E) 1 1 5 2: (A,B), (C,F), (A,C) 5 2: (A,B), (C,F), (A,C) 2 E 2 E 1 1 3: (E,G) 3: (E,G) D D 1 1 3 3 5 5 C C 5: (D,G), (B,D) 5: (D,G), (B,D) 6 6 G G 6: (D,F) 6: (D,F) 2 2 10 10 10: (F,G) 10: (F,G) F F Output: (A,D), (C,D), (B,E) Output: (A,D), (C,D), (B,E), (D,E) Note: At each step, the union/find sets are the trees in the forest Note: At each step, the union/find sets are the trees in the forest Winter 2016 CSE 373: Data Structures & Algorithms 9 Winter 2016 CSE 373: Data Structures & Algorithms 10 Kruskal’s Example Kruskal’s Example 2 Edges in sorted order: 2 Edges in sorted order: B B A A 1 1 1: (A,D), (C,D), (B,E), (D,E) 1: (A,D), (C,D), (B,E), (D,E) 1 1 5 2: (A,B), (C,F), (A,C) 5 2: (A,B), (C,F), (A,C) 2 E 2 E 1 1 3: (E,G) 3: (E,G) D D 1 1 3 3 5 5 C C 5: (D,G), (B,D) 5: (D,G), (B,D) 6 6 G 6: (D,F) G 6: (D,F) 2 2 10 10 10: (F,G) 10: (F,G) F F Output: (A,D), (C,D), (B,E), (D,E) Output: (A,D), (C,D), (B,E), (D,E), (C,F) Note: At each step, the union/find sets are the trees in the forest Note: At each step, the union/find sets are the trees in the forest Winter 2016 CSE 373: Data Structures & Algorithms 11 Winter 2016 CSE 373: Data Structures & Algorithms 12 2

  3. 2/5/2016 Kruskal’s Example Kruskal’s Example 2 Edges in sorted order: 2 Edges in sorted order: B B A 1 A 1 1: (A,D), (C,D), (B,E), (D,E) 1: (A,D), (C,D), (B,E), (D,E) 1 1 5 2: (A,B), (C,F), (A,C) 5 2: (A,B), (C,F), (A,C) 2 E 2 E 1 1 3: (E,G) 3: (E,G) D D 1 3 1 3 5 5 C 5: (D,G), (B,D) C 5: (D,G), (B,D) 6 6 G 6: (D,F) G 6: (D,F) 2 2 10 10 10: (F,G) 10: (F,G) F F Output: (A,D), (C,D), (B,E), (D,E), (C,F) Output: (A,D), (C,D), (B,E), (D,E), (C,F), (E,G) Note: At each step, the union/find sets are the trees in the forest Note: At each step, the union/find sets are the trees in the forest Winter 2016 CSE 373: Data Structures & Algorithms 13 Winter 2016 CSE 373: Data Structures & Algorithms 14 Kruskal’s Algorithm Kruskal’s Algorithm Analysis Idea: Grow a forest out of edges that do not grow a cycle. (This is List the edges in similar to the maze-construction problem: knocking down a wall was order of size: B 5 essentially adding an edge that connected adjacent cells.) C ED 2 – But now consider the edges in order by weight AB 3 3 So: AE 4 4 6 8 CD 4 – Sort edges: O ( |E| log |E| ) BC 5 – Iterate through edges using union-find for cycle detection almost 8 EF 5 O ( |E| ) A D CF 6 F 7 AF 7 Somewhat better: BF 8 5 – Floyd’s algorithm to build min-heap with edges O ( |E| ) CF 8 4 – Iterate through edges using UNION-FIND for cycle prevention and 2 deleteMin to get next edge O ( |E| log |E| ) – Not better worst-case asymptotically, but often stops long before E considering all edges. Winter 2016 CSE 373: Data Structures & Algorithms 15 Winter 2016 CSE 373: Data Structures & Algorithms 16 Kruskal’s Algorithm Kruskal’s Algorithm Select the edge Select the next with min cost minimum cost B 5 B 5 edge that does not C C ED 2 create a cycle 3 3 ED 2 4 4 6 6 8 8 AB 3 8 8 A A D D F F 7 7 5 5 4 4 2 2 E E Winter 2016 CSE 373: Data Structures & Algorithms 17 Winter 2016 CSE 373: Data Structures & Algorithms 18 3

  4. 2/5/2016 Kruskal’s Algorithm Kruskal’s Algorithm Select the next Select the next minimum cost minimum cost B 5 B 5 edge that does not edge that does not C C create a cycle create a cycle 3 3 ED 2 ED 2 4 4 6 6 8 8 AB 3 AB 3 CD 4 (or AE 4) CD 4 8 8 AE 4 A A D D F F 7 7 5 5 4 4 2 2 E E Winter 2016 CSE 373: Data Structures & Algorithms 19 Winter 2016 CSE 373: Data Structures & Algorithms 20 Kruskal’s Algorithm Kruskal’s Algorithm Select the next All vertices have been minimum cost connected. B 5 B 5 edge that does not C C create a cycle The solution is 3 3 ED 2 ED 2 4 4 6 6 8 8 AB 3 AB 3 CD 4 CD 4 8 8 AE 4 AE 4 A A D D BC 5 – forms a cycle EF 5 F F 7 7 EF 5 5 5 Total weight of tree: 18 4 4 2 2 E E Winter 2016 CSE 373: Data Structures & Algorithms 21 Winter 2016 CSE 373: Data Structures & Algorithms 22 4

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