Anda di halaman 1dari 45

Minimum Spanning Trees algorithm

Algoritma Minimum Spanning Trees

algoritma Kruskal and algoritma Prim.


Kedua algoritma ini berbeda dalam metodologinya, tetapi keduanya mempunyai tujuan menemukan minimum spanning algorithm Kruskal menggunakan edge, dan algorithm Prim menggunakan vertex yang terhubung
2

Perbedaan antara algoritma prim dan kruskal

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

Contoh algoritma Kruskal

Contoh algoritma Kruskal

Contoh algoritma Kruskal

Langkah 0 1 2 3 4 5 6 7

Sisi

bobot

N1,N2 N7,N8 N2,N3 N1,N6 N3,N4 N2,N7 N4,N5

1 1 2 3 4 5 7
9

Contoh algoritma Kruskal


Langkah 2

Langkah 1

Langkah 3
10

Contoh algoritma Kruskal


Langkah 4 Langkah 6

Langkah 0 1 2 3 4

Sisi 1,4 6,7 3,4 1,2 4,7 5,7

bobot 1 1 2 2 4 6
11

Langkah 5

5 6

12

Kruskal's algorithm(basic part)


1 2 3 3 (Sort the edges in an increasing order) A:={} while E is not empty do { take an edge (u, v) that is shortest in E and delete it from E if u and v are in different components then add (u, v) to A

Note: each time a shortest edge in E is considered.

The execution of Kruskal's algorithm (Moderate part)

The edges are considered by the algorithm in sorted order by weight.


The edge under consideration at each step is shown with a red weight number. 8 7 c d b 9 4
a

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

Sisi e-c d-e

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

Prim's algorithm(basic part)


MST_PRIM(G,w,r) 1. A={} 2. S:={r} (r is an arbitrary node in V) 3. Q=V-{r}; 4. while Q is not empty do { 5 take an edge (u, v) such that (1) u S and v Q (v S ) and (u, v) is the shortest edge satisfying (1) 6 add (u, v) to A, add v to S and delete v from Q }

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)

The execution of Prim's algorithm(moderate part)


the root vertex
a

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

minimum- spanning tree

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

Anda mungkin juga menyukai