Shortest Paths and Dijkstra's Algorithm
description
Transcript of Shortest Paths and Dijkstra's Algorithm
![Page 1: Shortest Paths and Dijkstra's Algorithm](https://reader033.fdocuments.net/reader033/viewer/2022042423/56815fcd550346895dcecb8c/html5/thumbnails/1.jpg)
Shortest Pathsand
Dijkstra's Algorithm
CS 110: Data Structures and Algorithms
First Semester, 2010-2011
![Page 2: Shortest Paths and Dijkstra's Algorithm](https://reader033.fdocuments.net/reader033/viewer/2022042423/56815fcd550346895dcecb8c/html5/thumbnails/2.jpg)
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
![Page 3: Shortest Paths and Dijkstra's Algorithm](https://reader033.fdocuments.net/reader033/viewer/2022042423/56815fcd550346895dcecb8c/html5/thumbnails/3.jpg)
Dijkstra’s Algorithm►Solves the single-source shortest
path problem►Involves keeping a table of current
shortest path lengths from a source vertex (initialize to infinity for all vertices except v, which has length 0)
![Page 4: Shortest Paths and Dijkstra's Algorithm](https://reader033.fdocuments.net/reader033/viewer/2022042423/56815fcd550346895dcecb8c/html5/thumbnails/4.jpg)
Dijkstra’s Algorithm►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
![Page 5: Shortest Paths and Dijkstra's Algorithm](https://reader033.fdocuments.net/reader033/viewer/2022042423/56815fcd550346895dcecb8c/html5/thumbnails/5.jpg)
Dijkstra’s Algorithm►Can make use of a priority queue to
facilitate selection of shortest lengths►Need to refine the data structure to
allow the updating of key values►Time complexity: O(n+m) or O(n2 log
n)►O(n2 ) if computation of minimum is
simplified
![Page 6: Shortest Paths and Dijkstra's Algorithm](https://reader033.fdocuments.net/reader033/viewer/2022042423/56815fcd550346895dcecb8c/html5/thumbnails/6.jpg)
MIA
JFK
PVD
BOS
DFW
SFO
LAX
BWI
ORD
2704
1846
337
1235
1464
802621
2342
1391
1121
144
849
740
867
187
946
184
1090
1258
Dijkstra’s Algorithm
![Page 7: Shortest Paths and Dijkstra's Algorithm](https://reader033.fdocuments.net/reader033/viewer/2022042423/56815fcd550346895dcecb8c/html5/thumbnails/7.jpg)
MIA
JFK
PVD
BOS
DFW
SFO
LAX
BWI
ORD
2704
1846
337
1235
1464
802621
2342
1391
1121
144
849
740
867
187
946
184
1090
1258
∞
∞
∞
∞∞
∞
∞
∞
0
Dijkstra’s Algorithm
![Page 8: Shortest Paths and Dijkstra's Algorithm](https://reader033.fdocuments.net/reader033/viewer/2022042423/56815fcd550346895dcecb8c/html5/thumbnails/8.jpg)
MIA
JFK
PVD
BOS
DFW
SFO
LAX
BWI
ORD
2704
1846
337
1235
1464
802621
2342
1391
1121
144
849
740
867
187
946
184
1090
1258
∞
621
∞
∞946
∞
184
∞
0
Dijkstra’s Algorithm
![Page 9: Shortest Paths and Dijkstra's Algorithm](https://reader033.fdocuments.net/reader033/viewer/2022042423/56815fcd550346895dcecb8c/html5/thumbnails/9.jpg)
MIA
JFK
PVD
BOS
DFW
SFO
LAX
BWI
ORD
2704
1846
337
1235
1464
802621
2342
1391
1121
144
849
740
867
187
946
184
1090
1258
∞
621
∞
∞946
∞
184
∞
0
Dijkstra’s Algorithm
![Page 10: Shortest Paths and Dijkstra's Algorithm](https://reader033.fdocuments.net/reader033/viewer/2022042423/56815fcd550346895dcecb8c/html5/thumbnails/10.jpg)
MIA
JFK
PVD
BOS
DFW
SFO
LAX
BWI
ORD
2704
1846
337
1235
1464
802621
2342
1391
1121
144
849
740
867
187
946
184
1090
1258
328
621
∞
∞946
371
184
1575
0
Dijkstra’s Algorithm
![Page 11: Shortest Paths and Dijkstra's Algorithm](https://reader033.fdocuments.net/reader033/viewer/2022042423/56815fcd550346895dcecb8c/html5/thumbnails/11.jpg)
MIA
JFK
PVD
BOS
DFW
SFO
LAX
BWI
ORD
2704
1846
337
1235
1464
802621
2342
1391
1121
144
849
740
867
187
946
184
1090
1258
328
621
∞
∞946
371
184
1575
0
Dijkstra’s Algorithm
![Page 12: Shortest Paths and Dijkstra's Algorithm](https://reader033.fdocuments.net/reader033/viewer/2022042423/56815fcd550346895dcecb8c/html5/thumbnails/12.jpg)
MIA
JFK
PVD
BOS
DFW
SFO
LAX
BWI
ORD
2704
1846
337
1235
1464
802621
2342
1391
1121
144
849
740
867
187
946
184
1090
1258
328
621
∞
∞946
371
184
1575
0
Dijkstra’s Algorithm
![Page 13: Shortest Paths and Dijkstra's Algorithm](https://reader033.fdocuments.net/reader033/viewer/2022042423/56815fcd550346895dcecb8c/html5/thumbnails/13.jpg)
MIA
JFK
PVD
BOS
DFW
SFO
LAX
BWI
ORD
2704
1846
337
1235
1464
802621
2342
1391
1121
144
849
740
867
187
946
184
1090
1258
328
621
∞
∞946
371
184
1575
0
Dijkstra’s Algorithm
![Page 14: Shortest Paths and Dijkstra's Algorithm](https://reader033.fdocuments.net/reader033/viewer/2022042423/56815fcd550346895dcecb8c/html5/thumbnails/14.jpg)
MIA
JFK
PVD
BOS
DFW
SFO
LAX
BWI
ORD
2704
1846
337
1235
1464
802621
2342
1391
1121
144
849
740
867
187
946
184
1090
1258
328
621
3075
∞946
371
184
1575
0
Dijkstra’s Algorithm
![Page 15: Shortest Paths and Dijkstra's Algorithm](https://reader033.fdocuments.net/reader033/viewer/2022042423/56815fcd550346895dcecb8c/html5/thumbnails/15.jpg)
MIA
JFK
PVD
BOS
DFW
SFO
LAX
BWI
ORD
2704
1846
337
1235
1464
802621
2342
1391
1121
144
849
740
867
187
946
184
1090
1258
328
621
3075
∞946
371
184
1575
0
Dijkstra’s Algorithm
![Page 16: Shortest Paths and Dijkstra's Algorithm](https://reader033.fdocuments.net/reader033/viewer/2022042423/56815fcd550346895dcecb8c/html5/thumbnails/16.jpg)
MIA
JFK
PVD
BOS
DFW
SFO
LAX
BWI
ORD
2704
1846
337
1235
1464
802621
2342
1391
1121
144
849
740
867
187
946
184
1090
1258
328
621
2467
∞946
371
184
1423
0
Dijkstra’s Algorithm
![Page 17: Shortest Paths and Dijkstra's Algorithm](https://reader033.fdocuments.net/reader033/viewer/2022042423/56815fcd550346895dcecb8c/html5/thumbnails/17.jpg)
MIA
JFK
PVD
BOS
DFW
SFO
LAX
BWI
ORD
2704
1846
337
1235
1464
802621
2342
1391
1121
144
849
740
867
187
946
184
1090
1258
328
621
2467
∞946
371
184
1423
0
Dijkstra’s Algorithm
![Page 18: Shortest Paths and Dijkstra's Algorithm](https://reader033.fdocuments.net/reader033/viewer/2022042423/56815fcd550346895dcecb8c/html5/thumbnails/18.jpg)
MIA
JFK
PVD
BOS
DFW
SFO
LAX
BWI
ORD
2704
1846
337
1235
1464
802621
2342
1391
1121
144
849
740
867
187
946
184
1090
1258
328
621
2467
3288
946
371
184
1423
0
Dijkstra’s Algorithm
![Page 19: Shortest Paths and Dijkstra's Algorithm](https://reader033.fdocuments.net/reader033/viewer/2022042423/56815fcd550346895dcecb8c/html5/thumbnails/19.jpg)
MIA
JFK
PVD
BOS
DFW
SFO
LAX
BWI
ORD
2704
1846
337
1235
1464
802621
2342
1391
1121
144
849
740
867
187
946
184
1090
1258
328
621
2467
3288
946
371
184
1423
0
Dijkstra’s Algorithm
![Page 20: Shortest Paths and Dijkstra's Algorithm](https://reader033.fdocuments.net/reader033/viewer/2022042423/56815fcd550346895dcecb8c/html5/thumbnails/20.jpg)
MIA
JFK
PVD
BOS
DFW
SFO
LAX
BWI
ORD
2704
1846
337
1235
1464
802621
2342
1391
1121
144
849
740
867
187
946
184
1090
1258
328
621
2467
3288
946
371
184
1423
0
Dijkstra’s Algorithm
![Page 21: Shortest Paths and Dijkstra's Algorithm](https://reader033.fdocuments.net/reader033/viewer/2022042423/56815fcd550346895dcecb8c/html5/thumbnails/21.jpg)
MIA
JFK
PVD
BOS
DFW
SFO
LAX
BWI
ORD
2704
1846
337
1235
1464
802621
2342
1391
1121
144
849
740
867
187
946
184
1090
1258
328
621
2467
2658
946
371
184
1423
0
Dijkstra’s Algorithm
![Page 22: Shortest Paths and Dijkstra's Algorithm](https://reader033.fdocuments.net/reader033/viewer/2022042423/56815fcd550346895dcecb8c/html5/thumbnails/22.jpg)
Pseudo-Code: Dijkstrafunction Dijkstra( Graph g, Vertex source )
for each vertex v in gdist[v] <-- infinityprevious[v] <-- undefined
dist[source] <-- 0Q <-- the set of all nodes in Graphwhile Q is not empty
u <-- vertex in Q with smallest dist[]if dist[u] == infinity
breakremove u from Qfor each neighbor v of u
alt = dist[u] + dist_between( u, v )if alt < dist[v]
dist[v] = altprevious[v] = u
return dist[]