350 likes | 541 Views
Minimum Spanning Tree. Dr. Bernard Chen Ph.D. University of Central Arkansas Fall 2008. Minimum Spanning Tree. Example of MST. Problem: Laying Telephone Wire. Central office. Wiring: Naïve Approach. Central office. Expensive!. Wiring: Better Approach. Central office.
E N D
Minimum Spanning Tree Dr. Bernard Chen Ph.D. University of Central Arkansas Fall 2008
Problem: Laying Telephone Wire Central office
Wiring: Naïve Approach Central office Expensive!
Wiring: Better Approach Central office Minimize the total length of wire connecting the customers
Growing an MST: general idea • GENERIC-MST(G,w) • A{} • while A does not form a spanning tree • do find an edge (u,v) that is safe for A • A A U {(u,v)} • return A
Tricky part • How do you find a safe edge? • This safe edge is part of the minimum spanning tree
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
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
Example C 4 1 B F 2 7 3 3 2 E A I 4 6 7 5 H D 9 8 G
= in heap Min EdgePick a root C 4 1 B F 2 7 3 3 2 E A I 4 6 7 5 H D 9 8 G
Min Edge = 1 C 4 1 B F 2 7 3 3 2 E A I 4 6 7 5 H D 9 8 G
Min Edge = 2 C 4 1 B F 2 7 3 3 2 E A I 4 6 7 5 H D 9 8 G
Min Edge = 2 C 4 1 B F 2 7 3 3 2 E A I 4 6 7 5 H D 9 8 G
Min Edge = 3 C 4 1 B F 2 7 3 3 2 E A I 4 6 7 5 H D 9 8 G
Min Edge = 4 C 4 1 B F 2 7 3 3 2 E A I 4 6 7 5 H D 9 8 G
Min Edge = 3 C 4 1 B F 2 7 3 3 2 E A I 4 6 7 5 H D 9 8 G
Min Edge = 4 C 4 1 B F 2 7 3 3 2 E A I 4 6 7 5 H D 9 8 G
Min Edge = 6 C 4 1 B F 2 7 3 3 2 E A I 4 6 7 5 H D 9 8 G
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.
Example C 4 1 B F 2 7 3 3 2 E A I 4 6 7 5 H D 9 8 G
Min Edge = 1 C 4 1 B F 2 7 3 3 2 E A I 4 6 7 5 H D 9 8 G
Min Edge = 2 C 4 1 B F 2 7 3 3 2 E A I 4 6 7 5 H D 9 8 G
Min Edge = 2 C 4 1 B F 2 7 3 3 2 E A I 4 6 7 5 H D 9 8 G
Min Edge = 3 C 4 1 B F 2 7 3 3 2 E A I 4 6 7 5 H D Now have 2 disjoint components: ABFG and CH 9 8 G
Min Edge = 3 C 4 1 B F 2 7 3 3 2 E A I 4 6 7 5 H D 9 8 G
Min Edge = 4 C 4 1 B F 2 7 3 3 2 E A I 4 6 7 5 H D Two components now merged into one. 9 8 G
Min Edge = 4 C 4 1 B F 2 7 3 3 2 E A I 4 6 7 5 H D 9 8 G
Min Edge = 5 C 4 1 B F Rejected due to a cycle BFGB 2 7 3 3 2 E A I 4 6 7 5 H D 9 8 G
Min Edge = 6 C 4 1 B F 2 7 3 3 2 E A I 4 6 7 5 H D 9 8 G