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

minimum spanning trees and kruskal s algorithm
SMART_READER_LITE
LIVE PREVIEW

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


slide-1
SLIDE 1

2/5/2016 1

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!

Minimum Spanning Trees

The minimum-spanning-tree problem – Given a weighted undirected graph, compute a spanning tree of minimum weight

Winter 2016 2 CSE 373: Data Structures & Algorithms

Minimum Spanning Tree Algorithms

  • 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 3 CSE 373: Data Structures & Algorithms

Kruskal’s Algorithm

Winter 2016 4 CSE 373: Data Structures & Algorithms

Kruskal’s Algorithm Pseudocode

1. Sort edges by weight (better: put in min-heap) 2. Put each node in its own subset (of a UNION-FIND instance). 3. While output size < |V|-1 – Consider next smallest edge (u,v) – if find(u) and find(v) indicate u and v are in different sets

  • utput (u,v)
  • Perform union(find(u),find(v))

Recall invariant: u and v in same set if and only if connected in output-so-far

Winter 2016 5 CSE 373: Data Structures & Algorithms

Kruskal’s Example

Winter 2016 6 CSE 373: Data Structures & Algorithms

A B C D F E G 2 1 2 5 1 1 1 2 6 5 3 10 Edges in sorted order: 1: (A,D), (C,D), (B,E), (D,E) 2: (A,B), (C,F), (A,C) 3: (E,G) 5: (D,G), (B,D) 6: (D,F) 10: (F,G) Output: Note: At each step, the UNION-FIND subsets correspond to the trees in a forest.

slide-2
SLIDE 2

2/5/2016 2

Kruskal’s Example

Winter 2016 7 CSE 373: Data Structures & Algorithms

A B C D F E G 2 1 2 5 1 1 1 2 6 5 3 10 Edges in sorted order: 1: (A,D), (C,D), (B,E), (D,E) 2: (A,B), (C,F), (A,C) 3: (E,G) 5: (D,G), (B,D) 6: (D,F) 10: (F,G) Output: (A,D) Note: At each step, the union/find sets are the trees in the forest

Kruskal’s Example

Winter 2016 8 CSE 373: Data Structures & Algorithms

A B C D F E G 2 1 2 5 1 1 1 2 6 5 3 10 Edges in sorted order: 1: (A,D), (C,D), (B,E), (D,E) 2: (A,B), (C,F), (A,C) 3: (E,G) 5: (D,G), (B,D) 6: (D,F) 10: (F,G) Output: (A,D), (C,D) Note: At each step, the union/find sets are the trees in the forest

Kruskal’s Example

Winter 2016 9 CSE 373: Data Structures & Algorithms

A B C D F E G 2 1 2 5 1 1 1 2 6 5 3 10 Edges in sorted order: 1: (A,D), (C,D), (B,E), (D,E) 2: (A,B), (C,F), (A,C) 3: (E,G) 5: (D,G), (B,D) 6: (D,F) 10: (F,G) Output: (A,D), (C,D), (B,E) Note: At each step, the union/find sets are the trees in the forest

Kruskal’s Example

Winter 2016 10 CSE 373: Data Structures & Algorithms

A B C D F E G 2 1 2 5 1 1 1 2 6 5 3 10 Edges in sorted order: 1: (A,D), (C,D), (B,E), (D,E) 2: (A,B), (C,F), (A,C) 3: (E,G) 5: (D,G), (B,D) 6: (D,F) 10: (F,G) Output: (A,D), (C,D), (B,E), (D,E) Note: At each step, the union/find sets are the trees in the forest

Kruskal’s Example

Winter 2016 11 CSE 373: Data Structures & Algorithms

A B C D F E G 2 1 2 5 1 1 1 2 6 5 3 10 Edges in sorted order: 1: (A,D), (C,D), (B,E), (D,E) 2: (A,B), (C,F), (A,C) 3: (E,G) 5: (D,G), (B,D) 6: (D,F) 10: (F,G) Output: (A,D), (C,D), (B,E), (D,E) Note: At each step, the union/find sets are the trees in the forest

