410 likes | 488 Views
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.
E N D
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 2 Initialize array 3 F C 10 A 7 3 4 8 18 4 B D 9 10 H 25 2 3 G E 7
2 Start with any node, say D 3 F C 10 A 7 3 4 8 18 4 B D 9 10 H 25 2 3 G E 7
Update distances of adjacent, unselected nodes 2 3 F C 10 A 7 3 4 8 18 4 B D 9 10 H 25 2 3 G E 7
Select node with minimum distance 2 3 F C 10 A 7 3 4 8 18 4 B D 9 10 H 25 2 3 G E 7
Update distances of adjacent, unselected nodes 2 3 F C 10 A 7 3 4 8 18 4 B D 9 10 H 25 2 3 G E 7
Select node with minimum distance 2 3 F C 10 A 7 3 4 8 18 4 B D 9 10 H 25 2 3 G E 7
Update distances of adjacent, unselected nodes 2 3 F C 10 A 7 3 4 8 18 4 B D 9 10 H 25 2 3 G E 7
Select node with minimum distance 2 3 F C 10 A 7 3 4 8 18 4 B D 9 10 H 25 2 3 G E 7
Update distances of adjacent, unselected nodes 2 3 F C 10 A 7 3 4 8 18 4 B D 9 10 H 25 2 3 G E 7
Select node with minimum distance 2 3 F C 10 A 7 3 4 8 18 4 B D 9 10 H 25 2 3 G E 7
Update distances of adjacent, unselected nodes 2 3 F C 10 A 7 3 4 8 18 4 B D 9 10 H 25 2 3 G E 7 Table entries unchanged
Select node with minimum distance 2 3 F C 10 A 7 3 4 8 18 4 B D 9 10 H 25 2 3 G E 7
Update distances of adjacent, unselected nodes 2 3 F C 10 A 7 3 4 8 18 4 B D 9 10 H 25 2 3 G E 7
Select node with minimum distance 2 3 F C 10 A 7 3 4 8 18 4 B D 9 10 H 25 2 3 G E 7
Update distances of adjacent, unselected nodes 2 3 F C 10 A 7 3 4 8 18 4 B D 9 10 H 25 2 3 G E 7 Table entries unchanged
Select node with minimum distance 2 3 F C 10 A 7 3 4 8 18 4 B D 9 10 H 25 2 3 G E 7
Cost of Minimum Spanning Tree = dv = 21 2 3 F C 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 Consider an undirected, weight graph 3 F C 10 A 4 3 4 8 6 5 B D 4 4 H 1 2 3 G E 3
Sort the edges by increasing edge weight 3 F C 10 A 4 3 4 8 6 5 B D 4 4 H 1 2 3 G E 3
Select first |V|–1 edges which do not generate a cycle 3 F C 10 A 4 3 4 8 6 5 B D 4 4 H 1 2 3 G E 3
Select first |V|–1 edges which do not generate a cycle 3 F C 10 A 4 3 4 8 6 5 B D 4 4 H 1 2 3 G E 3
Select first |V|–1 edges which do not generate a cycle 3 F C 10 A 4 3 4 8 6 5 B D 4 4 H 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 10 A 4 3 4 8 6 5 B D 4 4 H 1 2 3 G E 3
Select first |V|–1 edges which do not generate a cycle 3 F C 10 A 4 3 4 8 6 5 B D 4 4 H 1 2 3 G E 3
Select first |V|–1 edges which do not generate a cycle 3 F C 10 A 4 3 4 8 6 5 B D 4 4 H 1 2 3 G E 3
Select first |V|–1 edges which do not generate a cycle 3 F C 10 A 4 3 4 8 6 5 B D 4 4 H 1 2 3 G E 3
Select first |V|–1 edges which do not generate a cycle 3 F C 10 A 4 3 4 8 6 5 B D 4 4 H 1 2 3 G E 3
Select first |V|–1 edges which do not generate a cycle 3 F C 10 A 4 3 4 8 6 5 B D 4 4 H 1 2 3 G E 3
Select first |V|–1 edges which do not generate a cycle 3 F C 10 A 4 3 4 8 6 5 B D 4 4 H 1 2 3 G E 3
Select first |V|–1 edges which do not generate a cycle 3 F C 10 A 4 3 4 8 6 5 B D 4 4 H 1 2 3 G E 3
Select first |V|–1 edges which do not generate a cycle 3 F C 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)