# Finding the shortest path between any two nodes using Floyd Warshall Algorithm

C++ has a macro which is defined as a piece of code or desired value and it will use repetitively again and again whenever the user wants. The Floyd Warshall Algorithm is the process of finding the shortest path between all pairs of vertices in a given weighted graph. This algorithm follows the dynamic programming approach to find the minimal weightage graph.

Let us understand what is Floyd Warshall Algorithm with the help of diagrams −

Take vertex 1 as the source and vertex 4 as the destination and find the shortest path between them.

We have seen that there are two paths to connect with destination vertex 4.

• 1 -> 4 – edge weight is 5

• 1 -> 8 -> 3 -> 4 – edge weight ( 1+2+1 ) is 4.

In the given figure I, we see the minimal edge connected between two vertices. So here vertex 8 and vertex 3 connect the path of vertex 1 to vertex 4 and the minimum weightage of an edge is 4. On the other side, there is a directed edge between vertex 1 to vertex 4 and the weightage of an edge is 5.

Now we compare both weightage i.e. 4 and 5. So here 4 is the minimal weightage of the graph in terms of the Floyd Warshall Algorithm.

In this article, we are going to solve the shortest path between any two given nodes using Floyd Warshall Algorithm.

## Syntax

The following syntax is used in the program −

data_type[][] array_name;

### Parameter

data_type[][] − The type of data mentioned by the user. The first array represents the rows whereas the second is for columns. So, this represents the two-dimensional array.

array_name − The name given to the array.

## Algorithm

• We will start the program with header files namely ‘iostream’ and define the macros place to ‘INF’(infinity) as later it will meet the 2D array matrix and if-else statement.

• Following that we are creating the global function definition named ‘printPath’ and accept the three parameters namely ‘parent[]’, ‘i’, and ‘j’ to verify whether the path exists or not.

• Then we start the main function and store the value ‘4’ to the variable ‘V’ which represents the number of vertices. Secondly, storing the value in the form adjacency matrix to the ‘dist[V][V]’ variable. As we know that dist[i][j] represents the 2D matrix which defines the weight of the edge from ‘i’ to ‘j’ by storing the adjacency matrix.

• Next, we are initializing the 2D array for the ‘parent’ variable and have size [V][V].

In this variable, we used to show each pair of vertices ‘i’ and ‘j’ w.r.t ‘parent[i][j]’.

• By using two nested for loops we will be going to find the shortest path. The first for loop iterates the row in the matrix. Moving ahead with the second for loop it iterates the column in the matrix and then we will check the following condition using if-else statement −

• if (dist[i][j] != INF && i != j){ parent[i][j] = i;}

In the if-statement, we are using the ‘and’ (&&) operator to show two conditions and if both these conditions satisfy then ‘i’ will store to ‘parent[i][j]’ which verifies that the path exists.

• else { parent[i][j] = -1;}

In the else-statement, we are initializing ‘-1’ to ‘parent[i][j]’ which verifies that the path does not exist.

• Now we start with three nested for loops and apply k vertex in the range of 0 to V-1, with the help of this range our shortest distance and path will be updated. Inside the third nested loop, we are using the following condition such as

if (dist[i][j] < dist[i][k] + dist[k][j]){
dist[i][j] = dist[i][k] + dist[k][j];
parent[i][j] = parent[k][j];
}

Here K is updating the part of rows and columns in the 2D array matrix and this verifies the newly updated shortest path and distance.

• Next, we are printing the shortest distance and path of all pairs of vertices by giving the following condition such as

• By using two nested for loops, we are printing the shortest distance and path.

• By using an if-statement under a second for loop we will check whether dist[i][j] is equivalent to infinity or not. If it is found to be infinity, then it will print ‘INF’ otherwise it will print the shortest path.

• Finally, we are calling the function named ‘printPath()’ to get the value of the shortest path by passing three parameters namely parent[i], i, and j.

## Example

In this program, we are going to calculate the shortest path between any two nodes using Floyd Warshall Algorithm.

#include <iostream>
using namespace std;
#define INF 1000000000 // Infinity

void printPath(int parent[], int i, int j) {
if (i == j)
cout << i << " ";
else if (parent[j] == -1)
cout << "No path exists";
else
{
printPath(parent, i, parent[j]);
cout << j << " ";
}
}

int main()
{
int V = 4;
// V represent number of vertices
int dist[V][V] = {{0, 2, INF, 4},
{6, 0, 5, 3},
{INF, 10, 0, 1},
{7, 9, 8, 0}};
// Represent the graph using adjacency matrix

// Apply the Floyd Warshall algorithm to find the shortest paths
int parent[V][V];
for (int i = 0; i < V; i++)
{
for (int j = 0; j < V; j++)
{
if (dist[i][j] != INF && i != j)
parent[i][j] = i;
else
parent[i][j] = -1;
}
}
// update the path and distance using the k vertex range from 0 to V-1.
for (int k = 0; k < V; k++)
{
for (int i = 0; i < V; i++)
{
for (int j = 0; j < V; j++)
{
if (dist[i][j] > dist[i][k] + dist[k][j])
{
dist[i][j] = dist[i][k] + dist[k][j];
parent[i][j] = parent[k][j];
}
}
}
}

// Print shortest distances and paths between all pairs of vertices
for (int i = 0; i < V; i++)
{
for (int j = 0; j < V; j++)
{
cout << "The Shortest distance between " << i << " and " << j << " is ";
if (dist[i][j] == INF)
cout << "INF ";
else
cout << dist[i][j] << " ";

cout << "and the shortest path is:- ";
printPath(parent[i], i, j);
cout << endl;
}
}

return 0;
}

## Output

The Shortest distance between 0 and 0 is 0 and the shortest path is:- 0
The Shortest distance between 0 and 1 is 2 and the shortest path is:- 0 1
The Shortest distance between 0 and 2 is 7 and the shortest path is:- 0 1 2
The Shortest distance between 0 and 3 is 4 and the shortest path is:- 0 3
The Shortest distance between 1 and 0 is 6 and the shortest path is:- 1 0
The Shortest distance between 1 and 1 is 0 and the shortest path is:- 1
The Shortest distance between 1 and 2 is 5 and the shortest path is:- 1 2
The Shortest distance between 1 and 3 is 3 and the shortest path is:- 1 3
The Shortest distance between 2 and 0 is 8 and the shortest path is:- 2 3 0
The Shortest distance between 2 and 1 is 10 and the shortest path is:- 2 1
The Shortest distance between 2 and 2 is 0 and the shortest path is:- 2
The Shortest distance between 2 and 3 is 1 and the shortest path is:- 2 3
The Shortest distance between 3 and 0 is 7 and the shortest path is:- 3 0
The Shortest distance between 3 and 1 is 9 and the shortest path is:- 3 1
The Shortest distance between 3 and 2 is 8 and the shortest path is:- 3 2
The Shortest distance between 3 and 3 is 0 and the shortest path is:- 3

## Conclusion

We learned the concept of a shortest path between any two nodes using Floyd Warshall Algorithm. We used an adjacency matrix as input to the program where we find the shortest path and distance. Along with that for updating the path and distance we used k vertex to update the rows and columns. In our daily life, we search on Google Maps to find the shortest route or path from one source to destination.

Updated on: 10-May-2023

590 Views