-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathmst.cpp
337 lines (247 loc) · 7.86 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
//TODO: rewrite boruvka and prim. possibly parallelize boruvka
#ifndef _MST_CPP
#define _MST_CPP
#include <iostream>
#include <vector>
#include <algorithm>
#include <queue>
using namespace std;
struct Edge{
int head, tail;
float cost;
Edge(int h, int t, float c):head(h),tail(t),cost(c){}
bool operator<(const Edge& e)const {return cost<e.cost; }
};
struct UnionFind{
vector<int> parent; //parent for each node
vector<int> rank; //rank for each node
UnionFind(int n): parent(n), rank(n)
{
for(int i=0;i<n;i++)
make_set(i);
}
void make_set(int i)
{
parent[i]=i;
rank[i]=0;
}
int find_set(int i)
{
if(i!=parent[i])
parent[i] = find_set(parent[i]); //path compression
return parent[i];
}
void join(int i, int j)
{
i=find_set(i);
j=find_set(j);
if (i==j)
return;
if (rank[i]>rank[j]) //i longer. attach j to i
parent[j]=i;
else{
parent[i]=j; //i not longer. attach i to j
if(rank[i]==rank[j]) //rank grows if equal length
rank[j]+=1;
}
}
};
vector<Edge> kruskal(int n, vector<vector<float> >& cost)
{
vector<Edge> edges;
for(int i=0; i<n;i++)
for(int j=i+1;j<n;j++)
if(cost[i][j]>0)
edges.push_back(Edge(i,j,cost[i][j]));
vector<Edge> tree;
UnionFind forest(n); //does make_set for each node
sort(edges.begin(),edges.end());
int size = 0;
for(int i=0;size<n-1;i++){
int u = edges[i].head;
int v = edges[i].tail;
if(forest.find_set(u) != forest.find_set(v)){
tree.push_back(edges[i]);
forest.join(u,v);
size++;
cout<< "("<<edges[i].head <<","<<edges[i].tail<<"):"<<edges[i].cost<<endl;
}
}
return tree;
}
struct Node{
int index;
int pred; //predecessor (Prim)
float d; //distance from spanning tree (Prim)
vector<Edge*> adj; //incident edges
Node(int i, int p, float dd):index(i),pred(p),d(dd){}
bool operator<(const Node& n)const{return d>n.d;}//smaller = higher priority
};
struct Compare_node_pointers {
bool operator() ( const Node* first, const Node* second ) const {
if(first->d > second->d)
return true;
else
return false;
}
};
struct Node_priority_queue{ //implements a priority queue for Node* where I can update the heap ('manually')
vector<Node*> v;
Node_priority_queue(){}
Node_priority_queue(vector<Node*> ve):v(ve){
make_heap (v.begin(),v.end(),Compare_node_pointers());
}
Node* pop(){
Node* nd= v.front();
pop_heap (v.begin(),v.end(),Compare_node_pointers()); //the front item is moved to the last place
v.pop_back(); //remove the item in the last place
return nd;
}
Node* top(){
return v.front();
}
void push(Node* nd){
v.push_back(nd);
push_heap (v.begin(),v.end(),Compare_node_pointers());
}
void update(){
make_heap (v.begin(),v.end(),Compare_node_pointers()); //call make_heap on the vector
}
};
vector<Edge> prim(int n, vector<vector<float> >& cost){
Node_priority_queue Q;
vector<Node*> nodes;
vector<bool> marked(n,false); //marked nodes
vector<Edge> tree;
//Build adjacency list
Node* node = new Node(0,0,0);
nodes.push_back(node);
Q.push(node);
for(int i=1; i<n;i++){
Node* node = new Node(i,-1,9999);
nodes.push_back(node);
Q.push(node);
}
vector<Edge*> edges;
for(int i=0; i<n;i++)
for(int j=i+1;j<n;j++)
if(cost[i][j]>0){
Edge* edge = new Edge(i,j,cost[i][j]);
edges.push_back(edge);
nodes[i]->adj.push_back(edge);
nodes[j]->adj.push_back(edge);
}
//Prim
for(int i=0;i<n;i++){
Node* current = Q.pop();
marked[current->index]=true;
if(i>0){
cout<<"("<<current->index<<","<<current->pred<<"):"<<current->d<<endl;
tree.push_back(Edge(current->index,current->pred,current->d));
}
for(int j=0;j<(int)current->adj.size();j++)
if(marked[current->adj[j]->tail]==false && nodes[current->adj[j]->tail]->d > current->adj[j]->cost)
{ nodes[current->adj[j]->tail]->d=current->adj[j]->cost;
nodes[current->adj[j]->tail]->pred=current->index;
Q.update();
}
else if(marked[current->adj[j]->head]==false && nodes[current->adj[j]->head]->d > current->adj[j]->cost)
{ nodes[current->adj[j]->head]->d=current->adj[j]->cost;
nodes[current->adj[j]->head]->pred=current->index;
Q.update();
}
}
return tree;
}
vector<Edge> boruvka(int n, vector<vector<float> >& cost){ //Boruvka using UnionFind structure
vector<Edge*> compon_min; //compon_min[i]: minimum-cost edge going out of component 'i'
vector<float> min_cost; //^its cost
vector<Edge> tree; //edges in mst
UnionFind forest(n);
//Build adjacency list
vector<Node*> nodes;
for(int i=0; i<n;i++){
Node* node = new Node(i,-1,0);
nodes.push_back(node);
}
vector<Edge*> edges;
for(int i=0; i<n;i++)
for(int j=i+1;j<n;j++)
if(cost[i][j]>0){
Edge* edge = new Edge(i,j,cost[i][j]);
edges.push_back(edge);
nodes[i]->adj.push_back(edge);
nodes[j]->adj.push_back(edge);
}
//Boruvka
int size=0;
while(size < n-1)
{
vector<int> roots;
min_cost=vector<float>(n,9999); //not nice, could be done better?
compon_min=vector<Edge*>(n);
for(int i=0;i<n;i++){
int this_comp = forest.find_set(i);
if(i == this_comp) //list of roots of the connected components
roots.push_back(i);
//cout<<"i: "<<i<<" this_comp:"<<this_comp<<endl;
for(int j=0;j<(int)nodes[i]->adj.size();j++){
int other= (nodes[i]->adj[j]->head != i)? nodes[i]->adj[j]->head : nodes[i]->adj[j]->tail;
// Consider edge if outgoing from current component and has smaller cost than current best
// (or equal and lexicographically smaller)
if( forest.find_set(other) != this_comp
&& (nodes[i]->adj[j]->cost < min_cost[this_comp]
|| (nodes[i]->adj[j]->cost == min_cost[this_comp]
&& ( nodes[i]->adj[j]->head < compon_min[this_comp]->head
|| (nodes[i]->adj[j]->head == compon_min[this_comp]->head && nodes[i]->adj[j]->tail < compon_min[this_comp]->tail))
)))
{
min_cost[this_comp] = nodes[i]->adj[j]->cost;
compon_min[this_comp] = nodes[i]->adj[j];
//cout<<"min for component "<<this_comp<<" is now "<<nodes[i]->adj[j]->cost<<endl;
}
}
}
for(int i=0;i<(int)roots.size();i++){
// Loop on unconnected components (one member for each one, their roots)
// At the beginning of the loop, for each i it holds: find_set(roots[i])==roots[i],
// but this does not hold after join() is called, thus connecting components.
// => Necessary to do every time "find_set" so not to add twice the same edge in case it was selected by 2 components
// and, at the same time, build the new connected components
int this_comp = forest.find_set(roots[i]);
if(forest.find_set(compon_min[this_comp]->head)!=forest.find_set(compon_min[this_comp]->tail)){
tree.push_back(*compon_min[this_comp]);
//tree.push_back(Edge(compon_min[this_comp]->head,compon_min[this_comp]->tail,min_cost[this_comp]));
cout<<"("<<compon_min[this_comp]->head<<","<<compon_min[this_comp]->tail<<"):";
cout<<min_cost[this_comp]<<endl;
forest.join(compon_min[this_comp]->head,compon_min[this_comp]->tail);
size++;
//cout<<"Size: "<<size<<endl;
}
}
}
return tree;
}
//int main()
//{
//int n = 6;
//vector< vector<float> > c(n, vector<float>(n));
////c[0][1] = c[1][0] = 1;
////Positive costs. If 0 (default value), assume nodes not connected.
//c[0][2] = c[2][0] = 3.2;
//c[0][3] = c[3][0] = 2.2;
//c[1][2] = c[2][1] = 2;
//c[1][3] = c[3][1] = 4;
//c[2][3] = c[3][2] = 3.1;
//c[5][4] = c[4][5] = 1;
//c[3][4] = c[4][3] = 3;
//c[0][5] = c[5][0] = 2;
//cout<<"========== Kruskal =========="<<endl;
//vector<Edge> tree = kruskal(n, c);
//cout<<"========== Prim =========="<<endl;
//vector<Edge> tree2 = prim(n,c);
//cout<<"========== Boruvka =========="<<endl;
//vector<Edge> tree3 = boruvka(n,c);
//return 0;
//}
#endif