DIJKSTRA’S
SINGLE SOURCE SHORTEST PATH
Informatics Department
Parahyangan Catholic University
SHORTEST PATH
Given a graph G, a source vertex s, and a destination vertex d. Find a shortest path between s and d.
Unweighted Graph
Can be solved using simple BFS from s
Weighted Graph
Bellman-Ford Algorithm (single source)
Dijkstra’s Algorithm (no negative cycle)
Floyd-Warshall Algorithm (all-pairs shortest path)
etc.
SINGLE SOURCE SHORTEST PATH
Given a graph G and a source vertex s. Find the shortest path from s to all other vertices.
Can solve one-pair shortest path problem.
Very similar to Prim’s MST Algorithm
RECALL, PRIM’S MST
At each step, a light edge is added to the tree A that connects A to an isolated vertex
A
b
d a
c 14
8 5
22
Light edge ≡ edge with smallest weight that connects A
to an isolated vertex
DIJKSTRA’S ALGORITHM
Maintain a set A of vertices whose shortest path (from source vertex s) is already known
a
b
d c
s
4 8
2 3
DIJKSTRA’S ALGORITHM
On each iteration, pick a vertex u S with the smallest shortest path estimate
b
’
d
’ a
’
c
’ 14
8 22
5 a
b
d c
s
4 8
2 3
sse = 2+22 = 24
sse = 3+8 = 11
sse = 8+5 = 13 sse = 4+14 = 18
SHORTEST PATH ESTIMATE
d
’ 8
5 a
b c
s
8
2 3
4
sse is calculated as minimum of
•2+4 = 6
•3+8 = 11
•8+5 = 13
= 6
sse is calculated as minimum of
•2+4 = 6
•3+8 = 11
•8+5 = 13
= 6
EXAMPLE
a 0
b c
i
h g f
d
e 8
8
11 4
7
4 2
7 6
1 2
14
9
10
A
EXAMPLE
a 0
b
4 c
i
h
8 g f
d
e 8
8
11 4
7
4 2
7 6
1 2
14
9
10
A
EXAMPLE
a 0
b 4
c 12
i
h
8 g f
d
e
8
8
11 4
7
4 2
7 6
1 2
14
9
10
A
distance(a,h)via b is 4+11, which is worse than directly a-h (8)
EXAMPLE
a 0
b 4
c 12
i 15
h 8
g
9 f
d
e
8
8
11 4
7
4 2
7 6
1 2
14
9
10
A
EXAMPLE
a 0
b 4
c 12
i 15
h 8
g 9
f 11 d
e
8
8
11 4
7
4 2
7 6
1 2
14
9
10
A
distance(a,i) via g is 9+6, which is equally good as via h (8+7)
EXAMPLE
a 0
b 4
c 12
i 15
h 8
g 9
f 11 d 25
e 21
8
8
11 4
7
4 2
7 6
1 2
14
9
10
A
EXAMPLE
a 0
b 4
c 12
i 14
h 8
g 9
f 11 d 19
e 21
8
8
11 4
7
4 2
7 6
1 2
14
9
10
A
distance(a,i) via c is 12+2, which is better than via h (15) distance(a,d) via c is 12+7, which is better than via f (25)
EXAMPLE
a 0
b 4
c 12
i 14
h 8
g 9
f 11 d 19
e 21
8
8
11 4
7
4 2
7 6
1 2
14
9
10
A
EXAMPLE
a 0
b 4
c 12
i 14
h 8
g 9
f 11 d 19
e 21
8
8
11 4
7
4 2
7 6
1 2
14
9
10
A
distance(a,e) via d is 19+9, which is worse than via f (21)
EXAMPLE
a 0
b 4
c 12
i 14
h 8
g 9
f 11 d 19
e 21
8
8
11 4
7
4 2
7 6
1 2
14
9
10
A
PSEUDOCODE
1. Dijkstra-SSSP(G, w, s)
2. for each u V do
3. dist[u] =
4. parent[u] = NIL
5. dist[s] = 0
6. Q = all vertices of G
7. While Q ≠ do
8. u = EXTRACT-MIN(Q)
9. for each v adjacent to u do
10. if vQ and dist[u]+w(u,v)<dist[v] then
11. parent[v] = u
12. dist[v] = dist[u]+w(u,v)
Initialization :
•Set all vertices’ dist to
except the source, so that it will be the first vertex processed.
•Parent of each vertex is set to NIL.
•Min-priority queue Q contains all vertices.
IMPLEMENTATION
The performance of Dijkstra’s Algorithm depends on how we implement the min- priority queue Q (very similar analysis to Prim’s MST)
Suppose we implement Q using a min-heap:
Store vertices’ id on the heap’s array
Store vertices’ dist on a separate array
Store vertices’ location on a separate array
Additionally, we need an array parent to store the resulting tree