Shortest Path Algorithms
Shortest Path Algorithms
All Tracks Algorithms Graphs Shortest Path Algorithms
Algorithms
Rank: 39,051
View Leaderboard
The shortest path problem is about nding a path between vertices in a graph such that the total
sum of the edges weights is minimum.
This problem could be solved easily using (BFS) if all edge weights were ( ), but here weights can take
any value. Three di erent algorithms are discussed below depending on the use-case.
Bellman Ford's algorithm is used to nd the shortest paths from the source vertex to all other vertices
in a weighted graph. It depends on the following concept: Shortest path contains at most
edges, because the shortest path couldn't have a cycle.
Algorithm Steps:
https://www.hackerearth.com/practice/algorithms/graphs/shortest-path-algorithms/tutorial/ 1/5
4/25/2021 Shortest Path Algorithms Tutorials & Notes | Algorithms | HackerEarth
This algorithm depends on the relaxation principle where the shortest distance for all vertices is
gradually replaced by more accurate values until eventually reaching the optimum solution. In the
beginning all vertices have a distance of "In nity", but only the distance of the source vertex = , then
update all the connected vertices with the new distances (source vertex distance + edge weights), then
apply the same concept for the new vertices with new distances and so on.
Implementation:
v[i].clear();
dis[i] = 2e9;
}
v[i].push_back(from);
v[i].push_back(next);
v[i].push_back(weight);
}
dis[0] = 0;
for(int i = 0; i < n - 1; i++){
int j = 0;
while(v[j].size() != 0){
A very important application of Bellman Ford is to check if there is a negative cycle in the graph,
https://www.hackerearth.com/practice/algorithms/graphs/shortest-path-algorithms/tutorial/ 2/5
4/25/2021 Shortest Path Algorithms Tutorials & Notes | Algorithms | HackerEarth
Dijkstra's Algorithm
Dijkstra's algorithm has many variants but the most common one is to nd the shortest paths from
the source vertex to all other vertices in the graph.
Algorithm Steps:
Set all vertices distances = in nity except for the source vertex, set the source distance = .
Push the source vertex in a min-priority queue in the form (distance , vertex), as the
comparison in the min-priority queue will be according to vertices distances.
Pop the vertex with the minimum distance from the priority queue (at rst the popped vertex =
source).
Update the distances of the connected vertices to the popped vertex in case of "current vertex
distance + edge weight < next vertex distance", then push the vertex
with the new distance to the priority queue.
If the popped vertex is visited before, just continue without using it.
Apply the same algorithm again until the priority queue is empty.
Implementation:
vector < pair < int , int > > v [SIZE]; // each vertex has all the connected
vertices with the edges weights
int dist [SIZE];
bool vis [SIZE];
void dijkstra(){
// set the vertices distances
as infinity
memset(vis, false , sizeof vis); // set all vertex as unvisited
dist[1] = 0;
multiset < pair < int , int > > s; // multiset do the job as a min-
priority queue
while(!s.empty()){
Time Complexity of Dijkstra's Algorithm is but with min-priority queue it drops down to
.
However, if we have to nd the shortest path between all pairs of vertices, both of the above
methods would be expensive in terms of time. Discussed below is another alogorithm designed for
this case.
Floyd\u2013Warshall's Algorithm
Floyd\u2013Warshall's Algorithm is used to nd the shortest paths between between all pairs of
vertices in a graph, where each edge in the graph has a weight which is positive or negative. The
biggest advantage of using this algorithm is that all the shortest distances between any vertices
could be calculated in , where is the number of vertices in a graph.
https://www.hackerearth.com/practice/algorithms/graphs/shortest-path-algorithms/tutorial/ 4/5
4/25/2021 Shortest Path Algorithms Tutorials & Notes | Algorithms | HackerEarth
represents the shortest path that only uses the rst vertices, represents the
shortest path between the pair . As the shortest path will be a concatenation of the shortest path
from to , then from to .
https://www.hackerearth.com/practice/algorithms/graphs/shortest-path-algorithms/tutorial/ 5/5