Download presentation
Presentation is loading. Please wait.
1
1 Pertemuan 24 Shortest Path Matakuliah: T0026/Struktur Data Tahun: 2005 Versi: 1/1
2
2 Learning Outcomes Pada akhir pertemuan ini, diharapkan mahasiswa akan mampu : Mahasiswa dapat memilih algoritma yang tepat dalam memecahkan masalah shortest path
3
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)
4
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 3 13 8 10 5 4 15 B E DC A 8 6 6 12 7 3 Given by this graph, which path is shortest path from A to : 1.B ? 2.C ? 3.D ? 4.E ?
5
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]; }
6
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; }
7
7 2 3 13 8 10 5 4 B E DC A 8 6 6 12 7 3 Adjacency Matrix (Cost) ABCDE A010813999 B12099927 C6 06 D153804 E9995 30 Set of Vertices (S) = { } 01234 FFFFF Distance 01234 010813999 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
8
8 i = 0, Return( 2 ) iFound [i ]Distance[ i ]MinMinPos 0T0999 1F10999 => 10-1 => 1 2F810 => 81 => 2 3F1382 4F99982 S= { A } WFound[ w ]Distance[ u ]Cost[ u ][ w ]Distance [w] 0T860 1F899910 2T808 3F8613 4F8999 Distance 01234 010813999 Found [v] = True, Distance [v]=0 Found [2] = True S= { A, C }
9
9 i = 1, Return( 1 ) iFound [i ]Distance[ i ]MinMinPos 0T0999 1F10999 => 10-1 => 1 2T8101 3F13101 4F999101 WFound[ w ]Distance[ u ]Cost[ u ][ w ]Distance [w] 0T10120 1T10 0 2T 9998 3F10213 => 12 4F107999 => 17 Distance 01234 01081217 Found [1] = True S= { A, C } S= { A, C, B }
10
10 i = 2, Return( 3 ) iFound [i ]Distance[ i ]MinMinPos 0T0999 1T10999 2T8999 3F12999 => 12-1 => 3 4F1713 3 WFound[ w ]Distance[ u ]Cost[ u ][ w ]Distance [w] 0T12150 1T12 3 10 2T 128 8 3T 0 4F 4 17 => 16 Distance 01234 01081216 Found [3] = True S= { A, C, B, D } S= { A, C, B }
11
11 i = 3, Return( 4 ) iFound [i ]Distance[ i ]MinMinPos 0T0999 1T10999 2T8999 3T12999 4F16999 => 16 -1 => 4 WFound[ w ]Distance[ u ]Cost[ u ][ w ]Distance [w] 0T12150 1T12 3 10 2T 128 8 3T 0 4T 4 16 Distance 01234 01081216 Found [4] = True S= {A, C, B, D, E } S= { A, C, B, D }
12
12 2 3 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 01234 01081216 S= {A, C, B, D, E } Shortest-Path From Vertex A to All Destinations
13
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 012 00411 1 602 23 ∞ 0 K=2 ( V 2 ) 012 0046 1 502 23 7 0 K=0 ( V 0 ) 012 00411 1 602 23 7 0 2 4 V1V1 V2V2 V0V0 3 6 K=1 ( V 1 ) 012 0046 1 602 23 7 0 Distance [i][k] + Distance [k][j] Distance [i][j] = Distance [i][k] + Distance [k][j] i j All Pair Shortest Paths (APSP)
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.