Download presentation
Presentation is loading. Please wait.
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 dv records edge weights, not path lengths
5
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
6
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
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
8
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
9
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
10
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
11
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
12
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
13
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
14
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
15
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
16
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
17
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
18
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
19
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
20
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
21
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
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 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
24
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
25
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
26
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
27
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
28
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
29
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
30
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
31
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
32
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
33
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
34
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
35
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
36
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
37
Detecting Cycles Use Disjoint Sets (Chapter 8)
Similar presentations
© 2024 SlidePlayer.com Inc.
All rights reserved.