-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathprpack_base_graph.cpp
298 lines (285 loc) · 8.48 KB
/
prpack_base_graph.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
#include "prpack_base_graph.h"
#include "prpack_utils.h"
#include <cassert>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <vector>
#include <limits>
using namespace prpack;
using namespace std;
void prpack_base_graph::initialize() {
heads = NULL;
tails = NULL;
vals = NULL;
}
prpack_base_graph::prpack_base_graph(const prpack_csc* g) {
initialize();
num_vs = g->num_vs;
num_es = g->num_es;
// fill in heads and tails
num_self_es = 0;
int* hs = g->heads;
int* ts = g->tails;
tails = new int[num_vs];
memset(tails, 0, num_vs*sizeof(tails[0]));
for (int h = 0; h < num_vs; ++h) {
const int start_ti = hs[h];
const int end_ti = (h + 1 != num_vs) ? hs[h + 1] : num_es;
for (int ti = start_ti; ti < end_ti; ++ti) {
const int t = ts[ti];
++tails[t];
if (h == t)
++num_self_es;
}
}
for (int i = 0, sum = 0; i < num_vs; ++i) {
const int temp = sum;
sum += tails[i];
tails[i] = temp;
}
heads = new int[num_es];
int* osets = new int[num_vs];
memset(osets, 0, num_vs*sizeof(osets[0]));
for (int h = 0; h < num_vs; ++h) {
const int start_ti = hs[h];
const int end_ti = (h + 1 != num_vs) ? hs[h + 1] : num_es;
for (int ti = start_ti; ti < end_ti; ++ti) {
const int t = ts[ti];
heads[tails[t] + osets[t]++] = h;
}
}
// clean up
delete[] osets;
}
prpack_base_graph::prpack_base_graph(const prpack_int64_csc* g) {
initialize();
// TODO remove the assert and add better behavior
assert(num_vs <= std::numeric_limits<int>::max());
num_vs = (int)g->num_vs;
num_es = (int)g->num_es;
// fill in heads and tails
num_self_es = 0;
int64_t* hs = g->heads;
int64_t* ts = g->tails;
tails = new int[num_vs];
memset(tails, 0, num_vs*sizeof(tails[0]));
for (int h = 0; h < num_vs; ++h) {
const int start_ti = (int)hs[h];
const int end_ti = (h + 1 != num_vs) ? (int)hs[h + 1] : num_es;
for (int ti = start_ti; ti < end_ti; ++ti) {
const int t = (int)ts[ti];
++tails[t];
if (h == t)
++num_self_es;
}
}
for (int i = 0, sum = 0; i < num_vs; ++i) {
const int temp = sum;
sum += tails[i];
tails[i] = temp;
}
heads = new int[num_es];
int* osets = new int[num_vs];
memset(osets, 0, num_vs*sizeof(osets[0]));
for (int h = 0; h < num_vs; ++h) {
const int start_ti = (int)hs[h];
const int end_ti = (h + 1 != num_vs) ? (int)hs[h + 1] : num_es;
for (int ti = start_ti; ti < end_ti; ++ti) {
const int t = (int)ts[ti];
heads[tails[t] + osets[t]++] = h;
}
}
// clean up
delete[] osets;
}
prpack_base_graph::prpack_base_graph(const prpack_csr* g) {
initialize();
assert(false);
// TODO
}
prpack_base_graph::prpack_base_graph(const prpack_edge_list* g) {
initialize();
num_vs = g->num_vs;
num_es = g->num_es;
// fill in heads and tails
num_self_es = 0;
int* hs = g->heads;
int* ts = g->tails;
tails = new int[num_vs];
memset(tails, 0, num_vs*sizeof(tails[0]));
for (int i = 0; i < num_es; ++i) {
++tails[ts[i]];
if (hs[i] == ts[i])
++num_self_es;
}
for (int i = 0, sum = 0; i < num_vs; ++i) {
const int temp = sum;
sum += tails[i];
tails[i] = temp;
}
heads = new int[num_es];
int* osets = new int[num_vs];
memset(osets, 0, num_vs*sizeof(osets[0]));
for (int i = 0; i < num_es; ++i)
heads[tails[ts[i]] + osets[ts[i]]++] = hs[i];
// clean up
delete[] osets;
}
prpack_base_graph::prpack_base_graph(const char* filename, const char* format, const bool weighted) {
initialize();
FILE* f = fopen(filename, "r");
const string s(filename);
const string t(format);
const string ext = (t == "") ? s.substr(s.rfind('.') + 1) : t;
if (ext == "smat")
read_smat(f, weighted);
else {
prpack_utils::validate(!weighted, "Error: graph format is not compatible with weighted option.");
if (ext == "edges" || ext == "eg2")
read_edges(f);
else if (ext == "graph-txt")
read_ascii(f);
else
prpack_utils::validate(false, "Error: invalid graph format.");
}
fclose(f);
}
prpack_base_graph::~prpack_base_graph() {
delete[] heads;
delete[] tails;
delete[] vals;
}
void prpack_base_graph::read_smat(FILE* f, const bool weighted) {
// read in header
double blah;
assert(fscanf(f, "%d %lf %d", &num_vs, &blah, &num_es) == 3);
// fill in heads and tails
num_self_es = 0;
int* hs = new int[num_es];
int* ts = new int[num_es];
heads = new int[num_es];
tails = new int[num_vs];
double* vs = NULL;
if (weighted) {
vs = new double[num_es];
vals = new double[num_es];
}
memset(tails, 0, num_vs*sizeof(tails[0]));
for (int i = 0; i < num_es; ++i) {
assert(fscanf(f, "%d %d %lf", &hs[i], &ts[i], &((weighted) ? vs[i] : blah)) == 3);
++tails[ts[i]];
if (hs[i] == ts[i])
++num_self_es;
}
for (int i = 0, sum = 0; i < num_vs; ++i) {
const int temp = sum;
sum += tails[i];
tails[i] = temp;
}
int* osets = new int[num_vs];
memset(osets, 0, num_vs*sizeof(osets[0]));
for (int i = 0; i < num_es; ++i) {
const int idx = tails[ts[i]] + osets[ts[i]]++;
heads[idx] = hs[i];
if (weighted)
vals[idx] = vs[i];
}
// clean up
delete[] hs;
delete[] ts;
delete[] vs;
delete[] osets;
}
void prpack_base_graph::read_edges(FILE* f) {
vector<vector<int> > al;
int h, t;
num_es = num_self_es = 0;
while (fscanf(f, "%d %d", &h, &t) == 2) {
const int m = (h < t) ? t : h;
if ((int) al.size() < m + 1)
al.resize(m + 1);
al[t].push_back(h);
++num_es;
if (h == t)
++num_self_es;
}
num_vs = al.size();
heads = new int[num_es];
tails = new int[num_vs];
for (int tails_i = 0, heads_i = 0; tails_i < num_vs; ++tails_i) {
tails[tails_i] = heads_i;
for (int j = 0; j < (int) al[tails_i].size(); ++j)
heads[heads_i++] = al[tails_i][j];
}
}
void prpack_base_graph::read_ascii(FILE* f) {
assert(fscanf(f, "%d", &num_vs) == 1);
while (getc(f) != '\n');
vector<int>* al = new vector<int>[num_vs];
num_es = num_self_es = 0;
char s[32];
for (int h = 0; h < num_vs; ++h) {
bool line_ended = false;
while (!line_ended) {
for (int i = 0; ; ++i) {
s[i] = getc(f);
if ('9' < s[i] || s[i] < '0') {
line_ended = s[i] == '\n';
if (i != 0) {
s[i] = '\0';
const int t = atoi(s);
al[t].push_back(h);
++num_es;
if (h == t)
++num_self_es;
}
break;
}
}
}
}
heads = new int[num_es];
tails = new int[num_vs];
for (int tails_i = 0, heads_i = 0; tails_i < num_vs; ++tails_i) {
tails[tails_i] = heads_i;
for (int j = 0; j < (int) al[tails_i].size(); ++j)
heads[heads_i++] = al[tails_i][j];
}
delete[] al;
}
prpack_base_graph::prpack_base_graph(int nverts, int nedges,
std::pair<int,int>* edges) {
initialize();
num_vs = nverts;
num_es = nedges;
// fill in heads and tails
num_self_es = 0;
int* hs = new int[num_es];
int* ts = new int[num_es];
tails = new int[num_vs];
memset(tails, 0, num_vs*sizeof(tails[0]));
for (int i = 0; i < num_es; ++i) {
assert(edges[i].first >= 0 && edges[i].first < num_vs);
assert(edges[i].second >= 0 && edges[i].second < num_vs);
hs[i] = edges[i].first;
ts[i] = edges[i].second;
++tails[ts[i]];
if (hs[i] == ts[i])
++num_self_es;
}
for (int i = 0, sum = 0; i < num_vs; ++i) {
int temp = sum;
sum += tails[i];
tails[i] = temp;
}
heads = new int[num_es];
int* osets = new int[num_vs];
memset(osets, 0, num_vs*sizeof(osets[0]));
for (int i = 0; i < num_es; ++i)
heads[tails[ts[i]] + osets[ts[i]]++] = hs[i];
// clean up
delete[] hs;
delete[] ts;
delete[] osets;
}