-
Notifications
You must be signed in to change notification settings - Fork 0
/
decomposer.cpp
444 lines (358 loc) · 11.6 KB
/
decomposer.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
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
#include "undirected/base.hpp"
#include "undirected/bctree.hpp"
#include <iostream>
#include <fstream>
#include <sstream>
namespace Wailea {
namespace Undirected {
/**
* Input file format:
* NODES
* [Nod Num]
*
* EDGES
* [Node1] [Node2]
*
* Output file format:
* CUT_VERTICES
* [Index] [Nod Num] [Block Num1] ... [Block NumX]
*
* BLOCK_BEGIN
* [Block Num]
*
* BLOCK_CUT_VERTICES
* [Nod Num] [Cut vertex index]
*
* BLOCK_ORDINARY_VERTICES
* [Nod Num]
*
* BLOCK_EDGES
* [Node Num1] [Node Num2]
*
* BLOCK_END
*/
class CommandLineDecomposer {
public:
void parseSpec(const char* filename);
void generateBCTree();
void emitBCTreeSpec(const char* filename);
class _node {public:long n; node_list_it_t it;};
class _edge {public:long n1; long n2; edge_list_it_t it;};
vector<_node> mNodes;
vector<_edge> mEdges;
private:
static const std::string NODES;
static const std::string EDGES;
static const std::string CUT_VERTICES;
static const std::string BLOCK_BEGIN;
static const std::string BLOCK_CUT_VERTICES;
static const std::string BLOCK_ORDINARY_VERTICES;
static const std::string BLOCK_EDGES;
static const std::string BLOCK_END;
enum parseState {
INIT,
IN_NODES,
IN_EDGES,
END
};
bool isSectionHeader(std::string line, enum parseState& state);
bool isCommentLine(std::string line);
size_t splitLine(
const std::string& txt, std::vector<std::string>& strs, char ch);
void handleNode(
std::string line, const char* filename, long lineNumber,bool& errorFlag);
void handleEdge(
std::string line, const char* filename, long lineNumber, bool& errorFlag);
void emitError(
const char* filename, long lineNumber, const char* mess, bool& errorFlag);
Node& toNodeRef(long num);
long toNodeNum(node_list_it_t it);
Graph mG;
BCTree mBCTree;
};
const std::string CommandLineDecomposer::NODES = "NODES";
const std::string CommandLineDecomposer::EDGES = "EDGES";
const std::string CommandLineDecomposer::CUT_VERTICES = "CUT_VERTICES";
const std::string CommandLineDecomposer::BLOCK_BEGIN = "BLOCK_BEGIN";
const std::string CommandLineDecomposer::BLOCK_CUT_VERTICES
= "BLOCK_CUT_VERTICES";
const std::string CommandLineDecomposer::BLOCK_ORDINARY_VERTICES
= "BLOCK_ORDINARY_VERTICES";
const std::string CommandLineDecomposer::BLOCK_EDGES = "BLOCK_EDGES";
const std::string CommandLineDecomposer::BLOCK_END = "BLOCK_END";
void CommandLineDecomposer::parseSpec(const char* filename)
{
std::ifstream is(filename);
long lineNumber = 0;
bool error = false;
enum parseState state = INIT;
while (!is.eof() && !error) {
std::string line;
std::getline(is, line);
if (line.empty()) {continue;}
if ((line[line.size()-1] == '\n')||(line[line.size()- 1] == '\r')) {
line.erase(line.size() - 1);
}
lineNumber++;
if (isCommentLine(line)) {continue;}
if(isSectionHeader(line, state)){continue;}
switch(state) {
case INIT:
case END:
emitError(filename, lineNumber, "", error);
break;
case IN_NODES:
handleNode(line, filename, lineNumber, error);
break;
case IN_EDGES:
handleEdge(line, filename, lineNumber, error);
break;
}
}
}
bool CommandLineDecomposer::isSectionHeader(
std::string line, enum parseState& state)
{
if (line.compare(0, NODES.size(), NODES)==0) {
state = IN_NODES;
return true;
}
else if (line.compare(0, EDGES.size(), EDGES)==0) {
state = IN_EDGES;
return true;
}
return false;
}
void CommandLineDecomposer::emitError(
const char* filename,
long lineNumber,
const char* message ,
bool& errorFlag
) {
std::cerr << "Syntax Error: "
<< filename
<< " at line: "
<< lineNumber
<< " "
<< message
<< "\n";
errorFlag = true;
}
bool CommandLineDecomposer::isCommentLine(std::string line)
{
return line.at(0) == '#';
}
size_t CommandLineDecomposer::splitLine(
const std::string& txt,
std::vector<std::string>& strs,
char ch
) {
auto pos = txt.find( ch );
size_t initialPos = 0;
strs.clear();
while( pos != std::string::npos && initialPos < txt.size()) {
if (pos > initialPos) {
strs.push_back( txt.substr( initialPos, pos - initialPos) );
}
initialPos = pos + 1;
if (initialPos < txt.size()) {
pos = txt.find( ch, initialPos );
}
}
if(initialPos < txt.size()) {
strs.push_back( txt.substr( initialPos, txt.size() - initialPos) );
}
return strs.size();
}
void CommandLineDecomposer::handleNode(
std::string line,
const char* filename,
long lineNumber,
bool& errorFlag
) {
vector<std::string> fields;
if (splitLine(line, fields, ' ')!= 1) {
emitError(filename, lineNumber, "Invalid Node", errorFlag);
}
_node n;
n.n = std::stol(fields[0]);
mNodes.push_back(n);
}
void CommandLineDecomposer::handleEdge(
std::string line,
const char* filename,
long lineNumber,
bool& errorFlag
) {
vector<std::string> fields;
if (splitLine(line, fields, ' ')!= 2) {
emitError(filename, lineNumber, "Invalid Edge", errorFlag);
}
_edge e;
e.n1 = std::stol(fields[0]);
e.n2 = std::stol(fields[1]);
mEdges.push_back(e);
}
void CommandLineDecomposer::generateBCTree()
{
for (auto& n : mNodes) {
auto np = std::make_unique<Node>();
auto& nr = dynamic_cast<Node&>(mG.addNode(std::move(np)));
n.it = nr.backIt();
}
for (auto& e : mEdges) {
auto ep = std::make_unique<Edge>();
auto& er = dynamic_cast<Edge&>(mG.addEdge(
std::move(ep), toNodeRef(e.n1), toNodeRef(e.n2)));
e.it = er.backIt();
}
BiconnectedDecomposer decomposer;
mBCTree = decomposer.decompose(mG);
return;
}
Node& CommandLineDecomposer::toNodeRef(long num)
{
for(auto& n : mNodes) {
if (n.n == num) {
return dynamic_cast<Node&>(*(*n.it));
}
}
cerr << "ERROR\n";
return dynamic_cast<Node&>(*(*(mNodes[0].it)));
}
long CommandLineDecomposer::toNodeNum(node_list_it_t it)
{
for(auto& n : mNodes) {
if (n.it == it) {
return n.n;
}
}
cerr << "ERROR\n";
return -1;
}
void CommandLineDecomposer::emitBCTreeSpec(const char* filename)
{
std::ofstream os(filename);
utility_t cutVertexIndex = 0;
utility_t blockIndex = 0;
for (auto tnit = mBCTree.nodes().first; tnit != mBCTree.nodes().second;
tnit++) {
auto& TN = dynamic_cast<BCTreeNode&>(*(*tnit));
if (TN.type() == BCTreeNode::CutVertexType) {
TN.pushUtility(++cutVertexIndex);
}
else {
TN.pushUtility(++blockIndex);
}
}
os << CUT_VERTICES << "\n";
os << "#[Index] [Node Num] [Incindent Block Num1]"
"...[Incindent Block NumX]\n";
for (auto tnit = mBCTree.nodes().first; tnit != mBCTree.nodes().second;
tnit++) {
auto& TN = dynamic_cast<BCTreeNode&>(*(*tnit));
if (TN.type() == BCTreeNode::CutVertexType) {
os << TN.utility() << " "
<< toNodeNum(TN.IGBackwardLink());
for (auto iit = TN.incidentEdges().first;
iit != TN.incidentEdges().second; iit++) {
auto& TE = dynamic_cast<BCTreeEdge&>(*(*(*iit)));
auto& TNB = TE.blockNode();
os << " " << TNB.utility();
}
os << "\n";
}
}
os << "\n";
bool commentShown = false;
for (auto tnit = mBCTree.nodes().first; tnit != mBCTree.nodes().second;
tnit++) {
auto& TN = dynamic_cast<BCTreeNode&>(*(*tnit));
if (TN.type() == BCTreeNode::BlockType) {
os << BLOCK_BEGIN << "\n" << TN.utility() << "\n";
auto& B = TN.block();
os << BLOCK_CUT_VERTICES << "\n";
if (!commentShown) {
os <<"#[Node Num] [Cut vertex index]\n";
}
for (auto bnit=B.nodes().first; bnit!=B.nodes().second; bnit++) {
auto& BN = dynamic_cast<BlockNode&>(*(*(bnit)));
if (BN.type()==BlockNode::CutVertexType) {
auto& TE = BN.treeEdge();
auto& TN = TE.cutVertexNode();
os << toNodeNum(BN.IGBackwardLink()) << " "
<< TN.utility() << "\n";
}
}
os << BLOCK_ORDINARY_VERTICES << "\n";
if (!commentShown) {
os <<"#[Node Num]\n";
}
for (auto bnit=B.nodes().first; bnit!=B.nodes().second; bnit++) {
auto& BN = dynamic_cast<BlockNode&>(*(*(bnit)));
if (BN.type()==BlockNode::OrdinaryNodeType) {
os << toNodeNum(BN.IGBackwardLink()) << "\n";
}
}
os << BLOCK_EDGES << "\n";
if (!commentShown) {
os << "#[Node Num 1] [Node Num 2]\n";
}
for (auto beit=B.edges().first; beit!=B.edges().second; beit++) {
auto& BE = dynamic_cast<BlockEdge&>(*(*(beit)));
auto& N1 = dynamic_cast<BlockNode&>(BE.incidentNode1());
auto& N2 = dynamic_cast<BlockNode&>(BE.incidentNode2());
os << toNodeNum(N1.IGBackwardLink()) << " ";
os << toNodeNum(N2.IGBackwardLink()) << "\n";
}
os << BLOCK_END << "\n";
os << "\n";
commentShown = true;
}
}
}
} // namespace Undirected
} // namespace Wailea
static void print_usage()
{
std::cerr << "decomposer : decomposes the given connected graph into BC-tree\n";
std::cerr << " Usage : decomposer <input_file> <output_file>\n";
std::cerr << "\n";
std::cerr << " Input file format:\n";
std::cerr << " NODES\n";
std::cerr << " [Node Num]\n";
std::cerr << "\n";
std::cerr << " EDGES\n";
std::cerr << " [Node1] [Node2]\n";
std::cerr << "\n";
std::cerr << " Output file format:\n";
std::cerr << " CUT_VERTICES\n";
std::cerr << " [Index] [Nod Num] [Block Num1] ... [Block NumX]\n";
std::cerr << "\n";
std::cerr << " BLOCK_BEGIN\n";
std::cerr << " [Block Num]\n";
std::cerr << "\n";
std::cerr << " BLOCK_CUT_VERTICES\n";
std::cerr << " [Node Num] [Cut Vertex Index]\n";
std::cerr << "\n";
std::cerr << " BLOCK_ORDINARY_VERTICES\n";
std::cerr << " [Node Num]\n";
std::cerr << "\n";
std::cerr << " BLOCK_EDGES\n";
std::cerr << " [Node Num1] [Node Num2]\n";
std::cerr << "\n";
std::cerr << " BLOCK_END\n";
std::cerr << "\n";
}
int main(int argc, char *argv[])
{
if (argc!=3) {
print_usage();
return 1;
}
Wailea::Undirected::CommandLineDecomposer p;
p.parseSpec(argv[1]);
p.generateBCTree();
p.emitBCTreeSpec(argv[2]);
return 0;
}