Dijkstras Shortest Path
Dijkstras Shortest Path
5 7
y z 2
2
Some Applications
●
Finding shortest path between citites in a
Google Map
●
Routing of packets in computer network
3
Data Structure
●
Maintain two sets:
● S : set of vertices whose shortest path is
determined
● Q : set of vertices whose shortest path is yet to
be determined. It is implemented as a minimum-
priority queue, keyed by the distance value
●
Initially S is empty and Q = V (all vertices in G)
4
Initial Setup
∞ ∞
1 x
S: { }
t
source 10
9 Q s t y x z
2 3 4 6
s
Vertex with minimum key
0 7
5
y z
2
Vertex in set S (Processed)
∞ ∞
s t y x z
Vertex in Q (To be processed)
0 ∞ ∞ ∞ ∞
Distance
Edge with shortest length
Parent - - - - -
5
Working of Dijkstra’s
∞ ∞
1 x
S: { }
t
source 10
9 Q s t y x z
2 3 4 6
s
Idea:
0 7 1. Extract Minimum from Q (say u)
5
2. For each neighbour (v) of u
y z
2 Update key of v(if required)
∞ ∞
s t y x z
0 ∞ ∞ ∞ ∞
Distance
Parent - - - - -
6
Working of Dijkstra’s
10 ∞
∞
S s
1 x
t
source 10
9 Q t y x z
2 3 4 6
s
0 7
5
y z
2
∞ ∞
5
s t y x z
0 10 5 ∞ ∞
Distance
Parent - s s - -
7
Working of Dijkstra’s
10 ∞
∞
S s
1 x
t
source 10
9 Q t y x z
2 3 4 6
s
0 7
5
y z
2
∞ ∞
5
s t y x z
0 10 5 ∞ ∞
Distance
Parent - s s - -
8
Working of Dijkstra’s
10 8 ∞ 14
S s y
1 x
t
source 10
9 Q t x z
2 3 4 6
s
0 7
5
y z
2
5 ∞7
s t y x z
0 8 5 14 7
Distance
Parent - y s y y
9
Working of Dijkstra’s
8 14
S s y
1 x
t
source 10
9 Q t x z
2 3 4 6
s
0 7
5
y z
2
5 7
s t y x z
0 8 5 14 7
Distance
Parent - y s y y
10
Working of Dijkstra’s
8 14 13
S s y z
1 x
t
source 10
9 Q t x
2 3 4 6
s
0 7
5
y z
2
5 7
s t y x z
0 8 5 13 7
Distance
Parent - y s z y
11
Working of Dijkstra’s
8 13
S s y z
1 x
t
source 10
9 Q t x
2 3 4 6
s
0 7
5
y z
2
5 7
s t y x z
0 8 5 13 7
Distance
Parent - y s y z
12
Working of Dijkstra’s
8 13 9
S s y z t
1 x
t
source 10
9 Q x
2 3 4 6
s
0 7
5
y z
2
5 7
s t y x z
0 8 5 9 7
Distance
Parent - y s t y
13
Working of Dijkstra’s
8 9
S s y z t
1 x
t
source 10
9 Q x
2 3 4 6
s
0 7
5
y z
2
5 7
s t y x z
0 8 5 9 7
Distance
Parent - y s t y
14
Working of Dijkstra’s
8 9
S s y z t x
1 x
t
source 10
9 Q: { }
2 3 4 6
s
0 7
5
y z
2
5 7
s t y x z
0 8 5 9 7
Distance
Parent - y s t y
15
Dijkstra’s Algorithm (Pseudocode)
Dijkstra(G,w,s)
{
for each vertex v in V
{
v.d = ∞
v.parent = NIL
}
s.d = 0
S = { }
Q = G.V
while(Q is not empty)
{
u = EXTRACT-MIN(Q)
for each neighbour v in Adjacency[u]
{
if(v.d > u.d+w(u,v))
{
v.d = u.d + w(u,v)
v.parent = u
}
}
}
}
16
Time Complexity
( Q is an array)
Dijkstra(G,w,s)
{
for each vertex v in V Time required:
{
O(V) =O(V) + O(V) + O(V). (O(V)+O(E’))
v.d =
v.parent
∞ = NIL =O(V) + O(V) + O(V2)+ O(V.E’)
} =O(V) + O(V) + O(V2)+ O(E)
s.d = 0 = O(V2)
S={}
Q = G.V O(V)
while(Q is not empty) O(V)
{
u = EXTRACT-MIN(Q) O(V)
for each neighbour v in Adjacency[u] O(E’), where
{ E’: number of adjacent
if(v.d > u.d+w(u,v))
{ edges
v.d = u.d + w(u,v) O(1)
v.parent = u
}
}
}
}
17
Time Complexity
( Q is a binary heap)
Dijkstra(G,w,s)
{
for each vertex v in V Time required:
{ =O(V) + O(V) + O(V). (O(lg V)+O(E’ lg V))
v.d = O(V) =O(V) + O(V) + O(V lg V)+ O(V.E’ lg V)
v.parent
∞ = NIL =O(V) + O(V) + O(V lg V)+ O(E lg V)
} = O(E lg V)
s.d = 0 S
={} Q=
G.V O(V)
while(Q is not empty) O(V)
{
u = EXTRACT-MIN(Q) O(lg V)
for each neighbour v in Adjacency[u] O(E’), where
{ E’: number of adjacent
if(v.d > u.d+w(u,v))
{ edges
v.d = u.d + w(u,v) O(lg V)
v.parent = u
}
}
}
}
18
References
1).Introduction to Algorithms, 3rd edition by
Corment et. al.
2).Introduction to The Design and Analysis of
Algorithms, 3rd Edition by Anany Levitin
3).Data Structures and Algorithms, Alfred V. Aho
et. al
19