Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
graph-traverse-generators.H
Go to the documentation of this file.
1/*
2 Aleph_w
3
4 Data structures & Algorithms
5 https://github.com/lrleon/Aleph-w
6
7 This file is part of Aleph-w library
8
9 Copyright (c) 2002-2026 Leandro Rabindranath Leon
10
11 Permission is hereby granted, free of charge, to any peen rson obtaining a copy
12 of this software and associated documentation files (the "Software"), to deal
13 in the Software without restriction, including without limitation the rights
14 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
15 copies of the Software, and to permit persons to whom the Software is
16 furnished to do so, subject to the following conditions:
17
18 The above copyright notice and this permission notice shall be included in all
19 copies or substantial portions of the Software.
20
21 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
24 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
26 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
27 SOFTWARE.
28*/
29
70#ifndef GRAPH_TRAVERSE_GENERATORS_H
71#define GRAPH_TRAVERSE_GENERATORS_H
72
73# include <ah-graph-concepts.H>
74
75#include <cassert>
76
77#include <ah-errors.H>
78#include <ah-generator.H>
79#include <graph-traverse.H>
80
81namespace Aleph
82{
83
86{
92enum class Traverse_State : unsigned char
93{
94 Unprocessed = 0,
95 Processing = 1,
96 Processed = 2
97};
98} // namespace graph_traverse_generator_detail
100
130template <AlephGraph GT, class Itor,
131 template <typename T> class Q = DynListStack,
134{
135 using State = graph_traverse_generator_detail::Traverse_State;
136
139
140 static unsigned char raw(State s) noexcept { return static_cast<unsigned char>(s); }
141
149 static bool seed_arc(typename GT::Arc *a, typename GT::Node *tgt) noexcept
150 {
151 if (tgt->state() != raw(State::Unprocessed))
152 {
153 a->set_state(raw(State::Processed));
154 return false;
155 }
156 a->set_state(raw(State::Processing));
157 tgt->set_state(raw(State::Processing));
158 return true;
159 }
160
161public:
164
193 {
194 ah_invalid_argument_if(start == nullptr)
195 << "Graph_Traverse_Generator::traverse(): null start node";
196 g_.reset_nodes();
197 g_.reset_arcs();
198
199 size_t count = 1;
200 start->set_state(raw(State::Processed));
201 co_yield start;
202
204 for (Itor it(start, sa_); it.has_curr(); it.next_ne())
205 {
206 typename GT::Arc *a = it.get_curr();
207 typename GT::Node *tgt = g_.get_connected_node(a, start);
208 if (seed_arc(a, tgt))
209 q.put(a);
210 }
211
212 const size_t n = g_.vsize();
213 while (not q.is_empty() and count < n)
214 {
215 typename GT::Arc *arc = q.get();
216 assert(arc->state() == raw(State::Processing));
217 arc->set_state(raw(State::Processed));
218
219 typename GT::Node *s = g_.get_src_node(arc);
220 typename GT::Node *t = g_.get_tgt_node(arc);
221 if (s->state() == raw(State::Processed) and t->state() == raw(State::Processed))
222 continue;
223
224 typename GT::Node *curr = s->state() == raw(State::Processed) ? t : s;
225 assert(curr->state() == raw(State::Processing));
226 ++count;
227 curr->set_state(raw(State::Processed));
228 co_yield curr;
229
230 for (Itor it(curr, sa_); it.has_curr(); it.next_ne())
231 {
232 typename GT::Arc *a = it.get_curr();
233 if (a->state() != raw(State::Unprocessed))
234 continue;
235 typename GT::Node *tgt = g_.get_connected_node(a, curr);
236 if (seed_arc(a, tgt))
237 q.put(a);
238 }
239 }
240 }
241};
242
245template <class GT, class Itor, class Show_Arc = Dft_Show_Arc<GT>>
248
251template <class GT, class Itor, class Show_Arc = Dft_Show_Arc<GT>>
254
255} // end namespace Aleph
256
257#endif // GRAPH_TRAVERSE_GENERATORS_H
Exception handling system with formatted messages for Aleph-w.
#define ah_invalid_argument_if(C)
Throws std::invalid_argument if condition holds.
Definition ah-errors.H:644
Lazy sequence type (Aleph::Generator<T>) built on C++20 coroutines.
C++20 concepts for the protocol shared by graph algorithms.
List_Graph< Graph_Node< Node_Info >, Graph_Arc< Arc_Info > > GT
Dynamic stack of elements of generic type T based on a singly linked list.
Lazy, single-pass sequence of T values produced by a coroutine.
Lazily traverse a graph depth-first or breadth-first.
Graph_Traverse_Generator(GT &__g, Show_Arc __sa=Show_Arc())
Construct a lazy traverser bound to graph __g with arc filter __sa.
static bool seed_arc(typename GT::Arc *a, typename GT::Node *tgt) noexcept
Prepare arc a (leading to tgt) for frontier expansion, and report whether the caller should enqueue i...
graph_traverse_generator_detail::Traverse_State State
Aleph::Generator< typename GT::Node * > traverse(typename GT::Node *start) &
Lazily traverse the graph from start.
static unsigned char raw(State s) noexcept
void set_state(unsigned int s) noexcept
Set the state of arc to value s
Definition graph-dry.H:631
unsigned int state() const noexcept
Return the state of arc.
Definition graph-dry.H:628
unsigned int state() const noexcept
Return the state's value.
Definition graph-dry.H:542
void set_state(unsigned int s) noexcept
Set the state to value s
Definition graph-dry.H:545
void reset_arcs() const
Reset all the arcs of graph (the control bits, the state, the counter and the cookie)
Definition graph-dry.H:975
Node * get_src_node(Arc *arc) const noexcept
Return the source node of arc (only for directed graphs)
Definition graph-dry.H:779
Node * get_connected_node(Arc *arc, Node *node) const noexcept
Return the adjacent node to node through arc.
Definition graph-dry.H:820
constexpr size_t vsize() const noexcept
Definition graph-dry.H:746
void reset_nodes() const
Reset all the nodes of graph (the control bits, the state, the counter and the cookie)
Definition graph-dry.H:968
Node * get_tgt_node(Arc *arc) const noexcept
Return the target node of arc (only for directed graphs)
Definition graph-dry.H:785
Graph traversal algorithms (DFS, BFS).
const unsigned char Processed
The node or arc has already been processed.
Definition aleph-graph.C:39
const unsigned char Processing
The node are being processed; probably it is inside a queue, stack or heap.
Definition aleph-graph.C:38
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
const unsigned char Unprocessed
The node have not bees processed.
Definition aleph-graph.C:37
Main namespace for Aleph-w library functions.
Definition ah-arena.H:89
and
Check uniqueness with explicit hash + equality functors.
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:1001