Matakuliah
Tahun
Versi
: T0026/Struktur Data
: 2005
: 1/1
Pertemuan 23
Minimum Cost Spanning Tree
1
Learning Outcomes
Pada akhir pertemuan ini, diharapkan mahasiswa
akan mampu :
• Mahasiswa dapat menghasilkan program
modular untuk mengimplementasikan
Minimum cost spanning tree
2
Outline Materi
•
•
•
•
•
•
•
•
•
•
•
Definition
Greedy Strategy
Kruskal's algorithm
Kruskal's algorithm(cont.)
Example of Kruskal's algorithm
Example of Kruskal's algorithm(cont.)
Prim's algorithm
Prim's algorithm(Cont.)
Example of Prime's algorithm
Example of Prime's algorithm(Cont.)
Sollin's algorithm
3
MINIMUM COST SPANNING TREE
• Tree : undirected, acyclic, connected graph
• Spanning tree : mencakup semua verteks
suatu graph, dimana semua verteks
dihubungkan dgn edge sehingga spanning
tree merupakan subgraph
• Minimum spanning tree : spanning tree dari
weighted graph, dengan total weight
minimum
• Penerapan : route paling murah, network
yang paling efisien
4
Graph – MINIMUM COST SPANNING TREE
3 kemungkinan spanning tree
minimum spanning tree :
5
Minimum Cost Spanning Tree
• Algoritma Kruskal
– menggunakan edge
– dalam tiap tahapan membetuk forest
• Algoritma Prim
– menggunakan verteks
– dalm tiap tahapan membetuk tree
6
Graph – MST – ALGORITMA KRUSKAL
Algoritma Kruskal
1. Create Tree berisi semua verteks tanpa edge
2. Tambahkan edge minimum cost
Hapus edge dr graph
(edge baru tidak boleh membentuk cycle
graph dgn tree terbentuk)
3. Ulang langkah 2 hingga n-1 edge berada
dlm tree (n: jumlah vertex dlm graph)
7
Graph – MST – Contoh Kruskal (1)
0
0
14
5
10
28
10
16
18
22
3
16
6
2
25
2
25
1
14
5
1
6
24
28
24
18
22
3
12
4
12
4
0
10
14
5
0
28
6
10
1
16
2
25
24
18
22
3
4
28
12
14
5
6
1
16
2
25
24
18
22
3
12
4
8
Graph – MST – Contoh Kruskal (1) - lanjutan
0
10
14
5
6
18
28
10
1
16
2
25
24
0
28
1
14
5
16
6
2
25
12
4
24
18
22
3
12
4
22
3
0
Beberapa edge yg digambar dgn garis putus
diabaikan karena akan membentuk cycle pd
MST.
28
10
14
5
6
16
2
25
MST dgn titik awal 0 : Total cost = 99
1
24
18
22
3
12
4
9
Graph – Contoh Kruskal (2)
N2 2
N3 4 N4
1
N1
N2
N3
N4
7
4
5
6
N5
7 6
3
N1
N5
8
N6
8
N2
N7
1
N3
N8
N6
N4
N1
N2
N5
N6
N2
N7
N3
N6
N7
N4
N8
N4
N5
N6
N2
N5
N3
N8
N1
N8
N1
N7
N7
N3
N8
N4
N1
N5
N6
N7
N8
10
Graph – MST – Contoh Kruskal (2) - Lanjutan
N2 2
N3 4 N4
1
N1
N2
N3
N4
7
4
5
6
N5
7 6
3
N1
N5
8
N6
N2
8
N7
N3
1
N8
N6
N4
N1
N2
N5
N6
N7
N8
N7
N3
N8
N4
N1
N5
N6
N7
N8
MST : cost = 23
11
Graph – MST - Contoh Kruskal (3)
12
Graph – MST – Algoritma Prim
//T dan TV berisi edge dan verteks MST
T = { }; //no edge
TV={0}; //1 verteks sembarang
while (edge di T < n-1) {
let (u,v) be a least cost edge such that u Є TV and v Є
TV;
if (there no such edge) break;
add v to TV;
add (u,v) to T;
}
if (T contains fewer than n-1 edges) printf (“no spanning
tree”);
13
Graph – MST - Alternatif Algoritma Prim
Alternatif Algoritma Prim ((Sugih Jamin ([email protected]))
1. given G = (V;E) a weighted, connected, undirected graph
2. separate V into two sets:
T: nodes on the MST
Tc: those not
3. T initially empty, choose a random node and add it to T
4. select an edge with the smallest cost/weight/ distance
from any node in T that connects to a node v in Tc, move
v to T
5. repeat step 4 until T c is empty
14
Graph – MST - Contoh Prim (1)
0
28
10
14
5
6
18
4
22
16
2
25
24
1
3
12
VT
T
cost
mst
0
(0,5)
10
(0,5)
5
(5,4)
25
(5,4)
4
(4,3)
22
(4,3)
3
(3,2)
12
(3,2)
2
(2,1)
16
(2,1)
1
(1,6)
14
(1,6)
15
Graph – MST – Contoh Prim (2)
T
VT
alternatif edge
cost
(a,d)
a
b
13
c
8
d
1
1
16
Graph – MST – Contoh Prim (2) - lanjutan
T
VT
alternatif edge
cost
(a,d)
a
b
13
c
8
c
5
e
4
f
5
(d,e)
d
5
T
VT
alternatif edge
cost
(a,d)
a
b
13
c
8
c
5
f
5
c
3
f
2
(d,e)
(e,f)
d
e
7
17
Graph – MST – Contoh Prim (2) - lanjutan
T
VT
alternatif edge
cost
(a,d)
a
b
13
c
8
c
5
f
5
(d,e)
(e,f)
d
(e,c)
10
e
c
3
T
VT
alternatif edge
cost
(a,d)
a
b
13
c
8
c
5
f
5
e
-
-
c
b
15
(d,e)
(e,f)
d
(e,c)
(a,b)
23
18
(a,c), (d,c) dan (d,f) tidak dapat dipilih karena akan membentuk cycle
Graph – MST – Contoh Prim (2) - lanjutan
19
Graph – MST - Contoh Prim (3)
N2
2
N3
N4
4
1
7
4
N1
edge yang mungkin
edge yg dipilih
7
6
5
N5
6
3
8
8
N6
1
N7
N2
N8
2
N3
N4
4
1
7
4
N1
7
6
5
N5
6
3
N2
2
N3
N4
4
1
3
N6
8
N7
1
N8
7
4
N1
8
5
6
7
N5
6
Edge yg mungkin
8
1
N6
N7
N8
Edge yg dipilih
8
20
Graph – MST – Contoh Prim (3)
N2
2
N3
N4
4
1
7
4
N1
7
6
5
N5
6
3
8
8
N6
N7
1
N2
N8
2
N3
4
N4
1
7
4
N1
6
5
7
3
N2
2
N3
4
1
8
N7
1
N8
7
4
N1
8
N6
N4
N5
6
7
6
5
N5
6
3
8
N6
8
N7
1
N8
21
Graph – MST – Contoh Prim (3)
N2
N3
2
N4
4
1
7
4
N1
7
6
5
N5
6
3
8
N6
N2
8
2
N7
N3
1
N8
N4
4
1
7
4
N1
7
6
5
N5
6
3
8
N6
8
N7
1
N8
22
23
© Copyright 2026 Paperzz