Download as pdf or txt
Download as pdf or txt
You are on page 1of 10

Shortest Path Problem

 Shortest path problem is a problem of finding the shortest path(s) between


vertices of a given graph.
 Shortest path between two vertices is a path that has the least cost as
compared to all other existing paths.

Shortest path algorithms have a wide range of applications such as in-


 Google Maps
 Road Networks
 Logistics Research
Single-Pair Shortest Path Problem

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

Single-Source 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.

Single-Destination 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.

All Pairs 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

 Dijkstra Algorithm is a very famous greedy algorithm.


 It is used for solving the single source shortest path problem.
 It computes the shortest path from one particular source node to all other
remaining nodes of the graph.

Conditions

It is important to note the following points regarding Dijkstra Algorithm-


 Dijkstra algorithm works only for connected graphs.
 Dijkstra algorithm works only for those graphs that do not contain any
negative weight edge.
 It only provides the value or cost of the shortest paths.
 Dijkstra algorithm works for directed as well as undirected graphs.

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 a Dijkstra algorithm, once a node is marked as visited it cannot be reconsidered


even if there is another path with less cost or distance. This issue arises only if
there exists a negative weight or edge in the graph. For this reason, this algorithm
fails to find the minimum distance in case of 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.

 Does Dijkstra's algorithm work with negative weights?

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.

 What is a Negative cycle?


A negative cycle in a weighted graph is a cycle whose total weight is negative.

Lets see two examples:

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

 Bellman Ford algorithm has a time complexity of O(VE).


 The space complexity is O(V).

Applications

 For calculating shortest paths in routing algorithms


 For finding the shortest path

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 Dijkstra’s Algorithm

Bellman Ford’s Algorithm works when Dijkstra’s Algorithm doesn’t work when
there is negative weight edge. there is negative weight edge.

It is more time consuming than It is less time consuming.


Dijkstra’s algorithm.

Its time complexity is O(VE). The time complexity is O(E logV).

Bellman Ford’s Algorithm use Dynamic Dijkstra Algorithm use Greedy approach
Programming

It can be easily implemented in a It can not be implemented easily in a


distributed way. distributed way.

Floyd Warshall Algorithm

 Floyd Warshall Algorithm is a famous algorithm.


 It is used to solve All Pairs Shortest Path Problem.
 It computes the shortest path between every pair of vertices of the given graph.
 Floyd Warshall Algorithm is an example of dynamic programming approach.

Time Complexity
 Floyd Warshall Algorithm has a time complexity of O(n3).
When Floyd Warshall Algorithm is used?

 Floyd Warshall Algorithm is best suited for dense graphs.


 This is because its complexity depends only on the number of vertices in the
given graph.
 For sparse graphs, Johnson’s Algorithm is more suitable.

Problem:- Consider the following directed weighted graph-

Using Floyd Warshall Algorithm, find the shortest path distance between every pair
of vertices.

Solution: Initial distance matrix for the given graph is-

Using Floyd Warshall Algorithm, write the following 4 matrices-


The last matrix D4 represents the shortest path distance between every pair of
vertices.

You might also like