Skip to content

Instantly share code, notes, and snippets.

@kartikmaji
Created April 10, 2015 23:30
Show Gist options
  • Select an option

  • Save kartikmaji/84b7a4b8196881927f0a to your computer and use it in GitHub Desktop.

Select an option

Save kartikmaji/84b7a4b8196881927f0a to your computer and use it in GitHub Desktop.
Floyd Warshall Algorithm
#include<stdio.h>
# define INF 999999
int vertices,edges;
void floydWarshall(int graph[vertices][vertices],int vertices)
{
int k,i,j;
int distance[vertices][vertices];
for(i=0;i<vertices;i++)
{
for(j=0;j<vertices;j++)
{
distance[i][j] = graph[i][j];
}
}
for(k=0;k<vertices;k++)
{
for(i=0;i<vertices;i++)
{
for(j=0;j<vertices;j++)
{
if(distance[i][k]+distance[k][j]<distance[i][j])
{
distance[i][j] = distance[i][k]+distance[k][j];
}
}
}
}
for(i=0;i<vertices;i++)
{
for(j=0;j<vertices;j++)
{
printf("%d ",distance[i][j]);
}
printf("\n");
}
}
int main()
{
scanf("%d %d",&vertices,&edges);
int graph[vertices][vertices];
int i,j;
for(i=0;i<vertices;i++)
for(j=0;j<vertices;j++)
{
if(i==j)
graph[i][j] = 0;
else
graph[i][j] = INF;
}
int a,b,c;
for(i=0;i<edges;i++)
{
scanf("%d %d %d",&a,&b,&c);
graph[a][b] = c;
}
/*for(i=0;i<4;i++)
for(j=0;j<4;j++)
printf("%d\n",graph[i][j]);*/
floydWarshall(graph,vertices);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment