• Tidak ada hasil yang ditemukan

Prim's Minimum Spanning Tree Algorithm

N/A
N/A
Sohuturon Fernando

Academic year: 2024

Membagikan "Prim's Minimum Spanning Tree Algorithm"

Copied!
27
0
0

Teks penuh

(1)

PRIM’S MINIMUM SPANNING TREE

Informatics Department

Parahyangan Catholic University

(2)

PRIM’S MST

Used to solve minimum spanning tree problem

Has the property that edges in the set A always forms a single tree

The tree starts from an arbitrary vertex r,

and grows until the tree spans all the vertices

in V

(3)

PRIM’S MST

At each step, a light edge is added to the tree A that connects A to an isolated vertex

A

b

d a

c 14

8 5

22

Light edge ≡ edge with smallest weight that connects A

to an isolated vertex

(4)

EXAMPLE

a

b c

i

h g f

d

e 8

8

11 4

7

4 2

7 6

1 2

14

9

10

A

(5)

EXAMPLE

a

b c

i

h g f

d

e 8

8

11 4

7

4 2

7 6

1 2

14

9

10

A

(6)

EXAMPLE

a

b c

i

h g f

d

e 8

8

11 4

7

4 2

7 6

1 2

14

9

10

A

(7)

EXAMPLE

a

b c

i

h g f

d

e 8

8

11 4

7

4 2

7 6

1 2

14

9

10

A

(8)

EXAMPLE

a

b c

i

h g f

d

e 8

8

11 4

7

4 2

7 6

1 2

14

9

10

A

(9)

EXAMPLE

a

b c

i

h g f

d

e 8

8

11 4

7

4 2

7 6

1 2

14

9

10

A

(10)

EXAMPLE

a

b c

i

h g f

d

e 8

8

11 4

7

4 2

7 6

1 2

14

9

10

A

(11)

EXAMPLE

a

b c

i

h g f

d

e 8

8

11 4

7

4 2

7 6

1 2

14

9

10

A

(12)

EXAMPLE

a

b c

i

h g f

d

e 8

8

11 4

7

4 2

7 6

1 2

14

9

10

A

(13)

EXAMPLE :: INITIALIZATION

a

b c

i

h g f

d

e 8

8

11 4

7

4 2

7 6

1 2

14

9

10

a

0

b

c

d

e

f

g

h

i

Priority queue Q :

(14)

EXAMPLE :: ITERATION #1

a

b c

i

h g f

d

e 8

8

11 4

7

4 2

7 6

1 2

14

9

10

a

0

b

4

h

8

c

d

e

f

g

i

Priority queue Q :

w(a,b) = 4 w(a,h) = 8

(15)

EXAMPLE :: ITERATION #2

a

b c

i

h g f

d

e 8

8

11 4

7

4 2

7 6

1 2

14

9

10

b

4

h

8

c

8

d

e

f

g

i

Priority queue Q :

w(b,a) = 4 w(b,h) = 11

w(b,c) = 8

a not in Q 11 > key[h]

(16)

EXAMPLE :: ITERATION #3

a

b c

i

h g f

d

e 8

8

11 4

7

4 2

7 6

1 2

14

9

10

h

8

g

1

c

8

i

7

d

e

f

Priority queue Q :

w(h,a) = 8 w(h,b) = 11

w(h,i) = 7 w(h,g) = 1

a not in Q b not in Q

(17)

EXAMPLE :: ITERATION #4

a

b c

i

h g f

d

e 8

8

11 4

7

4 2

7 6

1 2

14

9

10

g

1

f

2

i

6

c

8

d

e

Priority queue Q :

w(g,h) = 1 w(g,i) = 6 w(g,f) = 2

h not in Q

(18)

EXAMPLE :: ITERATION #5

a

b c

i

h g f

d

e 8

8

11 4

7

4 2

7 6

1 2

14

9

10

f

2

c

4

i

6

e

10

d

14

Priority queue Q :

w(f,g) = 2 w(f,c) = 4 w(f,d) = 14 w(f,e) = 10

g not in Q

(19)

EXAMPLE :: ITERATION #6

a

b c

i

h g f

d

e 8

8

11 4

7

4 2

7 6

1 2

14

9

10

c

4

i

2

d

7

e

10

Priority queue Q :

w(c,b) = 8 w(c,i) = 2 w(c,f) = 4 w(c,d) = 7

b not in Q f not in Q

(20)

EXAMPLE :: ITERATION #7

a

b c

i

h g f

d

e 8

8

11 4

7

