Professional Documents
Culture Documents
L07
L07
Prims Algorithm
CLRS Chapter 23
Outline of this Lecture
Spanning Trees
d
c
e
a
b
c
e
Graph
a
b
c
e
spanning tree 1
a
b
c
e
spanning tree 2
spanning tree 3
Spanning Trees
Theorem: Every connected graph has a spanning
tree.
Question: Why is this true?
Question: Given a connected graph
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
d
23
e
a
10
d
c
9
32
23
e
weighted graph
a
b
32
7
23
c
e
9
Tree 1.
a
10 7
d
c
w=74
b
32
23
e
10
c 9
Tree 2, w=71
Minimum spanning tree
Tree 3, w=72
7 32
d
23
e
a
10
d
c
9
32
23
e
weighted graph
a
b
32
7
23
c
e
9
Tree 1.
a
10 7
d
c
w=74
b
32
23
10
c 9
Tree 2, w=71
Minimum spanning tree
Tree 3, w=72
24
100
weighted graph
67
2
24
67
MST1
2
24
67
MST2
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 find 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 find a safe edge?
8
Cut A cut
be a
of G is a partition of V.
of edges if no edge
Let
be a connected, undirected graph
be a subset of
that is included in some minimum
Proof
1. Let
, where
is a MST. Suppose
11
be
path from to that crosses the cut. Let
.
such edge. Since the cut respects ,
Since
is a light edge crossing the cut, we
.
have
another MST T
MST T
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
hence
is also a MST.
7. Since
is also a subset of
is safe for .
(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 find 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
Running time is
15
More Details
Step 2: If
, then stop & output (minimum)
. Otherwise go to Step 1.
spanning tree
24
20
b
a
26
c 16
14
g
8
h
12
24
20
b
12
23
a
i
r
26
c
16
14
g
8
12
12
i
23
new
e
new edge
16
Prims Algorithm
Worked Example
10
b
4
a
e
7
8
9
6
5
1
10
b
8
9
6
5
2
c
Step 0
Connected graph
g
2
S={a}
V \ S = {b,c,d,e,f,g}
lightest edge = {a,b}
17
Prims Algorithm
Prims Example Continued
10
b
4
a
e
7
8
9
10
8
9
8
e
7
6
5
d
9
2
c
b
a
2
f
18
Prims Algorithm
Prims Example Continued
10
b
4
a
e
7
8
9
10
8
e
7
6
5
2
c
b
a
2
f
19
Prims Algorithm
Prims Example Continued
10
b
4
a
e
7
8
9
10
8
9
8
e
7
6
5
d
9
2
c
b
a
2
f
20
Prims Algorithm
Prims Example Continued
10
b
4
a
e
7
8
9
10
8
9
8
e
7
6
5
d
9
2
c
b
a
2
f
21
Prims Algorithm
Prims Example Continued
10
b
4
a
e
7
8
9
10
b
8
9
a
8
6
5
d
9
2
c
2
f
22
Prims Algorithm
Prims Example Continued
10
b
4
a
e
7
8
9
6
5
10
b
4
8
9
a
8
6
5
d
9
2
c
g
2
23
and
Step 2: If
, then stop and output the minimum span .
ning tree
Otherwise go to Step 1.
Questions:
efficiently?
24
Prims Algorithm
Question: How does the algorithm update
efficiently?
25
insert(
):
Insert with the key value
in .
u = extractMin():
Extract the item with the minimum key value in .
decreaseKey(
):
26
24
20
is a vertex in
,
is the weight of the lightest edge
24
20
b
a
26
c 16
14
g
8
h
12
b
12
a
i
23
26
c
key[f] = 8, pred[f] = e
16
14
g
8
12
12
i
23
e
new edge
27
)
Prim(
for
each
initialize
;
;
;
;
new
PriQueue( );
while(
is nonempty)
u= extraxtMin();
for
each
(
)
if((
)&&(
decreaseKey(
);
start at root
put vertices in
until all vertices in MST
lightest edge
))
new lightest edge
a
1
10
3
4
d
1
5
c
u
f
b
key[u]
pred[u]
29
Let
and
. The data structure PriQueue
supports the following two operations: (See CLRS)
30
2n
key[r] = 0;
pred[r] = nil;
Q = new PriQueue(V);
while (Q. nonempty())
{
u = Q.extractMin();
for each (v in adj[u])
{
if ((color[v] == white) &
(w(u,v) < key[v])
{
key[v] = w(u, v);
Q.decreaseKey(v, key[v]);
pred[v] = u;
}
}
color[u] = black;
}
1
1
n
1
O(log n)
1
1 O(deg(u) log n)
1
O(log n)
1
1
}
[O(log n) + O(deg(u) log n)]
u in V
31
32