-
Notifications
You must be signed in to change notification settings - Fork 0
/
sequential_boruvka.h
76 lines (60 loc) · 2.49 KB
/
sequential_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
#ifndef __SEQUENTIAL_MST_H
#define __SEQUENTIAL_MST_H
#include <algorithm>
#include <vector>
#include "graph.h"
#include "parallel_array.h"
#include "sequential_dsu.h"
struct SequentialBoruvkaMST {
ParallelArray<Edge> calculate_mst(Graph graph) {
SequentialDSU 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) {
std::vector<std::pair<u32, u32>> shortest_edges(initial_num_nodes,
{ 0, std::numeric_limits<u32>::max() });
for (u32 i = 0; i < graph.num_edges(); ++i) {
const Edge& e = graph.edges[i];
if (shortest_edges[e.from].second > e.weight) {
shortest_edges[e.from] = { i, e.weight };
}
}
for (u32 i = 0; i < graph.num_nodes(); ++i) {
u32 u = graph.nodes[i];
const Edge& min_edge_u = graph.edges[shortest_edges[u].first];
u32 v = min_edge_u.to;
const Edge& min_edge_v = graph.edges[shortest_edges[v].first];
if (min_edge_v.to != u || (min_edge_v.to == u && u < v)) {
node_sets.unite(u, v);
mst[current_mst_size++] = min_edge_u;
}
}
std::vector<Edge> new_edges;
for (u32 i = 0; i < graph.num_edges(); ++i) {
if (!node_sets.same_set(graph.edges[i].from, graph.edges[i].to)) {
Edge e = graph.edges[i];
e.from = node_sets.find_root(e.from);
e.to = node_sets.find_root(e.to);
new_edges.push_back(e);
}
}
std::vector<u32> new_nodes;
for (u32 i = 0; i < graph.num_nodes(); ++i) {
if (node_sets.find_root(graph.nodes[i]) == graph.nodes[i]) {
new_nodes.push_back(graph.nodes[i]);
}
}
graph.nodes = ParallelArray<u32>(new_nodes.size());
graph.edges = ParallelArray<Edge>(new_edges.size());
for (u32 i = 0; i < new_nodes.size(); ++i) {
graph.nodes[i] = new_nodes[i];
}
for (u32 i = 0; i < new_edges.size(); ++i) {
graph.edges[i] = new_edges[i];
}
}
return mst;
}
};
#endif