-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadjlist.cu
202 lines (176 loc) · 5.75 KB
/
adjlist.cu
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
/* Structures for compact adjacency lists, built from a list of edges */
#include "adjlist.cuh"
#include "tools.cuh"
#include "edgelist.cuh"
#include <algorithm>
using namespace std;
// --------------------------------------------------
// ----------- General Adjlist structure ------------
// --------------------------------------------------
Adjlist::Adjlist(Edgelist &g, int edge_factor/*=1*/, int node_factor/*=1*/) :
edge_factor(edge_factor),
node_factor(node_factor),
directed(edge_factor == node_factor),
n(g.n),
e(edge_factor * g.e) {
cd.reserve(n*node_factor + 1); // warning: .size() will not be defined
adj.reserve(g.e*edge_factor);
ranker_reset();
}
void Adjlist::ranker_reset() {
ranker=[](ul u) { return u; };
}
void Adjlist::print_some(int a) const {
cout << "# printing " << a << " edges" << endl;
for (ul u = 0; u < n; ++u) {
for (auto &v : neigh_iter(u)) {
cout << u << " " << v << endl;
if(a-- < 0) return;
}
}
}
void Adjlist::reorder(const vector<ul> &rank) {
Edgelist h = to_edgelist_ranked(true, rank);
build_from_edgelist(h, true);
}
Edgelist Adjlist::to_edgelist(bool directed/*=true*/) {
Edgelist h(n, directed ? e / edge_factor : e);
for (ul u = 0; u < n; ++u) {
for (auto &v : neigh_iter(u))
if (build_edge_condition(u, v)) // if undirected, put only half of two-sided edges
h.edges.push_back(edge( ranker(u), ranker(v) ));
cd[u] = 0;
}
h.sort_edges();
return h;
}
Edgelist Adjlist::to_edgelist_ranked(bool directed, const vector<ul> &rank) {
ranker = [&rank](ul u) { return rank[u]; };
Edgelist h = to_edgelist(directed);
ranker_reset();
return h;
}
void Adjlist::build_from_edgelist(Edgelist &g, bool sorted/*=false*/) {
vector<ul> deg = compute_degrees(g);
compute_cumulated_degrees(deg);
build_adj_array(g, deg);
if(sorted) sort_neighbours(); // sort neighbours wrt new order
}
void Adjlist::build_from_edgelist_ranked(Edgelist &g, const vector<ul> &rank) {
ranker = [&rank](ul u) { return rank[u]; };
build_from_edgelist(g, true);
ranker_reset();
}
void Adjlist::compute_cumulated_degrees(vector<ul> °) {
cd[0] = 0;
for (ul u=0; u < n*node_factor; ++u) {
cd[u+1] = cd[u] + deg[u];
deg[u] = 0; // https://stackoverflow.com/questions/8848575/fastest-way-to-reset-every-value-of-stdvectorint-to-0/8849789#8849789
}
}
void Adjlist::sort_neighbours() {
for (ul u = 0; u < n*node_factor; ++u) {
sort(neigh_beg(u), neigh_end(u));
}
}
// --------------------------------------------------
// ---------- Undirected Adjlist structure ----------
// --------------------------------------------------
Uadjlist::Uadjlist(Edgelist &g) : Adjlist(g, 2) { build_from_edgelist(g); } // 2*e for undirected
Uadjlist::Uadjlist(Edgelist &g, const vector<ul> &rank) : Adjlist(g, 2) { build_from_edgelist_ranked(g, rank); } // 2*e for undirected
vector<ul> Uadjlist::compute_degrees(Edgelist &g) { // warning: if input file is directed, edges may be counted twice
vector<ul> deg;
if(g.deg_computed) {
Info("Transfering degrees instead of recomputing");
deg.reserve(n);
for (ul u = 0; u < n; u++)
deg[ranker(u)] = g.deg[u];
}
else {
deg.resize(n, 0);
for (auto &edge : g.edges) {
++deg[ranker(edge.first)];
++deg[ranker(edge.second)]; // if undirected
}
}
return deg;
}
void Uadjlist::build_adj_array(Edgelist &g, vector<ul> °) {
for (auto &edge : g.edges) {
ul u = ranker(edge.first);
ul v = ranker(edge.second);
adj[ cd[u] + deg[u]++ ] = v;
adj[ cd[v] + deg[v]++ ] = u; // if undirected
}
}
// --------------------------------------------------
// ---------- Directed Adjlist structure ------------
// --------------------------------------------------
Dadjlist::Dadjlist(Edgelist &g) : Adjlist(g, 1) { build_from_edgelist(g); }
Dadjlist::Dadjlist(Edgelist &g, const vector<ul> &rank) : Adjlist(g, 1) { build_from_edgelist_ranked(g, rank); }
vector<ul> Dadjlist::compute_degrees(Edgelist &g) {
vector<ul> deg;
if(g.deg_computed) {
Info("Transfering degrees instead of recomputing");
deg.reserve(n);
for (ul u = 0; u < n; u++)
deg[ranker(u)] = g.degOut[u];
}
else {
deg.resize(n, 0);
for (auto &edge : g.edges)
++deg[ranker(edge.first)];
}
return deg;
}
void Dadjlist::build_adj_array(Edgelist &g, vector<ul> °) {
for (auto &edge : g.edges) {
ul u = ranker(edge.first);
adj[ cd[u] + deg[u]++ ] = ranker(edge.second);
}
}
// --------------------------------------------------
// --------- Both sided Adjlist structure -----------
// --------------------------------------------------
Badjlist::Badjlist(Edgelist &g) : Adjlist(g, 2, 2) { build_from_edgelist(g); }
Badjlist::Badjlist(Edgelist &g, const vector<ul> &rank) : Adjlist(g, 2, 2) { build_from_edgelist_ranked(g, rank); }
vector<ul> Badjlist::compute_degrees(Edgelist &g) {
vector<ul> deg;
if(g.deg_computed) {
Info("Transfering degrees instead of recomputing");
deg.reserve(2*n);
for (ul u = 0; u < n; u++) {
deg[ranker(u)] = g.degOut[u];
deg[ranker(u) + n] = g.degIn[u];
}
}
else {
deg.resize(2*n, 0);
for (auto &edge : g.edges) {
++deg[ranker(edge.first)]; // degOut
++deg[ranker(edge.second) + n]; // degIn is slided by n slots in the array
}
}
return deg;
}
void Badjlist::build_adj_array(Edgelist &g, vector<ul> °) {
for (auto &edge : g.edges) {
ul u = ranker(edge.first);
ul v = ranker(edge.second);
adj[ cd[u] + deg[u]++ ] = v; // degOut
adj[ cd[v+n] + deg[v+n]++ ] = u; // degIn
}
}
void Badjlist::print_some(int a) const {
cout << "# printing " << a << " edges" << endl;
for (ul u = 0; u < n; ++u) {
for (auto &v : neighOut_iter(u)) {
cout << u << " -> " << v << endl;
if(a-- < 0) return;
}
for (auto &v : neighIn_iter(u)) {
cout << u << " <- " << v << endl;
if(a-- < 0) return;
}
}
}