-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
155 lines (133 loc) · 3.98 KB
/
main.cpp
File metadata and controls
155 lines (133 loc) · 3.98 KB
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#include <cstring> // for memset()
#include <fstream>
#include <iostream>
#include <set>
#include <stack>
#include <vector>
using std::cin, std::cout, std::cerr, std::ifstream, std::ofstream, std::endl,
std::vector, std::stack, std::set;
const int N = 1e6 + 1; // maximum of n
int discovered[N]; // -1 means not discovered, otherwise EqClassID
void
DFS1(int v, const vector<vector<int>>& E, stack<int>& order)
{
discovered[v] = -2; // mark as discovered
for (auto neighbor = E[v].begin(); neighbor != E[v].end(); neighbor++)
if (discovered[*neighbor] == -1)
DFS1(*neighbor, E, order);
order.push(v);
}
void
DFS2(int v,
const vector<vector<int>>& E,
set<int>& EqNodes,
set<int>& EqOuts,
int EqClassID)
{
discovered[v] = EqClassID;
EqNodes.insert(v);
for (auto neighbor = E[v].begin(); neighbor != E[v].end(); neighbor++)
if (discovered[*neighbor] == -1)
DFS2(*neighbor, E, EqNodes, EqOuts, EqClassID);
else if (discovered[*neighbor] != EqClassID)
EqOuts.insert(discovered[*neighbor]);
}
void
printEqClass(const set<int>& EqNodes, int EqClassID, ofstream& fout1)
{
fout1 << " subgraph cluster_" << EqClassID << " {\n";
fout1 << " label = \"EqClass_" << EqClassID << "\";\n";
fout1 << " ";
for (auto node_it = EqNodes.begin(); node_it != EqNodes.end(); node_it++)
fout1 << *node_it << "; ";
fout1 << "\n";
fout1 << " }\n";
}
void
printEqClassDAG(const set<int>& EqOuts, int EqClassID, ofstream& fout2)
{
fout2 << " Eq_" << EqClassID << ";\n";
for (auto out_it = EqOuts.begin(); out_it != EqOuts.end(); out_it++)
fout2 << "Eq_" << EqClassID << " -> Eq_" << *out_it << ";\n";
}
void
printDiGraph(const vector<vector<int>>& E,
stack<int>& order,
ofstream& fout1,
ofstream& fout2)
{
fout1 << "digraph {\n rankdir = LR;\n";
fout2 << "digraph {\n rankdir = LR;\n";
memset(discovered, -1, sizeof(discovered));
set<int> sinkClassNodes;
set<int> EqOuts;
int NumEqClass = 0;
while (!order.empty()) {
int sink = order.top();
order.pop();
if (discovered[sink] != -1)
continue;
sinkClassNodes.clear();
EqOuts.clear();
DFS2(sink, E, sinkClassNodes, EqOuts, NumEqClass);
printEqClass(sinkClassNodes, NumEqClass, fout1);
printEqClassDAG(EqOuts, NumEqClass, fout2);
NumEqClass++;
}
for (int i = 0; i != E.size(); i++)
for (auto node_out = E[i].begin(); node_out != E[i].end(); node_out++)
fout1 << " " << i << " -> " << *node_out << ";\n";
fout1 << "}" << endl;
fout2 << "}" << endl;
}
void
SCC(ifstream& fin, ofstream& fout1, ofstream& fout2)
{
int n, m; // n nodes, m edges
fin >> n >> m; // node range: [0, n)
vector<vector<int>> E1;
vector<vector<int>> E2;
E1.resize(n);
E2.resize(n);
int u, v;
for (int i = 0; i < m; i++) {
fin >> u >> v;
E1[u].push_back(v);
E2[v].push_back(u);
}
memset(discovered, -1, sizeof(discovered));
stack<int> order;
for (int i = 0; i < n; i++)
if (discovered[i] == -1)
DFS1(i, E2, order);
printDiGraph(E1, order, fout1, fout2);
}
int
main(int argc, char* argv[])
{
if (argc != 4) {
cerr << "Usage: " << argv[0]
<< " <input_filename> <output1_filename> <output2_filename>" << endl;
cerr << "Please provide exactly 3 arguments." << endl;
return 1;
}
// Open input file
ifstream input_file(argv[1]);
if (!input_file) {
cerr << "Error: Unable to open input file: " << argv[1] << endl;
return 1;
}
// Open output files
ofstream output_file1(argv[2]);
ofstream output_file2(argv[3]);
if (!output_file1 || !output_file2) {
cerr << "Error: Unable to open output files." << endl;
return 1;
}
SCC(input_file, output_file1, output_file2);
// Close all files
input_file.close();
output_file1.close();
output_file2.close();
return 0;
}