Wednesday, 23 January 2013
program to find shortest path of the graph using floyd warshell algorithm
//*floyd*//
#include<stdio.h>
#define maxval 999
int a[10][10],i,j,k,n;
void read()
{
printf("\nENTER THE SIZE OF MATRIX:");
scanf("%d",&n);
printf("\nENTER THE ELEMENTS OF MATRIX:");
for(i=0;i<n;i++)
for(j=0;j<n;j++)
scanf("%d",&a[i][j]);
}
void print()
{
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
printf("%d",a[i][j]);
printf("\n");
}
}
void convert()
{
for(i=0;i<=n;i++)
{
for(j=0;j<n;j++)
{
if(a[i][j]==0 && i!=j)
a[i][j]=maxval;
else
a[i][j]=a[i][j];
}
}
}
void shortpath()
{
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
for(k=0;k<n;k++)
{
if(a[j][i]+a[i][k]<a[j][k])
a[j][k]=a[j][i]+a[i][k];
else
a[j][k]=a[j][k];
}
}
}
}
main()
{
read();
convert();
printf("\nAFTER REPLACING ZEROS BY MAX VALUE\n");
print();
shortpath();
printf("\nSHORTEST PATH MATRIX\n");
print();
}
Labels:
C Programs
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment