1 / 22

Shortest Paths and Dijkstra's Algorithm

Shortest Paths and Dijkstra's Algorithm. CS 110: Data Structures and Algorithms First Semester, 2010-2011. 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

kiara
Download Presentation

Shortest Paths and Dijkstra's Algorithm

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Shortest PathsandDijkstra's Algorithm CS 110: Data Structures and Algorithms First Semester, 2010-2011

  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

  3. 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)

  4. 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

  5. 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

  6. Dijkstra’s Algorithm 2704 BOS 867 849 1846 ORD PVD 187 740 621 802 144 JFK SFO 1258 1391 184 1464 BWI 1090 337 DFW 946 1235 LAX 1121 MIA 2342

  7. Dijkstra’s Algorithm ∞ 2704 BOS 867 ∞ 849 1846 ORD PVD 187 740 ∞ 621 802 144 JFK ∞ ∞ SFO 1258 1391 0 184 1464 BWI 1090 337 DFW ∞ 946 1235 LAX 1121 ∞ MIA 2342 ∞

  8. Dijkstra’s Algorithm ∞ 2704 BOS 867 621 849 1846 ORD PVD 187 740 ∞ 621 802 144 JFK ∞ 184 SFO 1258 1391 0 184 1464 BWI 1090 337 DFW ∞ 946 1235 LAX 1121 ∞ MIA 2342 946

  9. Dijkstra’s Algorithm ∞ 2704 BOS 867 621 849 1846 ORD PVD 187 740 ∞ 621 802 144 JFK ∞ 184 SFO 1258 1391 0 184 1464 BWI 1090 337 DFW ∞ 946 1235 LAX 1121 ∞ MIA 2342 946

  10. Dijkstra’s Algorithm 371 2704 BOS 867 621 849 1846 ORD PVD 187 740 328 621 802 144 JFK ∞ 184 SFO 1258 1391 0 184 1464 BWI 1090 337 DFW 1575 946 1235 LAX 1121 ∞ MIA 2342 946

  11. Dijkstra’s Algorithm 371 2704 BOS 867 621 849 1846 ORD PVD 187 740 328 621 802 144 JFK ∞ 184 SFO 1258 1391 0 184 1464 BWI 1090 337 DFW 1575 946 1235 LAX 1121 ∞ MIA 2342 946

  12. Dijkstra’s Algorithm 371 2704 BOS 867 621 849 1846 ORD PVD 187 740 328 621 802 144 JFK ∞ 184 SFO 1258 1391 0 184 1464 BWI 1090 337 DFW 1575 946 1235 LAX 1121 ∞ MIA 2342 946

  13. Dijkstra’s Algorithm 371 2704 BOS 867 621 849 1846 ORD PVD 187 740 328 621 802 144 JFK ∞ 184 SFO 1258 1391 0 184 1464 BWI 1090 337 DFW 1575 946 1235 LAX 1121 ∞ MIA 2342 946

  14. Dijkstra’s Algorithm 371 2704 BOS 867 621 849 1846 ORD PVD 187 740 328 621 802 144 JFK 3075 184 SFO 1258 1391 0 184 1464 BWI 1090 337 DFW 1575 946 1235 LAX 1121 ∞ MIA 2342 946

  15. Dijkstra’s Algorithm 371 2704 BOS 867 621 849 1846 ORD PVD 187 740 328 621 802 144 JFK 3075 184 SFO 1258 1391 0 184 1464 BWI 1090 337 DFW 1575 946 1235 LAX 1121 ∞ MIA 2342 946

  16. Dijkstra’s Algorithm 371 2704 BOS 867 621 849 1846 ORD PVD 187 740 328 621 802 144 JFK 2467 184 SFO 1258 1391 0 184 1464 BWI 1090 337 DFW 1423 946 1235 LAX 1121 ∞ MIA 2342 946

  17. Dijkstra’s Algorithm 371 2704 BOS 867 621 849 1846 ORD PVD 187 740 328 621 802 144 JFK 2467 184 SFO 1258 1391 0 184 1464 BWI 1090 337 DFW 1423 946 1235 LAX 1121 ∞ MIA 2342 946

  18. Dijkstra’s Algorithm 371 2704 BOS 867 621 849 1846 ORD PVD 187 740 328 621 802 144 JFK 2467 184 SFO 1258 1391 0 184 1464 BWI 1090 337 DFW 1423 946 1235 LAX 1121 3288 MIA 2342 946

  19. Dijkstra’s Algorithm 371 2704 BOS 867 621 849 1846 ORD PVD 187 740 328 621 802 144 JFK 2467 184 SFO 1258 1391 0 184 1464 BWI 1090 337 DFW 1423 946 1235 LAX 1121 3288 MIA 2342 946

  20. Dijkstra’s Algorithm 371 2704 BOS 867 621 849 1846 ORD PVD 187 740 328 621 802 144 JFK 2467 184 SFO 1258 1391 0 184 1464 BWI 1090 337 DFW 1423 946 1235 LAX 1121 3288 MIA 2342 946

  21. Dijkstra’s Algorithm 371 2704 BOS 867 621 849 1846 ORD PVD 187 740 328 621 802 144 JFK 2467 184 SFO 1258 1391 0 184 1464 BWI 1090 337 DFW 1423 946 1235 LAX 1121 2658 MIA 2342 946

  22. Pseudo-Code: Dijkstra function Dijkstra( Graph g, Vertex source ) for each vertex v in g dist[v] <-- infinity previous[v] <-- undefined dist[source] <-- 0 Q <-- the set of all nodes in Graph while Q is not empty u <-- vertex in Q with smallest dist[] if dist[u] == infinity break remove u from Q for each neighbor v of u alt = dist[u] + dist_between( u, v ) if alt < dist[v] dist[v] = alt previous[v] = u return dist[]

More Related