Upload
audra-rogers
View
224
Download
0
Embed Size (px)
Citation preview
Shortest Paths andDijkstra’s Algorithm
CS 105
SSSPSlide 2
Single-source shortest paths
Given a weighted graph G and a source vertex v in G, determine the shortest paths from v to all other vertices in G Path length: sum of all edges in path
Useful in road map applications (e.g., google maps or map quest) for example
SSSPSlide 3
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 v, 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 Need to refine data structure so that the update of key-
values in priority queue is allowed Time complexity: O( (n + m) log n ) or O( n2 log
n ),O( n2 ) if computation of minimum is simplified
SSSPSlide 4
Dijkstra’s algorithm
ORD
BOS
PVD
JFK
BWI
MIA
DFWLAX
SFO
•
•
•
0
•
•
•
•
•
849
867
187144
1841258
1090
946
740621
1391
802
1121
2342
1235
1464
337
1846
2704
SSSPSlide 5
•
•
•
Dijkstra’s algorithm
ORD
BOS
PVD
JFK
BWI
MIA
DFWLAX
SFO
•
•
184
0
946
621
•
•
•
849
867
187144
1841258
1090
946
740621
1391
802
1121
2342
1235
1464
337
1846
2704
SSSPSlide 6
Dijkstra’s algorithm
ORD
BOS
PVD
JFK
BWI
MIA
DFWLAX
SFO
•
•
184
0
946
621
•
•
•
849
867
187144
1841258
1090
946
740621
1391
802
1121
2342
1235
1464
337
1846
2704
JFK
SSSPSlide 7
Dijkstra’s algorithm
ORD
BOS
PVD
JFK
BWI
MIA
DFWLAX
SFO
371
328
184
0
946
621
1575
•
•
849
867
187144
1841258
1090
946
740621
1391
802
1121
2342
1235
1464
337
1846
2704
PVD
SSSPSlide 8
•
Dijkstra’s algorithm
ORD
BOS
PVD
JFK
BWI
MIA
DFWLAX
SFO
371
328
184
0
946
621
1575
•
3075
849
867
187144
1841258
1090
946
740621
1391
802
1121
2342
1235
1464
337
1846
2704BOS
SSSPSlide 9
3075
1575
Dijkstra’s algorithm
ORD
BOS
PVD
JFK
BWI
MIA
DFWLAX
SFO
371
328
184
0
946
621
1423
•
2467
849
867
187144
1841258
1090
946
740621
1391
802
1121
2342
1235
1464
337
1846
2704
ORD
SSSPSlide 10
•
Dijkstra’s algorithm (cont)
ORD
BOS
PVD
JFK
BWI
MIA
DFWLAX
SFO
371
328
184
0
946
621
1423
3288
2467
849
867
187144
1841258
1090
946
740621
1391
802
1121
2342
1235
1464
337
1846
2704
MIA
SSSPSlide 11
3288
Dijkstra’s algorithm (cont)
ORD
BOS
PVD
JFK
MIA
DFWLAX
SFO
371
328
184
0
946
621
1423
2658
2467
849
867
187144
1841258
1090
946
740621
1391
802
1121
2342
1235
1464
337
1846
2704
BWI
DFW
SSSPSlide 12
Dijkstra’s algorithm (cont)
LAX
SFO
371
328
184
0
946
621
1423
2658
2467
849
867
187144
1841258
1090
946
740621
1391
802
1121
2342
1235
1464
337
1846
2704
ORD
BOS
PVD
JFK
BWI
MIA
DFW
SFO
SSSPSlide 13
Dijkstra’s algorithm (cont)
LAX
SFO
371
328
184
0
946
621
1423
2658
2467
849
867
187144
1841258
1090
946
740621
1391
802
1121
2342
1235
1464
337
1846
2704
ORD
BOS
PVD
JFK
BWI
MIA
DFWLAX