Skip to content

Commit

Permalink
floyd warshall algorithm for finding all pair shortest path in a conn…
Browse files Browse the repository at this point in the history
…ected graph
  • Loading branch information
vivekcrux authored Oct 5, 2018
1 parent f298c76 commit 4c70db5
Showing 1 changed file with 73 additions and 0 deletions.
73 changes: 73 additions & 0 deletions floyd_warshall_algorithm/CPP/floyd-warshall algorithm.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
//Floyd-Warshall Algorithm: all pair shortest path
#include <iostream>
#include <climits>
using namespace std;

#define INF 999999999

int main()
{
int graph[1001][1001],i,j,k,n,m;
cin>>n>>m; //number of vertices and edges respectively

for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
if(i==j) graph[i][j]=0; //Shortest distance between (i,i) is 0
else
graph[i][j] = INF; //Initializing other distances as infinite
}

for(i=1;i<=m;i++)
{
int u,v,w;
cin>>u>>v>>w;
graph[u][v] = w; //input graph
graph[v][u] = w; //Shortest distance between (i,j) using atmost one edge
}


for(k=1;k<=n;k++)
{
for(int u=1;u<=n;u++)
{
for(int v=1;v<=n;v++)
{
//Shortest distance between (i,j) using atmost k edges
graph[u][v] = min(graph[u][v],graph[u][k]+graph[k][v]);
}
}
}

//Resulting all pair shortest path in given graph
for(int u=1;u<=n;u++)
{
for(int v=1;v<=n;v++)
{
cout<<graph[u][v]<<" ";
}
cout<<endl;
}
cout<<endl;
return 0;
}

/*
Input:-
First line contain two space separated integer n and m
where n is the number of vertices and m is the number
of edges.Next m lines contains three space seprated integers
u, v and w which represents that there is an edge between u and v
and weight of the edge is w
Sample Input:-
6 9
1 2 1
1 3 2
1 4 5
2 4 5
2 5 2
3 4 3
4 5 1
4 6 1
5 6 4
*/

1 comment on commit 4c70db5

@sangamcse
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment on 4c70db5.

There are 42 results for the section all.cpplint. They have been shortened and will not be shown inline because they are more than 10.

