330 likes | 732 Views
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.
E N D
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 • 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.
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
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.
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
Dijkstra’s Algorithm example 2 B C 20 3 A 4 8 E G 6 5 D 2 F
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 ?
Bellman-Ford Algorithm BellmanFord( 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 )
Bellman-Ford Algorithm example 2 B C 20 3 A 4 8 E G 6 5 D 2 F
All-Pairs Shortest Path • Repeat Single-Source Shortest Path n times • Matrix Exponentiation – Recursive Squaring • Dynamic Programming – Floyd-Warshall
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 )
References • http://en.wikipedia.org/wiki/Shortest_path_problem • Introduction to Algorithms • Thomas H. Cormen, Charles E. Leiserson, Ronald L. Rivest, Clifford Stein