Thanks to visit codestin.com
Credit goes to www.scribd.com

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

Transitive Closure of A Graph

The document presents a C program that calculates the transitive closure of a graph using an adjacency matrix. It prompts the user to input the number of vertices and the adjacency matrix, then computes and displays the transitive closure. The output shows both the original adjacency matrix and the resulting transitive closure matrix.

Uploaded by

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

Transitive Closure of A Graph

The document presents a C program that calculates the transitive closure of a graph using an adjacency matrix. It prompts the user to input the number of vertices and the adjacency matrix, then computes and displays the transitive closure. The output shows both the original adjacency matrix and the resulting transitive closure matrix.

Uploaded by

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

TRANSITIVE CLOSURE OF A GRAPH

#include<stdio.h>
void main()
{
int i,j,k,adjmat[10][10],n,t[10][10];
printf("Enter the no of vertices:");
scanf("%d", &n);
printf("Enter the adjacent matrix:\n");
for(i = 1; i <= n; i++)
{
for(j = 1; j <= n; j++)
{
scanf("%d", &adjmat[i][j]);
t[i][j]=adjmat[i][j];
}
}
printf("Adjacent matrix is\n");
for(i=1;i<=n;i++)
{
for(j = 1; j <= n; j++)
{
printf("%d\t",adjmat[i][j]);
}
printf("\n");
}
for(k = 1; k <= n; k++)
{
for(i = 1; i <= n; i++)
{
for(j = 1; j <= n; j++)
{
if(t[i][j]==1||(t[i][k]&&t[k][j]))
{
t[i][j]=1;
}
}
}
}
printf("Transitive closure of a graph is\n");
for(i = 1; i <= n; i++)
{
for(j = 1; j <= n; j++)
{
printf("%d\t",t[i][j]);
}
printf("\n");
}
}

OUTPUT:

Enter the no of vertices:3


Enter the adjacent matrix:
1 0 1
1 1 0
0 1 1
Adjacent matrix is
1 0 1
1 1 0
0 1 1
Transitive closure of a graph is
1 1 1
1 1 1
1 1 1

You might also like