1 / 13

Shortest Path

Shortest Path. Graph Theory Basics. Introduction. Weighted Graph – Edges have weights Shortest Path problem is finding a path between two vertices such that the sum of the weights of edges along the path is minimized. 2. B. C. 20. 3. A. 4. 8. E. G. 6. 5. D. 2. F. Variations.

ryder
Download Presentation

Shortest Path

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 Path Graph Theory Basics

  2. Introduction • Weighted Graph – Edges have weights • Shortest Path problem is finding a path between two vertices such that the sum of the weights of edges along the path is minimized 2 B C 20 3 A 4 8 E G 6 5 D 2 F

  3. Variations • Single-Source Shortest Path - find shortest paths from a source vertex S to all other vertices • Single-Destination Shortest Path - find shortest paths from all vertices in the directed graph to a single destination vertex D • All-Pairs Shortest Path - find shortest paths between every pair of vertices u, v in the graph.

  4. Applications • Road Network • Cities in a country can be considered as vertices and the roads connecting them are edges • Edge weight is length of the road ( distance to be travelled ) • One-way roads ( Directed Edges ) • Find shortest path from city A to city B • … many other

  5. Algorithms • Dijkstra's algorithm : single-source shortest path problem • Bellman–Ford algorithm : single-source shortest path problem if edge weights may be negative. • Floyd–Warshall algorithm : all pairs shortest paths.

  6. Dijkstra’s Algorithm Dijkstra( S ) set dist[u] = INF for all vertices u set dist[S] = 0 and insert S in a data structure q while( q is not empty ) u = remove the vertex with minimum dist in q for v in nbrs(u) newDist = dist[u] + weight(u,v) if( newDist < dist[v] ) dist[v] = newDist; end-if end-for end-while end-Diijkstra

  7. Dijkstra’s Algorithm example 2 B C 20 3 A 4 8 E G 6 5 D 2 F

  8. Dijkstra’s Algorithm Analysis • Running time depends mainly on the associated data structure q • q • Array : O(n2) • Binary Heap : O( (n+m) logn ) • Fibonacci Heap : O( m + n logn ) • What if the edge weights are negative ?

  9. Bellman-Ford Algorithm Bellman­Ford( S ) set dist[u] = INF for all u dist[S] = 0; Repeat (n-1) times for all edges (u,v) in the graph if(dist[v] > dist[u] + weight(u,v) ) dist[v] = dist[u] + graph[u][v]; end-if end-for end-repeat end-Bellman-Ford // Complexity : O( n m )

  10. Bellman-Ford Algorithm example 2 B C 20 3 A 4 8 E G 6 5 D 2 F

  11. All-Pairs Shortest Path • Repeat Single-Source Shortest Path n times • Matrix Exponentiation – Recursive Squaring • Dynamic Programming – Floyd-Warshall

  12. Floyd-Warshall Algorithm Floyd-Warshall set dist[u][v] = INF for all u,v for each edge (u,v) in the graph dist[u][v] = weight(u,v) end-for // Each stage has shortest paths using intermediate vertices (1..k−1) for k: 1 to n for u : 1 to n for v : 1 to n dist[u][v] = minimum( dist[u][v], dist[u][k] + dist[k][v] ) end-for-v end-for-u end-for-k end-floyd-warshall// Complexity : O( n3 )

  13. References • http://en.wikipedia.org/wiki/Shortest_path_problem • Introduction to Algorithms • Thomas H. Cormen, Charles E. Leiserson, Ronald L. Rivest, Clifford Stein

More Related