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 d v records edge weights, not path lengths
Walk-Through Initialize array Kdvdv pvpv AF BF CF DF EF FF GF HF 4 25 A H B F E D C G
4 25 A H B F E D C G Start with any node, say D Kdvdv pvpv A B C DT0 E F G H 2
4 25 A H B F E D C G Update distances of adjacent, unselected nodes Kdvdv pvpv A B C 3D DT0 E25D F18D G2D H 2
4 25 A H B F E D C G Select node with minimum distance Kdvdv pvpv A B C 3D DT0 E25D F18D GT2D H 2
4 25 A H B F E D C G Update distances of adjacent, unselected nodes Kdvdv pvpv A B C 3D DT0 E7G F18D GT2D H3G 2
4 25 A H B F E D C G Select node with minimum distance Kdvdv pvpv A B CT 3D DT0 E7G F18D GT2D H3G 2
4 25 A H B F E D C G Update distances of adjacent, unselected nodes Kdvdv pvpv A B4C CT 3D DT0 E7G F3C GT2D H3G 2
4 25 A H B F E D C G Select node with minimum distance Kdvdv pvpv A B4C CT 3D DT0 E7G FT3C GT2D H3G 2
4 25 A H B F E D C G Update distances of adjacent, unselected nodes Kdvdv pvpv A10F B4C CT 3D DT0 E2F FT3C GT2D H3G 2
4 25 A H B F E D C G Select node with minimum distance Kdvdv pvpv A10F B4C CT 3D DT0 ET2F FT3C GT2D H3G 2
4 25 A H B F E D C G Update distances of adjacent, unselected nodes Kdvdv pvpv A10F B4C CT 3D DT0 ET2F FT3C GT2D H3G 2 Table entries unchanged
4 25 A H B F E D C G Select node with minimum distance Kdvdv pvpv A10F B4C CT 3D DT0 ET2F FT3C GT2D HT3G 2
4 25 A H B F E D C G Update distances of adjacent, unselected nodes Kdvdv pvpv A4H B4C CT 3D DT0 ET2F FT3C GT2D HT3G 2
4 25 A H B F E D C G Select node with minimum distance Kdvdv pvpv AT4H B4C CT 3D DT0 ET2F FT3C GT2D HT3G 2
4 25 A H B F E D C G Update distances of adjacent, unselected nodes Kdvdv pvpv AT4H B4C CT 3D DT0 ET2F FT3C GT2D HT3G 2 Table entries unchanged
4 25 A H B F E D C G Select node with minimum distance Kdvdv pvpv AT4H BT4C CT 3D DT0 ET2F FT3C GT2D HT3G 2
4 A H B F E D C G Cost of Minimum Spanning Tree = d v = 21 Kdvdv pvpv AT4H BT4C CT 3D DT0 ET2F FT3C GT2D HT3G 2 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 5 1 A H B F E D C G
Sort the edges by increasing edge weight edgedvdv (D,E)1 (D,G)2 (E,G)3 (C,D)3 (G,H)3 (C,F)3 (B,C)4 5 1 A H B F E D C G edgedvdv (B,E)4 (B,F)4 (B,H)4 (A,H)5 (D,F)6 (A,B)8 (A,F)10
Select first |V|–1 edges which do not generate a cycle edgedvdv (D,E)1 (D,G)2 (E,G)3 (C,D)3 (G,H)3 (C,F)3 (B,C)4 5 1 A H B F E D C G edgedvdv (B,E)4 (B,F)4 (B,H)4 (A,H)5 (D,F)6 (A,B)8 (A,F)10
Select first |V|–1 edges which do not generate a cycle edgedvdv (D,E)1 (D,G)2 (E,G)3 (C,D)3 (G,H)3 (C,F)3 (B,C)4 5 1 A H B F E D C G edgedvdv (B,E)4 (B,F)4 (B,H)4 (A,H)5 (D,F)6 (A,B)8 (A,F)10
Select first |V|–1 edges which do not generate a cycle edgedvdv (D,E)1 (D,G)2 (E,G)3 (C,D)3 (G,H)3 (C,F)3 (B,C)4 5 1 A H B F E D C G edgedvdv (B,E)4 (B,F)4 (B,H)4 (A,H)5 (D,F)6 (A,B)8 (A,F)10 Accepting edge (E,G) would create a cycle
Select first |V|–1 edges which do not generate a cycle edgedvdv (D,E)1 (D,G)2 (E,G)3 (C,D)3 (G,H)3 (C,F)3 (B,C)4 5 1 A H B F E D C G edgedvdv (B,E)4 (B,F)4 (B,H)4 (A,H)5 (D,F)6 (A,B)8 (A,F)10
Select first |V|–1 edges which do not generate a cycle edgedvdv (D,E)1 (D,G)2 (E,G)3 (C,D)3 (G,H)3 (C,F)3 (B,C)4 5 1 A H B F E D C G edgedvdv (B,E)4 (B,F)4 (B,H)4 (A,H)5 (D,F)6 (A,B)8 (A,F)10
Select first |V|–1 edges which do not generate a cycle edgedvdv (D,E)1 (D,G)2 (E,G)3 (C,D)3 (G,H)3 (C,F)3 (B,C)4 5 1 A H B F E D C G edgedvdv (B,E)4 (B,F)4 (B,H)4 (A,H)5 (D,F)6 (A,B)8 (A,F)10
Select first |V|–1 edges which do not generate a cycle edgedvdv (D,E)1 (D,G)2 (E,G)3 (C,D)3 (G,H)3 (C,F)3 (B,C)4 5 1 A H B F E D C G edgedvdv (B,E)4 (B,F)4 (B,H)4 (A,H)5 (D,F)6 (A,B)8 (A,F)10
Select first |V|–1 edges which do not generate a cycle edgedvdv (D,E)1 (D,G)2 (E,G)3 (C,D)3 (G,H)3 (C,F)3 (B,C)4 5 1 A H B F E D C G edgedvdv (B,E)4 (B,F)4 (B,H)4 (A,H)5 (D,F)6 (A,B)8 (A,F)10
Select first |V|–1 edges which do not generate a cycle edgedvdv (D,E)1 (D,G)2 (E,G)3 (C,D)3 (G,H)3 (C,F)3 (B,C)4 5 1 A H B F E D C G edgedvdv (B,E)4 (B,F)4 (B,H)4 (A,H)5 (D,F)6 (A,B)8 (A,F)10
Select first |V|–1 edges which do not generate a cycle edgedvdv (D,E)1 (D,G)2 (E,G)3 (C,D)3 (G,H)3 (C,F)3 (B,C)4 5 1 A H B F E D C G edgedvdv (B,E)4 (B,F)4 (B,H)4 (A,H)5 (D,F)6 (A,B)8 (A,F)10
Select first |V|–1 edges which do not generate a cycle edgedvdv (D,E)1 (D,G)2 (E,G)3 (C,D)3 (G,H)3 (C,F)3 (B,C)4 5 1 A H B F E D C G edgedvdv (B,E)4 (B,F)4 (B,H)4 (A,H)5 (D,F)6 (A,B)8 (A,F)10
Select first |V|–1 edges which do not generate a cycle edgedvdv (D,E)1 (D,G)2 (E,G)3 (C,D)3 (G,H)3 (C,F)3 (B,C)4 5 1 A H B F E D C G edgedvdv (B,E)4 (B,F)4 (B,H)4 (A,H)5 (D,F)6 (A,B)8 (A,F)10 Done Total Cost = d v = 21 4 } not considered
Detecting Cycles Use Disjoint Sets (Chapter 8)