-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathtransitive closure
45 lines (44 loc) · 900 Bytes
/
transitive closure
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include<stdio.h>
#include<conio.h>
#include<iostream>
using namespace std;
void printSolution(int reach[][4])
{
cout<<"Following matrix is transitive closure of the given graph\n";
for (int i = 0; i < 4; i++)
{
for (int j = 0; j < 4; j++)
{
cout<<reach[i][j]<<" ";
}
cout<<endl;
}
}
void transitiveClosure(int graph[][4])
{
int reach[4][4], i, j, k;
for (i = 0; i < 4; i++)
{
for (j = 0; j < 4; j++)
{
reach[i][j] = graph[i][j];
}
}
for (k = 0; k < 4; k++)
{
for (i = 0; i < 4; i++)
{
for (j = 0; j < 4; j++)
{
reach[i][j] = reach[i][j] || (reach[i][k] && reach[k][j]);
}
}
}
printSolution(reach);
}
int main()
{
int graph[4][4] = { {1, 1, 0, 1},{0, 1, 1, 0},{0, 0, 1, 1},{0, 0, 0, 1}};
transitiveClosure(graph);
getch();
}