-
Notifications
You must be signed in to change notification settings - Fork 0
/
InputManagement.cpp
453 lines (362 loc) · 16.2 KB
/
InputManagement.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
444
445
446
447
448
449
450
451
452
453
/*
* File: InputManagement.cpp
* Author: RM
*
* Created on 26. Juli 2013, 22:46
*/
#include "InputManagement.h"
#include "RandomNumberGenerator.h"
InputManagement::InputManagement()
{
}
InputManagement::InputManagement(const InputManagement& orig)
{
}
InputManagement::~InputManagement()
{
}
// Clarification: Preprocessed input files have source IDs in right column,
// destination IDs in left column - and are sorted according to the
// destination IDs.
// I. e.: Edges recorded within a vertex are _incoming_ edges.
// Vertices covered in Graph.scanForPassiveVertices() therefore are vertices with an
// arbitrary number of outgoing, but zero incoming edges.
int InputManagement::parseInputFile(string inputPath, FileMetadataConstants::DataFileType fileType, Graph& graph)
{
ofstream& protocol = Protocol::protocol();
cout << " :: Input path is " << inputPath << "." << endl;
protocol << " :: Input path is " << inputPath << "." << endl;
// Open file
ifstream inputFile;
inputFile.open(inputPath.c_str(), ios::in);
if (!inputFile.is_open()) {
cout << " -- Failed opening file; exiting program." << endl << endl;
protocol << " -- Failed opening file; exiting program." << endl << endl;
return -1;
}
// Call parsing method fitted to file type
switch (fileType) {
case FileMetadataConstants::DBLP:
cout << " :: Type of examined data file: DBLP" << endl;
protocol << " :: Type of examined data file: DBLP" << endl;
parseDBLPFile(inputFile, graph);
break;
case FileMetadataConstants::ADVOGATO:
cout << " :: Type of examined data file: ADVOGATO" << endl;
protocol << " :: Type of examined data file: ADVOGATO" << endl;
InputManagement::parseADVOGATOFile(inputFile, graph);
break;
case FileMetadataConstants::SNAP:
cout << " :: Type of examined data file: SNAP" << endl;
protocol << " :: Type of examined data file: SNAP" << endl;
InputManagement::parseSNAPFile(inputFile, graph);
break;
default:
cout << " :: Unknown file type; exiting program.";
protocol << " :: Unknown file type; exiting program.";
}
cout << " :: Graph status " << endl;
cout << " :: Size:\t\t\t" << graph.vertices().size() << endl;
cout << " :: Capacity:\t\t" << graph.vertices().capacity() << endl;
cout << " :: Passive/deaf vertices:\t" << graph.passiveVertexCount() << endl;
protocol << " :: Graph status " << endl;
protocol << " :: Size:\t\t\t\t\t" << graph.vertices().size() << endl;
protocol << " :: Capacity:\t\t\t\t" << graph.vertices().capacity() << endl;
protocol << " :: Passive/deaf vertices:\t" << graph.passiveVertexCount() << endl;
return 1;
}
uint InputManagement::parseSNAPFile(ifstream& inputFile, Graph& graph)
{
// Strings for storing input temporarily
string currentLine = "#";
string tempID;
// Vertex IDs
int lastFID = -2;
int firstID = -1;
int secondID = -1;
// Number of processed vertices
int count = 0;
// Temporary collection for edges of current vertex
vector<Edge> edges;
// Current vertex (storing all read information)
Vertex currVertex;
// Vertex collection
vector<Vertex>& vertices = graph.vertices();
// Vertex map
map<uint, uint>& vertexIDMap = graph.vertexIDMap();
// Container for random numbers for edge weights.
vector<float> edgeWeights;
// Container for vertex-round assignations
vector<float> roundAssignations;
// Random number generator
RandomNumberGenerator rng(chrono::system_clock::now().time_since_epoch().count());
// Allocating memory and generating random numbers
edgeWeights.reserve(InputManagement::SNAP_MAX_NUMBER_OF_EDGE_WEIGHTS);
roundAssignations.reserve(InputManagement::SNAP_MAX_NUMBER_OF_EDGE_WEIGHTS);
rng.generateRandomNumbers(edgeWeights);
rng.generateRandomNumbers(roundAssignations);
// Skip comment lines
streampos position;
do {
position = inputFile.tellg();
getline(inputFile, currentLine);
}
while (!inputFile.eof() && currentLine[0] == '#');
// Jump back to begin of first content line
inputFile.seekg(position);
// Parse actual data
while (!inputFile.eof()) {
tempID.clear();
// Note: g++ stubbornly refuses knowledge of std::stoi(),
// so atoi() is used here instead.
// Read first ID
inputFile >> tempID;
// Check for blank line
if (tempID.size()) {
// Convert first ID to integer
firstID = atoi(tempID.c_str());
// Read second ID
inputFile >> tempID;
// Convert second ID to integer
secondID = atoi(tempID.c_str());
// New source (vertex) detected - push old vertex to graph,
// reset edge collection
if (lastFID != firstID) {
// Add old vertex to collection
if (lastFID >= 0) {
// Swap edge data with currVertex edge data
currVertex.swapEdges(edges);
// Determine round in which vertex will be processed
currVertex.roundAssignation() = roundAssignations[count % InputManagement::SNAP_MAX_NUMBER_OF_EDGE_WEIGHTS]
* ParametrizationSettings::NUMBER_OF_ROUNDS + 1;
// Add currVertex to vertex collection
vertices.push_back(currVertex);
// Map vertex (ID is key, index is value)
vertexIDMap[currVertex.vertexID()] = count;
}
// Set new vertex ID
currVertex.vertexID() = firstID;
// Delete currently stored edges
edges.clear();
}
// Add edge between firstID and secondID to edge collection.
// Weight is taken from randomly generated edge weights.
edges.push_back(Edge(currVertex.vertexID(), secondID, edgeWeights[count % InputManagement::SNAP_MAX_NUMBER_OF_EDGE_WEIGHTS]));
// Set new source ID, iterate counter
lastFID = firstID;
count++;
}
}
// After loop has finished: Wrap up - add one last vertex to graph.
currVertex.swapEdges(edges);
vertices.push_back(currVertex);
// Finalize input parsing and processing
graph.finalizeInputParsing(roundAssignations);
}
uint InputManagement::parseDBLPFile(ifstream& inputFile, Graph& graph)
{
// Strings for storing input temporarily
string tempID;
// Auxiliary variables for handling vertex data
int lastFID = -2;
int firstID = -1;
int secondID = -1;
int edgeCount = -1;
float edgeWeight = -1;
// Number of processed vertices
int count = 0;
// Temporary collection for edges of current vertex
vector<Edge> edges;
// Current vertex (storing all read information)
Vertex currVertex;
// Vertex collection
vector<Vertex>& vertices = graph.vertices();
// Vertex map
map<uint, uint>& vertexIDMap = graph.vertexIDMap();
// Container for vertex-round assignations
vector<float> roundAssignations;
// Random number generator
RandomNumberGenerator rng(chrono::system_clock::now().time_since_epoch().count());
// Allocating memory and generating random numbers
roundAssignations.reserve(InputManagement::SNAP_MAX_NUMBER_OF_EDGE_WEIGHTS);
rng.generateRandomNumbers(roundAssignations);
// Parse actual data
while (!inputFile.eof()) {
tempID.clear();
// Note: g++ stubbornly refuses knowledge of std::stoi(),
// so atoi() is used here instead.
// Read first ID
inputFile >> tempID;
// Check for blank line
if (tempID.size()) {
// Convert first ID to integer
firstID = atoi(tempID.c_str());
// Read second ID
inputFile >> tempID;
// Convert second ID to integer
secondID = atoi(tempID.c_str());
// Read number of edges, calculate edge weight
inputFile >> tempID;
// Convert edge count to integer
edgeCount = atoi(tempID.c_str());
// Calculate edge weight as log(1 + edgeCount)
edgeWeight = log(1 + edgeCount);
// New source (vertex) detected? Push old vertex to graph,
// reset edge collection
if (lastFID != firstID) {
// Add old vertex to collection
if (lastFID >= 0) {
// Swap edge data with currVertex edge data
currVertex.swapEdges(edges);
// Determine round in which vertex will be processed
currVertex.roundAssignation() = roundAssignations[count % InputManagement::SNAP_MAX_NUMBER_OF_EDGE_WEIGHTS]
* ParametrizationSettings::NUMBER_OF_ROUNDS + 1;
// Add currVertex to vertex collection
vertices.push_back(currVertex);
// Map vertex (ID is key, index is value)
vertexIDMap[currVertex.vertexID()] = count;
}
// Set new vertex ID
currVertex.vertexID() = firstID;
// Delete currently stored edges
edges.clear();
}
// Add edge between firstID and secondID to edge collection.
// Weight is taken from randomly generated edge weights.
edges.push_back(Edge(currVertex.vertexID(), secondID, edgeWeight));
// Set new source ID, iterate counter
lastFID = firstID;
count++;
}
}
// After loop has finished: Wrap up - add one last vertex to graph.
currVertex.swapEdges(edges);
vertices.push_back(currVertex);
// Finalize input parsing and processing
graph.finalizeInputParsing(roundAssignations);
}
uint InputManagement::parseADVOGATOFile(ifstream& inputFile, Graph& graph)
{
// Strings for storing input temporarily
string tempID;
// Vertex IDs
int lastFID = -2;
int firstID = -1;
int secondID = -1;
// Current edge weight
float weight = 0;
// Number of processed vertices
int count = 0;
// Temporary collection for edges of current vertex
vector<Edge> edges;
// Current vertex (storing all read information)
Vertex currVertex;
// Vertex collection
vector<Vertex>& vertices = graph.vertices();
// Vertex map
map<uint, uint>& vertexIDMap = graph.vertexIDMap();
// Container for vertex-round assignations
vector<float> roundAssignations;
// Random number generator
RandomNumberGenerator rng(chrono::system_clock::now().time_since_epoch().count());
// Allocating memory and generating random numbers
roundAssignations.reserve(InputManagement::SNAP_MAX_NUMBER_OF_EDGE_WEIGHTS);
rng.generateRandomNumbers(roundAssignations);
// Parse actual data
while (!inputFile.eof()) {
tempID.clear();
// Note: g++ stubbornly refuses knowledge of std::stoi(),
// so atoi() is used here instead.
// Read first ID
inputFile >> tempID;
// Check for blank line
if (tempID.size()) {
// Convert first ID to integer
firstID = atoi(tempID.c_str());
// Read second ID
inputFile >> tempID;
// Convert second ID to integer
secondID = atoi(tempID.c_str());
// Read edge weight
inputFile >> tempID;
// Convert second ID (destination ID) to integer
weight = atof(tempID.c_str());
// New source (vertex) detected - push old vertex to graph,
// reset edge collection
if (lastFID != firstID) {
// Add old vertex to collection
if (lastFID >= 0) {
// Swap edge data with currVertex edge data
currVertex.swapEdges(edges);
// Determine round in which vertex will be processed
currVertex.roundAssignation() = roundAssignations[count % InputManagement::SNAP_MAX_NUMBER_OF_EDGE_WEIGHTS]
* ParametrizationSettings::NUMBER_OF_ROUNDS + 1;
// Add currVertex to vertex collection
vertices.push_back(currVertex);
// Map vertex (ID is key, index is value)
vertexIDMap[currVertex.vertexID()] = count;
}
// Set new vertex ID
currVertex.vertexID() = firstID;
// Delete currently stored edges
edges.clear();
}
// Add edge between firstID and secondID to edge collection.
// Weight is taken from randomly generated edge weights.
edges.push_back(Edge(currVertex.vertexID(), secondID, weight));
// Set new source ID, iterate counter
lastFID = firstID;
count++;
}
}
// After loop has finished: Wrap up - add one last vertex to graph.
currVertex.swapEdges(edges);
vertices.push_back(currVertex);
// Finalize input parsing and processing
graph.finalizeInputParsing(roundAssignations);
}
void InputManagement::prepareDBLP(string inputPath, string outputPath)
{
ifstream inputFile;
ofstream outputFile;
string firstID;
string secondID;
const uint expectedSize = 17631144;
uint count = 0;
uint fID = 0;
uint sID = 0;
// Contains edges, used for sorting
vector<pair<uint, uint>> edges(expectedSize / 2);
cout << endl << " :: Trying to open " << inputPath << endl;
inputFile.open(inputPath.c_str(), ios::in);
outputFile.open(outputPath.c_str(), ios::out);
// Skip comment line
inputFile.ignore(100, '\n');
while (!inputFile.eof()) {
inputFile >> firstID;
inputFile >> secondID;
// Ignore rest of the line
inputFile.ignore(100, '\n');
// Cast to numbers
fID = atoi(firstID.c_str());
sID = atoi(secondID.c_str());
// Reverse source/destination order
edges.push_back(pair<uint, uint>(sID, fID));
}
cout << " :: Finished reading." << endl;
// Sort edges (by destination first, source second)
sort(edges.begin(), edges.end(), DBLP_Edge_SortPred());
cout << " :: Finished sorting." << endl;
// Write sorted edges/vertex combinations to file
// Modified version with weights
for (uint index = 0; index < edges.size(); index++) {
// Count how many identical pairs appear
for (count = index; count < edges.size() && edges[count] == edges[index]; count++);
outputFile << edges[index].first << " " << edges[index].second << " " << (count - index) << endl;
index = count - 1;
count = 0;
}
cout << " :: Finished writing." << endl << endl;
inputFile.close();
outputFile.close();
}