Download presentation
Presentation is loading. Please wait.
1
Shortest Path Graph Theory Basics Anil Kishore
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 … many other
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
B C D E F G 20 4 6 2 5 8 3
8
Dijkstra’s Algorithm example
B C D E F G 20 4 6 2 5 8 3
9
Dijkstra’s Algorithm example
20 A B C D E F G 20 4 6 2 5 8 3 4 6
10
Dijkstra’s Algorithm example
20 A B C D E F G 20 4 6 2 5 8 3 4 6
11
Dijkstra’s Algorithm example
20 A B C D E F G 20 4 6 2 5 8 3 4 12 6 9
12
Dijkstra’s Algorithm example
20 A B C D E F G 20 4 6 2 5 8 3 4 12 6 9
13
Dijkstra’s Algorithm example
20 A B C D E F G 20 4 6 2 5 8 3 4 12 6 8
14
Dijkstra’s Algorithm example
20 A B C D E F G 20 4 6 2 5 8 3 4 12 6 8
15
Dijkstra’s Algorithm example
20 A B C D E F G 20 4 6 2 5 8 3 4 12 6 8
16
Dijkstra’s Algorithm example
20 A B C D E F G 20 4 6 2 5 8 3 4 12 6 8
17
Dijkstra’s Algorithm example
20 15 A B C D E F G 20 4 6 2 5 8 3 4 12 6 8
18
Dijkstra’s Algorithm example
20 15 A B C D E F G 20 4 6 2 5 8 3 4 12 6 8
19
Dijkstra’s Algorithm example
17 15 A B C D E F G 20 4 6 2 5 8 3 4 12 6 8
20
Dijkstra’s Algorithm example
17 15 A B C D E F G 20 4 6 2 5 8 3 4 12 6 8
21
Dijkstra’s Algorithm example
17 15 A B C D E F G 4 6 2 8 3 4 12 6 8 Shortest Path Tree
22
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 ?
23
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 )
24
Bellman-Ford Algorithm example
C D E F G 20 4 6 2 5 8 3
25
All-Pairs Shortest Path
Repeat Single-Source Shortest Path n times Matrix Exponentiation – Recursive Squaring Dynamic Programming – Floyd-Warshall
26
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 )
27
References http://en.wikipedia.org/wiki/Shortest_path_problem
Introduction to Algorithms Thomas H. Cormen, Charles E. Leiserson, Ronald L. Rivest, Clifford Stein
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.