forked from ZigRazor/CXXGraph
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBoruvka_impl.hpp
More file actions
229 lines (200 loc) · 8.09 KB
/
Boruvka_impl.hpp
File metadata and controls
229 lines (200 loc) · 8.09 KB
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
/***********************************************************/
/*** ______ ____ ______ _ ***/
/*** / ___\ \/ /\ \/ / ___|_ __ __ _ _ __ | |__ ***/
/*** | | \ / \ / | _| '__/ _` | '_ \| '_ \ ***/
/*** | |___ / \ / \ |_| | | | (_| | |_) | | | | ***/
/*** \____/_/\_\/_/\_\____|_| \__,_| .__/|_| |_| ***/
/*** |_| ***/
/***********************************************************/
/*** Header-Only C++ Library for Graph ***/
/*** Representation and Algorithms ***/
/***********************************************************/
/*** Author: ZigRazor ***/
/*** E-Mail: zigrazor@gmail.com ***/
/***********************************************************/
/*** Collaboration: ----------- ***/
/***********************************************************/
/*** License: MPL v2.0 ***/
/***********************************************************/
#ifndef __CXXGRAPH_BORUVKA_IMPL_H__
#define __CXXGRAPH_BORUVKA_IMPL_H__
#pragma once
#include <limits.h>
#include <unordered_map>
#include "CXXGraph/Graph/Graph_decl.h"
#include "CXXGraph/Utility/ConstString.hpp"
namespace CXXGraph {
template <typename T>
const MstResult Graph<T>::boruvka() const {
MstResult result;
result.success = false;
result.errorMessage = "";
result.mstCost = INF_DOUBLE;
if (!isUndirectedGraph()) {
result.errorMessage = ERR_DIR_GRAPH;
return result;
}
const auto nodeSet = Graph<T>::getNodeSet();
const auto n = nodeSet.size();
// Use std map for storing n subsets.
auto subsets = make_shared<std::unordered_map<CXXGraph::id_t, Subset>>();
// Initially there are n different trees.
// Finally there will be one tree that will be MST
auto numTrees = n;
// check if all edges are weighted and store the weights
// in a map whose keys are the edge ids and values are the edge weights
const auto edgeSet = Graph<T>::getEdgeSet();
std::unordered_map<CXXGraph::id_t, double> edgeWeight;
for (const auto &edge : edgeSet) {
if (edge->isWeighted().value_or(false))
edgeWeight[edge->getId()] =
(std::dynamic_pointer_cast<const Weighted>(edge))->getWeight();
else {
// No Weighted Edge
result.errorMessage = ERR_NO_WEIGHTED_EDGE;
return result;
}
}
for (const auto &node : nodeSet) {
Subset set{node->getId(), 0};
(*subsets)[node->getId()] = set;
}
result.mstCost = 0; // we will store the cost here
// exit when only 1 tree i.e. mst
while (numTrees > 1) {
// Everytime initialize cheapest map
// It stores index of the cheapest edge of subset.
std::unordered_map<CXXGraph::id_t, CXXGraph::id_t> cheapest;
for (const auto &node : nodeSet) cheapest[node->getId()] = INT_MAX;
// Traverse through all edges and update
// cheapest of every component
for (const auto &edge : edgeSet) {
auto elem = edge->getNodePair();
auto edgeId = edge->getId();
// Find sets of two corners of current edge
auto set1 = Graph<T>::setFind(subsets, elem.first->getId());
auto set2 = Graph<T>::setFind(subsets, elem.second->getId());
// If two corners of current edge belong to
// same set, ignore current edge
if (set1 == set2) continue;
// Else check if current edge is closer to previous
// cheapest edges of set1 and set2
if (cheapest[set1] == INT_MAX ||
edgeWeight[cheapest[set1]] > edgeWeight[edgeId])
cheapest[set1] = edgeId;
if (cheapest[set2] == INT_MAX ||
edgeWeight[cheapest[set2]] > edgeWeight[edgeId])
cheapest[set2] = edgeId;
}
// iterate over all the vertices and add picked
// cheapest edges to MST
for (const auto &[nodeId, edgeId] : cheapest) {
// Check if cheapest for current set exists
if (edgeId != INT_MAX) {
auto cheapestNode = Graph<T>::getEdge(edgeId).value()->getNodePair();
auto set1 = Graph<T>::setFind(subsets, cheapestNode.first->getId());
auto set2 = Graph<T>::setFind(subsets, cheapestNode.second->getId());
if (set1 == set2) continue;
result.mstCost += edgeWeight[edgeId];
auto newEdgeMST = std::make_pair(cheapestNode.first->getUserId(),
cheapestNode.second->getUserId());
result.mst.push_back(newEdgeMST);
// take union of set1 and set2 and decrease number of trees
Graph<T>::setUnion(subsets, set1, set2);
numTrees--;
}
}
}
result.success = true;
return result;
}
template <typename T>
const MstResult Graph<T>::boruvka_deterministic() const {
MstResult result;
result.success = false;
result.errorMessage = "";
result.mstCost = INF_DOUBLE;
if (!isUndirectedGraph()) {
result.errorMessage = ERR_DIR_GRAPH;
return result;
}
const auto nodeSet = Graph<T>::getNodeSet();
const auto n = nodeSet.size();
// Use std map for storing n subsets.
auto subsets = make_shared<std::unordered_map<CXXGraph::id_t, Subset>>();
// Initially there are n different trees.
// Finally there will be one tree that will be MST
auto numTrees = n;
// check if all edges are weighted and store the weights
// in a map whose keys are the edge ids and values are the edge weights
const auto edgeSet = Graph<T>::getEdgeSet();
std::unordered_map<CXXGraph::id_t, double> edgeWeight;
for (const auto &edge : edgeSet) {
if (edge->isWeighted().value_or(false))
edgeWeight[edge->getId()] =
(std::dynamic_pointer_cast<const Weighted>(edge))->getWeight();
else {
// No Weighted Edge
result.errorMessage = ERR_NO_WEIGHTED_EDGE;
return result;
}
}
for (const auto &node : nodeSet) {
Subset set{node->getId(), 0};
(*subsets)[node->getId()] = set;
}
result.mstCost = 0; // we will store the cost here
// exit when only 1 tree i.e. mst
while (numTrees > 1) {
// Everytime initialize cheapest map
// It stores index of the cheapest edge of subset.
std::unordered_map<CXXGraph::id_t, CXXGraph::id_t> cheapest;
for (const auto &node : nodeSet) cheapest[node->getId()] = INT_MAX;
// Traverse through all edges and update
// cheapest of every component
for (const auto &edge : edgeSet) {
auto elem = edge->getNodePair();
auto edgeId = edge->getId();
// Find sets of two corners of current edge
auto set1 = Graph<T>::setFind(subsets, elem.first->getId());
auto set2 = Graph<T>::setFind(subsets, elem.second->getId());
// If two corners of current edge belong to
// same set, ignore current edge
if (set1 == set2) continue;
// Else check if current edge is closer to previous
// cheapest edges of set1 and set2
if (cheapest[set1] == INT_MAX ||
(edgeWeight[cheapest[set1]] > edgeWeight[edgeId]) ||
(edgeWeight[cheapest[set1]] == edgeWeight[edgeId] &&
cheapest[set1] > edgeId))
cheapest[set1] = edgeId;
if (cheapest[set2] == INT_MAX ||
(edgeWeight[cheapest[set2]] > edgeWeight[edgeId]) ||
(edgeWeight[cheapest[set2]] == edgeWeight[edgeId] &&
cheapest[set2] > edgeId))
cheapest[set2] = edgeId;
}
// iterate over all the vertices and add picked
// cheapest edges to MST
for (const auto &[nodeId, edgeId] : cheapest) {
// Check if cheapest for current set exists
if (edgeId != INT_MAX) {
auto cheapestNode = Graph<T>::getEdge(edgeId).value()->getNodePair();
auto set1 = Graph<T>::setFind(subsets, cheapestNode.first->getId());
auto set2 = Graph<T>::setFind(subsets, cheapestNode.second->getId());
if (set1 == set2) continue;
result.mstCost += edgeWeight[edgeId];
auto newEdgeMST = std::make_pair(cheapestNode.first->getUserId(),
cheapestNode.second->getUserId());
result.mst.push_back(newEdgeMST);
// take union of set1 and set2 and decrease number of trees
Graph<T>::setUnion(subsets, set1, set2);
numTrees--;
}
}
}
result.success = true;
return result;
}
} // namespace CXXGraph
#endif // __CXXGRAPH_BORUVKA_IMPL_H__