Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
Kruskal.H
Go to the documentation of this file.
1
2/*
3 Aleph_w
4
5 Data structures & Algorithms
6 version 2.0.0b
7 https://github.com/lrleon/Aleph-w
8
9 This file is part of Aleph-w library
10
11 Copyright (c) 2002-2026 Leandro Rabindranath Leon
12
13 Permission is hereby granted, free of charge, to any person obtaining a copy
14 of this software and associated documentation files (the "Software"), to deal
15 in the Software without restriction, including without limitation the rights
16 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
17 copies of the Software, and to permit persons to whom the Software is
18 furnished to do so, subject to the following conditions:
19
20 The above copyright notice and this permission notice shall be included in all
21 copies or substantial portions of the Software.
22
23 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
24 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
26 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
28 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
29 SOFTWARE.
30*/
31
32
44# ifndef KRUSKAL_H
45# define KRUSKAL_H
46
47# include <ah-graph-concepts.H>
48
49# include <ahFunction.H>
50# include <tpl_agraph.H>
51# include <tpl_graph_utils.H>
52# include <tpl_test_acyclique.H>
53# include <tpl_union.H>
54# include <ah-errors.H>
55
56namespace Aleph
57{
88 template <AlephGraph GT,
89 class Distance = Dft_Dist<GT>,
92 {
95 SA sa;
96 bool painted;
97
99 struct Init_Node
100 {
101 long count;
102
103 Init_Node() noexcept : count(0) { /* empty */ }
104
105 void operator ()(const GT &, typename GT::Node *p) noexcept
106 {
107 NODE_COUNTER(p) = count++;
108 NODE_BITS(p).set_bit(Aleph::Spanning_Tree, false);
109 }
110 };
111
112 static bool arc_is_in_tree(Fixed_Relation & tree, long i, long j) noexcept
113 {
114 return tree.are_connected(i, j);
115 }
116
117 public:
125 {
126 /* empty */
127 }
128
132 {
133 /* empty */
134 }
135
138
140 template <class G, class GT_SA>
142 {
144
145 Paint_Filt(GT_SA & __sa) : sa(__sa) { /* empty */ }
146
147 bool operator ()(typename G::Arc *a) const noexcept
148 {
149 if (not sa(a))
150 return false;
151
153 }
154 };
155
169 {
170 ah_domain_error_if(g.is_digraph()) << "g is a digraph";
171
172 g.reset_bit_arcs(Aleph::Spanning_Tree); // clear arc marking bits
174
176 DCMP comp(dist);
177 // Safe const_cast: sort_arcs only changes physical arc order, not structure
178 const_cast<GT &>(g).template sort_arcs<DCMP>(comp);
179 const size_t V = g.get_num_nodes();
180
181 Fixed_Relation tree(V);
182
183 // Traverse sorted arcs of g until all nodes are in one component
184 for (Arc_Iterator<GT, SA> it(g, sa); tree.get_num_blocks() > 1 and
185 it.has_curr(); it.next_ne())
186 { // next smallest arc
187 auto arc = it.get_current_arc_ne();
188 const long i = NODE_COUNTER(g.get_src_node(arc));
189 const long j = NODE_COUNTER(g.get_tgt_node(arc));
190 if (arc_is_in_tree(tree, i, j))
191 continue;
192
193 tree.join(i, j);
194 ARC_BITS(arc).set_bit(Aleph::Spanning_Tree, true);
195 }
196
197 painted = true;
198 }
199
211 void paint_min_spanning_tree(const GT & g, GT & tree)
212 {
214 clear_graph(tree); // clear destination graph
215
216 for (typename GT::Node_Iterator it(g); it.has_curr(); it.next_ne())
217 {
218 auto gp = it.get_curr();
219 auto tp = tree.insert_node(gp->get_info());
220 GT::map_nodes(gp, tp);
221 }
222
223 typedef Paint_Filt<GT, SA> F;
224 for (Arc_Iterator<GT, F> it(g, F(sa)); it.has_curr(); it.next_ne())
225 {
226 auto ga = it.get_current_arc_ne();
229 auto ta = tree.insert_arc(tsrc, ttgt, ga->get_info());
231 }
232 }
233
243 void operator ()(const GT & g, GT & tree)
244 {
246 }
247
258 void operator ()(const GT & g)
259 {
261 }
262 };
263} // end namespace Aleph
264
265# endif // KRUSKAL_H
Exception handling system with formatted messages for Aleph-w.
#define ah_domain_error_if(C)
Throws std::domain_error if condition holds.
Definition ah-errors.H:527
C++20 concepts for the protocol shared by graph algorithms.
Standard functor implementations and comparison objects.
List_Graph< Graph_Node< Node_Info >, Graph_Arc< Arc_Info > > GT
Default distance accessor for arc weights.
void next_ne() noexcept
Advances the iterator to the next filtered element (noexcept version).
Computes the minimum spanning tree of a graph using Kruskal's algorithm.
Definition Kruskal.H:92
bool is_painted() const noexcept
Returns true if the spanning tree has been painted on the graph.
Definition Kruskal.H:137
void paint_min_spanning_tree(const GT &g)
Paints the minimum spanning tree arcs on the graph.
Definition Kruskal.H:168
Kruskal_Min_Spanning_Tree(Distance __dist=Distance(), SA __sa=SA())
Constructor.
Definition Kruskal.H:123
void paint_min_spanning_tree(const GT &g, GT &tree)
Paints the MST on g and copies it to a separate tree graph.
Definition Kruskal.H:211
static bool arc_is_in_tree(Fixed_Relation &tree, long i, long j) noexcept
Definition Kruskal.H:112
void operator()(const GT &g, GT &tree)
Computes the minimum spanning tree using Kruskal's algorithm.
Definition Kruskal.H:243
Kruskal_Min_Spanning_Tree(Distance &__dist, SA __sa=SA())
Constructor with reference to external distance accessor.
Definition Kruskal.H:130
virtual Node * insert_node(Node *node) noexcept
Insertion of a node already allocated.
Definition tpl_graph.H:525
_Graph_Arc Arc
The node class type.
Definition tpl_graph.H:434
Arc * insert_arc(Node *src_node, Node *tgt_node, void *a)
Definition tpl_graph.H:605
Binary relation between a set of integers.
Definition tpl_union.H:82
constexpr size_t get_num_blocks() const noexcept
Return the number of connected blocks, which is the number of equivalence classes.
Definition tpl_union.H:173
void join(size_t i, size_t j)
Insert the pair '(i,j)' in the relation.
Definition tpl_union.H:198
Node * get_src_node(Arc *arc) const noexcept
Return the source node of arc (only for directed graphs)
Definition graph-dry.H:779
constexpr size_t get_num_nodes() const noexcept
Return the total of nodes of graph.
Definition graph-dry.H:737
bool is_digraph() const noexcept
Return true if the graph this is directed.
Definition graph-dry.H:699
static void map_arcs(A1 *p, A2 *q) noexcept
Map the arcs through their cookies.
Definition graph-dry.H:1074
void reset_bit_arcs(int bit) const noexcept
Reset bit to zero for all the arcs of graph.
Definition graph-dry.H:1094
Node * get_tgt_node(Arc *arc) const noexcept
Return the target node of arc (only for directed graphs)
Definition graph-dry.H:785
static void map_nodes(N1 *p, N2 *q) noexcept
Map the nodes through their cookies.
Definition graph-dry.H:1043
#define NODE_COUNTER(p)
Get the counter of a node.
#define ARC_BITS(p)
Return the control bits of arc p.
void clear_graph(GT &g) noexcept
Clean a graph: all its nodes and arcs are removed and freed.
Definition tpl_graph.H:3659
size_t blossom_maximum_cardinality_matching(const GT &g, DynDlist< typename GT::Arc * > &matching, SA sa=SA())
Alias of compute_maximum_cardinality_general_matching().
Definition Blossom.H:466
#define IS_ARC_VISITED(p, bit)
Determine whether the bit field is or not set to one.
#define NODE_BITS(p)
Get the control bits of a node.
@ Spanning_Tree
Definition aleph-graph.H:79
Main namespace for Aleph-w library functions.
Definition ah-arena.H:89
and
Check uniqueness with explicit hash + equality functors.
Filtered iterator on all the arcs of a graph.
Definition tpl_graph.H:1165
Default filter for filtered iterators on arcs.
Definition tpl_graph.H:1001
Comparison functor for arc weights/distances.
Helper struct for initializing node counters during DFS.
Definition Kruskal.H:100
void operator()(const GT &, typename GT::Node *p) noexcept
Definition Kruskal.H:105
Filter for arcs painted by Kruskal's algorithm.
Definition Kruskal.H:142
bool operator()(typename G::Arc *a) const noexcept
Definition Kruskal.H:147
Distance accessor.
size_t V
Array-based graph implementation.
Utility algorithms and operations for graphs.
DAG (acyclic) graph testing.
Union-Find (Disjoint Set Union) data structure.