Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

1 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

2 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

3 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

4 Prim’s Algorithm Similar to Dijkstra’s Algorithm except that d v records edge weights, not path lengths

5 Walk-Through Initialize array Kdvdv pvpv AF  BF  CF  DF  EF  FF  GF  HF  4 25 A H B F E D C G 7 2 10 18 3 4 3 7 8 9 3 10 2

6 4 25 A H B F E D C G 7 2 10 18 3 4 3 7 8 9 3 10 Start with any node, say D Kdvdv pvpv A B C DT0  E F G H 2

7 4 25 A H B F E D C G 7 2 10 18 3 4 3 7 8 9 3 10 Update distances of adjacent, unselected nodes Kdvdv pvpv A B C 3D DT0  E25D F18D G2D H 2

8 4 25 A H B F E D C G 7 2 10 18 3 4 3 7 8 9 3 10 Select node with minimum distance Kdvdv pvpv A B C 3D DT0  E25D F18D GT2D H 2

9 4 25 A H B F E D C G 7 2 10 18 3 4 3 7 8 9 3 10 Update distances of adjacent, unselected nodes Kdvdv pvpv A B C 3D DT0  E7G F18D GT2D H3G 2

10 4 25 A H B F E D C G 7 2 10 18 3 4 3 7 8 9 3 10 Select node with minimum distance Kdvdv pvpv A B CT 3D DT0  E7G F18D GT2D H3G 2

11 4 25 A H B F E D C G 7 2 10 18 3 4 3 7 8 9 3 10 Update distances of adjacent, unselected nodes Kdvdv pvpv A B4C CT 3D DT0  E7G F3C GT2D H3G 2

12 4 25 A H B F E D C G 7 2 10 18 3 4 3 7 8 9 3 10 Select node with minimum distance Kdvdv pvpv A B4C CT 3D DT0  E7G FT3C GT2D H3G 2

13 4 25 A H B F E D C G 7 2 10 18 3 4 3 7 8 9 3 10 Update distances of adjacent, unselected nodes Kdvdv pvpv A10F B4C CT 3D DT0  E2F FT3C GT2D H3G 2

14 4 25 A H B F E D C G 7 2 10 18 3 4 3 7 8 9 3 10 Select node with minimum distance Kdvdv pvpv A10F B4C CT 3D DT0  ET2F FT3C GT2D H3G 2

15 4 25 A H B F E D C G 7 2 10 18 3 4 3 7 8 9 3 10 Update distances of adjacent, unselected nodes Kdvdv pvpv A10F B4C CT 3D DT0  ET2F FT3C GT2D H3G 2 Table entries unchanged

16 4 25 A H B F E D C G 7 2 10 18 3 4 3 7 8 9 3 10 Select node with minimum distance Kdvdv pvpv A10F B4C CT 3D DT0  ET2F FT3C GT2D HT3G 2

17 4 25 A H B F E D C G 7 2 10 18 3 4 3 7 8 9 3 10 Update distances of adjacent, unselected nodes Kdvdv pvpv A4H B4C CT 3D DT0  ET2F FT3C GT2D HT3G 2

18 4 25 A H B F E D C G 7 2 10 18 3 4 3 7 8 9 3 10 Select node with minimum distance Kdvdv pvpv AT4H B4C CT 3D DT0  ET2F FT3C GT2D HT3G 2

19 4 25 A H B F E D C G 7 2 10 18 3 4 3 7 8 9 3 10 Update distances of adjacent, unselected nodes Kdvdv pvpv AT4H B4C CT 3D DT0  ET2F FT3C GT2D HT3G 2 Table entries unchanged

20 4 25 A H B F E D C G 7 2 10 18 3 4 3 7 8 9 3 10 Select node with minimum distance Kdvdv pvpv AT4H BT4C CT 3D DT0  ET2F FT3C GT2D HT3G 2

21 4 A H B F E D C G 2 3 4 3 3 Cost of Minimum Spanning Tree =  d v = 21 Kdvdv pvpv AT4H BT4C CT 3D DT0  ET2F FT3C GT2D HT3G 2 Done

22 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

23 Walk-Through Consider an undirected, weight graph 5 1 A H B F E D C G 3 2 4 6 3 4 3 4 8 4 3 10

24 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 3 2 4 6 3 4 3 4 8 4 3 10 edgedvdv (B,E)4 (B,F)4 (B,H)4 (A,H)5 (D,F)6 (A,B)8 (A,F)10

25 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 3 2 4 6 3 4 3 4 8 4 3 10 edgedvdv (B,E)4 (B,F)4 (B,H)4 (A,H)5 (D,F)6 (A,B)8 (A,F)10

26 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 3 2 4 6 3 4 3 4 8 4 3 10 edgedvdv (B,E)4 (B,F)4 (B,H)4 (A,H)5 (D,F)6 (A,B)8 (A,F)10

27 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 3 2 4 6 3 4 3 4 8 4 3 10 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

28 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 3 2 4 6 3 4 3 4 8 4 3 10 edgedvdv (B,E)4 (B,F)4 (B,H)4 (A,H)5 (D,F)6 (A,B)8 (A,F)10

29 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 3 2 4 6 3 4 3 4 8 4 3 10 edgedvdv (B,E)4 (B,F)4 (B,H)4 (A,H)5 (D,F)6 (A,B)8 (A,F)10

30 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 3 2 4 6 3 4 3 4 8 4 3 10 edgedvdv (B,E)4 (B,F)4 (B,H)4 (A,H)5 (D,F)6 (A,B)8 (A,F)10

31 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 3 2 4 6 3 4 3 4 8 4 3 10 edgedvdv (B,E)4 (B,F)4 (B,H)4 (A,H)5 (D,F)6 (A,B)8 (A,F)10

32 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 3 2 4 6 3 4 3 4 8 4 3 10 edgedvdv (B,E)4  (B,F)4 (B,H)4 (A,H)5 (D,F)6 (A,B)8 (A,F)10

33 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 3 2 4 6 3 4 3 4 8 4 3 10 edgedvdv (B,E)4  (B,F)4  (B,H)4 (A,H)5 (D,F)6 (A,B)8 (A,F)10

34 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 3 2 4 6 3 4 3 4 8 4 3 10 edgedvdv (B,E)4  (B,F)4  (B,H)4  (A,H)5 (D,F)6 (A,B)8 (A,F)10

35 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 3 2 4 6 3 4 3 4 8 4 3 10 edgedvdv (B,E)4  (B,F)4  (B,H)4  (A,H)5  (D,F)6 (A,B)8 (A,F)10

36 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 2 3 3 3 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

37 Detecting Cycles Use Disjoint Sets (Chapter 8)

38

39

40

41


Download ppt "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."

Similar presentations


Ads by Google