Shortest Path algorithm
Shortest Path algorithm
The shortest path problem is about finding a path between 2 vertices in a graph such that the total
sum of weight of the edges is minimum.
Dijkstra’s Algorithm:
Dijkstra’s algorithm is a popular algorithms for solving many single-source shortest path
problems having non-negative edge weight in the graphs i.e., it is to find the shortest distance
between two vertices on a graph.
It was conceived by Dutch computer scientist Edsger W. Dijkstra in 1956.
Dijkstra’s algorithm is also called single-source shortest path algorithm. The output
obtained is called shortest path spanning tree.
Algorithm:
1. Mark the source node with a current distance of 0 and the rest with infinity.
2. Set the non-visited node with the smallest current distance as the current
node.
3. For each neighbor, N of the current node adds the current distance of the
adjacent node with the weight of the edge connecting 0->1. If it is smaller
than the current distance of Node, set it as the new current distance of N.
4. Mark the current node 1 as visited.
5. Go to step 2 if there are any nodes are unvisited.
Examples
Step 1
Initialize the distances of all the vertices as ∞, except the source node S.
Vertex S A B C D E
Distance 0 ∞ ∞ ∞ ∞ ∞
Now that the source vertex S is visited, add it into the visited array.
Visited = {S}
Step 2
The vertex S has three adjacent vertices with various distances and the vertex with
minimum distance among them all is A. Hence, A is visited and the dist[A] is changed
from ∞ to 6.
S→A=6
S→D=8
S→E=7
Visited = {S, A}
Vertex S A B C D E
Distance 0 6 ∞ ∞ 8 7
Step 3
There are two vertices visited in the visited array, therefore, the adjacent vertices
must be checked for both the visited vertices.
Vertex S has two more adjacent vertices to be visited yet: D and E. Vertex A has one
adjacent vertex B.
Step 4
Calculate the distances of the adjacent vertices – S, A, E – of all the visited arrays and
select the vertex with minimum distance.
S→D=8
S → B = 15
S → C = S → E + E → C = 7 + 5 = 12
Visited = {S, A, E, D}
Vertex S A B C D E
Distance 0 6 15 12 8 7
Step 5
Recalculate the distances of unvisited vertices and if the distances minimum than
existing distance is found, replace the value in the distance array.
S → C = S → E + E → C = 7 + 5 = 12
S → C = S → D + D → C = 8 + 3 = 11
Step 6
The remaining unvisited vertex in the graph is B with the minimum distance 15, is
added to the output spanning tree.
Visited = {S, A, E, D, C, B}
Complexity Analysis:
Time Complexity: O(V^2) in the worst case, where V is the number of vertices. This can
be improved to O(V^2) with some optimizations.
Auxiliary Space: O(V), where V is the number of vertices and E is the number of edges in
the graph.
The Floyd-Warshall algorithm is a graph algorithm that is deployed to find the shortest path
between all the vertices present in a weighted graph. The Floyd-Warshall algorithm, named
after its creators Robert Floyd and Stephen Warshall.
This algorithm is highly efficient and can handle graphs with both positive and negative edge
weights, making it a versatile tool for solving a wide range of network and connectivity
problems.
Consider a graph, G = {V, E} where V is the set of all vertices present in the graph and E is the
set of all the edges in the graph. The graph, G, is represented in the form of an adjacency
matrix, A, that contains all the weights of every edge connecting two vertices.
Algorithm
Step 1 − Construct an adjacency matrix A with all the costs of edges present in the
graph. If there is no path between two vertices, mark the value as ∞.
Step 2 − Derive another adjacency matrix A1 from A keeping the first row and first
column of the original adjacency matrix intact in A1. And for the remaining values,
say A1[i,j], if A[i,j]>A[i,k]+A[k,j] then replace A1[i,j] with A[i,k]+A[k,j]. Otherwise, do
not change the values. Here, in this step, k = 1 (first vertex acting as pivot).
Step 3 − Repeat Step 2 for all the vertices in the graph by changing the k value for
every pivot vertex until the final matrix is achieved.
Step 4 − The final adjacency matrix obtained is the final solution with all the shortest
paths.
Pseudo-code-
Example-
1. Create a matrix A0 of dimension n*n where n is the number of vertices. The row and the column
are indexed as i and j respectively. i and j are the vertices of the graph.
Each cell A[i][j] is filled with the distance from the ith vertex to the jth vertex. If there is no path
3. Now, create a matrix A1 using matrix A0. The elements in the first column and the first row are
left as they are. The remaining cells are filled in the following way.
Let k be the intermediate vertex in the shortest path from source to destination. In this
step, k is the first vertex. A[i][j] is filled with (A[i][k] + A[k][j]) if (A[i][j] > A[i][k] + A[k][j]).
That is, if the direct distance from the source to the destination is greater than the path
through the vertex k, then the cell is filled with A[i][k] + A[k][j].
In this step, k is vertex 1. We calculate the distance from source vertex to destination vertex
Calculate the distance from the source vertex to destination vertex through this vertex k.
For example: For A1[2, 4], the direct distance from vertex 2 to 4 is 4 and the sum of the distance
from vertex 2 to 4 through vertex (ie. from vertex 2 to 1 and from vertex 1 to 4) is 7. Since 4 <
4. Similarly, A2 is created using A1. The elements in the second column and the second row are left
as they are.
In this step, k is the second vertex (i.e. vertex 2). The remaining steps are the same as in step 2.
Calculate the distance from the source vertex to destination vertex through this vertex 2
Calculate the distance from the source vertex to destination vertex through this vertex 3
6. Calculate the distance from the source vertex to destination vertex through this vertex 4
Complexity Analysis:
Time Complexity: O(V3), where V is the number of vertices in the graph and we run
three nested loops each of size V
Auxiliary Space: O(V2), to create a 2-D matrix in order to store the shortest distance for
each pair of nodes.