Dijkstra's algorithm is an important topic in graph theory, where we can find the shortest path
between two possible points in a graph. However, we can find the paths if they are having a cost > or = 0. This is an
implementation of All pairs algorithm, where the cost is > or = 0.
The main motive of this algorithm is to minimise the distance between two points in a map. Since this is a
farely old algorithm, it is quite slow and takes a lot of space in the memory. There are several other algorithms
that we can study, but this forms the basis of graph theory.
The basic algorithm is as follows, Note this is in layman's term:
INF <- A very large number Input the number of nodes as N Initialise an array A <- [N+1][N+1] to INF A[i][j] <- 0 where i = j for K = 1 to N do: for I = 1 to N do: for J = I+1 to N do: if A[K][J] > A[K][I] + A[I][J] A[K][J] = A[K][I] + A[I][J] A[J][K] = A[K][I] + A[I][J] endif endfor endfor endfor A <- Will contain the cost matrix A[source][dest] <- Will have the cost from source to destination
/*
* Floyd-Warshall - All pairs algorithm, written in CPP. Author: Jimut Bahan Pal.
* 4rth June, 2019.
*/
#include <iostream> // for input output
#include <bits/stdc++.h> // for templates, just in case
#include <iomanip> // for formatting and stuffs!
using namespace std; // for standard namespace, else use std:: for everything!
#define INF 999999
int main()
{
int N;
cin>>N;
int matrix[N+1][N+1];
for(int i=1;i<=N;i++)
for(int j=1;j<=N;j++)
{
matrix[i][j]=INF;
if(i==j)
matrix[i][j]=0;
}
cout<<"\n Initial cost matrix :=> \n\n";
for(int i=1;i<=N;i++)
{
for(int j=1;j<=N;j++)
{
cout<< setw(7)<< matrix[i][j];
}
cout<<"\n";
}
6 7 1 2 4 1 3 9 2 3 3 2 4 7 3 4 9 4 5 9 5 6 6 Initial cost matrix :=> 0 999999 999999 999999 999999 999999 999999 0 999999 999999 999999 999999 999999 999999 0 999999 999999 999999 999999 999999 999999 0 999999 999999 999999 999999 999999 999999 0 999999 999999 999999 999999 999999 999999 0
int T,i,j,cost;
cin>>T;
while(T--)
{
cin>>i>>j>>cost;
matrix[i][j]=cost;
matrix[j][i]=cost;
}
cout<<"\n Next cost matrix :=> \n\n";
//disp_matrix(matrix,N);
for(int i=1;i<=N;i++)
{
for(int j=1;j<=N;j++)
{
cout<< setw(6)<< matrix[i][j]<<" ";
}
cout<<"\n";
}
Next cost matrix :=> 0 4 9 999999 999999 999999 4 0 3 7 999999 999999 9 3 0 9 999999 999999 999999 7 9 0 9 999999 999999 999999 999999 9 0 6 999999 999999 999999 999999 6 0
for(int k=1;k<=N;k++)
{
for(int i=1;i<=N;i++)
{
for(int j=i+1;j<=N;j++)
{
if(matrix[k][j]>matrix[k][i]+matrix[i][j])
{
matrix[k][j]=matrix[k][i]+matrix[i][j];
matrix[j][k]=matrix[k][i]+matrix[i][j];
}
}
}
}
cout<<"\n Final cost matrix :=> \n\n";
for(int i=1;i<=N;i++)
{
cout<< setw(6)<< i<<" ";
}
cout<< endl;
cout<< setw(6)<<"-------";
for(int i=1;i<=N;i++)
{
cout<< setw(6) <<"-------";
}
cout<< endl;
for(int i=1;i<=N;i++)
{
cout<< i<<"|";
for(int j=1;j<=N;j++)
{
cout<< setw(6)<< matrix[i][j]<<" ";
}
cout<<"\n";
}
return 0;
}
Final cost matrix :=> 1 2 3 4 5 6 ------------------------------------------------- 1| 0 4 7 11 20 26 2| 4 0 3 7 16 22 3| 7 3 0 9 18 24 4| 11 7 9 0 9 15 5| 20 16 18 9 0 6 6| 26 22 24 15 6 0