Minimum Spanning Trees Text Read Weiss, §9.5 Prim’s Algorithm Weiss §9.5.1 Similar to Dijkstra’s Algorithm Kruskal’s Algorithm Weiss §9.5.2 Focuses on edges, rather than nodes
Definition A Minimum Spanning Tree (MST) is a subgraph of an undirected graph such that the subgraph spans (includes) all nodes, is connected, is acyclic, and has minimum total edge weight
Algorithm Characteristics Both Prim’s and Kruskal’s Algorithms work with undirected graphs Both work with weighted and unweighted graphs but are more interesting when edges are weighted Both are greedy algorithms that produce optimal solutions
Prim’s Algorithm Similar to Dijkstra’s Algorithm except that dv records edge weights, not path lengths
Walk-Through F C A B D H G E Initialize array K dv pv A F B C D E 2 Initialize array 3 F C K dv pv A F B C D E G H 10 A 7 3 8 4 18 4 B D 9 H 10 25 2 3 G E 7
F C A B D H G E Start with any node, say D K dv pv A B C D T E F G H 2 Start with any node, say D 3 F C K dv pv A B C D T E F G H 10 A 7 3 8 4 18 4 B D 9 H 10 25 2 3 G E 7
Update distances of adjacent, unselected nodes 2 3 F C K dv pv A B C 3 D T E 25 F 18 G 2 H 10 A 7 3 8 4 18 4 B D 9 H 10 25 2 3 G E 7
Select node with minimum distance 2 3 F C K dv pv A B C 3 D T E 25 F 18 G 2 H 10 A 7 3 8 4 18 4 B D 9 H 10 25 2 3 G E 7
Update distances of adjacent, unselected nodes 2 3 F C K dv pv A B C 3 D T E 7 G F 18 2 H 10 A 7 3 8 4 18 4 B D 9 H 10 25 2 3 G E 7
Select node with minimum distance 2 3 F C K dv pv A B C T 3 D E 7 G F 18 2 H 10 A 7 3 8 4 18 4 B D 9 H 10 25 2 3 G E 7
Update distances of adjacent, unselected nodes 2 3 F C K dv pv A B 4 C T 3 D E 7 G F 2 H 10 A 7 3 8 4 18 4 B D 9 H 10 25 2 3 G E 7
Select node with minimum distance 2 3 F C K dv pv A B 4 C T 3 D E 7 G F 2 H 10 A 7 3 8 4 18 4 B D 9 H 10 25 2 3 G E 7
Update distances of adjacent, unselected nodes 2 3 F C K dv pv A 10 F B 4 C T 3 D E 2 G H 10 A 7 3 8 4 18 4 B D 9 H 10 25 2 3 G E 7
Select node with minimum distance 2 3 F C K dv pv A 10 F B 4 C T 3 D E 2 G H 10 A 7 3 8 4 18 4 B D 9 H 10 25 2 3 G E 7
F C A B D H G E Update distances of adjacent, unselected nodes 2 3 F C K dv pv A 10 F B 4 C T 3 D E 2 G H 10 A 7 3 8 4 18 4 B D 9 H 10 25 2 3 G E 7 Table entries unchanged
Select node with minimum distance 2 3 F C K dv pv A 10 F B 4 C T 3 D E 2 G H 10 A 7 3 8 4 18 4 B D 9 H 10 25 2 3 G E 7
Update distances of adjacent, unselected nodes 2 3 F C K dv pv A 4 H B C T 3 D E 2 F G 10 A 7 3 8 4 18 4 B D 9 H 10 25 2 3 G E 7
Select node with minimum distance 2 3 F C K dv pv A T 4 H B C 3 D E 2 F G 10 A 7 3 8 4 18 4 B D 9 H 10 25 2 3 G E 7
F C A B D H G E Update distances of adjacent, unselected nodes 2 3 F C K dv pv A T 4 H B C 3 D E 2 F G 10 A 7 3 8 4 18 4 B D 9 H 10 25 2 3 G E 7 Table entries unchanged
Select node with minimum distance 2 3 F C K dv pv A T 4 H B C 3 D E 2 F G 10 A 7 3 8 4 18 4 B D 9 H 10 25 2 3 G E 7
Cost of Minimum Spanning Tree = dv = 21 3 F C K dv pv A T 4 H B C 3 D E 2 F G A 3 4 4 B D H 2 3 G E Done
Kruskal’s Algorithm Work with edges, rather than nodes Two steps: Sort edges by increasing edge weight Select the first |V| – 1 edges that do not generate a cycle
Walk-Through F C A B D H G E Consider an undirected, weight graph 3 10 4 3 8 4 6 5 B D 4 H 4 1 2 3 G E 3
F C A B D H G E Sort the edges by increasing edge weight edge dv (D,E) 3 F C edge dv (D,E) 1 (D,G) 2 (E,G) 3 (C,D) (G,H) (C,F) (B,C) 4 edge dv (B,E) 4 (B,F) (B,H) (A,H) 5 (D,F) 6 (A,B) 8 (A,F) 10 10 A 4 3 8 4 6 5 B D 4 H 4 1 2 3 G E 3
Select first |V|–1 edges which do not generate a cycle 3 F C edge dv (D,E) 1 (D,G) 2 (E,G) 3 (C,D) (G,H) (C,F) (B,C) 4 edge dv (B,E) 4 (B,F) (B,H) (A,H) 5 (D,F) 6 (A,B) 8 (A,F) 10 10 A 4 3 8 4 6 5 B D 4 H 4 1 2 3 G E 3
Select first |V|–1 edges which do not generate a cycle 3 F C edge dv (D,E) 1 (D,G) 2 (E,G) 3 (C,D) (G,H) (C,F) (B,C) 4 edge dv (B,E) 4 (B,F) (B,H) (A,H) 5 (D,F) 6 (A,B) 8 (A,F) 10 10 A 4 3 8 4 6 5 B D 4 H 4 1 2 3 G E 3
F C A B D H G E Select first |V|–1 edges which do not generate a cycle 3 F C edge dv (D,E) 1 (D,G) 2 (E,G) 3 (C,D) (G,H) (C,F) (B,C) 4 edge dv (B,E) 4 (B,F) (B,H) (A,H) 5 (D,F) 6 (A,B) 8 (A,F) 10 10 A 4 3 8 4 6 5 B D 4 H 4 1 2 3 G E 3 Accepting edge (E,G) would create a cycle
Select first |V|–1 edges which do not generate a cycle 3 F C edge dv (D,E) 1 (D,G) 2 (E,G) 3 (C,D) (G,H) (C,F) (B,C) 4 edge dv (B,E) 4 (B,F) (B,H) (A,H) 5 (D,F) 6 (A,B) 8 (A,F) 10 10 A 4 3 8 4 6 5 B D 4 H 4 1 2 3 G E 3
Select first |V|–1 edges which do not generate a cycle 3 F C edge dv (D,E) 1 (D,G) 2 (E,G) 3 (C,D) (G,H) (C,F) (B,C) 4 edge dv (B,E) 4 (B,F) (B,H) (A,H) 5 (D,F) 6 (A,B) 8 (A,F) 10 10 A 4 3 8 4 6 5 B D 4 H 4 1 2 3 G E 3
Select first |V|–1 edges which do not generate a cycle 3 F C edge dv (D,E) 1 (D,G) 2 (E,G) 3 (C,D) (G,H) (C,F) (B,C) 4 edge dv (B,E) 4 (B,F) (B,H) (A,H) 5 (D,F) 6 (A,B) 8 (A,F) 10 10 A 4 3 8 4 6 5 B D 4 H 4 1 2 3 G E 3
Select first |V|–1 edges which do not generate a cycle 3 F C edge dv (D,E) 1 (D,G) 2 (E,G) 3 (C,D) (G,H) (C,F) (B,C) 4 edge dv (B,E) 4 (B,F) (B,H) (A,H) 5 (D,F) 6 (A,B) 8 (A,F) 10 10 A 4 3 8 4 6 5 B D 4 H 4 1 2 3 G E 3
Select first |V|–1 edges which do not generate a cycle 3 F C edge dv (D,E) 1 (D,G) 2 (E,G) 3 (C,D) (G,H) (C,F) (B,C) 4 edge dv (B,E) 4 (B,F) (B,H) (A,H) 5 (D,F) 6 (A,B) 8 (A,F) 10 10 A 4 3 8 4 6 5 B D 4 H 4 1 2 3 G E 3
Select first |V|–1 edges which do not generate a cycle 3 F C edge dv (D,E) 1 (D,G) 2 (E,G) 3 (C,D) (G,H) (C,F) (B,C) 4 edge dv (B,E) 4 (B,F) (B,H) (A,H) 5 (D,F) 6 (A,B) 8 (A,F) 10 10 A 4 3 8 4 6 5 B D 4 H 4 1 2 3 G E 3
Select first |V|–1 edges which do not generate a cycle 3 F C edge dv (D,E) 1 (D,G) 2 (E,G) 3 (C,D) (G,H) (C,F) (B,C) 4 edge dv (B,E) 4 (B,F) (B,H) (A,H) 5 (D,F) 6 (A,B) 8 (A,F) 10 10 A 4 3 8 4 6 5 B D 4 H 4 1 2 3 G E 3
Select first |V|–1 edges which do not generate a cycle 3 F C edge dv (D,E) 1 (D,G) 2 (E,G) 3 (C,D) (G,H) (C,F) (B,C) 4 edge dv (B,E) 4 (B,F) (B,H) (A,H) 5 (D,F) 6 (A,B) 8 (A,F) 10 10 A 4 3 8 4 6 5 B D 4 H 4 1 2 3 G E 3
Select first |V|–1 edges which do not generate a cycle 3 F C edge dv (D,E) 1 (D,G) 2 (E,G) 3 (C,D) (G,H) (C,F) (B,C) 4 edge dv (B,E) 4 (B,F) (B,H) (A,H) 5 (D,F) 6 (A,B) 8 (A,F) 10 A 3 4 5 B D H 1 2 } 3 G E not considered Done Total Cost = dv = 21
Detecting Cycles Use Disjoint Sets (Chapter 8)