Kruskal’s Example

Winter 2016 12 CSE 373: Data Structures & Algorithms

A B C D F E G 2 1 2 5 1 1 1 2 6 5 3 10 Edges in sorted order: 1: (A,D), (C,D), (B,E), (D,E) 2: (A,B), (C,F), (A,C) 3: (E,G) 5: (D,G), (B,D) 6: (D,F) 10: (F,G) 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

slide-3
SLIDE 3

2/5/2016 3

Kruskal’s Example

Winter 2016 13 CSE 373: Data Structures & Algorithms

A B C D F E G 2 1 2 5 1 1 1 2 6 5 3 10 Edges in sorted order: 1: (A,D), (C,D), (B,E), (D,E) 2: (A,B), (C,F), (A,C) 3: (E,G) 5: (D,G), (B,D) 6: (D,F) 10: (F,G) 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

Kruskal’s Example

Winter 2016 14 CSE 373: Data Structures & Algorithms

A B C D F E G 2 1 2 5 1 1 1 2 6 5 3 10 Edges in sorted order: 1: (A,D), (C,D), (B,E), (D,E) 2: (A,B), (C,F), (A,C) 3: (E,G) 5: (D,G), (B,D) 6: (D,F) 10: (F,G) 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

Kruskal’s Algorithm Analysis

Idea: Grow a forest out of edges that do not grow a cycle. (This is similar to the maze-construction problem: knocking down a wall was essentially adding an edge that connected adjacent cells.) – But now consider the edges in order by weight So: – Sort edges: O(|E|log |E|) – Iterate through edges using union-find for cycle detection almost O(|E|) Somewhat better: – Floyd’s algorithm to build min-heap with edges O(|E|) – Iterate through edges using UNION-FIND for cycle prevention and deleteMin to get next edge O(|E|log|E|) – Not better worst-case asymptotically, but often stops long before considering all edges.

Winter 2016 15 CSE 373: Data Structures & Algorithms

A F B C D E 2 7 4 5 8 6 4 5 3 8

List the edges in

  • rder of size:

ED 2 AB 3 AE 4 CD 4 BC 5 EF 5 CF 6 AF 7 BF 8 CF 8

Kruskal’s Algorithm

Winter 2016 16 CSE 373: Data Structures & Algorithms

Select the edge with min cost ED 2

A F B C D E 2 7 4 5 8 6 4 5 3 8

Kruskal’s Algorithm

Winter 2016 17 CSE 373: Data Structures & Algorithms

Select the next minimum cost edge that does not create a cycle ED 2 AB 3

A F B C D E 2 7 4 5 8 6 4 5 3 8

Kruskal’s Algorithm

Winter 2016 18 CSE 373: Data Structures & Algorithms

slide-4
SLIDE 4

2/5/2016 4

Select the next minimum cost edge that does not create a cycle ED 2 AB 3 CD 4 (or AE 4)

A F B C D E 2 7 4 5 8 6 4 5 3 8

Kruskal’s Algorithm

Winter 2016 19 CSE 373: Data Structures & Algorithms

Select the next minimum cost edge that does not create a cycle ED 2 AB 3 CD 4 AE 4

A F B C D E 2 7 4 5 8 6 4 5 3 8

Kruskal’s Algorithm

Winter 2016 20 CSE 373: Data Structures & Algorithms

Select the next minimum cost edge that does not create a cycle ED 2 AB 3 CD 4 AE 4 BC 5 – forms a cycle EF 5

A F B C D E 2 7 4 5 8 6 4 5 3 8

Kruskal’s Algorithm

Winter 2016 21 CSE 373: Data Structures & Algorithms

All vertices have been connected. The solution is ED 2 AB 3 CD 4 AE 4 EF 5 Total weight of tree: 18

A F B C D E 2 7 4 5 8 6 4 5 3 8

Kruskal’s Algorithm

Winter 2016 22 CSE 373: Data Structures & Algorithms