0% found this document useful (0 votes)
20 views3 pages

Program3

The document provides C/C++ programs to solve the All-Pairs Shortest Paths problem using Floyd's algorithm and to find the transitive closure using Warshall's algorithm. Both programs prompt the user for the number of nodes and graph data, then compute and display the respective matrices. The output examples illustrate the results for a graph with 4 nodes.

Uploaded by

vijethviju04
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)
20 views3 pages

Program3

The document provides C/C++ programs to solve the All-Pairs Shortest Paths problem using Floyd's algorithm and to find the transitive closure using Warshall's algorithm. Both programs prompt the user for the number of nodes and graph data, then compute and display the respective matrices. The output examples illustrate the results for a graph with 4 nodes.

Uploaded by

vijethviju04
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/ 3

3A.

Design and implement C/C++ Program to solve All-Pairs Shortest Paths


problem using Floyd’s algorithm.

#include<stdio.h>
#include<conio.h>
#define INF 999

int min(int a,int b)


{
return(a<b)?a:b;
}

void floyd(int p[][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++)
p[i][j]=min(p[i][j],p[i][k]+p[k][j]);
}

void main()
{
int a[10][10],n,i,j;
printf("\nEnter the n value:");
scanf("%d",&n);
printf("\nEnter the graph data:\n");
for(i=1; i<=n; i++)
for(j=1; j<=n; j++)
scanf("%d",&a[i][j]);
floyd(a,n);
printf("\nShortest path matrix\n");
for(i=1; i<=n; i++)
{
for(j=1; j<=n; j++)
printf("%d ",a[i][j]);
printf("\n");
}
getch();
}

Output:

Enter the n value:4


Enter the graph data:
0 999 3 999
2 0 999 999
999 7 0 1
6 999 999 0
Shortest path matrix
0 10 3 4
2056
7701
6 16 9 0

3B. Design and implement C/C++ Program to find the transitive closure using
Warshal’s algorithm.

#include<stdio.h>

void warsh(int p[][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++)
p[i][j]=p[i][j] || p[i][k] && p[k][j];
}

int main()
{
int a[10][10],n,i,j;
printf("\nEnter the n value:");
scanf("%d",&n);
printf("\nEnter the graph data:\n");
for(i=1; i<=n; i++)
for(j=1; j<=n; j++)
scanf("%d",&a[i][j]);
warsh(a,n);
printf("\nResultant path matrix\n");
for(i=1; i<=n; i++)
{
for(j=1; j<=n; j++)
printf("%d ",a[i][j]);
printf("\n");
}
return 0;
}

Output:

Enter the n value:4

Enter the graph data:


0100
0001
0000
1010
Resultant path matrix
1111
1111
0000
1111

You might also like