forked from Adam27X/hybrid_BC
-
Notifications
You must be signed in to change notification settings - Fork 0
/
parse.cpp
424 lines (378 loc) · 9.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
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
#include "parse.h"
bool is_alphanumeric(const std::string &s)
{
std::string::const_iterator it = s.begin();
while(it!=s.end() && (std::isalnum(*it))) ++it;
return !s.empty() && it == s.end();
}
bool is_number(const std::string& s)
{
std::string::const_iterator it = s.begin();
while (it != s.end() && std::isdigit(*it)) ++it;
return !s.empty() && it == s.end();
}
graph parse(char *file)
{
std::string s(file);
if(s.find(".graph") != std::string::npos)
{
return parse_metis(file);
}
else if(s.find(".txt") != std::string::npos)
{
return parse_edgelist(file);
}
else if(s.find(".edge") != std::string::npos)
{
return parse_edgelist(file);
}
else
{
std::cerr << "Error: Unsupported file type." << std::endl;
exit(-1);
}
}
graph parse_metis(char *file)
{
graph g;
//Get n,m
std::ifstream metis(file,std::ifstream::in);
std::string line;
bool firstline = true;
int current_node = 0;
int current_edge = 0;
if(!metis.good())
{
std::cerr << "Error opening graph file." << std::endl;
exit(-1);
}
while(std::getline(metis,line))
{
if(line[0] == '%')
{
continue;
}
std::vector<std::string> splitvec;
boost::split(splitvec,line, boost::is_any_of(" \t"), boost::token_compress_on); //Now tokenize
//If the first or last element is a space or tab itself, erase it
if(!splitvec.empty() && !is_number(splitvec[0]))
{
splitvec.erase(splitvec.begin());
}
if(!splitvec.empty() && !is_number(splitvec[splitvec.size()-1]))
{
splitvec.erase(splitvec.end()-1);
}
if(firstline)
{
g.n = stoi(splitvec[0]);
g.m = stoi(splitvec[1]);
if(splitvec.size() > 3)
{
std::cerr << "Error: Weighted graphs are not yet supported." << std::endl;
exit(-2);
}
else if((splitvec.size() == 3) && (stoi(splitvec[2]) != 0))
{
std::cerr << "Error: Weighted graphs are not yet supported." << std::endl;
exit(-2);
}
firstline = false;
g.R = new int[g.n+1];
g.F = new int[2*g.m];
g.C = new int[2*g.m];
g.R[0] = 0;
current_node++;
}
else
{
//Count the number of edges that this vertex has and add that to the most recent value in R
g.R[current_node] = splitvec.size()+g.R[current_node-1];
for(unsigned i=0; i<splitvec.size(); i++)
{
//coPapersDBLP uses a space to mark the beginning of each line, so we'll account for that here
if(!is_number(splitvec[i]))
{
//Need to adjust g.R
g.R[current_node]--;
continue;
}
//Store the neighbors in C
//METIS graphs are indexed by one, but for our convenience we represent them as if
//they were zero-indexed
g.C[current_edge] = stoi(splitvec[i])-1;
g.F[current_edge] = current_node-1;
current_edge++;
}
current_node++;
}
}
return g;
}
/*graph parse_snap(char *file)
{
}*/
graph parse_edgelist(char *file)
{
graph g;
std::set<std::string> vertices;
//Scan the file
std::ifstream edgelist(file,std::ifstream::in);
std::string line;
if(!edgelist.good())
{
std::cerr << "Error opening graph file." << std::endl;
exit(-1);
}
std::vector<std::string> from;
std::vector<std::string> to;
while(std::getline(edgelist,line))
{
if((line[0] == '%') || (line[0] == '#')) //Allow comments
{
continue;
}
std::vector<std::string> splitvec;
boost::split(splitvec,line,boost::is_any_of(" \t"),boost::token_compress_on);
if(splitvec.size() != 2)
{
std::cerr << "Warning: Found a row that does not represent an edge or comment." << std::endl;
std::cerr << "Row in question: " << std::endl;
for(unsigned i=0; i<splitvec.size(); i++)
{
std::cout << splitvec[i] << std::endl;
}
exit(-1);
}
for(unsigned i=0; i<splitvec.size(); i++)
{
vertices.insert(splitvec[i]);
}
from.push_back(splitvec[0]);
to.push_back(splitvec[1]);
}
edgelist.close();
g.n = vertices.size();
g.m = from.size();
unsigned id = 0;
for(std::set<std::string>::iterator i = vertices.begin(), e = vertices.end(); i!=e; ++i)
{
g.IDs.insert(boost::bimap<unsigned,std::string>::value_type(id++,*i));
}
g.R = new int[g.n+1];
g.F = new int[2*g.m];
g.C = new int[2*g.m];
for(int i=0; i<g.m; i++)
{
boost::bimap<unsigned,std::string>::right_map::iterator itf = g.IDs.right.find(from[i]);
boost::bimap<unsigned,std::string>::right_map::iterator itc = g.IDs.right.find(to[i]);
if((itf == g.IDs.right.end()) || (itc == g.IDs.right.end()))
{
std::cerr << "Error parsing graph file." << std::endl;
exit(-1);
}
else
{
if(itf->second == itc->second)
{
std::cerr << "Error: self edge! " << itf->second << " -> " << itc->second << std::endl;
std::cerr << "Aborting. Graphs with self-edges aren't supported." << std::endl;
exit(-1);
}
g.F[2*i] = itf->second;
g.C[2*i] = itc->second;
//Treat undirected edges as two directed edges
g.F[(2*i)+1] = itc->second;
g.C[(2*i)+1] = itf->second;
}
}
//Sort edges by F
std::vector< std::pair<int,int> >edges;
for(int i=0; i<2*g.m; i++)
{
edges.push_back(std::make_pair(g.F[i],g.C[i]));
}
std::sort(edges.begin(),edges.end()); //By default, pair sorts with precedence to it's first member, which is precisely what we want.
g.R[0] = 0;
int last_node = 0;
for(int i=0; i<2*g.m; i++)
{
g.F[i] = edges[i].first;
g.C[i] = edges[i].second;
while(edges[i].first > last_node)
{
g.R[++last_node] = i;
}
}
g.R[g.n] = 2*g.m;
edges.clear();
return g;
}
void graph::print_R()
{
if(R == NULL)
{
std::cerr << "Error: Attempt to print CSR of a graph that has not been parsed." << std::endl;
}
std::cout << "R = [";
for(int i=0; i<(n+1); i++)
{
if(i == n)
{
std::cout << R[i] << "]" << std::endl;
}
else
{
std::cout << R[i] << ",";
}
}
}
void graph::print_number_of_isolated_vertices()
{
if(R == NULL)
{
std::cerr << "Error: Attempt to print CSR of a graph that has not been parsed." << std::endl;
}
int isolated = 0;
for(int i=0; i<n; i++)
{
int degree = R[i+1]-R[i];
if(degree == 0)
{
isolated++;
}
}
std::cout << "Number of isolated vertices: " << isolated << std::endl;
}
void graph::print_CSR()
{
if((R == NULL) || (C == NULL) || (F == NULL))
{
std::cerr << "Error: Attempt to print CSR of a graph that has not been parsed." << std::endl;
exit(-1);
}
std::cout << "R = [";
for(int i=0; i<(n+1); i++)
{
if(i == n)
{
std::cout << R[i] << "]" << std::endl;
}
else
{
std::cout << R[i] << ",";
}
}
std::cout << "C = [";
for(int i=0; i<(2*m); i++)
{
if(i == ((2*m)-1))
{
std::cout << C[i] << "]" << std::endl;
}
else
{
std::cout << C[i] << ",";
}
}
std::cout << "F = [";
for(int i=0; i<(2*m); i++)
{
if(i == ((2*m)-1))
{
std::cout << F[i] << "]" << std::endl;
}
else
{
std::cout << F[i] << ",";
}
}
}
void graph::print_high_degree_vertices()
{
if(R == NULL)
{
std::cerr << "Error: Attempt to search adjacency list of graph that has not been parsed." << std::endl;
exit(-1);
}
int max_degree = 0;
for(int i=0; i<n; i++)
{
int degree = R[i+1]-R[i];
if(degree > max_degree)
{
max_degree = degree;
std::cout << "Max degree: " << degree << std::endl;
}
}
}
void graph::print_adjacency_list()
{
if(R == NULL)
{
std::cerr << "Error: Attempt to print adjacency list of graph that has not been parsed." << std::endl;
exit(-1);
}
std::cout << "Edge lists for each vertex: " << std::endl;
for(int i=0; i<n; i++)
{
int begin = R[i];
int end = R[i+1];
boost::bimap<unsigned,std::string>::left_map::iterator itr = IDs.left.find(i);
for(int j=begin; j<end; j++)
{
boost::bimap<unsigned,std::string>::left_map::iterator itc = IDs.left.find(C[j]);
if(j==begin)
{
std::cout << itr->second << " | " << itc->second;
}
else
{
std::cout << ", " << itc->second;
}
}
if(begin == end) //Single, unconnected node
{
std::cout << itr->second << " | ";
}
std::cout << std::endl;
}
}
void graph::print_numerical_edge_file(char *outfile)
{
std::ofstream ofs(outfile, std::ios::out);
if(!ofs.good())
{
std::cerr << "Error opening output file." << std::endl;
exit(-1);
}
for(int i=0; i<2*m; i++)
{
if(F[i] < C[i])
{
ofs << F[i] << " " << C[i] << std::endl;
}
}
}
void graph::print_BC_scores(const std::vector<float> bc, char *outfile)
{
std::ofstream ofs;
if(outfile != NULL)
{
ofs.open(outfile, std::ios::out);
}
std::ostream &os = (outfile ? ofs : std::cout);
for(int i=0; i<n; i++)
{
boost::bimap<unsigned,std::string>::left_map::iterator it = IDs.left.find(i);
if(it != IDs.left.end())
{
os << it->second << " " << bc[i] << std::endl;
}
else
{
//Just print the numeric id
os << i << " " << bc[i] << std::endl;
}
}
}