1
Dijkstra’s Algorithm
Data Structures & Algorithms
2
Single-Source Shortest-Paths Problem
 Problem: given a connected graph G with non-negative weights
on the edges and a source vertex s in G, determine the shortest
paths from s to all other vertices in G.
 Useful in many applications (e.g., road map applications)
14
10
3
6 4
5
2
9
15
8
3
Single-Source Shortest-Paths
 For instance, the following edges form the shortest paths from node A
H B C
G E D
F
A
14
10
3
6 4
5
2
9
15
8
4
Single-Source Shortest-Paths
H B C
G E D
F
A
14
10
3
6 4
5
2
9
15
8
• Hint: does this problem looks similar?
• Can we modify Prim’s algorithm to solve this problem?
5
Dijkstra’s Algorithm
 Solves the single-source shortest paths problem
 Involves keeping a table of current shortest path lengths from
source vertex (initialize to infinity for all vertices except s, which
has length 0)
 Repeatedly select the vertex u with shortest path length, and
update other lengths by considering the path that passes through
that vertex
 Stop when all vertices have been selected
 Could use a priority queue to facilitate selection of shortest lengths
» Here, priority is defined differently from that of the Prim’s algorithm
» Like Prim’s algorithm, it needs to refine data structure so that the update of
key-values in priority queue is allowed
» Like Prim’s algorithm, time complexity is O( (n + m) log n )
6
Dijkstra’s algorithm
ORD
BOS
PVD
JFK
BWI
MIA
DFW
LAX
SFO
•
•
•
0
•
•
•
•
•
849
867
187
144
184
1258
1090
946
740
621
1391
802
1121
2342
1235
1464
337
1846
2704
7
•
•
•
Dijkstra’s algorithm
ORD
BOS
PVD
JFK
BWI
MIA
DFW
LAX
SFO
•
•
184
0
946
621
•
•
•
849
867
187
144
184
1258
1090
946
740
621
1391
802
1121
2342
1235
1464
337
1846
2704
8
Dijkstra’s algorithm
ORD
BOS
PVD
JFK
BWI
MIA
DFW
LAX
SFO
•
•
184
0
946
621
•
•
•
849
867
187
144
184
1258
1090
946
740
621
1391
802
1121
2342
1235
1464
337
1846
2704
JFK
9
Dijkstra’s algorithm
ORD
BOS
PVD
JFK
BWI
MIA
DFW
LAX
SFO
371
328
184
0
946
621
1575
•
•
849
867
187
144
184
1258
1090
946
740
621
1391
802
1121
2342
1235
1464
337
1846
2704
PVD
10
•
Dijkstra’s algorithm
ORD
BOS
PVD
JFK
BWI
MIA
DFW
LAX
SFO
371
328
184
0
946
621
1575
•
3075
849
867
187
144
184
1258
1090
946
740
621
1391
802
1121
2342
1235
1464
337
1846
2704
BOS
11
3075
1575
Dijkstra’s algorithm
ORD
BOS
PVD
JFK
BWI
MIA
DFW
LAX
SFO
371
328
184
0
946
621
1423
•
2467
849
867
187
144
184
1258
1090
946
740
621
1391
802
1121
2342
1235
1464
337
1846
2704
ORD
12
•
Dijkstra’s algorithm (cont)
ORD
BOS
PVD
JFK
BWI
MIA
DFW
LAX
SFO
371
328
184
0
946
621
1423
3288
2467
849
867
187
144
184
1258
1090
946
740
621
1391
802
1121
2342
1235
1464
337
1846
2704
MIA
13
3288
Dijkstra’s algorithm (cont)
ORD
BOS
PVD
JFK
MIA
DFW
LAX
SFO
371
328
184
0
946
621
1423
2658
2467
849
867
187
144
184
1258
1090
946
740
621
1391
802
1121
2342
1235
1464
337
1846
2704
BWI
DFW
14
Dijkstra’s algorithm (cont)
LAX
SFO
371
328
184
0
946
621
1423
2658
2467
849
867
187
144
184
1258
1090
946
740
621
1391
802
1121
2342
1235
1464
337
1846
2704
ORD
BOS
PVD
JFK
BWI
MIA
DFW
SFO
15
Dijkstra’s algorithm (cont)
LAX
SFO
371
328
184
0
946
621
1423
2658
2467
849
867
187
144
184
1258
1090
946
740
621
1391
802
1121
2342
1235
1464
337
1846
2704
ORD
BOS
PVD
JFK
BWI
MIA
DFW
LAX
16
In-Class Exercises

Dijkstra_Algorithm with illustarted example