Floyd Warshall Algorithm
PROBLEM :
The problem is to find shortest distances between every pair of vertices in a given edge weighted directed Graph.
Input:
The first line of input contains an integer T denoting the no of test cases . Then T test cases follow . The first line of each test case contains an integer V denoting the size of the adjacency matrix and in the next line are V*V space separated values of the matrix (graph) .
Output:
For each test case output will be V*V space separated integers where the i-jth integer denote the shortest distance of ith vertex from jth vertex.
Constraints:
1<=T<=20
1<=V<=20
-1000<=graph[][]<=1000
Example:
Input
2
2
0 25 25 0
3
0 1 43 1 0 6 43 6 0
Output
0 25 25 0
0 1 7 1 0 6 7 6 0
--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION :
--------------------------------------------------------------------------------
#include<iostream>
using namespace std;
#define MAX 1000
void FloydWarshall(int graph[MAX][MAX],int ) ;
int main()
{
int t ;
cin>>t ;
while(t--)
{
int no ;
cin>>no ;
int graph[MAX][MAX] ;
for(int i=0;i<no;i++)
for(int j=0;j<no;j++)
cin>>graph[i][j] ;
FloydWarshall(graph,no) ;
cout<<endl ;
}
return 0;
}
void FloydWarshall(int graph[MAX][MAX],int no)
{
int path[MAX][MAX] ;
for(int i=0;i<no;i++)
for(int j=0;j<no;j++)
path[i][j]=graph[i][j] ;
for(int k=0;k<no;k++)
{
for(int j=0;j<no;j++)
{
for(int i=0;i<no;i++)
{
if(path[i][k] + path[k][j] < path[i][j])
path[i][j] = path[i][k] + path[k][j] ;
}
}
}
for(int i=0;i<no;i++)
for(int j=0;j<no;j++)
cout<<path[i][j]<<" " ;
}
--------------------------------------------------------------------------------
The problem is to find shortest distances between every pair of vertices in a given edge weighted directed Graph.
Input:
The first line of input contains an integer T denoting the no of test cases . Then T test cases follow . The first line of each test case contains an integer V denoting the size of the adjacency matrix and in the next line are V*V space separated values of the matrix (graph) .
Output:
For each test case output will be V*V space separated integers where the i-jth integer denote the shortest distance of ith vertex from jth vertex.
Constraints:
1<=T<=20
1<=V<=20
-1000<=graph[][]<=1000
Example:
Input
2
2
0 25 25 0
3
0 1 43 1 0 6 43 6 0
Output
0 25 25 0
0 1 7 1 0 6 7 6 0
--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION :
--------------------------------------------------------------------------------
#include<iostream>
using namespace std;
#define MAX 1000
void FloydWarshall(int graph[MAX][MAX],int ) ;
int main()
{
int t ;
cin>>t ;
while(t--)
{
int no ;
cin>>no ;
int graph[MAX][MAX] ;
for(int i=0;i<no;i++)
for(int j=0;j<no;j++)
cin>>graph[i][j] ;
FloydWarshall(graph,no) ;
cout<<endl ;
}
return 0;
}
void FloydWarshall(int graph[MAX][MAX],int no)
{
int path[MAX][MAX] ;
for(int i=0;i<no;i++)
for(int j=0;j<no;j++)
path[i][j]=graph[i][j] ;
for(int k=0;k<no;k++)
{
for(int j=0;j<no;j++)
{
for(int i=0;i<no;i++)
{
if(path[i][k] + path[k][j] < path[i][j])
path[i][j] = path[i][k] + path[k][j] ;
}
}
}
for(int i=0;i<no;i++)
for(int j=0;j<no;j++)
cout<<path[i][j]<<" " ;
}
--------------------------------------------------------------------------------
Comments
Post a Comment