Perbedaan prinsip antara algoritma prim dan kruskal adalah, jika pada algoritma prim sisi yang dimasukkan ke dalam T harus bersisian dengan sebuah simpul di T, maka pada algoritma kruskal sisi yang dipilih tidak perlu bersisian dengan sebuah simpul di T. asalkan penambahan sisi tersebut tidak membentuk cycle.
Kruskal's Algorithm:
Pada algoritma kruskal, sisi (edge) dari Graph diurut terlebih dahulu berdasarkan bobotnya dari kecil ke besar. Sisi yang dimasukkan ke dalam himpunan T adalah sisi graph G yang sedemikian sehingga T adalah Tree (pohon). Sisi dari Graph G ditambahkan ke T jika ia tidak membentuk cycle. 1. T masih kosong 2. pilih sisi (i,j) dengan bobot minimum 3. pilih sisi (i,j) dengan bobot minimum berikutnya yang tidak membentuk cycle di T, tambahkan (i,j) ke T 4. Ulangi langkah 3 sebanyak (n-2) kali. 5. Total langkah (n-1) kali
4
Kruskal's Algorithm:
a 15 d
10
b 13
20 5
c 12 f
Langkah
0 1 2 3 4 5
Sisi
e-c a-b d-e c-f b-e
bobot
5 10 11 12 13
11
16
21
Kruskal's Algorithm:
a
15
10
b
13
20 5
c
12
a
15
10
b
13
20 5
c
12
a
15
10
b
13
20 5
c
12
11
16
21
11
16
21
11
16
21
a 15 d
10
b 13
20 5
c 12 f
a 15 d
10
b 13
20 5
c 12
6f
11
21
11
21
16
16
Langkah 0 1 2 3 4 5 6 7
Sisi
bobot
1 1 2 3 4 5 7
9
Langkah 1
Langkah 3
10
Langkah 0 1 2 3 4
bobot 1 1 2 2 4 6
11
Langkah 5
5 6
12
11
7 8
h
2
i
6
g
4
f
14
10 2
4
a
9
e
11 7 8
h
2 6 4
g
14 2
f
10
7
c d
4
a
9
e
11 7 8
h
2
i
6 1
g
4 2
14
f
10
8 4
a b
9
e
11
7 8
8 7
c d b h
6
g
4 2
f
14
10
4
a
9
e
11 7 8
h
2
i
6
g
4
2
14
f
10
8 4
a b
9
e
11
7 8
8 4
a b c h
6
1
g
4 2
f
14
10
7
d
9
e
11 7
2
i
6 1
g
4 2
14
f
10
8 4
a b c
7
d
9
e
11 7 8
h
2
i
6
g
4
f
14
10
1
8 4
a b c
9
e
11 8
7
h
2 6
g
4 2
f
14
10
8 4
a b
c
7
d
9
e
11
2
i
7
8 8 4
a b c h
6
g
4
f
14 2
10
7
d
1 9
e
11 7
8
h
2
i
6
g
4 2
14
f
10
Algorithma Prim
Pada algoritma prim, dimulai pada vertex yang mempunyai sisi (edge) dengan bobot terkecil.
Sisi yang dimasukkan ke dalam himpunan T adalah sisi graph G yang bersisian dengan sebuah simpul di T, sedemikian sehingga T adalah Tree (pohon). Sisi dari Graph G ditambahkan ke T jika ia tidak membentuk cycle.
(NOTE: dua atau lebih edge kemungkinan mempunyai bobot yang sama, sehingga terdapat pilihan vertice, dalam hal ini dapat diambil salah satunya.)
20
Algorithma Prim
1. Ambil sisi (edge) dari graph yg berbobot minimum, masukkan ke dalam T 2. Pilih sisi (edge) (i,j) yg berbobot minimum dan bersisisan dengan simpul di T, tetapi (i,j) tidak membentuk cycle di T. tambahkan (i,j) ke dalam T 3. Ulangi prosedur no 2 sebanyak (n-2) kali
21
Algorithma Prim
PROCEDURE Prim (G: weighted connected undirected graph with n vertices) BEGIN T := a minimum-weight edge FOR i := 1 to n-2 DO BEGIN e := a minimum-weight edge one of whose vertices is in T, and one is not in T T := T with e added END RETURN T END
22
Algorithma Prim
a 15 d
10
b 13
20 5
Langkah
c 12 f
bobot 5 11
0 1 2
11 16
21
3
4 5
c-f
b-e a-b
12
13 10
23
Algorithm Prim
10
20 5 21 16 10 10 20 5 21 16 10
a
15
b
13
c
12
a
15
b
13
c
12
a
15
b
13
20 5
c
12
11
11
11
16
21
a 15 d
b 13
20 5
c 12 f
a 15 d
10
b 13
20 5
c 12
24f
11
21
11
21
16
16
Algorithm Prim
LANGKAH 1 SISI (H,G) (G,F) (F,C) (C,I) (C,D) (C,B) (B,A) (D,E) BOBOT 1 2 4 2 7 8 4 9
B 4 A 8 H 11 7
8 2 I 6 1
D 9 14 2 10 F
2 3
4 5 6 7 8
25
Algorithm Prim
Langkah 1
1
Langkah 3
C
G
4
Langkah 2
Langkah 3
C 2
I 1
26
Algorithm Prim
Langkah 4
C 2 I 1 4 7 D
Langkah 6
B 4 A I 1 8 2 4 C 7 D
Langkah 5
B 8 2 I 1 4 C 7 D
Langkah 7
B 4 A I 1 8 2 4 C 7 D 9 E 2 F
27
Algorithm Prim
28
Grow the minimum spanning tree from the root vertex r. Q is a priority queue, holding all vertices that are not in the tree now. key[v] is the minimum weight of any edge connecting v to a vertex in the tree. parent[v] names the parent of v in the tree. When the algorithm terminates, Q is empty; the minimum spanning tree A for G is thus A={(v,parent[v]):vV-{r}}. Running time: O(|E|+|V|lg |V|). (Analysis is not required)(Fibonacci heap: decreas-key in O(1) time)
8 4
b c
7
d
9
e
11 7 8
h
2
i
6
g
4
f
14
10 2 7
c d
1 8 4
a b
9
e
11 7
2
i
6
g
4
f
14
10 2
8 4
a b c
7
d
9
e
11 7
2
i
6
g
4
f
14
10 2 7
c d
1 8 4
a
b
9
e
11 7 8
h
2
i
6
g
4
f
14
10
2
8
4
a b c
7
d
9
e
11 7
2
i
6
g
4
f
14
10
1
8 4
a
b
2
7
c d
9
e
11 7 8
h
2
i
6
g
4
f
14
10
2
8 4
a b c
7
d
9
e
11 7
2
i
6
g
4
f
14
10 2 7
c d
1 8 4
a b
9
e
11 7
2
i
6
g
4
f
14
10 2
8 4
a b c
7
d
9
e
11 7
8
h
2
i
6
g
4
f
14
10
2
Bottleneck spanning tree: A spanning tree of G whose largest edge weight is minimum over all spanning trees of G. The value of the bottleneck spanning tree is the weight of the maximum-weight edge in T. Theorem: A minimum spanning tree is also a bottleneck spanning tree. (Challenge problem)
soal
Cari minimum spanning tree dengan menggunakan algoritma prim dan kruskal !
2
4 1 8 2
11
5
3 6 1 6
9
4
36
Barvkas Algorithm
37
Barvkas Algorithm
1. For all vertices search the edge with the smallest weight of this vertex and mark these edges 2. Search connected vertices (clusters) and replace them by a new vertex C i (cluster) 3. Remove the cycles and, if two vertices are connected by more than one edge, delete all edges except the cheapest
Baruvka's Algorithm
4
C 3 E 2
6
D 1 4
Baruvka's Algorithm
2
3 2 F
4
C 3 E 2
6
D 1 4
Baruvka's Algorithm
2
3 2 F
4
C 3 E 2
6
D 1 4
Baruvka's Algorithm
2
3 2 F
Ci
4
C 3 E 2
6
D 1 4
Baruvka's Algorithm
2
3 2 F
2
C 3 E
Baruvka's Algorithm
2 1
D 2 F
2
C 3 E
Baruvka's Algorithm
2 1
D 2 F
soal
Tentukan minimum spanning tree dengan menggunakan algoritma kruskal, baruvka dan prim
13 2 11 3 24 20 16 18 1 15 12 17 4 7 15
45
6
22
11
12