-
Notifications
You must be signed in to change notification settings - Fork 3
/
transitive.cpp
43 lines (41 loc) · 1.06 KB
/
transitive.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
#include <iostream>
#include <Eigen/Dense>
#include <memory>
#include <mpc/graph.h>
#include <mpc/pflow.h>
#include <mpc/reach.h>
#include <mpc/naive.h>
std::unique_ptr<Graph> transitive_closure(Graph &g) {
auto reach_idx = graph_reachability(g);
auto result = std::make_unique<Graph>(g.n);
for(int i=1; i<=g.n; i++) {
for(int j=1; j<=g.n; j++) {
if(i != j && reach_idx->reaches(i, j))
result->add_edge(i, j);
}
}
return result;
}
std::unique_ptr<Graph> transitive_reduction(Graph &g) {
auto reach_idx = graph_reachability(g);
Eigen::MatrixX<bool> A = Eigen::MatrixX<bool>::Zero(g.n, g.n);
for(int i=1; i<=g.n; i++) {
for(auto u:g.edge_out[i])
A(i-1, u-1) = 1;
}
Eigen::MatrixX<bool> B = Eigen::MatrixX<bool>::Zero(g.n, g.n);
for(int i=1; i<=g.n; i++) {
for(int j=1; j<=g.n; j++) {
if(i != j && reach_idx->reaches(i,j))
B(i-1, j-1) = 1;
}
}
Eigen::MatrixX<bool> AB = A*B;
auto result = std::make_unique<Graph>(g.n);
for(int i=1; i<=g.n; i++) {
for(auto u:g.edge_out[i])
if(AB(i-1,u-1) == 0)
result->add_edge(i,u);
}
return result;
}