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 <ahFunction.H>
48# include <tpl_agraph.H>
49# include <tpl_graph_utils.H>
50# include <tpl_test_acyclique.H>
51# include <tpl_union.H>
52# include <ah-errors.H>
53
54namespace Aleph
55{
86 template <class GT,
87 class Distance = Dft_Dist<GT>,
88 class SA = Dft_Show_Arc<GT>>
90 {
94 bool painted;
95
97 struct Init_Node
98 {
99 long count;
100
101 Init_Node() noexcept : count(0) { /* empty */ }
102
103 void operator ()(const GT &, typename GT::Node *p) noexcept
104 {
105 NODE_COUNTER(p) = count++;
106 NODE_BITS(p).set_bit(Aleph::Spanning_Tree, false);
107 }
108 };
109
110 static bool arc_is_in_tree(Fixed_Relation & tree, long i, long j) noexcept
111 {
112 return tree.are_connected(i, j);
113 }
114
115 public:
126
130 {
131 /* empty */
132 }
133
136
138 template <class G, class GT_SA>
140 {
142
143 Paint_Filt(GT_SA & __sa) : sa(__sa) { /* empty */ }
144
145 bool operator ()(typename G::Arc *a) const noexcept
146 {
147 if (not sa(a))
148 return false;
149
151 }
152 };
153
167 {
168 ah_domain_error_if(g.is_digraph()) << "g is a digraph";
169
170 g.reset_bit_arcs(Aleph::Spanning_Tree); // clear arc marking bits
172
174 DCMP comp(dist);
175 // Safe const_cast: sort_arcs only changes physical arc order, not structure
176 const_cast<GT &>(g).template sort_arcs<DCMP>(comp);
177 const size_t V = g.get_num_nodes();
178
179 Fixed_Relation tree(V);
180
181 // Traverse sorted arcs of g until all nodes are in one component
182 for (Arc_Iterator<GT, SA> it(g, sa); tree.get_num_blocks() > 1 and
183 it.has_curr(); it.next_ne())
184 { // next smallest arc
185 auto arc = it.get_current_arc_ne();
186 const long i = NODE_COUNTER(g.get_src_node(arc));
187 const long j = NODE_COUNTER(g.get_tgt_node(arc));
188 if (arc_is_in_tree(tree, i, j))
189 continue;
190
191 tree.join(i, j);
192 ARC_BITS(arc).set_bit(Aleph::Spanning_Tree, true);
193 }
194
195 painted = true;
196 }
197
209 void paint_min_spanning_tree(const GT & g, GT & tree)
210 {
212 clear_graph(tree); // clear destination graph
213
214 for (typename GT::Node_Iterator it(g); it.has_curr(); it.next_ne())
215 {
216 auto gp = it.get_curr();
217 auto tp = tree.insert_node(gp->get_info());
218 GT::map_nodes(gp, tp);
219 }
220
221 typedef Paint_Filt<GT, SA> F;
222 for (Arc_Iterator<GT, F> it(g, F(sa)); it.has_curr(); it.next_ne())
223 {
224 auto ga = it.get_current_arc_ne();
227 auto ta = tree.insert_arc(tsrc, ttgt, ga->get_info());
229 }
230 }
231
241 void operator ()(const GT & g, GT & tree)
242 {
244 }
245
256 void operator ()(const GT & g)
257 {
259 }
260 };
261} // end namespace Aleph
262
263# 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:522
Standard functor implementations and comparison objects.
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:90
bool is_painted() const noexcept
Returns true if the spanning tree has been painted on the graph.
Definition Kruskal.H:135
void paint_min_spanning_tree(const GT &g)
Paints the minimum spanning tree arcs on the graph.
Definition Kruskal.H:166
Kruskal_Min_Spanning_Tree(Distance __dist=Distance(), SA __sa=SA())
Constructor.
Definition Kruskal.H:121
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:209
static bool arc_is_in_tree(Fixed_Relation &tree, long i, long j) noexcept
Definition Kruskal.H:110
void operator()(const GT &g, GT &tree)
Computes the minimum spanning tree using Kruskal's algorithm.
Definition Kruskal.H:241
Kruskal_Min_Spanning_Tree(Distance &__dist, SA __sa=SA())
Constructor with reference to external distance accessor.
Definition Kruskal.H:128
virtual Node * insert_node(Node *node) noexcept
Insertion of a node already allocated.
Definition tpl_graph.H:524
Arc * insert_arc(Node *src_node, Node *tgt_node, void *a)
Definition tpl_graph.H:604
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:731
constexpr size_t get_num_nodes() const noexcept
Return the total of nodes of graph.
Definition graph-dry.H:695
bool is_digraph() const noexcept
Return true if the graph this is directed.
Definition graph-dry.H:657
static void map_arcs(A1 *p, A2 *q) noexcept
Map the arcs through their cookies.
Definition graph-dry.H:1026
void reset_bit_arcs(int bit) const noexcept
Reset bit to zero for all the arcs of graph.
Definition graph-dry.H:1046
Node * get_tgt_node(Arc *arc) const noexcept
Return the target node of arc (only for directed graphs)
Definition graph-dry.H:737
static void map_nodes(N1 *p, N2 *q) noexcept
Map the nodes through their cookies.
Definition graph-dry.H:995
List_Graph< Graph_Node< int >, Graph_Arc< int > > GT
#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:3549
#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
DynList< T > maps(const C &c, Op op)
Classic map operation.
Filtered iterator on all the arcs of a graph.
Definition tpl_graph.H:1164
Default filter for filtered iterators on arcs.
Definition tpl_graph.H:1000
Comparison functor for arc weights/distances.
Helper struct for initializing node counters during DFS.
Definition Kruskal.H:98
void operator()(const GT &, typename GT::Node *p) noexcept
Definition Kruskal.H:103
Filter for arcs painted by Kruskal's algorithm.
Definition Kruskal.H:140
bool operator()(typename G::Arc *a) const noexcept
Definition Kruskal.H:145
Distance accessor.
Array-based graph implementation.
Utility algorithms and operations for graphs.
DAG (acyclic) graph testing.
Union-Find (Disjoint Set Union) data structure.