pertemuan 24 shortest path

13
1 Pertemuan 24 Shortest Path Matakuliah : T0026/Struktur Data Tahun : 2005 Versi : 1/1

Upload: pravat

Post on 20-Jan-2016

69 views

Category:

Documents


1 download

DESCRIPTION

Pertemuan 24 Shortest Path. Matakuliah: T0026/Struktur Data Tahun: 2005 Versi: 1/1. Learning Outcomes. Pada akhir pertemuan ini, diharapkan mahasiswa akan mampu : Mahasiswa dapat memilih algoritma yang tepat dalam memecahkan masalah shortest path. Outline Materi. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Pertemuan 24 Shortest Path

1

Pertemuan 24Shortest Path

Matakuliah : T0026/Struktur Data

Tahun : 2005

Versi : 1/1

Page 2: Pertemuan 24 Shortest Path

2

Learning Outcomes

Pada akhir pertemuan ini, diharapkan mahasiswa

akan mampu :

• Mahasiswa dapat memilih algoritma yang tepat dalam memecahkan masalah shortest path

Page 3: Pertemuan 24 Shortest Path

3

Outline Materi

• Shortest paths Definition• Single source all destinations• Dijkastra's Algorithm• Implementation of Dijkastra's algorithm• Analysis of Dijkastra's algorithm• Example of Dijkastra's algorithm• All pairs shortest paths• Algorithm concept• Example of all pairs shortest paths (APSP)

Page 4: Pertemuan 24 Shortest Path

4

Single-Source Shortest-Path

Definition : Find the shortest paths from a specific source vertex to every other vertex in a weighted, directed graph. Dijkstra's algorithm solves this if all weights are nonnegative. The Bellman-Ford algorithm handles any weights.

A. Single Source All Destinations

2 313

8

105

4

15

B

E

DC

A

86

6

12

7

3

Given by this graph, which path is shortest path from A to :1. B ?

2. C ?

3. D ?

4. E ?

Page 5: Pertemuan 24 Shortest Path

5

Dijkstra’s Algorithm

void shortestpath ( int v, int cost[][MAX_VERTICES], int distance[], int n, short int found[])

{

/* distance[i] represents the shortest path from vertex v to I, found[i] holds a 0 if the shortest path from vertex v has not been found and a 1 if if has, cost is the adjacency matrix */

int i, u, w;

for (i=0; i<n; i++) {

found[i]=FALSE; /* Vertex[i] is not in S */

distance[i] = coast[v][i];

}

found[v] = TRUE;

distance[v] = 0;

for (i=0; i<n-2; i++) {

u=choose( distance, n, found);

found[u] = TRUE;

for (w=0; w<n; w++)

if (!found[w])

if (distance[u] + cost[u][w] < distance[w])

distance[w] = distance[u] + cost[u][w];

}

}

Page 6: Pertemuan 24 Shortest Path

6

int choose ( int distance[], int n, short int found[])

{

/* find the smallest distance not yet checked */

int i, min, minpos;

min = INT_MAX;

minpos = -1;

for (i=0; i<n; i++) {

if (distance[i] < min && !found[i]) {

min = distance[i];

minpos =I;

}

return minpos;

}

Page 7: Pertemuan 24 Shortest Path

7

2

3

13

8

10 5

4

B

E

DC

A

8

6

6

12

7

3

Adjacency Matrix (Cost)

A B C D E

A 0 10 8 13 999

B 12 0 999 2 7

C 6 999 0 6 999

D 15 3 8 0 4

E 999 5 999 3 0

Set of Vertices (S) = { }

0 1 2 3 4

F F F F F

Distance

0 1 2 3 4

0 10 8 13 999• Vo = A• 999 indicates the maximum number• Found [i] = False (F), vertices [i] is not in S• Found [i] = True (T), vertices [i] is in S

15

Page 8: Pertemuan 24 Shortest Path

8

i = 0, Return( 2 )

i Found [i ] Distance[ i ] Min MinPos

0 T 0 999 -1

1 F 10 999 => 10 -1 => 1

2 F 8 10 => 8 1 => 2

3 F 13 8 2

4 F 999 8 2

S= { A }

W Found[ w ] Distance[ u ] Cost[ u ][ w ] Distance [w]

0 T 8 6 0

1 F 8 999 10

2 T 8 0 8

3 F 8 6 13

4 F 8 999 999

Distance

0 1 2 3 4

0 10 8 13 999

Found [v] = True, Distance [v]=0

Found [2] = True

S= { A, C }

Page 9: Pertemuan 24 Shortest Path

9

i = 1, Return( 1 )

i Found [i ] Distance[ i ] Min MinPos

0 T 0 999 -1

1 F 10 999 => 10 -1 => 1

2 T 8 10 1

3 F 13 10 1

4 F 999 10 1

W Found[ w ] Distance[ u ] Cost[ u ][ w ] Distance [w]

0 T 10 12 0

1 T 10 0 10

2 T 10 999 8

3 F 10 2 13 => 12

4 F 10 7 999 => 17

Distance

0 1 2 3 4

0 10 8 12 17

Found [1] = True

S= { A, C }

S= { A, C, B }

Page 10: Pertemuan 24 Shortest Path

10

i = 2, Return( 3 )

i Found [i ] Distance[ i ] Min MinPos

0 T 0 999 -1

1 T 10 999 -1

2 T 8 999 -1

3 F 12 999 => 12 -1 => 3

4 F 17 13 3

W Found[ w ] Distance[ u ] Cost[ u ][ w ] Distance [w]

0 T 12 15 0

1 T 12 3 10

2 T 12 8 8

3 T 12 0 12

4 F 12 4 17 => 16

Distance

0 1 2 3 4

0 10 8 12 16

Found [3] = True

S= { A, C, B, D }

S= { A, C, B }

Page 11: Pertemuan 24 Shortest Path

11

i = 3, Return( 4 )

i Found [i ] Distance[ i ] Min MinPos

0 T 0 999 -1

1 T 10 999 -1

2 T 8 999 -1

3 T 12 999 -1

4 F 16 999 => 16 -1 => 4

W Found[ w ] Distance[ u ] Cost[ u ][ w ] Distance [w]

0 T 12 15 0

1 T 12 3 10

2 T 12 8 8

3 T 12 0 12

4 T 12 4 16

Distance

0 1 2 3 4

0 10 8 12 16

Found [4] = True

S= {A, C, B, D, E }

S= { A, C, B, D }

Page 12: Pertemuan 24 Shortest Path

12

23

13

8

10 5

4

B

E

DC

A

8

6

6

12

7

3

15

2

10

4

B

E

DC

A

8

Distance

0 1 2 3 4

0 10 8 12 16

S= {A, C, B, D, E }

Shortest-Path From Vertex A to All Destinations

Page 13: Pertemuan 24 Shortest Path

13

In the APSP problem we must find the shortest paths between all pairs of vertices, Vi, Vj, i ≠ j. We could solve this problem using shortest path with each of the vertices in V(G) as the source.

Initial 0 1 2

0 0 4 11

1 6 0 2

2 3 ∞ 0

K=2 ( V2 ) 0 1 2

0 0 4 6

1 5 0 2

2 3 7 0

K=0 ( V0 ) 0 1 2

0 0 4 11

1 6 0 2

2 3 7 0

2

4V1

V2

V0

113

6

K=1 ( V1 ) 0 1 2

0 0 4 6

1 6 0 2

2 3 7 0

Distance [i][k] + Distance [k][j] < Distance [i][j] => Distance [i][j] = Distance [i][k] + Distance [k][j]

i

j

All Pair Shortest Paths (APSP)