Minimal Spanning TreesSection 9.4
Prof. Nathan Wodarz
Math 209 - Fall 2008
Contents1 Minimal Spanning Trees 2
1.1 Minimal Spanning Trees . . . . . . . . . . . . . . . . . . . . . . 2
2 Finding Minimal Spanning Trees 22.1 Prim’s Algorithm . . . . . . . . . . . . . . . . . . . . . . . . . . 22.2 Kruskal’s Algorithm . . . . . . . . . . . . . . . . . . . . . . . . 3
1
1 Minimal Spanning Trees
1.1 Minimal Spanning TreesMinimal Spanning Trees
• In a weighted graph, what is the cheapest way to connect all vertices?
• Must:
– Contain all vertices
– Be connected
– Have a unique simple path from any vertex to any other
• This must be a spanning tree
• Want sum of weights to be a minimum: a minimal spanning tree
2 Finding Minimal Spanning Trees
2.1 Prim’s AlgorithmPrim’s Algorithm
• Idea: Keep adding edges until minimal spanning tree is obtained
• Adds minimum-weight edge at each iteration
• To start:
– Weighted graph with n vertices
– A starting vertex s
• Repeat n − 1 times (once for each edge to add)
– List all edges with one end in the tree, and one end not in the tree
– Add the cheapest of these
• Another example of a greedy algorithm
2
• Prim’s Algorithm is correct - output tree is a minimal spanning tree
• This version examines Θ(n3) edges in worst case
• Possible to implement so that algorithm examinesΘ(n2) edges in worst case
Prim’s AlgorithmExample. Use Prim’s Algorithm to find a minimal spanning tree, starting with thevertex a
• • •
• • •
• • •
9
165
BBBB
BBBB
BBBB
BB10
154
BBBB
BBBB
BBBB
BB
11
6
112
BBBB
BBBB
BBBB
BB7
142
BBBB
BBBB
BBBB
BB
13
3 8
a b c
d e f
g h i
2.2 Kruskal’s AlgorithmKruskal’s Algorithm
• Idea: Keep adding edges until minimal spanning tree is obtained
• Adds minimum-weight edge at each iteration
• To start:
– Weighted graph with n vertices
– Include all vertices in beginning tree
• Repeat n − 1 times (once for each edge)
3
– List all edges that won’t complete a cycle
– Add the cheapest of these
• Yet another example of a greedy algorithm
• Kruskal’s Algorithm is also correct - output tree is a minimal spanning tree
Kruskal’s AlgorithmExample. Use Kruskal’s Algorithm to find a minimal spanning tree
• • •
• • •
• • •
9
165
BBBB
BBBB
BBBB
BB10
154
BBBB
BBBB
BBBB
BB
11
6
112
BBBB
BBBB
BBBB
BB7
142
BBBB
BBBB
BBBB
BB
13
3 8
a b c
d e f
g h i
SummarySummary
You should be able to:
• Be able to identify a minimal spanning tree
• Use Kruskal’s and Prim’s algorithms to find minimal spanning trees
4