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 Minimize the total length of wire connecting the customers
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
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
Prim’s MST algorithm
Example A B D G C F I E H
Min Edge Pick a root A B D G C F I E H = in heap
Min Edge = 1 A B D G C F I E H
Min Edge = 2 A B D G C F I E H
A B D G C F I E H
Min Edge = 3 A B D G C F I E H
Min Edge = 4 A B D G C F I E H
Min Edge = 3 A B D G C F I E H
Min Edge = 4 A B D G C F I E H
Min Edge = 6 A B D G C F I E H
Example II
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 A B D G C F I E H
Min Edge = 1 A B D G C F I E H
Min Edge = 2 A B D G C F I E H
A B D G C F I E H
Min Edge = 3 A B D G C F I E H Now have 2 disjoint components: ABFG and CH
Min Edge = 3 A B D G C F I E H
Min Edge = 4 A B D G C F I E H Two components now merged into one.
Min Edge = 4 A B D G C F I E H
Min Edge = 5 A B D G C F I E H Rejected due to a cycle BFGB
Min Edge = 6 A B D G C F I E H