Professional Documents
Culture Documents
Eedy Method-Spanning Tree
Eedy Method-Spanning Tree
• Example 4.5
Spanning trees
• Applications
– Obtaining an independent set of circuit equations for an electric
network
– network designs (i.e. telephone or cable networks)
– Computer Network Routing Protocol
Minimum-cost Spanning Trees
• Example of MCST
– Finding a spanning tree of G with minimum cost
1 1
28
10 2 10 2
14 16 14 16
6 7 3 6 7 3
24
25 18 12 25 12
5 5
22 4 22 4
(a) (b)
Prim’s Algorithm
1 1 1
10 10 2 10 2
2
6 7 3 6 7 3 6 7 3
25 25
5 5 5
4 4 22 4
(a) (b) (c)
1 1 1
10 10 2 10 2
2 16
16 14
3 6 7 3 6 7 3
6 7
25 25 12 25 12
12
5 5 5
22 4 22 4 22 4
(d) (e) (f)
Prim’s Algorithm
Prim’s algorithm
GENERIC-MST(G, w)
1. A=Φ
2. while A does not form a spanning tree
3. find an edge (u, v) that is safe for A
4. A ∪ {(u, v)}
5. return A
∞ ∞ ∞
Nodes Parent (v) Cost (v)
a Nil 0
0 ∞
b a 4
∞
h a 8
i
∞ ∞ ∞ c
g
ab<cost of b ah<cost of h d
4<∞ T 8<∞T f
e
‘ab’ and ‘ah’ edges are considered sin
ce they are adjacent to the node ‘a’
a b h i c g d f e
b is extracted
0 4 8 ∞ ∞ ∞ ∞ ∞ ∞ for each v ∈ G.Adj(u)
if v ∈ Q and w(u, v) < v.cost
v.parent = u
4 ∞ ∞ v.cost = w(u, v)
0 ∞
∞
Nodes Parent (v) Cost (v)
a Nil 0
8 ∞ ∞
b a 4
a∈ Q F h a 8
bh<cost of h
Not considered s
11 < 8 F i
ince ‘a’ is not ele
ment of Q c b 8
bc < cost of c
g
8<∞ T
d
a b h i c g d f e f
0 4 8 ∞ 8 ∞ ∞ ∞ ∞ e
a b h i c g d f e Now c is extracted
0 4 8 ∞ 8 ∞ ∞ ∞ ∞
for each v ∈ G.Adj(u)
if v ∈ Q and w(u, v) < v.cost
v.parent = u
4 8 ∞ v.cost = w(u, v)
b c
0 ∞
∞
Node (v) Parent (v) Cost (v)
8 ∞ ∞ a Nil 0
b a 4
b∈ Q F cd<cost of d h a 8
Not cosidered 7<∞T i c 2
c b 8
cf < cost of f ci < cost of i
g
4<∞ T 2<∞ T
d c 7
a b h i c g d f e
f c 4
0 4 8 2 8 ∞ 7 4 ∞ e
a b h i c g d f e
0 4 8 2 8 ∞ 7 4 ∞
i is extracted
4 8
7 for each v ∈ G.Adj(u)
b if v ∈ Q and w(u, v) < v.cost
c
v.parent = u
0 2 v.cost = w(u, v)
i ∞
b c
UNION(x,y)
Applications
• Disjoint-set data structures model the partitioning of a set, for ex
ample to keep track of the connected components of an undire
cted graph. This model can then be used to determine whether t
wo vertices belong to the same component, or whether adding an e
dge between them would result in a cycle.
• This data structure is used Graph Library to implement its Inc
remental Connected Components functionality. It is also used for i
mplementing Kruskal's algorithm to find the minimum spanning tre
e of a graph.
• Note that the implementation as disjoint-set forests doesn't allow d
eletion of edges — even without path compression or the rank heu
ristic, this is not as easy, although more complex schemes have be
en designed that can deal with this type of incremental update.
Kruskal's Algorithm
MST-Kruskal(G,w)
1. Sort the edges E in non-decreasing order by weight
2. A
3. for each vertex v V[G]
4. do make-set(v)
5. for each edge (u,v) E, taken in non-decreasing
order weight.
6. do if find-set(u) find-set(v)
7. then A = A ∪ {(u,v)}
8. union ( u,v )
9. return A
It uses a disjoint-set data structure to maintain several disjoint sets of elements. Eac
h set contains the vertices in one tree of the current forest. The operation FIND-SE
T (u) returns a representative element from the set that contains u. Thus, we can deter
mine whether two vertices u and belong to the same tree by testing whether FIND-
SET (u) equals FIND-SET (v). To combine trees, Kruskal’s algorithm calls the UNION
procedure.
Kruskal's Algorithm: Time Analysis
MST-Kruskal(G,w)
1. Sort the edges E in non-decreasing order ( E lg E )
by weight
2. A Count2= (1)
3. for each vertex v V[G] Count3= ( V )
4. do make-set(v)
5. for each edge (u,v) E, taken in Count5 = O( E )
non-decreasing order by weight
6. do if find-set(u) find-set(v)
Sorting dominates the runtime.
7. then A = A ∪ {(u,v)} We get T( V,E ) = ( E lg E),
8. union ( u,v )
9. return A
Example:
a 4
6
5
b u
14 2
10
c v
3
8 15
d