Shortest Path
Shortest Path
Shortest Path
It is a shortest path problem where the shortest path between a given pair
of vertices is computed.
A* Search Algorithm is a famous algorithm used for solving single-pair
shortest path problem.
It is a shortest path problem where the shortest path from a given source
vertex to all other remaining vertices is computed.
Dijkstra’s Algorithm and Bellman Ford Algorithm are the famous algorithms
used for solving single-source shortest path problem.
It is a shortest path problem where the shortest path from all the vertices to
a single destination vertex is computed.
By reversing the direction of each edge in the graph, this problem reduces
to single-source shortest path problem.
Dijkstra’s Algorithm is a famous algorithm adapted for solving single-
destination shortest path problem.
It is a shortest path problem where the shortest path between every pair of
vertices is computed.
Floyd-Warshall Algorithm and Johnson’s Algorithm are the famous
algorithms used for solving All pairs shortest path problem.
Dijkstra Algorithm
Conditions
Limitations
The graph should be weighted.
The weights should be non-negative.
Time Complexity
Time complexity O(ElogV)
This time complexity can be reduced to O(E+VlogV) using Fibonacci heap.
O(V + E*log(V)), when priority queue is used
Problem: Using Dijkstra’s Algorithm, find the shortest distance from source vertex
‘S’ to remaining vertices in the following graph-
s A B c d e
0 infinite infinite infinite infinite infinite
S 0 1 5 infinite infinite infinite
A 0 1 3 3 2 infinite
D 0 1 3 3 2 4
B 0 1 3 3 2 4
C 0 1 3 3 2 4
E 0 4
S->A->D->E
Why does Dijkstra’s Algorithm fail on negative weights?
In dijkstra algorithm, the shortest distance from A –> B is 5 but if traveled the
distance via node C that is the path A –> C –> B the distance will be as 3. As 3 is
less than 5, but Dijkstra’s algorithm gives the incorrect answer as 5, which is not
the shortest distance. Therefore Dijkstra’s Algorithm fails for negative cases.
May or may not , Dijkstra also works for some of the graphs with negative weighted
cycle too as long as the element that is already considered shortest is not relaxed
anymore.
Actually , Dijkstra's algorithm fails to work for most of the negative weight edged
graphs , but sometimes it works with some of the graphs with negative weighted
edges too provided the graph doesn't have negative weight cycles.
Bellman Ford's Algorithm
Bellman Ford algorithm helps us find the shortest path from a vertex to all other
vertices of a weighted graph. It is similar to Dijkstra's algorithm but it can work with
graphs in which edges can have negative weights.
Conside the following graph: Weight of the graph is equal to the weight of its edges.
So, weight = 1 + 2 + 3= 6, Positive value, so we don’t have a negative cycle.
Let us consider another graph: Weight of the graph is equal to the weight of its
edges.So, weight = 3 + 2 + (-6)= -1, Negative value, so we have a negative cycle.
Point to note
If there are N vertices then we will iterate N - 1 times to get the shortest
distance. And we do the Nth iteration to check if there is any negative cycle.
If there is no change in the value of N-1 iteration then the graph has no
negative cycle which means we can find the shortest path. Otherwise the
graph has negative cycle and we cannot find the shortest path
Complexity
Applications
Limitation
It can only work for directed graphs
If there are negative cycles (reachable from the source), Bellman-Ford can
be considered to fail.
What are the differences between Bellman Ford’s and Dijkstra’s algorithms?
Bellman Ford’s Algorithm works when Dijkstra’s Algorithm doesn’t work when
there is negative weight edge. there is negative weight edge.
Bellman Ford’s Algorithm use Dynamic Dijkstra Algorithm use Greedy approach
Programming
Time Complexity
Floyd Warshall Algorithm has a time complexity of O(n3).
When Floyd Warshall Algorithm is used?
Using Floyd Warshall Algorithm, find the shortest path distance between every pair
of vertices.