130 likes | 139 Views
Solve single-source shortest paths problem using Dijkstra’s algorithm in weighted graphs. Update path lengths to find shortest route efficiently. Ideal for road map applications. Implement with priority queue for O((n + m) log n) time complexity.
E N D
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
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
Dijkstra’s algorithm • 2704 BOS 867 • ORD • 849 PVD 187 1846 144 740 621 • 802 JFK SFO • 1464 184 1258 1391 337 BWI 1090 0 DFW 1235 LAX • 946 1121 • MIA 2342 •
Dijkstra’s algorithm • 2704 BOS 867 621 • ORD • 849 PVD 187 1846 144 740 621 • 802 JFK SFO • 184 1464 184 1258 1391 337 BWI 1090 0 DFW 1235 LAX • 946 1121 • MIA 2342 946 •
Dijkstra’s algorithm • 2704 BOS 867 621 ORD • 849 PVD 187 1846 144 740 621 • 802 JFK JFK SFO 184 1464 184 1258 1391 337 BWI 1090 0 DFW 1235 LAX • 946 1121 • MIA 2342 946
Dijkstra’s algorithm 371 2704 BOS 867 621 ORD 328 849 PVD PVD 187 1846 144 740 621 • 802 JFK SFO 184 1464 184 1258 1391 337 BWI 1090 0 DFW 1235 LAX 1575 946 1121 • MIA 2342 946
Dijkstra’s algorithm 371 2704 BOS BOS 867 621 ORD 328 849 PVD 187 1846 144 740 621 3075 • 802 JFK SFO 184 1464 184 1258 1391 337 BWI 1090 0 DFW 1235 LAX 1575 946 1121 • MIA 2342 946
Dijkstra’s algorithm 371 2704 BOS 867 621 ORD ORD 328 849 PVD 187 1846 144 740 621 2467 3075 802 JFK SFO 184 1464 184 1258 1391 337 BWI 1090 0 DFW 1235 LAX 1423 1575 946 1121 • MIA 2342 946
Dijkstra’s algorithm (cont) 371 2704 BOS 867 621 ORD 328 849 PVD 187 1846 144 740 621 2467 802 JFK SFO 184 1464 184 1258 1391 337 BWI 1090 0 DFW 1235 LAX 1423 946 1121 • 3288 MIA MIA 2342 946
Dijkstra’s algorithm (cont) 371 2704 BOS 867 621 ORD 328 849 PVD 187 1846 144 740 621 2467 802 JFK SFO 184 1464 184 1258 1391 337 BWI 1090 0 DFW DFW 1235 LAX 1423 946 1121 2658 3288 MIA 2342 946
Dijkstra’s algorithm (cont) 371 2704 BOS 867 621 ORD 328 849 PVD 187 1846 144 740 621 2467 802 JFK SFO SFO 184 1464 184 1258 1391 337 BWI 1090 0 DFW 1235 LAX 1423 946 1121 2658 MIA 2342 946
Dijkstra’s algorithm (cont) 371 2704 BOS 867 621 ORD 328 849 PVD 187 1846 144 740 621 2467 802 JFK SFO 184 1464 184 1258 1391 337 BWI 1090 0 DFW 1235 LAX LAX 1423 946 1121 2658 MIA 2342 946