Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
Prim.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
90# ifndef PRIM_H
91# define PRIM_H
92
93# include <tpl_agraph.H>
94# include <tpl_graph_utils.H>
95# include <archeap.H>
96# include <ah-errors.H>
97# include <cookie_guard.H>
98
99namespace Aleph
100{
101 using namespace Aleph;
102
103 template <class GT>
105 {
106 typename GT::Node *tree_node = nullptr; // imagen en el árbol abarcador
107 void *heap_node = nullptr; // puntero en el heap exclusivo
108
110 { /* empty */
111 }
112 };
113
114# define PRIMINFO(p) static_cast<Prim_Info<GT>*>(NODE_COOKIE(p))
115# define TREENODE(p) (PRIMINFO(p)->tree_node)
116# define HEAPNODE(p) (PRIMINFO(p)->heap_node)
117
118 template <class GT, class Distance>
120 {
121 // Returns reference to the heap_node pointer stored in Prim_Info
122 // The actual type is determined by ArcHeap, but stored as void*
123 void *& operator ()(typename GT::Node *p)
124 {
125 return PRIMINFO(p)->heap_node;
126 }
127 };
128
129 template <class GT, class Distance>
131 {
132 // Returns reference to the cookie pointer used as heap node storage
133 void *& operator ()(typename GT::Node *p)
134 {
135 return p->cookie;
136 }
137 };
138
139 template <class GT>
141 {
143
145 { /* empty */
146 }
147
148 void operator ()(const GT & g, typename GT::Node *p)
149 {
151 NODE_COOKIE(p) = new Prim_Info<GT>;
153 }
154 };
155
156 template <class GT>
158 {
159 void operator ()(const GT &, typename GT::Node *p)
160 {
161 const Prim_Info<GT> *aux = PRIMINFO(p);
162 GT::map_nodes(p, TREENODE(p));
163 delete aux;
164 }
165 };
166
167
210 template <class GT,
211 class Distance = Dft_Dist<GT>,
212 class SA = Dft_Show_Arc<GT>>
214 {
216
218
220
222
225
226 public:
233 : dist(__dist), sa(__sa)
234 {
235 // empty
236 }
237
238 private:
239 void paint_min_spanning_tree(const GT & g, typename GT::Node *first)
240 {
241 ah_domain_error_if(g.is_digraph()) << "g is a digraph";
242
243 g.reset_nodes();
244 g.reset_arcs();
245
246 NODE_BITS(first).set_bit(Aleph::Spanning_Tree, true); // visitado
247
249 for (Node_Arc_Iterator<GT, SA> it(first, sa); it.has_curr(); it.next_ne())
250 {
251 typename GT::Arc *arc = it.get_current_arc_ne();
252 heap.put_arc(arc, it.get_tgt_node_ne());
253 }
254
255 const size_t V1 = g.get_num_nodes() - 1;
256 size_t count = 0;
257
258 while (count < V1 and not heap.is_empty())
259 { // obtenga siguiente menor arco
260 typename GT::Arc *min_arc = heap.get_min_arc();
262 continue;
263
264 ARC_BITS(min_arc).set_bit(Aleph::Spanning_Tree, true);
265
266 typename GT::Node *src = g.get_src_node(min_arc);
267 typename GT::Node *tgt = g.get_tgt_node(min_arc);
270 continue; // este arco cerraría un ciclo en el árbol
271
272 typename GT::Node *tgt_node =
273 IS_NODE_VISITED(src, Aleph::Spanning_Tree) ? tgt : src;
274
275 NODE_BITS(tgt_node).set_bit(Aleph::Spanning_Tree, true);
276
277 // insertar en heap arcos no visitados de tgt_node
278 for (Node_Arc_Iterator<GT, SA> it(tgt_node, sa); it.has_curr();
279 it.next_ne())
280 {
281 typename GT::Arc *arc = it.get_current_arc_ne();
283 continue;
284
285 typename GT::Node *tgt = it.get_tgt_node_ne();
287 continue; // nodo visitado ==> causará ciclo
288
289 heap.put_arc(arc, tgt);
290 }
291
292 ++count;
293 ARC_BITS(min_arc).set_bit(Aleph::Spanning_Tree, true);
294 }
295 }
296
297 void min_spanning_tree(const GT & g, typename GT::Node *first, GT & tree)
298 {
299 ah_domain_error_if(g.is_digraph()) << "g is a digraph";
300
301 clear_graph(tree);
302
305 g.reset_arcs();
306
307 // Scope guard for exception safety - ensures Uninit is always called
308 Scope_Guard cleanup_guard(g, [](const GT & graph) {
310 });
311
312 NODE_BITS(first).set_bit(Aleph::Spanning_Tree, true); // visitado
313
314 Heap heap(dist, Acc_Heap());
315 // meter en heap arcos iniciales del primer nodo
316 for (Node_Arc_Iterator<GT, SA> it(first, sa); it.has_curr(); it.next_ne())
317 {
318 typename GT::Arc *arc = it.get_current_arc_ne();
319 heap.put_arc(arc, it.get_tgt_node_ne());
320 }
321
322 const size_t V1 = g.get_num_nodes() - 1;
323
324 while (tree.get_num_arcs() < V1 and not heap.is_empty())
325 { // obtenga siguiente menor arco
326 typename GT::Arc *min_arc = heap.get_min_arc();
328 continue;
329
330 ARC_BITS(min_arc).set_bit(Aleph::Spanning_Tree, true);
331
332 typename GT::Node *src = g.get_src_node(min_arc);
333 typename GT::Node *tgt = g.get_tgt_node(min_arc);
336 continue; // este arco cerraría un ciclo en el árbol
337
338 typename GT::Node *tgt_node =
339 IS_NODE_VISITED(src, Aleph::Spanning_Tree) ? tgt : src;
340
341 NODE_BITS(tgt_node).set_bit(Aleph::Spanning_Tree, true);
342
343 // insertar en heap arcos no visitados de tgt_node
344 for (Node_Arc_Iterator<GT, SA> it(tgt_node, sa); it.has_curr();
345 it.next_ne())
346 {
347 typename GT::Arc *arc = it.get_current_arc_ne();
349 continue;
350
351 typename GT::Node *tgt = it.get_tgt_node_ne();
353 continue; // nodo visitado ==> causará ciclo
354
355 heap.put_arc(arc, tgt);
356 }
357
358 // insertar nuevo arco en tree y mapearlo
359 typename GT::Arc *tree_arc =
360 tree.insert_arc(TREENODE(src), TREENODE(tgt), min_arc->get_info());
361 GT::map_arcs(min_arc, tree_arc);
362 }
363
364 // cleanup_guard destructor will call Uninit_Prim_Info for all nodes
365 }
366
367 public:
380 void operator ()(const GT & g, GT & tree)
381 {
382 min_spanning_tree(g, g.get_first_node(), tree);
383 }
384
398 void operator ()(const GT & g, typename GT::Node *start, GT & tree)
399 {
400 min_spanning_tree(g, start, tree);
401 }
402
404 void operator ()(const GT & g)
405 {
407 }
408
409 void operator ()(const GT & g, typename GT::Node *start)
410 {
411 paint_min_spanning_tree(g, start);
412 }
413 };
414
415
416# undef HEAPNODE
417# undef TREENODE
418# undef PRIMINFO
419} // end namespace Aleph
420
421# endif // PRIM_H
#define TREENODE(p)
Definition Dijkstra.H:120
#define PRIMINFO(p)
Definition Prim.H:114
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
Arc heap for graph algorithms.
Default distance accessor for arc weights.
void next_ne() noexcept
Advances the iterator to the next filtered element (noexcept version).
bool is_empty() const noexcept
Node * get_first_node() const
Return any node in the graph.
Definition tpl_graph.H:576
virtual Node * insert_node(Node *node) noexcept
Insertion of a node already allocated.
Definition tpl_graph.H:524
_Graph_Node Node
The graph type.
Definition tpl_graph.H:432
Arc * insert_arc(Node *src_node, Node *tgt_node, void *a)
Definition tpl_graph.H:604
Functor that traverses the nodes of a graph and performs an operation.
Definition tpl_graph.H:2535
Calcula el árbol abarcador mínimo de un grafo según el algoritmo de Prim.
Definition Prim.H:214
void paint_min_spanning_tree(const GT &g, typename GT::Node *first)
Definition Prim.H:239
void min_spanning_tree(const GT &g, typename GT::Node *first, GT &tree)
Definition Prim.H:297
Prim_Min_Spanning_Tree(Distance __dist=Distance(), SA __sa=SA())
Constructor.
Definition Prim.H:232
ArcHeap< GT, Distance, Acc_Simple_Heap > Simple_Heap
Definition Prim.H:221
ArcHeap< GT, Distance, Acc_Heap > Heap
Definition Prim.H:219
void operator()(const GT &g, GT &tree)
Invoca al cálculo del árbol abarcador mínimo por el algoritmo de Prim.
Definition Prim.H:380
Prim_Heap_Info< GT, Distance > Acc_Heap
Definition Prim.H:215
Simple_Prim_Heap< GT, Distance > Acc_Simple_Heap
Definition Prim.H:217
Generic RAII scope guard for cleanup operations on graphs.
void put_arc(typename GT::Arc *arc, typename GT::Node *tgt)
Insert or update an arc associated with a target node, keeping the smallest-distance arc per node in ...
Definition archeap.H:107
GT::Arc * get_min_arc()
Extract the arc with minimum distance from the heap and clear its node-to-heap mapping.
Definition archeap.H:136
ArcInfo & get_info() noexcept
Return a modifiable reference to the arc data.
Definition graph-dry.H:595
NodeInfo & get_info() noexcept
Return a modifiable reference to the data contained in the node.
Definition graph-dry.H:494
void reset_arcs() const
Reset all the arcs of graph (the control bits, the state, the counter and the cookie)
Definition graph-dry.H:927
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(Node *node, int bit) const noexcept
Reset the bit of node (to zero)
Definition graph-dry.H:801
constexpr size_t get_num_arcs() const noexcept
Definition graph-dry.H:778
void reset_nodes() const
Reset all the nodes of graph (the control bits, the state, the counter and the cookie)
Definition graph-dry.H:920
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
RAII guards for graph node/arc cookies.
List_Graph< Graph_Node< int >, Graph_Arc< int > > GT
#define IS_NODE_VISITED(p, bit)
Determine whether the control bit is set or not to one.
#define ARC_BITS(p)
Return the control bits of arc p.
#define NODE_COOKIE(p)
Return the node cookie
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
static bool init
Definition hash-fct.C:47
DynList< T > maps(const C &c, Op op)
Classic map operation.
Itor::difference_type count(const Itor &beg, const Itor &end, const T &value)
Count elements equal to a value.
Definition ahAlgo.H:127
Default filter for filtered iterators on arcs.
Definition tpl_graph.H:1000
Arc of graph implemented with double-linked adjacency lists.
Definition tpl_graph.H:222
void operator()(const GT &g, typename GT::Node *p)
Definition Prim.H:148
Init_Prim_Info(GT &__tree)
Definition Prim.H:144
Filtered iterator of adjacent arcs of a node.
Definition tpl_graph.H:1119
void *& operator()(typename GT::Node *p)
Definition Prim.H:123
void * heap_node
Definition Prim.H:107
GT::Node * tree_node
Definition Prim.H:106
void *& operator()(typename GT::Node *p)
Definition Prim.H:133
void operator()(const GT &, typename GT::Node *p)
Definition Prim.H:159
Distance accessor.
Array-based graph implementation.
Utility algorithms and operations for graphs.