-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGraphUtils.cpp
More file actions
189 lines (153 loc) · 4.54 KB
/
GraphUtils.cpp
File metadata and controls
189 lines (153 loc) · 4.54 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
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
/*
* GraphScheduler.cpp
*
* Created on: Oct 22, 2014
* Author: aepasto
*/
#include "GraphUtils.h"
#include <cstring>
#include <cassert>
#include <iostream>
#include <fstream>
using namespace std;
void import_graph(const string& file_name, Graph* graph) {
graph->clear();
ifstream file_stream(file_name.c_str(), ios_base::in);
std::string delimiter = " ";
string line;
vector<int> tokens;
assert(file_stream.is_open());
while (getline(file_stream, line)) {
//if (count % 10000000 == 0) {
// cout << "Imported: " << count / 1000000 << "M" << endl;
// cout.flush();
//}
//count++;
tokens.clear();
size_t pos = 0;
std::string token;
while (pos != line.npos) {
pos = line.find(delimiter);
token = line.substr(0, pos);
int token_int = atoi(token.c_str());
assert(token_int >= 0);
tokens.push_back(token_int);
line.erase(0, pos + delimiter.length());
}
if (line.size() == 0) { // last line
break;
}
graph->add_edge(tokens[0], tokens[1]);
}
assert(graph->num_edges() > 0);
}
// Input: file with
// Line i : "page id integer" <tab> "category string" <tab> "comma separated list of ids (interger) in order of ranking)"
void import_rankings(const string& file_name, vector<Ranking>* ranks,
vector<string>* classess_of_ranks) {
classess_of_ranks->clear();
ranks->clear();
ifstream file_stream(file_name.c_str(), ios_base::in);
std::string delimiter = "\t";
string line;
vector<string> tokens;
assert(file_stream.is_open());
string line_cp;
while (getline(file_stream, line)) {
line_cp = line;
tokens.clear();
size_t pos = 0;
std::string token;
while (pos != line.npos) {
pos = line.find(delimiter);
token = line.substr(0, pos);
tokens.push_back(token);
line.erase(0, pos + delimiter.length());
}
if (line.size() == 0 || tokens.size() != 3) {
cerr << "ERR LINE" << line << endl;
continue;
}
int id = atoi(tokens[0].c_str());
string cat = tokens[1];
string ranking_as_str = tokens[2];
assert(id >= 0);
classess_of_ranks->push_back(cat);
// Parsing ranking (comma separated)
vector<int> rank_int;
pos = 0;
while (pos != ranking_as_str.npos) {
pos = ranking_as_str.find(",");
token = ranking_as_str.substr(0, pos);
if (token.size() > 0) {
int elem = atoi(token.c_str());
rank_int.push_back(elem);
}
ranking_as_str.erase(0, pos + delimiter.length());
}
assert(rank_int.size() > 0);
Ranking rank(rank_int);
ranks->push_back(rank);
}
assert(ranks->size() > 0);
}
void import_nodes_to_rank(const string& file_name,
unordered_set<int>* to_rank) {
to_rank->clear();
ifstream file_stream(file_name.c_str(), ios_base::in);
std::string delimiter = "\t";
string line;
vector<string> tokens;
assert(file_stream.is_open());
string line_cp;
while (getline(file_stream, line)) {
line_cp = line;
tokens.clear();
size_t pos = 0;
std::string token;
while (pos != line.npos) {
pos = line.find(delimiter);
token = line.substr(0, pos);
tokens.push_back(token);
line.erase(0, pos + delimiter.length());
}
if (line.size() == 0 || tokens.size() != 2) {
cerr << "ERR LINE" << line << endl;
continue;
}
int id = atoi(tokens[0].c_str());
assert(id >= 0);
to_rank->insert(id);
}
assert(to_rank->size() > 0);
}
void import_seed_list(const string& file_name,
vector<pair<int, string> >* vec) {
vec->clear();
ifstream file_stream(file_name.c_str(), ios_base::in);
std::string delimiter = "\t";
string line;
vector<string> tokens;
assert(file_stream.is_open());
string line_cp;
while (getline(file_stream, line)) {
line_cp = line;
tokens.clear();
size_t pos = 0;
std::string token;
while (pos != line.npos) {
pos = line.find(delimiter);
token = line.substr(0, pos);
tokens.push_back(token);
line.erase(0, pos + delimiter.length());
}
if (line.size() == 0 || tokens.size() != 3) {
cerr << "ERR LINE" << line << endl;
continue;
}
int id = atoi(tokens[2].c_str());
assert(id >= 0);
vec->push_back(make_pair(id, tokens[1]));
}
assert(vec->size() > 0);
}