Program 3
Program 3
a. Design and implement C/C++ Program to solve All-Pairs Shortest Paths problem using Floyd's
algorithm.
#include<stdio.h>
#define INFINITY 999
for(k=1;k<=n;k++)
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
w[i][j]=min(w[i][j],w[i][k]+w[k][j]);
}
int main(void)
{
int a[10][10],n,i,j;
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
scanf("%d",&a[i][j]);
floyd(a,n);
return 0;
}
b. Design and implement C/C++ Program to find the transitive closure using Warshal's
algorithm.
#include<stdio.h>
void main()
{
int n,i,j,k,a[20][20],t[20][20];
printf("enter the no of vertices\n");
scanf("%d",&n);
printf("Enter the adjacency matrix. Enter 1 if an edge exists, enter 0 if there is no edge\n");
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
{
scanf("%d",&a[i][j]);
t[i][j]=a[i][j];
}
for(k=1;k<=n;k++)
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
t[i][j]=t[i][j] || t[i][k] && t[k][j];