Program
#include <stdio.h> #include <conio.h> void warshall(int[10][10], int); void main() { int a[10][10], i, j, n; printf("*********************************************************"); printf("\n*********************************************************"); printf("\n** WAP to find transitive closure of a graph in C **"); printf("\n** Created by Sheetal Garg **"); printf("\n** Assistant Professor **"); printf("\n** Phone No:9467863365 **"); printf("\n*********************************************************"); printf("\n*********************************************************"); printf("\nEnter the number of nodes:"); scanf("%d", &n); printf("\nEnter the adjacency matrix:\n"); for (i = 1; i <= n; i++) { for (j = 1; j <= n; j++) scanf("%d", &a[i][j]); } printf("The adjacency matrix is:\n"); for (i = 1; i <= n; i++) { for (j = 1; j <= n; j++) { printf("%d\t", a[i][j]); } printf("\n"); } warshall(a, n); } void warshall(int p[10][10], int n) { int i, j, k; for (k = 1; k <= n; k++) { for (j = 1; j <= n; j++) { for (i = 1; i <= n; i++) { if ((p[i][j] == 0) && (p[i][k] == 1) && (p[k][j] == 1)) { p[i][j] = 1; } } } } printf("\nThe path matrix is:\n"); for (i = 1; i <= n; i++) { for (j = 1; j <= n; j++) { printf("%d\t", p[i][j]); } printf("\n"); } }
Output 1
********************************************************* ********************************************************* ** WAP to find transitive closure of a graph in C ** ** Created by Sheetal Garg ** ** Assistant Professor ** ** Phone No:9467863365 ** ********************************************************* ********************************************************* Enter the number of nodes: 4 Enter the adjacency matrix: 0 1 1 0 0 0 0 1 0 0 0 0 0 0 1 0 The adjacency matrix is: 0 1 1 0 0 0 0 1 0 0 0 0 0 0 1 0 The path matrix is: 0 1 1 1 0 0 1 1 0 0 0 0 0 0 1 0