chapitre 4 - Tree and Spanning Tree Problem
chapitre 4 - Tree and Spanning Tree Problem
2
3
4
G1 G2 G3
6
8
11
12
2
G,p
13
T
19
*
20
21
Algorithm Kruskal
Inputs: A connected graph G=(V,E)with weights p(e)on the edges.
Outputs: A spanning tree T of G with minimum weight.
X : a subset of E;
Sort the edges of E={e1,..., em} in ascending order of their weights;
X ;
i 1; P(T) 0 ;
While |X| < |V|-1 do
If (V, X{ei}) is acyclic then
X X {ei} ;
P(T) P(T)+P(ei) ;
EndIf
i i + 1;
EndWhile
Return T=(V, X); P(T) ;
End.
22
1 2 3 4 5 6 7 8
i
23
1 1
2 2
3 3
4
5
6
7 7
1 2 3 7
24
(a) (b)
Algorithm Prim
Inputs: A connected graph G=(V,E)with weights p(e)on the edges.
Outputs: A spanning tree T of G with minimum weight.
X : a subset of E;
Choose an initial vertex a;
S {a} ; P(T) 0 ;
X ;
While S ≠ V do
Choose an edge uv E such that u S and v V S, with P(uv) being the
minimum.
X X {uv} ;
P(T) P(T)+P(uv) ;
S S{v} ;
EndWhile
Return T=(S, X); P(T) ;
End.
27