Dijkstra’s Algorithm for Single-Source Shortest Paths
The problem: Given directed graph G = (V, E),
a weight for each edge in G,
a source node v0,
Goal: determine the (length of) shortest paths from v0 to all the
remaining vertices in G
Def: Length of the path: Sum of the weight of the edges
Observation:
May have more than 1 paths between w and x (y and z)
But each individual path must be minimal length
(in order to form an overall shortest path form v0 to vi )
w x y z
V0 shortest paths from v0 to vi Vi
11/28/2024 Dr. T. Uma Devi, Department of CS, GIS, GU 1
Graphs can be used to represent the highway
structure of a state or country with vertices
representing cities and edges representing sections of
highway.
The edges can then be assigned weights which may be either the distance between
the two cities connected by the edge or the average time to drive along that section
of highway. A motorist wishing to drive from city A to B would be interested in
answers to the following questions
Is there a path from A to B?
If there is more than one path from A to B, which is the shortest path?
11/28/2024 Dr. T. Uma Devi, Department of CS, GIS, GU 2
Notation
cost adjacency matrix Cost, 1 a,b V
Cost (a, b) = cost from vertex a to vertex b if there is a edge
0 if a = b
¥ otherwise
1 if shortest path (v0 , w) is defined
s(w) = 0 otherwise
Dist ( j ) = the length of the shortest path from v0 to j
j in the vertex set V
From( j ) i
if i is the predecessor of j along the
shortest path from v0 to j
11/28/2024 Dr. T. Uma Devi, Department of CS, GIS, GU 3
45
Example:
50 10
V0 V1 V4
15 35
10
20 20
30
V2 V3 V5
15 3
v0 v1 v2 v3 v4 v5
a) Cost adjacent matrix v0 0 50 10 45
v1 0 15 10
v2 20 0 15
v3 20 0 35
v4 30 0
v5 3 0
11/28/2024 Dr. T. Uma Devi, Department of CS, GIS, GU 4
b) Steps in Dijkstra’s Algorithm
1. Dist (v0) = 0, From (v0) = v0 2. Dist (v2) = 10, From (v2) = v0
45 45
50 10 50 10
V0 V1 V0 V1 V4
V4
15
15 10
20 10 35 35
20 20
20
30 30
V2 V3 V5 V2 V3 V5
15 3 15 3
11/28/2024 Dr. T. Uma Devi, Department of CS, GIS, GU 5
3. Dist (v3) = 25, From (v3) = v2 4. Dist (v1) = 45, From (v1) = v3
45 45
50 10 50 10
V0 V1 V4 V0 V1 V4
15
35 15
20 10 35
20 10
20
20 30
30
V2 V3 V5 V2 V3 V5
15 3 15 3
11/28/2024 Dr. T. Uma Devi, Department of CS, GIS, GU 6
5. Dist (v4) = 45, From (v4) = v0 6. Dist (5) =
45
45
50 10
50 10 V0 V1
V0 V1 V4 V4
15 15
35 35
20 10 20 10
20 20
30 30
V2 V3 V5 V2 V3 V5
15 3 15 3
11/28/2024 Dr. T. Uma Devi, Department of CS, GIS, GU 7
c) Shortest paths from source v0
v0 v2 v3 v1 45
v0 v2 10
v0 v2 v3 25
v0 v4 45
v0 v5
11/28/2024 Dr. T. Uma Devi, Department of CS, GIS, GU 8
Dijkstra’s algorithm:
procedure Dijkstra (Cost, n, v, Dist, From)
// Cost, n, v are input, Dist, From are output
begin
for i 1 to n do
s (i ) 0;
Dist (i ) Cost (v, i );
From(i ) v;
s (v) 1;
for num 1 to (n – 1) do
choose u s.t. s(u) = 0 and Dist(u) is minimum;
s (u ) 1;
for all w with s(w) = 0 do
if ( Dist (u ) Cost (u , w) Dist ( w))
Dist ( w) Dist (u ) Cost (u , w);
From( w) u;
end;
11/28/2024 Dr. T. Uma Devi, Department(cont. nextGU
of CS, GIS, page) 9
Ex:
1
10 50
100 30
5 2
20
10 5
4 3
50
a) Cost adjacent matrix
01 02 03 04 05
1 0 50 30 100 10
2 0
3 5 0 50
4 20 0
5 10 0
11/28/2024 Dr. T. Uma Devi, Department of CS, GIS, GU 10
b) Steps in Dijkstra’s algorithm
1. Dist (1) = 0, From (1) = 1 2. Dist (5) = 10, From (5) = 1
1
1
10 50 10 50
100 30 30
5 2 100 2
5
20 20
10 5 10 5
4 3 4 3
50 50
11/28/2024 Dr. T. Uma Devi, Department of CS, GIS, GU 11
3. Dist (4) = 20, From (4) = 5 4. Dist (3) = 30, From (3) = 1
1 1
10 50 10 50
100 30 100 30
5 2 5 2
20 20
10 5 10 5
4 3 4 3
50 50
5. Dist (2) = 35, From (2) = 3
Shortest paths
from source 1 1
10 50
132
35 30
100 2
5
13
30 20
10 5
1 5 4
11/28/2024 4 Devi, Department of CS,3GIS, GU
Dr. T. Uma 12
20 50
11/28/2024 Dr. T. Uma Devi, Department of CS, GIS, GU 13
11/28/2024 Dr. T. Uma Devi, Department of CS, GIS, GU 14
11/28/2024 Dr. T. Uma Devi, Department of CS, GIS, GU 15
Dijkstra’s Algorithm
Algorithm Shortest Paths (int v, float cost[ ][size],float dist[ ],int n)
{
//dist[j],1≤j≤n is set to the length of the shortest path from v to j
//dist[v] is set to zero
//G is represented by its cost Adjacency matrix cost [1:n][1:n]
int u; bool s[size];
for(int i=1;i<=n;i++)
{
s[i]=false;
dist[i]=cost[v][i];
}
s[v]=true;
dist[v]=0.0;
for(int num=2; num<n; num++)
{
//choose u from among those vertexes not in such that dist[u] is minimum;
s[u]=true;//put u in s;
for(int w=1;w<=n;w++)
// for (each w adjacent to u with S[w]=false) do
//update distances.
If ((s[w]==false) && (dist[w]>dist[u]+cost[u][w]))
dist[w]=dist[u]+cost[u][w];
}
}
11/28/2024 Dr. T. Uma Devi, Department of CS, GIS, GU 16