Professional Documents
Culture Documents
Lecture 7: Minimum Spanning Trees and Prim's Algorithm: CLRS Chapter 23
Lecture 7: Minimum Spanning Trees and Prim's Algorithm: CLRS Chapter 23
Prims algorithm for the MST problem. The algorithm Correctness Implementation + Running Time
1
Spanning Trees Spanning Trees: A subgraph of a undirected graph is a spanning tree of if it is a tree and contains every vertex of . Example:
d e c a b % b ! a % ! " " %$ % $ $$ ! ! " " # d c e
spanning tree 1 a b d c e spanning tree 3
Graph a& b & $ " " " $$ $ " d c e spanning tree 2
Spanning Trees Theorem: Every connected graph has a spanning tree. Question: Why is this true? Question: Given a connected graph nd a spanning tree of ?
Weighted Graphs Weighted Graphs: A weighted graph is a graph, in which each edge has a weight (some real number). Weight of a Graph: The sum of the weights of all edges. Example:
7 32 10 d 23 c 9 e a b ! a ! " " %$ 10 ! !#" " d c 9
Tree 1. a 7 10 d c
% b % % 32 $ $$ 23 e
w=74 b 32 23 e
weighted graph a& b 32 &7 " $$$ " $ " " d 23 c 9 e
Tree 3, w=72
Minimum Spanning Trees A Minimum Spanning Tree in an undirected connected weighted graph is a spanning tree of minimum weight (among all spanning trees). Example:
7 32 10 d 23 e c 9 a b ! a ! " " %$ 10 ! !#" " d c 9
Tree 1. a 7 10 d c
% b % % 32 $ $$ 23 e
w=74 b 32 23 e
weighted graph a& b 32 &7 " $$$ " " " $ d 23 c 9 e
Tree 3, w=72
Minimum Spanning Trees Remark: The minimum spanning tree may not be unique. However, if the weights of all the edges are pairwise distinct, it is indeed unique (we wont prove this now). Example:
2 24
2 67 24
2 67
2 24 67
MST1
MST2
Minimum Spanning Tree Problem MST Problem: Given a connected weighted undi rected graph , design an algorithm that outputs a minimum spanning tree (MST) of . Question: What is most intuitive way to solve? Generic approach: A tree is an acyclic graph. The idea is to start with an empty graph and try to add edges one at a time, always making sure that what is built remains acyclic. And if we are sure every time the resulting graph always is a subset of some minimum spanning tree, we are done.
Generic Algorithm for MST problem Let be a set of edges such that , where is a MST. An edge is a safe edge for , if is also a subset of some MST. , we If at each step, we can nd a safe edge can grow a MST. This leads to the following generic approach: Generic-MST(G, w) Let A=EMPTY; while A does not form a spanning tree find an edge (u, v) that is safe for A add (u, v) to A return A How can we nd a safe edge?
8
How to nd a safe edge We rst give some denitions. Let connected and undirected graph. We dene:
be a
Cut A cut
of G is a partition of V.
crosses the cut Cross An edge if one of its endpoints is in , and the other is in .
of edges if no edge
Light edge An edge is a light edge crossing a cut if its weight is the minimum of any edge crossing the cut.
9
How to nd a safe edge Lemma Let be a connected, undirected graph with a real-valued weight function dened on . Let be a subset of that is included in some minimum spanning tree for , let be any cut of that respects , and let be a light edge crossing the cut . Then, edge is safe for .
Proof
1. Let .
, where
is a MST. Suppose
2. The trick is to construct another MST that con tains both and , thereby showing is a safe edge for .
11
3. Since , and are on opposite sides of the cut , there is at least one edge in on the be path from to that crosses the cut. Let . such edge. Since the cut respects ,
MST T
another MST T
y x v u
y x v u
a cut respects A
12
4. Add to , it creates a cycle. By removing an edge from the cycle, it becomes a tree again. In particular, we remove ( ) to make a new tree .
5. The weight of
is
(a MST),
13
Prims Algorithm The generic algorithm gives us an idea how to grow a MST. If you read the theorem and the proof carefully, you will notice that the choice of a cut (and hence the corresponding light edge) in each iteration is immaterial. We can select any cut (that respects the selected edges) and nd the light edge crossing that cut to proceed. The Prims algorithm makes a nature choice of the cut in each iteration it grows a single tree and adds a light edge in each iteration.
14
While the tree does not contain all vertices in the graph nd shortest edge leaving the tree and add it to the tree .
Running time is
#
15
More Details
Step 0: Choose any element ; set and . (Take as the root of our spanning tree.)
Step 1: Find a lightest edge such that one endpoint . Add this edge to is in and the other is in and its (other) endpoint to .
Step 2: If , then stop & output (minimum) . Otherwise go to Step 1. spanning tree
The idea: expand the current tree by adding the lightest (shortest) edge leaving it and its endpoint.
b a r d
26 c 16 14 e
24 20 g 8 f
h 12 12 i r a
b 26 c
24 20 16 14 8 e g f
12 i
23
b 4 a
e 6 g
17
b 4 a 8 c 9 2 8
10 7 d 9
e 6 5 2 f g
1 10 8 7 d 2 9 1
b 4 a 8 c 9
e 6 5 2 f g
Step 1.1 after S={a,b} V \ S = {c,d,e,f,g} A={{a,b}} lightest edge = {b,d}, {a,c}
18
b 4 a 8 c 9 2 8
10 7 d 9
e 6 5 2 f g
Step 1.2 before S={a,b} V \ S = {c,d,e,f,g} A={{a,b}} lightest edge = {b,d}, {a,c}
1 10 8 7 d 2 9 1
b 4 a 8 c 9
e 6 5 2 f g
19
b 4 a
e 6 g
20
b 4 a 8 c 9 2 8
10 7 d 9
e 6 5 2 f g
1 10 8 7 d 2 9 1
b 4 a 8 c 9
e 6 5 2 f g
21
b 4 a 8 c 9 2 8
10 7 d 9
e 6 5 2 f g
1 10 8 7 d 2 9 1
b 4 a 8 c 9
e 6 5 2 f g
Step 1.5 after S={a,b,c,d,f,g} V \ S = {e} A={{a,b},{b,d},{c,d},{c,f}, {f,g}} lightest edge = {f,e}
22
b 4 a 8 c 9 2 8
10 7 d 9
e 6 5 2 f g
1 10 8 7 d 2 9 1
Step 1.6 before S={a,b,c,d,f,g} V \ S = {e} A={{a,b},{b,d},{c,d},{c,f}, {f,g}} lightest edge = {f,e} Step 1.6 after S={a,b,c,d,e,f,g} V \ S = {} A={{a,b},{b,d},{c,d},{c,f}, {f,g},{f,e}} MST completed
b 4 a 8 c 9
e 6 5 2 f g
23
and
Step 1: Find a lightest edge such that one endpoint is in and the other is in . Add this edge to and its (other) endpoint to . Step 2: If , then stop and output the minimum span . ning tree Otherwise go to Step 1.
Questions:
How does the algorithm nd the lightest edge and update efciently? How does the algorithm update
efciently?
24
efciently?
Answer: Color the vertices. Initially all are white. Change the color to black when the vertex is moved to . Use color[ ] to store color.
Question: How does the algorithm nd the lightest edge and update efciently? Answer: (a) Use a priority queue to nd the lightest edge. (b) Use pred[ ] to update .
25
Reviewing Priority Queues Priority Queue is a data structure (can be implemented as a heap) which supports the following operations:
Remark: Priority Queues can be implemented so that each operation takes time . See CLRS!
26
b a r d
26 c 16 14 e
24 20 g 8 f
h 12 12 i r a
b 26 c
24 20 16 14 8 e g f
h 12 23 12 i
23
key[f] = 8, pred[f] = e key[i] = infinity, pred[i] = nil key[g] = 16, pred[g] = c key[h] = 24, pred[h] = b f has the minimum key
new edge key[i] = 23, pred[i] = f After adding the new edge and vertex f, update the key[v] and pred[v] for each vertex v adjacent to f
27
Description of Prims Algorithm Remark: is given by adjacency lists. The vertices in are stored in a priority queue with key=value of lightest edge to vertex in .
) Prim( for each !"
initialize ; ;
$ & % 0 ; 132 ' () ; % 4 new PriQueue( ); 4 while( is nonempty) 465 u= extraxtMin(); (A@B 8 7 9 for each ( )# !" 3 7 if (( )&&( EF 7G 73 ; EF 465 decreaseKey( 7 73 ); ' H() 73 ; $
$ $ !"
start at root put vertices in until all vertices in MST lightest edge
73DC 4
73
7
'
H()
73 6 RG78
a 1 b 2 c u key[u] pred[u] a 3 4 10
3 4 d 1 5
f b c d e f
29
Analysis of Prims Algorithm Let and . The data structure PriQueue supports the following two operations: (See CLRS)
to extract each vertex from the queue. Done once for each vertex
time to decrease the key value of neighboring vertex. Done at most once for each edge
30
2n
#
32