Matakuliah
Tahun
Versi
: T0026/Struktur Data
: 2005
: 1/1
Pertemuan 24
Shortest Path
1
Learning Outcomes
Pada akhir pertemuan ini, diharapkan mahasiswa
akan mampu :
• Mahasiswa dapat memilih algoritma yang
tepat dalam memecahkan masalah
shortest path
2
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)
3
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
12
A
B
10
5
7
68
15
13
3
2
E
4
6
C
8
3
D
Given by this graph, which path is shortest path from A to :
1.
2.
3.
4.
B?
C?
D?
E?
4
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];
}
}
5
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;
}
*/
6
12
A
6
B
10
5
7
13
15
2
E
3
8
4
6
C
3
D
8
Adjacency Matrix (Cost)
Set of Vertices (S) = { }
A
B
C
D
E
A
0
10
8
13
999
0
1
2
3
4
B
12
0
999
2
7
F
F
F
F
F
C
6
999
0
6
999
D
15
3
8
0
4
E
999
5
999
3
0
•
•
•
•
Distance
0
1
2
3
4
0
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
10
8
13
999
7
Found [v] = True, Distance [v]=0
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 }
Found [2] = True
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
0
1
2
3
4
4
F
8
999
999
0
10
8
13
999
S= { A, C }
Distance
8
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
S= { A, C }
Found [1] = True
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
0
1
2
3
4
4
F
10
7
999 => 17
0
10
8
12
17
S= { A, C, B }
Distance
9
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
S= { A, C, B }
Found [3] = True
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
0
1
2
3
4
4
F
12
4
17 => 16
0
10
8
12
16
S= { A, C, B, D }
Distance
10
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
S= { A, C, B, D }
Found [4] = True
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
0
1
2
3
4
4
T
12
4
16
0
10
8
12
16
S= {A, C, B, D, E }
Distance
11
Shortest-Path From Vertex A to All Destinations
12
A
B
10
6
13
15
7
2
3
8
6
8
C
5
E
4
3
D
S= {A, C, B, D, E }
A
B
10
Distance
2
E
8
C
4
0
1
2
3
4
0
10
8
12
16
D
12
All Pair Shortest Paths (APSP)
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.
j
6
V0
3
V1
4
11
Initial
0
1
2
0
0
4
11
1
6
0
2
2
3
∞
0
i
2
V2
K=0 ( V0 )
0
1
2
0
0
4
11
1
6
0
2
2
3
7
0
K=1 ( V1 )
0
1
2
K=2 ( V2 )
0
1
2
0
0
4
6
0
0
4
6
1
6
0
2
1
5
0
2
2
3
7
0
2
3
7
0
Distance [i][k] + Distance [k][j] < Distance [i][j] => Distance [i][j] = Distance [i][k] + Distance [k][j]
13
© Copyright 2026 Paperzz