download

Matakuliah
Tahun
Versi
: T0534/Struktur Data
: 2005
: September 2005
Pertemuan 13
Minimum Spanning Tree
(MST)
1
Learning Outcomes
Pada akhir pertemuan ini, diharapkan mahasiswa
akan mampu :
• menerangkan algoritma untuk MST (TIK-11)..
2
Outline Materi
• Spanning Tree.
• Algoritma untuk MST :
– Prim
– Kruskal
– Sollin
3
Spanning Trees
• Spanning (ST) Tree is any tree that consist solely of edges in Graph (G).
• DFS and BFS can be used to create ST :
•
•
DFS : depth first spanning tree.
BFS : breadth first spanning tree.
4
MST
MST is spanning tree of least cost (weight).
Three different algorithms (greedy method) can be
used to obtain a minimum spanning tree of
connected undirected graph.
• Prim
• Kruskal
• Sollin
5
Prim’s Algorithm
T =0
TV ={1} {start with vertex 1 and no edges }
done = false
While T contains less than n-1 edges and not done Do
Begin
Let (u,v) be a least-cost edge such that u is element of TV and v
is not element of TV
If there is no such edge Then
done = true
Else
Add v to TV and (u,v) to T
End
If T contains fewer than n-1 edges Then “No Spanning Tree”
6
Summary of Prim’s Algorithm
n=7
u
v
TV
-
-
{1}
1
6
6
T
Notes
Figure
{}
Initial
{1,6}
{(1,6)}
add 6 to TV, add (1,6) to T
b
5
{1,6,5}
{(1,6),(6,5)}
add 5 to TV, add (6,5) to T
c
5
4
(1,6,5,4}
{(1,6),(6,5),(5,4)}
add 4 to TV, add (5,4) to T
d
4
3
(1,6,5,4,3}
{(1,6),(6,5),(5,4),(4,3)}
add 3 to TV, add (4,3) to T
e
3
2
(1,6,5,4,3,2}
{(1,6),(6,5),(5,4),(4,3),(3,2)}
add 2 to TV, add (3,2) to T
f
2
7
(1,6,5,4,3,2,7}
{(1,6),(6,5),(5,4),(4,3),(3,2),(2,7)}
add 7 to TV, add (2,7) to T
g
Terminate
7
Example of Prim’s Algorithm
10
24
7
5
6
7
25
3
5
(b)
(c)
1
2
6
6
7
25
3
22
4
(e)
25
12
4
22
(f)
2
16
7
3
5
14
6
16
7
12
1
10
2
4
(d)
1
10
5
22
4
4
10
25
5
4
(a)
7
3
3
5
12
2
6
7
1
10
2
6
3
22
1
10
2
16
18
1
2
14
6
25
28
1
10
25
3
5
12
4
22
(g)
8
Kruskal’s Algorithm
T =0
While T contains less than n-1 edges and E not empty Do
Begin
Choose an edge(v,w) from E
If (v,w) does not create a cycle in T Then
Add (v,w) to T
Else
Discard(v,w)
End
If T contains fewer than n-1 edges Then “No Spanning Tree”
9
Summary of Kruskal’s Algorithm
Edge
Weight
-
-
(1,6)
T
Notes
Figure
{}
Initial
b
10
{(1,6)}
add (1,6) to T
c
(3,4)
12
{(1,6),(3,4)}
add (3,4) to T
d
(2,7)
14
{(1,6),(3,4),(2,7)}
add (2,7) to T
e
(2,3)
16
{(1,6),(3,4),(2,7),(2,3)}
add (2,3) to T
f
(4,7)
18
{(1,6),(3,4),(2,7),(2,3)}
discard (4,7)
(4,5)
22
{(1,6),(3,4),(2,7),(2,3),(4,5)}
add (4,5) to T
(5,7)
24
{(1,6),(3,4),(2,7),(2,3),(4,5)}
discard (5,7)
(5,6)
25
{(1,6),(3,4),(2,7),(2,3),(4,5),(5,6)}
add (5,6) to T
g
h
Terminate
10
Example of Kruskal’s Algorithm
14
6
25
28
1
10
24
12
(d)
1
10
2
3
2
7
12
4
4
(f)
14
6
16
2
16
7
25
3
5
1
10
14
6
16
7
12
4
(c)
14
6
12
4
1
2
7
(e)
5
5
4
10
5
3
3
(b)
14
7
7
3
5
(a)
6
6
6
4
1
2
2
6
7
1
10
1
2
3
22
10
10
2
16
7
18
5
1
3
5
12
4
22
(g)
3
5
12
4
22
(h)
11
Sollin’s Algorithm
Selects several edges at each stage.
• At the start of stage, the selected edges, together with all n graph
vertices, from spanning forest.
• This edge is minimum-cost edge that has exactly one vertex is the tree.
• The selected edges are added to the spanning tree being constructed. It
is possible for two trees in the forest to select the same edge.
• So, multiple copies of the same edges are to be eliminated.
• Also when a graph has several edges with the same cost , it is possible
for two tress to select two different edges that connect them together.
• These edges will, of course, have the same cost; only one of these
should be retained.
• At the start of the first stage, the set of selected edges is empty.
• The algorithm terminates when there is only one tree at the end of a
stage or when no edges remain to be selected.
12
Summary of Sollin’s Algorithm
Notes
Selected Edges
Figure
Initial
{}
Select edge for each of the vertices
{(1,6),(2,7),(3,4),(4,3),(5,4),(6,1),(7,2)}
Eliminate the duplicate edges
{(1,6),(2,7),(3,4),(5,4)}
b
Select least cost for joining the forest into tree
{(1,6),(2,7),(3,4),(5,4),(6,5),(2,3)}
c
Terminate
13
Example of Sollin’s Algorithm
24
7
12
(a)
1
10
14
2
2
14
6
6
7
3
4
22
2
16
18
5
1
10
14
6
25
28
1
10
3
5
12
22
(b)
4
16
7
25
3
5
12
4
22
(c)
14
Selesai
15