eguruchela

Write a C Program to implement Floyd’s Algorithm


C Program

#include<stdio.h> #include<conio.h> int min(int,int); void floyds(int p[10][10],int n) { int a,b,c; for (c=1;c<=n;c++) for (a=1;a<=n;a++) for (b=1;b<=n;b++) if(a==b) p[a][b]=0; else p[a][b]=min(p[a][b],p[a][c]+p[c][b]); } int min(int x,int y) { if(x<y) return(x); else return(y); } void main() { int p[10][10],w,n,e,u,v,a,b; ; clrscr(); printf("\n Enter the number of vertices:"); scanf("%d",&n); printf("\n Enter the number of edges:\n"); scanf("%d",&e); for (a=1;a<=n;a++) { for (b=1;b<=n;b++) p[a][b]=999; } for (a=1;a<=e;a++) { printf("\n Enter the end vertices of edge%d with its weight \n",a); scanf("%d%d%d",&u,&v,&w); p[u][v]=w; } printf("\n Matrix of input data:\n"); for (a=1;a<=n;a++) { for (b=1;b<=n;b++) printf("%d \t",p[a][b]); printf("\n"); } floyds(p,n); printf("\n Transitive closure:\n"); for (a=1;a<=n;a++) { for (b=1;b<=n;b++) printf("%d \t",p[a][b]); printf("\n"); } printf("\n The shortest paths are:\n"); for (a=1;a<=n;a++) for (b=1;b<=n;b++) { if(i!=b) printf("\n <%d,%d>=%d",a,b,p[a][b]); } getch(); }

👈       👉