Aleph-w 7.10.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
graph-traverse.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
42#ifndef GRAPH_TRAVERSE_H
43#define GRAPH_TRAVERSE_H
44
45#include <ah-graph-concepts.H>
46
47#include <tuple>
48#include <cassert>
49#include <tpl_agraph.H>
50#include <tpl_dynListStack.H>
51#include <tpl_dynListQueue.H>
52
53namespace // local constants to avoid collisions with aleph-graph.H
54{
55constexpr unsigned char GT_Unprocessed = 0;
56constexpr unsigned char GT_Processing = 1;
57constexpr unsigned char GT_Processed = 2;
58} // namespace
59
81template <AlephGraph GT, class Itor, template <typename T> class Q = DynListStack,
82 ArcFilter<GT> Show_Arc = Dft_Show_Arc<GT>>
84{
85 GT &g;
86 Show_Arc sa;
87
104 static bool seed_arc(typename GT::Arc *a, typename GT::Node *tgt) noexcept
105 {
106 if (tgt->state() != GT_Unprocessed)
107 {
108 a->set_state(GT_Processed);
109 return false;
110 }
111 a->set_state(GT_Processing);
112 tgt->set_state(GT_Processing);
113 return true;
114 }
115
116public:
118 Graph_Traverse(GT &__g, Show_Arc __sa = Show_Arc()) : g(__g), sa(__sa) {}
119
127 template <class Node_Op>
128 size_t operator () (typename GT::Node *start, Node_Op &op)
129 {
130 assert(start != nullptr);
131 g.reset_nodes();
132 g.reset_arcs();
133
134 size_t count = 1;
135 start->set_state(GT_Processed);
136 if (not op(start))
137 return count;
138
139 Q<typename GT::Arc *> q;
140 for (Itor it(start, sa); it.has_curr(); it.next_ne())
141 {
142 typename GT::Arc *a = it.get_curr();
143 typename GT::Node *tgt = g.get_connected_node(a, start);
144 if (seed_arc(a, tgt))
145 q.put(a);
146 }
147
148 const size_t n = g.vsize();
149 while (not q.is_empty() and count < n)
150 {
151 typename GT::Arc *arc = q.get();
152 assert(arc->state() == GT_Processing);
153 arc->set_state(GT_Processed);
154
155 typename GT::Node *s = g.get_src_node(arc);
156 typename GT::Node *t = g.get_tgt_node(arc);
157 if (s->state() == GT_Processed and t->state() == GT_Processed)
158 continue;
159
160 typename GT::Node *curr = s->state() == GT_Processed ? t : s;
161 assert(curr->state() == GT_Processing);
162 ++count;
163 curr->set_state(GT_Processed);
164 if (not op(curr))
165 return count;
166
167 for (Itor it(curr, sa); it.has_curr(); it.next_ne())
168 {
169 typename GT::Arc *a = it.get_curr();
170 if (a->state() != GT_Unprocessed)
171 continue;
172 typename GT::Node *tgt = g.get_connected_node(a, curr);
173 if (seed_arc(a, tgt))
174 q.put(a);
175 }
176 }
177
178 return count;
179 }
180
182 template <class Node_Op>
183 size_t operator () (typename GT::Node *start, Node_Op &&op = Node_Op())
184 {
185 return (*this)(start, op);
186 }
187
197 template <class Op>
198 size_t exec(typename GT::Node *start, Op &op)
199 {
200 assert(start != nullptr);
201 g.reset_nodes();
202 g.reset_arcs();
203
204 size_t count = 1;
205 start->set_state(GT_Processed);
206 if (not op(start, nullptr))
207 return count;
208
209 using Pair = std::tuple<typename GT::Node *, typename GT::Arc *>;
210 Q<Pair> q;
211 for (Itor it(start, sa); it.has_curr(); it.next_ne())
212 {
213 typename GT::Arc *a = it.get_curr();
214 typename GT::Node *tgt = g.get_connected_node(a, start);
215 if (seed_arc(a, tgt))
216 q.put(std::make_tuple(start, a));
217 }
218
219 const size_t n = g.vsize();
220 while (not q.is_empty() and count < n)
221 {
222 const Pair p = q.get();
223 typename GT::Arc *arc = get<1>(p);
224 assert(arc->state() == GT_Processing);
225 assert(get<0>(p)->state() == GT_Processed);
226 arc->set_state(GT_Processed);
227
228 typename GT::Node *curr = g.get_connected_node(arc, get<0>(p));
229 assert(curr->state() == GT_Processing);
230 ++count;
231 curr->set_state(GT_Processed);
232 if (not op(curr, arc))
233 return count;
234
235 for (Itor it(curr, sa); it.has_curr(); it.next_ne())
236 {
237 typename GT::Arc *a = it.get_curr();
238 if (a->state() != GT_Unprocessed)
239 continue;
240 typename GT::Node *tgt = g.get_connected_node(a, curr);
241 if (seed_arc(a, tgt))
242 q.put(std::make_tuple(curr, a));
243 }
244 }
245
246 return count;
247 }
248
250 template <class Operation>
251 size_t exec(typename GT::Node *start, Operation &&op = Operation())
252 {
253 return exec(start, op);
254 }
255
263 template <class Node_Op, class Arc_Op>
264 std::tuple<size_t, size_t> operator () (typename GT::Node *start, Node_Op &node_op, Arc_Op &arc_op)
265 {
266 assert(start != nullptr);
267 g.reset_nodes();
268 g.reset_arcs();
269 Q<typename GT::Arc *> q;
270
271 size_t node_count = 1;
272 size_t arc_count = 0;
273
274 start->set_state(GT_Processed);
275 if (not node_op(start))
276 return std::make_tuple(1, 0);
277
278 for (Itor it(start, sa); it.has_curr(); it.next_ne())
279 {
280 typename GT::Arc *a = it.get_curr();
281 typename GT::Node *tgt = g.get_connected_node(a, start);
282 // Deliberately NOT the shared seed_arc() helper: this overload
283 // reports "back edges" too (arcs into an already-Processing node
284 // discovered via another arc), which is why it tests
285 // `!= GT_Processed` instead of `== GT_Unprocessed` — a `Processing`
286 // tgt still enqueues `a` and calls arc_op(a) here, unlike the
287 // Unprocessed-only seed_arc() used by the other two overloads. See
288 // DualOpVisitsNodesAndArcs. This still guards the self-loop case
289 // (tgt == start is already Processed by this point).
290 if (tgt->state() != GT_Processed)
291 {
292 a->set_state(GT_Processing);
293 tgt->set_state(GT_Processing);
294 q.put(a);
295 ++arc_count;
296 if (not arc_op(a))
297 return std::make_tuple(node_count, arc_count);
298 }
299 else
300 a->set_state(GT_Processed);
301 }
302
303 while (not q.is_empty())
304 {
305 typename GT::Arc *arc = q.get();
306 assert(arc->state() == GT_Processing);
307 arc->set_state(GT_Processed);
308
309 typename GT::Node *s = g.get_src_node(arc);
310 typename GT::Node *t = g.get_tgt_node(arc);
311 if (s->state() == GT_Processed and t->state() == GT_Processed)
312 continue;
313
314 typename GT::Node *curr = s->state() == GT_Processed ? t : s;
315 assert(curr->state() == GT_Processing);
316 ++node_count;
317 curr->set_state(GT_Processed);
318 if (not node_op(curr))
319 return std::make_tuple(node_count, arc_count);
320
321 for (Itor it(curr, sa); it.has_curr(); it.next_ne())
322 {
323 typename GT::Arc *a = it.get_curr();
324 if (a->state() != GT_Unprocessed)
325 continue;
326 typename GT::Node *tgt = g.get_connected_node(a, curr);
327 if (tgt->state() != GT_Processed)
328 {
329 q.put(a);
330 tgt->set_state(GT_Processing);
331 a->set_state(GT_Processing);
332 ++arc_count;
333 if (not arc_op(a))
334 return std::make_tuple(node_count, arc_count);
335 }
336 else
337 a->set_state(GT_Processed);
338 }
339 }
340
341 return std::make_tuple(node_count, arc_count);
342 }
343
345 template <class Node_Op, class Arc_Op>
346 std::tuple<size_t, size_t> operator () (typename GT::Node *start, Node_Op &&node_op = Node_Op(),
347 Arc_Op &&arc_op = Arc_Op())
348 {
349 return (*this)(start, node_op, arc_op);
350 }
351};
352
353template <class GT, class Itor, class Show_Arc = Dft_Show_Arc<GT>>
355
356template <class GT, class Itor, class Show_Arc = Dft_Show_Arc<GT>>
358
359#endif // GRAPH_TRAVERSE_H
C++20 concepts for the protocol shared by graph algorithms.
Dynamic stack of elements of generic type T based on a singly linked list.
void set_state(unsigned int s) noexcept
Set the state of arc to value s
Definition graph-dry.H:848
unsigned int state() const noexcept
Return the state of arc.
Definition graph-dry.H:845
unsigned int state() const noexcept
Return the state's value.
Definition graph-dry.H:759
void set_state(unsigned int s) noexcept
Set the state to value s
Definition graph-dry.H:762
void reset_arcs() const
Reset all the arcs of graph (the control bits, the state, the counter and the cookie)
Definition graph-dry.H:1213
Node * get_src_node(Arc *arc) const noexcept
Return the source node of arc (only for directed graphs)
Definition graph-dry.H:1017
Node * get_connected_node(Arc *arc, Node *node) const noexcept
Return the adjacent node to node through arc.
Definition graph-dry.H:1058
constexpr size_t vsize() const noexcept
See get_num_nodes().
Definition graph-dry.H:984
void reset_nodes() const
Reset all the nodes of graph (the control bits, the state, the counter and the cookie)
Definition graph-dry.H:1206
Node * get_tgt_node(Arc *arc) const noexcept
Return the target node of arc (only for directed graphs)
Definition graph-dry.H:1023
Traverse a graph depth-first or breadth-first and execute a visit function.
static bool seed_arc(typename GT::Arc *a, typename GT::Node *tgt) noexcept
Prepare arc a (leading to tgt) for frontier expansion out of start/curr, and report whether the calle...
size_t operator()(typename GT::Node *start, Node_Op &op)
Traverse the graph starting from start and execute op on each node.
Graph_Traverse(GT &__g, Show_Arc __sa=Show_Arc())
Construct a traverser with a graph and arc filter.
size_t exec(typename GT::Node *start, Operation &&op=Operation())
This is an overloaded member function, provided for convenience. It differs from the above function o...
size_t exec(typename GT::Node *start, Op &op)
Execute operation op(curr, arc) where curr is the visited node and arc is the incoming arc.
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:1024
Array-based graph implementation.
Dynamic queue implementation based on linked lists.
Dynamic stack implementation based on linked lists.