-
Notifications
You must be signed in to change notification settings - Fork 0
/
mst.cpp
369 lines (313 loc) · 9.4 KB
/
mst.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
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
#include <set>
#include <vector>
#include <iostream>
#include <fstream>
#include <algorithm>
#include <unordered_map>
#include "fibonacci/fibonacci.hpp"
const long long inf = 1e17;
using std::vector;
using std::set;
using std::pair;
using std::swap;
using std::cout;
using std::endl;
vector <vector <long long> > graph;
vector <pair <long long, pair <long long, long long> > > edges;
vector <int> dsu_par, dsu_rnk;
int n, m;
long long power (long long a, long long b) {
if (!b)
return (a != 0);
if (b & 1)
return power(a, b - 1) * a;
long long y = power(a, b >> 1);
return y * y;
}
int get_par (int a) {
if (dsu_par[a] == a)
return a;
return dsu_par[a] = get_par(dsu_par[a]);
}
void join (int a, int b) {
if (dsu_rnk[a] > dsu_rnk[b])
swap(a, b);
dsu_rnk[b] += (dsu_rnk[a] == dsu_rnk[b]);
dsu_par[a] = b;
}
int join (int a, int b, set <pair <long long, long long> > & sta, set <pair <long long, long long> > & stb) {
if (dsu_rnk[a] > dsu_rnk[b]) {
dsu_par[b] = a;
sta.insert(stb.begin(), stb.end());
stb.clear();
return b;
}
else {
dsu_par[a] = b;
stb.insert(sta.begin(), sta.end());
sta.clear();
dsu_rnk[b] += (dsu_rnk[b] == dsu_rnk[a]);
return a;
}
}
void output_graph () {
for (int j = 0; j < n; j++) {
cout << j << ": ";
for (int i = 0; i < graph[j].size(); i ++) {
cout << graph[j][i] << ' ';
}
cout << '\n';
}
}
void output_vector (vector <int> & vect) {
cout << "\n=====\n";
cout << vect.size() << '\n';
for (int j = 0; j < vect.size(); ++j) {
cout << vect[j] << ' ';
}
cout << "\n=====\n";
}
void fill_graph (std::ifstream & fin) {
graph.clear();
fin >> n >> m;
graph.resize(n);
long long u, v, w;
for (int j = 0; j < m; j++) {
fin >> u >> v >> w;
graph[u].push_back(v);
graph[v].push_back(u);
graph[u].push_back(w);
graph[v].push_back(w);
}
}
void fill_graph_edges (std::ifstream & fin) {
fin >> n >> m;
edges.clear();
edges.resize(m);
long long u, v, w;
for (int j = 0; j < m; j++) {
fin >> u >> v >> w;
edges[j] = {w, {u, v}};
}
}
void fill_graph_boruvka (std::ifstream & fin, vector <set <pair <long long, long long> > > & edges) {
fin >> n >> m;
edges.resize(n);
long long u, v, w;
for (int j = 0; j < m; j++) {
fin >> u >> v >> w;
edges[u].insert({w, v});
edges[v].insert({w, u});
}
}
long long count_mst_weight_prim (std::ifstream & fin) {
cout << "Prim started\n";
fill_graph(fin);
set <pair <long long, long long> > st;
vector <long long> min_dist(n, inf);
vector <bool> used(n);
min_dist[0] = 0;
st.insert({0, 0});
long long sum = 0;
for (int j = 0; j < n; j++) {
int v = st.begin()->second;
sum += st.begin()->first;
st.erase(st.begin());
used[v] = true;
for (int i = 0; i < graph[v].size(); i += 2) {
int u = graph[v][i];
int w = graph[v][i + 1];
if (used[u])
continue;
if (min_dist[u] > w) {
auto p = st.find({min_dist[u], u});
if (p != st.end())
st.erase(p);
min_dist[u] = w;
st.insert({min_dist[u], u});
}
}
}
return sum;
}
long long count_mst_weight_boruvka (std::ifstream & fin) {
cout << "Boruvka started\n";
long long sum = 0;
vector <set <pair <long long, long long> > > edges;
fill_graph_boruvka(fin, edges);
dsu_par.clear();
dsu_rnk.clear();
dsu_rnk.resize(n);
dsu_par.resize(n);
set <long long> trees, deleted;
for (long long j = 0; j < n; ++j) {
dsu_par[j] = j;
trees.insert(j);
}
while (trees.size() > 1) {
auto it = trees.begin();
while (it != trees.end()) {
long long v = *it;
auto jt = edges[v].begin();
while (jt != edges[v].end() && get_par(jt->second) == v) {
edges[v].erase(jt);
jt = edges[v].begin();
}
if (edges[v].empty()) {
++it;
continue;
}
sum += jt->first;
long long u = get_par(jt->second);
edges[v].erase(edges[v].begin());
int erased = join(v, u, edges[v], edges[u]);
deleted.insert(erased);
++it;
}
while (!deleted.empty()) {
auto it = trees.find(*deleted.begin());
//if (it != trees.end())
trees.erase(it);
deleted.erase(deleted.begin());
}
}
return sum;
}
long long count_mst_weight_kruskal (std::ifstream & fin) {
cout << "Kruskal started\n";
long long sum = 0;
fill_graph_edges(fin);
sort(edges.begin(), edges.end());
dsu_par.clear();
dsu_rnk.clear();
dsu_rnk.resize(n);
dsu_par.resize(n);
for (int j = 0; j < n; ++j)
dsu_par[j] = j;
int components = n;
for (int j = 0; components > 1 && j < m; ++j) {
auto p = edges[j];
long long v = p.second.first, u = p.second.second, w = p.first;
v = get_par(v);
u = get_par(u);
if (u == v)
continue;
sum += w;
join(u, v);
--components;
}
if (components > 1)
throw "Graph is not connected!\n";
return sum;
}
void run_prim(int v, int max_heap_size, int current_components_amount, vector <bool> & used, vector <pair <int, int> > & connections, long long & sum) {
FibonacciHeap <pair <long long, long long> > fh;
std::unordered_map <long long, long long> dist;
std::unordered_map <int, node <pair <long long, long long> > * > nodes(current_components_amount);
std::unordered_map <long long, bool> used_in_prim;
used[v] = used_in_prim[v] = true;
int heap_size = 1;
fh.insert({0, v});
long long u, w;
do {
auto p = fh.removeMinimum();
--heap_size;
u = p.second, w = p.first;
sum += w;
connections.push_back({u, v});
if (used[u] && u != v) {
return;
}
used[u] = used_in_prim[u] = true;
;
for (int j = 0; j < graph[u].size(); j += 2) {
long long to = graph[u][j];
//cout << to << ' ';
if (used_in_prim[to])
continue;
w = graph[u][j + 1];
if (dist.find(to) == dist.end()) {
++heap_size;
nodes[to] = fh.insert({w, to});
dist[to] = w;
}
else if (dist[to] > w) {
fh.decreaseKey(nodes[to], {w, to});
dist[to] = w;
}
}
} while ((u == v || heap_size <= max_heap_size) && heap_size > 0);
}
void update_components (vector <pair <int, int> > & connections, vector <int> & old_components, vector <int> & new_components) {
for (int j = 0; j < connections.size(); ++j) {
int u = get_par(connections[j].first);
int v = get_par(connections[j].second);
join(u, v);
}
connections.clear();
std::unordered_map <int, vector <int> > tree(old_components.size());
for (int j = 0; j < old_components.size(); ++j) {
int v = old_components[j];
int u = get_par(v);
tree[u].push_back(v);
}
new_components.clear();
for (int j = 0; j < old_components.size(); j++) {
int v = old_components[j];
if (tree[v].empty())
continue;
new_components.push_back(v);
std::unordered_map <int, long long> dist;
for (int i = 0; i < tree[v].size(); ++i) {
int u = tree[v][i];
for (int k = 0; k < graph[u].size(); k += 2) {
long long to = get_par(graph[u][k]), w = graph[u][k + 1];
if (to == v)
continue;
if (dist.find(to) != dist.end())
dist[to] = std::min(dist[to], w);
else
dist[to] = w;
}
graph[u].clear();
}
graph[v].clear();
for (auto it = dist.begin(); it != dist.end(); ++it) {
graph[v].push_back(it->first);
graph[v].push_back(it->second);
}
}
old_components.clear();
old_components = std::move(new_components);
}
long long count_mst_weight_log_star (std::ifstream & fin) {
cout << "Log star launched\n";
fill_graph(fin);
vector <int> old_components(n), new_components;
vector <bool> used;
dsu_par.clear();
dsu_rnk.clear();
used.clear();
dsu_par.resize(n);
dsu_rnk.resize(n);
used.resize(n);
for (int j = 0; j < n; ++j) {
old_components[j] = dsu_par[j] = j;
}
vector <pair <int, int> > connections;
long long sum = 0, max_heap_size = 2;
while (old_components.size() > 1) {
//cout << max_heap_size << '\n';
for (int j = 0; j < old_components.size(); ++j) {
int v = old_components[j];
if (used[v])
continue;
run_prim(v, max_heap_size, old_components.size(), used, connections, sum);
}
max_heap_size = power(2, max_heap_size);
update_components(connections, old_components, new_components);
for (int j = 0; j < old_components.size(); ++j)
used[old_components[j]] = false;
}
return sum;
}