-
Notifications
You must be signed in to change notification settings - Fork 0
/
boruvka.h
369 lines (299 loc) · 14.2 KB
/
boruvka.h
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
#ifndef __BORUVKA_H
#define __BORUVKA_H
#include <algorithm>
#include <limits>
#include <omp.h>
#include <pair>
#include <unordered_map>
#include "dsu.h"
#include "sequential_dsu.h"
#include "graph.h"
#include "parallel_array.h"
#include "prefix_sum.h"
struct BoruvkaMST {
/**
* I use the same atomic pair that I use in dsu.h
* TODO: create a special class for this
*/
const u32 EDGE_BINARY_BUCKET_SIZE = 32;
const u64 EDGE_WEIGHT_MASK = 0xFFFFFFFF00000000ULL;
u64 encode_edge(u32 id, u32 weight) {
return (static_cast<u64>(weight) << EDGE_BINARY_BUCKET_SIZE) + id;
}
u32 get_id(u64 encoded_edge) {
return static_cast<u32>(encoded_edge);
}
u32 get_weight(u64 encoded_edge) {
return static_cast<u32>(encoded_edge >> EDGE_BINARY_BUCKET_SIZE);
}
/**
* Calculates MST of given graph and returns a ParallelArray<Edge> object
*/
ParallelArray<Edge> calculate_mst(Graph graph, u32 NUM_THREADS = omp_get_max_threads()) {
DSU node_sets(graph.num_nodes());
ParallelArray<Edge> mst(graph.num_nodes() - 1);
u32 current_mst_size = 0;
u32 initial_num_nodes = graph.num_nodes();
while (graph.num_nodes() != 1) {
ParallelArray<atomic_u64> shortest_edges(initial_num_nodes);
/* Calculating shortest edges from each node */
#pragma omp parallel num_threads(NUM_THREADS)
{
std::unordered_map<u32, std::pair<u32, u32>> local_shortest_edges(graph.num_nodes());
#pragma omp for
for (u32 i = 0; i < initial_num_nodes; ++i) {
shortest_edges[i] = encode_edge(0, std::numeric_limits<u32>::max());
}
#pragma omp for
for (u32 i = 0; i < graph.num_edges(); ++i) {
const Edge& e = graph.edges[i];
if (local_shortest_edges.count(e.from) == 0 ||
local_shortest_edges[e.from].first > e.weight) {
local_shortest_edges[e.from] = { e.weight, i };
}
}
for (const auto& p : local_shortest_edges) { /* O(M / p) operations in each thread */
u64 old = shortest_edges[p.first];
/* p.second = { weight, id } */
u64 encoded_edge = encode_edge(p.second.second, p.second.first);
while (true) { /* This loop is wait-free */
if (get_weight(old) < p.second.first ||
shortest_edges[p.first].compare_exchange_strong(old, encoded_edge)) {
break;
}
}
}
}
/* Calculating selected edges */
ParallelArray<u32> edge_selected(graph.num_edges());
#pragma omp parallel for
for (u32 i = 0; i < graph.num_edges(); ++i) {
edge_selected[i] = 0;
}
#pragma omp parallel for
for (u32 i = 0; i < graph.num_nodes(); ++i) {
u32 u = graph.nodes[i];
const Edge& min_edge_u = graph.edges[get_id(shortest_edges[u])];
u32 v = min_edge_u.to;
const Edge& min_edge_v = graph.edges[get_id(shortest_edges[v])];
if (min_edge_v.to != u || (min_edge_v.to == u && u < v)) {
node_sets.unite(u, v);
edge_selected[get_id(shortest_edges[u])] = 1;
}
}
/* Adding edges to MST */
PrefixSum edge_selected_prefix(graph.num_edges(), edge_selected);
#pragma omp parallel for
for (u32 i = 0; i < graph.num_edges(); ++i) {
if (edge_selected[i]) {
mst[current_mst_size + edge_selected_prefix[i] - 1] = graph.edges[i];
}
}
current_mst_size += edge_selected_prefix[graph.num_edges() - 1];
/* Calculating remaining edges */
ParallelArray<u32> edge_remains(graph.num_edges());
#pragma omp parallel for
for (u32 i = 0; i < graph.num_edges(); ++i) {
edge_remains[i] = !node_sets.same_set(graph.edges[i].from, graph.edges[i].to);
}
PrefixSum edge_remains_prefix(graph.num_edges(), edge_remains);
ParallelArray<Edge> new_edges(edge_remains_prefix[graph.num_edges() - 1]);
#pragma omp parallel for
for (u32 i = 0; i < graph.num_edges(); ++i) {
if (edge_remains[i]) {
const Edge& old_edge = graph.edges[i];
new_edges[edge_remains_prefix[i] - 1] = Edge(node_sets.find_root(old_edge.from),
node_sets.find_root(old_edge.to),
old_edge.weight);
}
}
/* Calculating remaining nodes */
ParallelArray<u32> node_remains(graph.num_nodes());
#pragma omp parallel for
for (u32 i = 0; i < graph.num_nodes(); ++i) {
node_remains[i] = (
node_sets.find_root(graph.nodes[i]) == graph.nodes[i]
);
}
PrefixSum node_remains_prefix(graph.num_nodes(), node_remains);
ParallelArray<u32> new_nodes(node_remains_prefix[graph.num_nodes() - 1]);
#pragma omp parallel for
for (u32 i = 0; i < graph.num_nodes(); ++i) {
if (node_remains[i]) {
new_nodes[node_remains_prefix[i] - 1] = graph.nodes[i];
}
}
/* Swapping old graph for new graph */
graph.nodes.swap(new_nodes);
graph.edges.swap(new_edges);
graph.sort_edges();
}
return mst;
}
};
/**
* I used this struct for debugging purposes only, it is out of date
* so you should never use it unless you're sure about it
*/
struct BoruvkaMST_verbose {
/**
* I use the same atomic pair that I use in dsu.h
* TODO: create a special class for this
*/
const u32 EDGE_BINARY_BUCKET_SIZE = 32;
const u64 EDGE_WEIGHT_MASK = 0xFFFFFFFF00000000ULL;
u64 encode_edge(u32 id, u32 weight) {
return (static_cast<u64>(weight) << EDGE_BINARY_BUCKET_SIZE) + id;
}
u32 get_id(u64 encoded_edge) {
return static_cast<u32>(encoded_edge);
}
u32 get_weight(u64 encoded_edge) {
return static_cast<u32>(encoded_edge >> EDGE_BINARY_BUCKET_SIZE);
}
/**
* Calculates MST of given graph and returns an EdgeSet object
* TODO: Current implementation uses a sequential sort, I should write my own parallel sort.
* Also I don't want to use the default unordered_map because it sucks
* since my keys are <= Graph.num_nodes() I can use a simple array with an std::list
*/
ParallelArray<Edge> calculate_mst(Graph graph, u32 NUM_THREADS = omp_get_max_threads()) {
std::cout << graph.num_nodes() << " " << graph.num_edges() << "\n";
for (auto e : graph.edges) {
std::cout << e.from << " " << e.to << " " << e.weight << "\n";
}
DSU node_sets(graph.num_nodes());
ParallelArray<Edge> mst(graph.num_nodes() - 1);
u32 current_mst_size = 0;
u32 initial_num_nodes = graph.num_nodes();
u32 step_cnt = 0;
while (graph.num_nodes() != 1) {
std::cout << "Step " << ++step_cnt << "\n";
ParallelArray<atomic_u64> shortest_edges(initial_num_nodes);
/* Calculating shortest edges from each node */
#pragma omp parallel num_threads(NUM_THREADS)
{
std::unordered_map<u32, std::pair<u32, u32>> local_shortest_edges(graph.num_nodes());
#pragma omp for
for (u32 i = 0; i < initial_num_nodes; ++i) {
shortest_edges[i] = encode_edge(0, std::numeric_limits<u32>::max());
}
#pragma omp single
std::cout << "Initialized shortest edges\n";
#pragma omp for
for (u32 i = 0; i < graph.num_edges(); ++i) {
const Edge& e = graph.edges[i];
if (local_shortest_edges.count(e.from) == 0 ||
local_shortest_edges[e.from].first > e.weight) {
local_shortest_edges[e.from] = { e.weight, i };
}
}
#pragma omp single
std::cout << "Calculated shortest edges in buckets\n";
for (const auto& p : local_shortest_edges) { /* O(M / p) operations in each thread */
#pragma omp critical
std::cout << "from: " << p.first << " weight: " << p.second.first << " id: " << p.second.second << "\n";
u64 old = shortest_edges[p.first];
/* p.second = { weight, id } */
u64 encoded_edge = encode_edge(p.second.second, p.second.first);
while (true) { /* This loop is wait-free */
if (get_weight(old) < p.second.first ||
shortest_edges[p.first].compare_exchange_strong(old, encoded_edge)) {
break;
}
}
}
}
std::cout << "Merged buckets\n";
for (u32 i = 0; i < graph.num_nodes(); ++i) {
std::cout << "node: " << graph.nodes[i] << "\n";
std::cout << "mineid: " << get_id(shortest_edges[graph.nodes[i]]) << "\n";
std::cout << "minew: " << get_weight(shortest_edges[graph.nodes[i]]) << "\n";
}
/* Calculating selected edges */
ParallelArray<u32> edge_selected(graph.num_edges());
#pragma omp parallel for
for (u32 i = 0; i < graph.num_edges(); ++i) {
edge_selected[i] = 0;
}
#pragma omp parallel for
for (u32 i = 0; i < graph.num_nodes(); ++i) {
u32 u = graph.nodes[i];
const Edge& min_edge_u = graph.edges[get_id(shortest_edges[u])];
u32 v = min_edge_u.to;
#pragma omp critical
std::cout << "u: " << u << " v: " << min_edge_u.to << " mu_id: " << get_id(shortest_edges[u]) << " mv_id: " << get_id(shortest_edges[v]) << "\n";
const Edge& min_edge_v = graph.edges[get_id(shortest_edges[v])];
if (min_edge_v.to != u || (min_edge_v.to == u && u < v)) {
node_sets.unite(u, v);
edge_selected[get_id(shortest_edges[u])] = 1;
#pragma omp critical
std:: cout << "selected " << get_id(shortest_edges[u]) << " from " << u << " to " << v << "\n";
}
}
std::cout << "Calculated selected edges\n";
/* Adding edges to MST */
PrefixSum edge_selected_prefix(graph.num_edges(), edge_selected);
#pragma omp parallel for
for (u32 i = 0; i < graph.num_edges(); ++i) {
if (edge_selected[i]) {
#pragma omp critical
std::cout << "Put edge no. " << i << " to position " << current_mst_size + edge_selected_prefix[i] - 1 << "\n";
mst[current_mst_size + edge_selected_prefix[i] - 1] = graph.edges[i];
}
}
current_mst_size += edge_selected_prefix[graph.num_edges() - 1];
std::cout << "Added mst edges\n";
/* Calculating remaining edges */
ParallelArray<u32> edge_remains(graph.num_edges());
#pragma omp parallel for
for (u32 i = 0; i < graph.num_edges(); ++i) {
edge_remains[i] = !node_sets.same_set(graph.edges[i].from, graph.edges[i].to);
}
PrefixSum edge_remains_prefix(graph.num_edges(), edge_remains);
ParallelArray<Edge> new_edges(edge_remains_prefix[graph.num_edges() - 1]);
#pragma omp parallel for
for (u32 i = 0; i < graph.num_edges(); ++i) {
if (edge_remains[i]) {
const Edge& old_edge = graph.edges[i];
new_edges[edge_remains_prefix[i] - 1] = Edge(node_sets.find_root(old_edge.from),
node_sets.find_root(old_edge.to),
old_edge.weight);
}
}
std::cout << "Created new edges\n";
/* Calculating remaining nodes */
ParallelArray<u32> node_remains(graph.num_nodes());
#pragma omp parallel for
for (u32 i = 0; i < graph.num_nodes(); ++i) {
node_remains[i] = (
node_sets.find_root(graph.nodes[i]) == graph.nodes[i]
);
}
PrefixSum node_remains_prefix(graph.num_nodes(), node_remains);
ParallelArray<u32> new_nodes(node_remains_prefix[graph.num_nodes() - 1]);
#pragma omp parallel for
for (u32 i = 0; i < graph.num_nodes(); ++i) {
if (node_remains[i]) {
new_nodes[node_remains_prefix[i] - 1] = graph.nodes[i];
}
}
std::cout << "Created new nodes\n";
/* Swapping old graph for new graph */
graph.nodes.swap(new_nodes);
graph.edges.swap(new_edges);
graph.sort_edges();
std::cout << "Num nodes: " << graph.num_nodes() << "\n";
for (u32 i = 0; i < graph.num_nodes(); ++i) {
std::cout << graph.nodes[i] << "\n";
}
std::cout << "Num edges: " << graph.num_edges() << "\n";
for (u32 i = 0; i < graph.num_edges(); ++i) {
std::cout << graph.edges[i].from << " " << graph.edges[i].to << "\n";
}
std::cout << "Swapped, sorted\n";
}
return mst;
}
};
#endif