Download presentation
Presentation is loading. Please wait.
Published byHortense Morris Modified over 9 years ago
1
Minimum Spanning Tree Dr. Bernard Chen Ph.D. University of Central Arkansas Fall 2008
2
Minimum Spanning Tree
3
Example of MST
4
Problem: Laying Telephone Wire Central office
5
Wiring: Naïve Approach Central office Expensive!
6
Wiring: Better Approach Central office Minimize the total length of wire connecting the customers
7
Growing an MST: general idea GENERIC-MST(G,w) 1. A {} 2. while A does not form a spanning tree 3. do find an edge (u,v) that is safe for A 4. A A U {(u,v)} 5. return A
8
Tricky part How do you find a safe edge? This safe edge is part of the minimum spanning tree
9
Algorithms for MST Prim’s Grow a MST by adding a single edge at a time Kruskal’s Choose a smallest edge and add it to the forest If an edge is formed a cycle, it is rejected
10
Prim’s greedy algorithm Start from some (any) vertex. Build up spanning tree T, one vertex at a time. At each step, add to T the lowest-weight edge in G that does not create a cycle. Stop when all vertices in G are touched
11
Prim’s MST algorithm
12
Example A B D G C F I E H 23 4 5 7 8 4 3 1 6 9 2 7
13
Min Edge Pick a root A B D G C F I E H 23 4 5 7 8 4 3 1 6 9 2 7 = in heap
14
Min Edge = 1 A B D G C F I E H 23 4 5 7 8 4 3 1 6 9 2 7
15
Min Edge = 2 A B D G C F I E H 23 4 5 7 8 4 3 1 6 9 2 7
16
A B D G C F I E H 23 4 5 7 8 4 3 1 6 9 2 7
17
Min Edge = 3 A B D G C F I E H 23 4 5 7 8 4 3 1 6 9 2 7
18
Min Edge = 4 A B D G C F I E H 23 4 5 7 8 4 3 1 6 9 2 7
19
Min Edge = 3 A B D G C F I E H 23 4 5 7 8 4 3 1 6 9 2 7
20
Min Edge = 4 A B D G C F I E H 23 4 5 7 8 4 3 1 6 9 2 7
21
Min Edge = 6 A B D G C F I E H 23 4 5 7 8 4 3 1 6 9 2 7
22
Example II
23
Kruskal’s Algorithm Choose the smallest edge and add it to a forest Keep connecting components until all vertices connected If an edge would form a cycle, it is rejected.
24
Example A B D G C F I E H 23 4 5 7 8 4 3 1 6 9 2 7
25
Min Edge = 1 A B D G C F I E H 23 4 5 7 8 4 3 1 6 9 2 7
26
Min Edge = 2 A B D G C F I E H 23 4 5 7 8 4 3 1 6 9 2 7
27
A B D G C F I E H 23 4 5 7 8 4 3 1 6 9 2 7
28
Min Edge = 3 A B D G C F I E H 23 4 5 7 8 4 3 1 6 9 2 7 Now have 2 disjoint components: ABFG and CH
29
Min Edge = 3 A B D G C F I E H 23 4 5 7 8 4 3 1 6 9 2 7
30
Min Edge = 4 A B D G C F I E H 23 4 5 7 8 4 3 1 6 9 2 7 Two components now merged into one.
31
Min Edge = 4 A B D G C F I E H 23 4 5 7 8 4 3 1 6 9 2 7
32
Min Edge = 5 A B D G C F I E H 23 4 5 7 8 4 3 1 6 9 2 7 Rejected due to a cycle BFGB
33
Min Edge = 6 A B D G C F I E H 23 4 5 7 8 4 3 1 6 9 2 7
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.