4 2

7 6

1 2

14

9

10

i

2

d

7

e

10

Priority queue Q :

w(i,h) = 7 w(i,g) = 6 w(i,c) = 2

h not in Q g not in Q c not in Q

(21)

EXAMPLE :: ITERATION #8

a

b c

i

h g f

d

e 8

8

11 4

7

4 2

7 6

1 2

14

9

10

d

7

e

9

Priority queue Q :

w(d,c) = 7 w(d,f) = 14

w(d,e) = 9

c not in Q f not in Q

(22)

EXAMPLE :: ITERATION #9

a

b c

i

h g f

d

e 8

8

11 4

7

4 2

7 6

1 2

14

9

10

e

9

Priority queue Q :

w(e,d) = 9 w(e,f) = 10

d not in Q f not in Q

(23)

RESULT #1 VS RESULT #2

a

b c

i

h g f

d

e 8

8

11 4

7

4 2

7 6

1 2

14 9

10

a

b c

i

h g f

d

e 8

8

11 4

7

4 2

7 6

1 2

14 9

10

(24)

PSEUDOCODE

1.

MST-Prim(G, w, r)

2.

for each u  V do

3.

key[u] = 

4.

parent[u] = NIL

5.

key[r] = 0

6.

Q = all vertices of G

7.

While Q ≠  do

8.

u = EXTRACT-MIN(Q)

9.

for each v adjacent to u do

10.

if v  Q and w(u,v) < key[v] then

11.

parent[v] = u

12.

key[v] = w(u,v)

Initialization :

Set all vertices’ key to 

except the root, so that it will be the first vertex processed.

•Parent of each vertex is set to NIL.

Min-priority queue Q contains all vertices.

(25)

IMPLEMENTATION

The performance of Prim’s MST depends on how we implement the min-priority queue Q

Suppose we implement Q using a min-heap:

Store vertices’ id on the heap’s array

Store vertices’ key on a separate array

Store vertices’ location on a separate array

Additionally, we need an array parent to

store the resulting tree

(26)

IMPLEMENTATION

Example:

vertex a b c d e f g h i

key 0 4 8     8 

parent NIL a b NIL NIL NIL NIL a NIL locatio

n -1 -1 2 3 4 5 6 1 7

h

8

c

8

d

e

f

g

i

idx 1 2 3 4 5 6 7 8 9

heap h c d e f g i

h

c d

e f g i

-1 means the vertex is not in

the heap

(27)

TIME COMPLEXITY

The performance of Prim’s MST depends on how we implement the min-priority queue Q

If Q is implemented using min-heap :

(V = #vertices , E = #edges)

Initialization phase :

The main loop has V iterations :

While Qu = EXTRACT-MIN(Q) do for each v adjacent to u do

if v Q and w(u,v) < key[v]

then parent[v] = u

key[v] = w(u,v) V

V.lg V E

E E

= DECREASE-KEY E lg(V) for each uV do

key[u] = 

parent[u] = NIL key[r] = 0

Q = all vertices of G

V V V 1 V

Referensi

Dokumen terkait

Dalam menyelesaikan persoalan degree constrained minimum spanning tree pada graph G ( V, E ) berbobot terhubung tak berarah merupakan permasalahan un- tuk menemukan spanning tree T di

Dalam menyelesaikan persoalan degree constrained minimum spanning tree pada graph G(V, E) berbobot terhubung tak berarah merupakan permasalahan un- tuk menemukan spanning tree T di

Implementasi sistem aplikasi algoritma Prim untuk menentukan pohon merentang minimum ( minimum spanning tree ) suatu graf berbobot dengan menggunakan program Delphi 7

Algoritma Prim, Algoritma Kruskal, dan Algoritma Sollin Untuk Menyelesaikan Masalah Minimum Spanning Tree. Jurnal

Dalam teori graf salah satu masalah optimasi adalah penentuan pohon rentang minimum, atau dikenal dengan istilah Minimum Spanning Tree (MST), dimana dapat

Perhitungan minimum spanning tree algoritma Kruskal dan algoritma Prim dimulai Tabel 2 yang merupakan urutan jarak terpendek dari pendistribusian gas 3 Kg

Aplikasi Algoritma Prim untuk Menemukan Minimum Spanning Tree Suatu Graf Berbobot dengan Menggunakan Pemrograman Berorientasi Objek.. Universitas

The LMMST algorithm proposed here calculates minimum spanning tree using k-means and prim‟s algorithm for wireless sensor network.. LMMST proposed here is novel for its implementation