-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKruskal.H
More file actions
263 lines (214 loc) · 8.91 KB
/
Kruskal.H
File metadata and controls
263 lines (214 loc) · 8.91 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
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
/*
Aleph_w
Data structures & Algorithms
version 2.0.0b
https://github.com/lrleon/Aleph-w
This file is part of Aleph-w library
Copyright (c) 2002-2026 Leandro Rabindranath Leon
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
/** @file Kruskal.H
* @brief Kruskal's minimum spanning tree algorithm.
*
* Implements Kruskal's algorithm for finding minimum spanning trees
* using union-find data structure. Sorts edges by weight and greedily
* adds edges that don't create cycles. O(E log E) complexity.
*
* @ingroup Graphs
* @author Leandro Rabindranath León
*/
# ifndef KRUSKAL_H
# define KRUSKAL_H
# include <ahFunction.H>
# include <tpl_agraph.H>
# include <tpl_graph_utils.H>
# include <tpl_test_acyclique.H>
# include <tpl_union.H>
# include <ah-errors.H>
namespace Aleph
{
/** @brief Computes the minimum spanning tree of a graph using
Kruskal's algorithm.
Kruskal's algorithm is recommended for sparse graphs. It works
by sorting all arcs by weight and greedily adding arcs that
don't form a cycle.
The procedure is parameterized with the following specifications:
-# GT: The graph type based on List_Graph.
-# Distance<GT>: The arc weight accessor class that must export
the following attributes:
-# typedef Distance<GT>::Distance_Type: The data type that
represents an arc weight.
-# Distance<GT>::Distance_Type operator()(typename GT::Arc *a):
Returns the weight value of arc a.
-# Distance<GT>::Max_Distance: Static constant representing the
maximum distance value that an algorithm would consider as
infinity.
-# typename Distance<GT>::Zero_Distance: Static constant
corresponding to the neutral element of addition. Traditionally,
in the vast majority of cases, this is zero.
.
-# SA: Arc filter for arc iterators.
Time complexity: O(E log E) where E is the number of arcs.
Space complexity: O(V) for the Union-Find structure.
@see Prim_Min_Spanning_Tree
@ingroup Graphs
*/
template <class GT,
class Distance = Dft_Dist<GT>,
class SA = Dft_Show_Arc<GT>>
class Kruskal_Min_Spanning_Tree
{
Distance dist_val;
Distance & dist;
SA sa;
bool painted;
/// @brief Helper struct for initializing node counters during DFS.
struct Init_Node
{
long count;
Init_Node() noexcept : count(0) { /* empty */ }
void operator ()(const GT &, typename GT::Node *p) noexcept
{
NODE_COUNTER(p) = count++;
NODE_BITS(p).set_bit(Aleph::Spanning_Tree, false);
}
};
static bool arc_is_in_tree(Fixed_Relation & tree, long i, long j) noexcept
{
return tree.are_connected(i, j);
}
public:
/** @brief Constructor.
@param __dist Distance accessor for arc weights.
@param __sa Arc filter for arc iterators.
*/
Kruskal_Min_Spanning_Tree(Distance __dist = Distance(), SA __sa = SA())
: dist_val(__dist), dist(dist_val), sa(__sa), painted(false)
{
/* empty */
}
/// @brief Constructor with reference to external distance accessor.
Kruskal_Min_Spanning_Tree(Distance & __dist, SA __sa = SA())
: dist_val(), dist(__dist), sa(__sa), painted(false)
{
/* empty */
}
/// @brief Returns true if the spanning tree has been painted on the graph.
[[nodiscard]] bool is_painted() const noexcept { return painted; }
/// @brief Filter for arcs painted by Kruskal's algorithm.
template <class G, class GT_SA>
struct Paint_Filt
{
GT_SA & sa;
Paint_Filt(GT_SA & __sa) : sa(__sa) { /* empty */ }
bool operator ()(typename G::Arc *a) const noexcept
{
if (not sa(a))
return false;
return IS_ARC_VISITED(a, Aleph::Spanning_Tree);
}
};
/** @brief Paints the minimum spanning tree arcs on the graph.
After calling this method, the arcs belonging to the MST are marked
with the Spanning_Tree bit. Use is_painted() to check if the tree
has been painted.
@note The const_cast on g is safe because sort_arcs only changes
the physical ordering of arcs, not the logical graph structure.
@param[in] g The graph for which to compute the minimum spanning tree.
@throw domain_error If g is a digraph.
*/
void paint_min_spanning_tree(const GT & g)
{
ah_domain_error_if(g.is_digraph()) << "g is a digraph";
g.reset_bit_arcs(Aleph::Spanning_Tree); // clear arc marking bits
Operate_On_Nodes<GT, Init_Node>()(g, Init_Node());
typedef Distance_Compare<GT, Distance> DCMP;
DCMP comp(dist);
// Safe const_cast: sort_arcs only changes physical arc order, not structure
const_cast<GT &>(g).template sort_arcs<DCMP>(comp);
const size_t V = g.get_num_nodes();
Fixed_Relation tree(V);
// Traverse sorted arcs of g until all nodes are in one component
for (Arc_Iterator<GT, SA> it(g, sa); tree.get_num_blocks() > 1 and
it.has_curr(); it.next_ne())
{ // next smallest arc
auto arc = it.get_current_arc_ne();
const long i = NODE_COUNTER(g.get_src_node(arc));
const long j = NODE_COUNTER(g.get_tgt_node(arc));
if (arc_is_in_tree(tree, i, j))
continue;
tree.join(i, j);
ARC_BITS(arc).set_bit(Aleph::Spanning_Tree, true);
}
painted = true;
}
/** @brief Paints the MST on g and copies it to a separate tree graph.
First paints the spanning tree arcs on g, then creates a copy of
the MST in the tree parameter.
@param[in] g The graph for which to compute the minimum spanning tree.
@param[out] tree The graph where the resulting minimum spanning tree
will be stored. This graph is cleared before the algorithm starts.
@throw bad_alloc If there is not enough memory to construct the tree.
@throw domain_error If g is a digraph.
*/
void paint_min_spanning_tree(const GT & g, GT & tree)
{
paint_min_spanning_tree(g);
clear_graph(tree); // clear destination graph
for (typename GT::Node_Iterator it(g); it.has_curr(); it.next_ne())
{
auto gp = it.get_curr();
auto tp = tree.insert_node(gp->get_info());
GT::map_nodes(gp, tp);
}
typedef Paint_Filt<GT, SA> F;
for (Arc_Iterator<GT, F> it(g, F(sa)); it.has_curr(); it.next_ne())
{
auto ga = it.get_current_arc_ne();
auto tsrc = mapped_node<GT>(g.get_src_node(ga));
auto ttgt = mapped_node<GT>(g.get_tgt_node(ga));
auto ta = tree.insert_arc(tsrc, ttgt, ga->get_info());
GT::map_arcs(ga, ta);
}
}
/** @brief Computes the minimum spanning tree using Kruskal's algorithm.
@param[in] g The graph for which to compute the minimum spanning tree.
@param[out] tree The graph where the resulting minimum spanning tree
will be stored. This graph is cleared before the algorithm starts.
@throw bad_alloc If there is not enough memory to construct the tree.
In this case, the value of tree is indeterminate and not clean.
@throw domain_error If g is a digraph.
*/
void operator ()(const GT & g, GT & tree)
{
paint_min_spanning_tree(g, tree);
}
/** @brief Paints the minimum spanning tree on the graph using Kruskal's
algorithm.
After the algorithm completes, the arcs of g that belong to the
spanning tree are marked with the Spanning_Tree bit.
@param[in] g The graph for which to compute the minimum spanning tree.
@throw bad_alloc If there is not enough memory.
@throw domain_error If g is a digraph.
*/
void operator ()(const GT & g)
{
paint_min_spanning_tree(g);
}
};
} // end namespace Aleph
# endif // KRUSKAL_H