-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtransitiveClosure.cpp
45 lines (41 loc) · 960 Bytes
/
transitiveClosure.cpp
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<bits/stdc++.h>
using namespace std;
bool* getTransitiveClosure(bool *graph, int N) {
bool *tgraph = new bool[N * N];
for(int i = 0; i < N; i++) {
for(int j = 0; j < N; j++) {
*(tgraph + i * N + j) = *(graph + i * N + j);
}
}
for(int k = 0; k < N; k++) {
for(int i = 0; i < N; i++) {
for(int j = 0; j < N; j++) {
*(tgraph + i * N + j) = *(tgraph + i * N + j) || (*(tgraph + k * N + j) && *(tgraph + i * N + k));
}
}
}
return tgraph;
}
void printGraph(bool *graph, int N) {
for(int i = 0; i < N; i++) {
for(int j = 0; j < N; j++) {
cout << *(graph + i * N + j) << " ";
}
cout << endl;
}
}
int main() {
int N;
bool *graph;
cout << "Enter size of graph: ";
cin >> N;
graph = new bool[N * N];
cout << "Enter graph: \n";
for(int i = 0; i < N; i++) {
for(int j = 0; j < N; j++) {
cin >> *(graph + i * N + j);
}
}
cout << "Transitive Closure is: \n";
printGraph(getTransitiveClosure(graph, N), N);
}