forked from gmgu/graph-analysis-tool
-
Notifications
You must be signed in to change notification settings - Fork 20
/
parse.cpp
328 lines (266 loc) · 8.57 KB
/
parse.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
#include "parse.h"
#include <algorithm>
#include <fstream>
#include <sstream>
namespace snu {
std::shared_ptr<Graph> parseFile(std::string input_path, bool directed) {
std::shared_ptr<snu::Graph> graph_shptr;
std::shared_ptr<snu::DSGraph> dsgraph_shptr;
std::shared_ptr<snu::USGraph> usgraph_shptr;
if (directed) {
graph_shptr = dsgraph_shptr = std::make_shared<snu::DSGraph>();
} else {
graph_shptr = usgraph_shptr = std::make_shared<snu::USGraph>();
}
auto &graph = *graph_shptr.get();
int parse_status = graph.parseGraph(input_path);
switch (parse_status) {
case snu::PARSE_SUCCESS:
break;
case snu::PARSE_FAILURE_NO_INPUT:
fprintf(stderr, "input file is not locatable.\n");
return nullptr;
case snu::PARSE_FAILURE_INVALID_INPUT:
fprintf(stderr, "input data is invalid.\n");
return nullptr;
case snu::PARSE_FAILURE_INVALID_FILETYPE:
fprintf(stderr, "this filetype is not supported.\n");
return nullptr;
case snu::PARSE_FAILURE_ADD_VERTEX:
case snu::PARSE_FAILURE_ADD_EDGE:
fprintf(stderr, "internal error\n");
return nullptr;
default:
return nullptr;
}
return graph_shptr;
}
// parse .snu file
int parseDSGraphSNU(std::string file_path, DSGraph &graph) {
std::ifstream infile(file_path);
if (infile.fail()) {
return PARSE_FAILURE_NO_INPUT;
}
std::string line;
Graph::Eid eid = 0;
while (getline(infile, line)) {
std::istringstream iss(line);
std::string sign;
iss >> sign;
if (sign == "t") {
} else if (sign == "v") {
Graph::Vid id;
Graph::Vlabel label;
std::vector<Graph::Vlabel> label_vector;
iss >> id;
while (iss >> label) {
label_vector.push_back(label);
}
if (graph.addVertex(id, &label_vector)) {
return PARSE_FAILURE_ADD_VERTEX;
}
} else if (sign == "e") {
Graph::Vid from, to;
Graph::Elabel label;
std::vector<Graph::Elabel> label_vector;
iss >> from >> to;
while (iss >> label) {
label_vector.push_back(label);
}
if (graph.addEdge(eid, &label_vector, from, to, 1)) {
return PARSE_FAILURE_ADD_EDGE;
}
++eid;
} else {
return PARSE_FAILURE_INVALID_INPUT;
}
}
graph.finalize();
return PARSE_SUCCESS;
}
// parse .net file
int parseDSGraphNET(std::string file_path, DSGraph &graph) {
std::ifstream infile(file_path);
if (infile.fail()) {
return PARSE_FAILURE_NO_INPUT;
}
std::string line;
bool check_vertex = false;
bool check_edge = false;
while (getline(infile, line)) {
std::istringstream iss(line);
if (line.find("*Vertices") != std::string::npos || line.find("*vertices") != std::string::npos) {
check_vertex = true;
check_edge = false;
continue;
} else if (line.find("*arcs") != std::string::npos || line.find("*Arcs") != std::string::npos) {
check_edge = true;
check_vertex = false;
continue;
}
if (check_vertex) {
Graph::Vid id;
std::vector<Graph::Vlabel> label_vector;
Graph::Vlabel label;
iss >> id;
while (iss >> label) {
label.erase(std::remove(label.begin(), label.end(), '\"'), label.end());
label_vector.push_back(label);
}
if (graph.addVertex(id, &label_vector)) {
return PARSE_FAILURE_ADD_VERTEX;
}
}
if (check_edge) {
Graph::Eid id;
Graph::Vid from;
Graph::Vid to;
std::vector<Graph::Elabel> label_vector;
Graph::Elabel label;
iss >> id >> from >> to;
while (iss >> label) {
if (label == "l")
continue;
label.erase(std::remove(label.begin(), label.end(), '\"'), label.end());
label_vector.push_back(label);
}
if (graph.addEdge(id, &label_vector, from, to, 1)) {
return PARSE_FAILURE_ADD_EDGE;
}
}
}
graph.finalize();
return PARSE_SUCCESS;
}
// parse .snap file
int parseDSGraphSNAP(std::string file_path, DSGraph &graph) {
std::ifstream infile(file_path);
if (infile.fail()) {
return PARSE_FAILURE_NO_INPUT;
}
std::string line;
std::unordered_set<int> set;
Graph::Eid eid = 0;
while (getline(infile, line)) {
std::istringstream iss(line);
if (line.find("#") != std::string::npos)
continue;
Graph::Vid from, to;
iss >> from >> to;
if (!set.count(from)) {
set.insert(from);
if (graph.addVertex(from, 0, NULL)) {
return PARSE_FAILURE_ADD_VERTEX;
}
}
if (!set.count(to)) {
set.insert(to);
if (graph.addVertex(to, 0, NULL)) {
return PARSE_FAILURE_ADD_VERTEX;
}
}
if (graph.addEdge(eid, 0, NULL, from, to, 1)) {
return PARSE_FAILURE_ADD_EDGE;
}
++eid;
}
graph.finalize();
return PARSE_SUCCESS;
}
// parsing DSGraph version follows
int parseDSGraph(std::string file_path, DSGraph &graph) {
if (file_path.rfind(".snu") == file_path.length() - 4)
return parseDSGraphSNU(file_path, graph);
else if (file_path.rfind(".snap") == file_path.length() - 5)
return parseDSGraphSNAP(file_path, graph);
else if (file_path.rfind(".net") == file_path.length() - 4)
return parseDSGraphNET(file_path, graph);
return PARSE_FAILURE_INVALID_FILETYPE;
}
int parseUSGraphSNU(std::string file_path, USGraph &graph) {
std::ifstream infile(file_path);
if (infile.fail()) {
return PARSE_FAILURE_NO_INPUT;
}
std::string line;
Graph::Eid eid = 0;
while (getline(infile, line)) {
std::istringstream iss(line);
std::string sign;
iss >> sign;
if (sign == "t") {
} else if (sign == "v") {
Graph::Vid id;
Graph::Vlabel label;
std::vector<Graph::Vlabel> label_vector;
iss >> id;
while (iss >> label) {
label_vector.push_back(label);
}
if (graph.addVertex(id, &label_vector)) {
return PARSE_FAILURE_ADD_VERTEX;
}
} else if (sign == "e") {
Graph::Vid from, to;
Graph::Elabel label;
std::vector<Graph::Elabel> label_vector;
iss >> from >> to;
while (iss >> label) {
label_vector.push_back(label);
}
if (graph.addEdge(eid, &label_vector, from, to, 1)) {
return PARSE_FAILURE_ADD_EDGE;
}
++eid;
} else {
return PARSE_FAILURE_INVALID_INPUT;
}
}
graph.finalize();
return PARSE_SUCCESS;
}
int parseUSGraphSNAP(std::string file_path, USGraph &graph) {
std::ifstream infile(file_path);
if (infile.fail()) {
return PARSE_FAILURE_NO_INPUT;
}
std::string line;
std::unordered_set<int> set;
Graph::Eid eid = 0;
while (getline(infile, line)) {
std::istringstream iss(line);
if (line.find("#") != std::string::npos) {
continue;
}
Graph::Vid from, to;
iss >> from >> to;
if (!set.count(from)) {
set.insert(from);
if (graph.addVertex(from, 0, NULL)) {
return PARSE_FAILURE_ADD_VERTEX;
}
}
if (!set.count(to)) {
set.insert(to);
if (graph.addVertex(to, 0, NULL)) {
return PARSE_FAILURE_ADD_VERTEX;
}
}
if (graph.addEdge(eid, 0, NULL, from, to, 1)) {
return PARSE_FAILURE_ADD_EDGE;
}
++eid;
}
graph.finalize();
return PARSE_SUCCESS;
}
int parseUSGraph(std::string file_path, USGraph &graph) {
if (file_path.rfind(".snu") == file_path.length() - 4)
return parseUSGraphSNU(file_path, graph);
else if (file_path.rfind(".snap") == file_path.length() - 5)
return parseUSGraphSNAP(file_path, graph);
else if (file_path.rfind(".net") == file_path.length() - 4)
return PARSE_FAILURE_INVALID_FILETYPE;
return PARSE_FAILURE_INVALID_FILETYPE;
}
} // namespace snu