0% found this document useful (0 votes)
10 views2 pages

Program 3

The document contains two C/C++ programs: the first implements Floyd's algorithm to solve the All-Pairs Shortest Paths problem, allowing users to input a cost matrix and output the shortest path matrix. The second program uses Warshall's algorithm to find the transitive closure of a directed graph based on an adjacency matrix input by the user. Both programs include sample input and output to demonstrate their functionality.

Uploaded by

abhinehalove71
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views2 pages

Program 3

The document contains two C/C++ programs: the first implements Floyd's algorithm to solve the All-Pairs Shortest Paths problem, allowing users to input a cost matrix and output the shortest path matrix. The second program uses Warshall's algorithm to find the transitive closure of a directed graph based on an adjacency matrix input by the user. Both programs include sample input and output to demonstrate their functionality.

Uploaded by

abhinehalove71
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

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

int min(int a,int b)


{
return a<b?a:b;
}

void floyd(int w[10][10],int n)


{
int i,j,k;

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;

printf("\n Enter the no. of vertices:");


scanf("%d",&n);
printf("\n Enter the cost matrix, 0-self loop and 999-no edge\n");

for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
scanf("%d",&a[i][j]);

floyd(a,n);

printf("\n Shortest path matrix:\n");


for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
printf("%d\t",a[i][j]);
printf("\n");
}

return 0;
}

Sample Input and Output:


Enter the no. of vertices:4

Enter the cost matrix, 0-self loop and 999-no edge


0241
2013
4102
1320
Shortest path matrix:
0 2 3 1
2 0 1 3
3 1 0 2
1 3 2 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];

printf("the transitive closure for a directed graph is\n");


for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
printf("%d",t[i][j]);
printf("\n");
}
}

Sample Input and Output:


enter the no of vertices
4
enter the adjacency matrix. Enter 1 if an edge exists, enter 0 if there is no edge
0101
1000
0001
1010
the transitive closure for a directed graph is
1111
1111
1111
1111

You might also like