Message File Line
{ should almost always be at the end of the previous line [whitespace/braces] [4] floyd_warshall_algorithm/CPP/floyd-warshall algorithm.cpp 9
Missing space after , [whitespace/comma] [3] floyd_warshall_algorithm/CPP/floyd-warshall algorithm.cpp 10
Missing spaces around >> [whitespace/operators] [3] floyd_warshall_algorithm/CPP/floyd-warshall algorithm.cpp 11
Missing spaces around <= [whitespace/operators] [3] floyd_warshall_algorithm/CPP/floyd-warshall algorithm.cpp 13
Missing space after ; [whitespace/semicolon] [3] floyd_warshall_algorithm/CPP/floyd-warshall algorithm.cpp 13
{ should almost always be at the end of the previous line [whitespace/braces] [4] floyd_warshall_algorithm/CPP/floyd-warshall algorithm.cpp 14
Missing space before { [whitespace/braces] [5] floyd_warshall_algorithm/CPP/floyd-warshall algorithm.cpp 14
Missing spaces around <= [whitespace/operators] [3] floyd_warshall_algorithm/CPP/floyd-warshall algorithm.cpp 15
Missing space after ; [whitespace/semicolon] [3] floyd_warshall_algorithm/CPP/floyd-warshall algorithm.cpp 15
Missing spaces around == [whitespace/operators] [3] floyd_warshall_algorithm/CPP/floyd-warshall algorithm.cpp 16
Missing spaces around <= [whitespace/operators] [3] floyd_warshall_algorithm/CPP/floyd-warshall algorithm.cpp 21
Missing space after ; [whitespace/semicolon] [3] floyd_warshall_algorithm/CPP/floyd-warshall algorithm.cpp 21
{ should almost always be at the end of the previous line [whitespace/braces] [4] floyd_warshall_algorithm/CPP/floyd-warshall algorithm.cpp 22
Missing space before { [whitespace/braces] [5] floyd_warshall_algorithm/CPP/floyd-warshall algorithm.cpp 22
Missing space after , [whitespace/comma] [3] floyd_warshall_algorithm/CPP/floyd-warshall algorithm.cpp 23
Missing spaces around >> [whitespace/operators] [3] floyd_warshall_algorithm/CPP/floyd-warshall algorithm.cpp 24
Line ends in whitespace. Consider deleting these extra spaces. [whitespace/end_of_line] [4] floyd_warshall_algorithm/CPP/floyd-warshall algorithm.cpp 30
Missing spaces around <= [whitespace/operators] [3] floyd_warshall_algorithm/CPP/floyd-warshall algorithm.cpp 30
Missing space after ; [whitespace/semicolon] [3] floyd_warshall_algorithm/CPP/floyd-warshall algorithm.cpp 30
{ should almost always be at the end of the previous line [whitespace/braces] [4] floyd_warshall_algorithm/CPP/floyd-warshall algorithm.cpp 31
Missing space before { [whitespace/braces] [5] floyd_warshall_algorithm/CPP/floyd-warshall algorithm.cpp 31
Missing spaces around <= [whitespace/operators] [3] floyd_warshall_algorithm/CPP/floyd-warshall algorithm.cpp 32
Missing space after ; [whitespace/semicolon] [3] floyd_warshall_algorithm/CPP/floyd-warshall algorithm.cpp 32
{ should almost always be at the end of the previous line [whitespace/braces] [4] floyd_warshall_algorithm/CPP/floyd-warshall algorithm.cpp 33
Missing space before { [whitespace/braces] [5] floyd_warshall_algorithm/CPP/floyd-warshall algorithm.cpp 33
Missing spaces around <= [whitespace/operators] [3] floyd_warshall_algorithm/CPP/floyd-warshall algorithm.cpp 34
Missing space after ; [whitespace/semicolon] [3] floyd_warshall_algorithm/CPP/floyd-warshall algorithm.cpp 34
{ should almost always be at the end of the previous line [whitespace/braces] [4] floyd_warshall_algorithm/CPP/floyd-warshall algorithm.cpp 35
Missing space before { [whitespace/braces] [5] floyd_warshall_algorithm/CPP/floyd-warshall algorithm.cpp 35
Line ends in whitespace. Consider deleting these extra spaces. [whitespace/end_of_line] [4] floyd_warshall_algorithm/CPP/floyd-warshall algorithm.cpp 37
Missing space after , [whitespace/comma] [3] floyd_warshall_algorithm/CPP/floyd-warshall algorithm.cpp 37
Missing spaces around <= [whitespace/operators] [3] floyd_warshall_algorithm/CPP/floyd-warshall algorithm.cpp 43
Missing space after ; [whitespace/semicolon] [3] floyd_warshall_algorithm/CPP/floyd-warshall algorithm.cpp 43
{ should almost always be at the end of the previous line [whitespace/braces] [4] floyd_warshall_algorithm/CPP/floyd-warshall algorithm.cpp 44
Missing space before { [whitespace/braces] [5] floyd_warshall_algorithm/CPP/floyd-warshall algorithm.cpp 44
Missing spaces around <= [whitespace/operators] [3] floyd_warshall_algorithm/CPP/floyd-warshall algorithm.cpp 45
Missing space after ; [whitespace/semicolon] [3] floyd_warshall_algorithm/CPP/floyd-warshall algorithm.cpp 45
{ should almost always be at the end of the previous line [whitespace/braces] [4] floyd_warshall_algorithm/CPP/floyd-warshall algorithm.cpp 46
Missing space before { [whitespace/braces] [5] floyd_warshall_algorithm/CPP/floyd-warshall algorithm.cpp 46
Missing spaces around << [whitespace/operators] [3] floyd_warshall_algorithm/CPP/floyd-warshall algorithm.cpp 47
Missing spaces around << [whitespace/operators] [3] floyd_warshall_algorithm/CPP/floyd-warshall algorithm.cpp 49
Missing spaces around << [whitespace/operators] [3] floyd_warshall_algorithm/CPP/floyd-warshall algorithm.cpp 51

Until GitMate provides an online UI to show a better overview, you can run coala locally for more details.

Please sign in to comment.