42#ifndef GRAPH_TRAVERSE_H
43#define GRAPH_TRAVERSE_H
55constexpr unsigned char GT_Unprocessed = 0;
56constexpr unsigned char GT_Processing = 1;
57constexpr unsigned char GT_Processed = 2;
81template <AlephGraph GT,
class Itor,
template <
typename T>
class Q =
DynListStack,
106 if (tgt->state() != GT_Unprocessed)
108 a->set_state(GT_Processed);
111 a->set_state(GT_Processing);
112 tgt->set_state(GT_Processing);
127 template <
class Node_Op>
130 assert(start !=
nullptr);
139 Q<typename GT::Arc *> q;
140 for (Itor it(start,
sa); it.has_curr(); it.next_ne())
142 typename GT::Arc *a = it.get_curr();
148 const size_t n =
g.
vsize();
149 while (not q.is_empty()
and count < n)
151 typename GT::Arc *arc = q.get();
152 assert(arc->
state() == GT_Processing);
157 if (s->
state() == GT_Processed
and t->
state() == GT_Processed)
160 typename GT::Node *curr = s->
state() == GT_Processed ? t : s;
161 assert(curr->
state() == GT_Processing);
167 for (Itor it(curr,
sa); it.has_curr(); it.next_ne())
169 typename GT::Arc *a = it.get_curr();
170 if (a->
state() != GT_Unprocessed)
182 template <
class Node_Op>
185 return (*
this)(start, op);
200 assert(start !=
nullptr);
206 if (not op(start,
nullptr))
209 using Pair = std::tuple<typename GT::Node *, typename GT::Arc *>;
211 for (Itor it(start,
sa); it.has_curr(); it.next_ne())
213 typename GT::Arc *a = it.get_curr();
216 q.put(std::make_tuple(start, a));
219 const size_t n =
g.
vsize();
220 while (not q.is_empty()
and count < n)
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);
229 assert(curr->
state() == GT_Processing);
232 if (not op(curr, arc))
235 for (Itor it(curr,
sa); it.has_curr(); it.next_ne())
237 typename GT::Arc *a = it.get_curr();
238 if (a->
state() != GT_Unprocessed)
242 q.put(std::make_tuple(curr, a));
250 template <
class Operation>
253 return exec(start, op);
263 template <
class Node_Op,
class Arc_Op>
266 assert(start !=
nullptr);
269 Q<typename GT::Arc *> q;
271 size_t node_count = 1;
272 size_t arc_count = 0;
275 if (not node_op(start))
276 return std::make_tuple(1, 0);
278 for (Itor it(start,
sa); it.has_curr(); it.next_ne())
280 typename GT::Arc *a = it.get_curr();
290 if (tgt->
state() != GT_Processed)
297 return std::make_tuple(node_count, arc_count);
303 while (not q.is_empty())
305 typename GT::Arc *arc = q.get();
306 assert(arc->
state() == GT_Processing);
311 if (s->
state() == GT_Processed
and t->
state() == GT_Processed)
314 typename GT::Node *curr = s->
state() == GT_Processed ? t : s;
315 assert(curr->
state() == GT_Processing);
318 if (not node_op(curr))
319 return std::make_tuple(node_count, arc_count);
321 for (Itor it(curr,
sa); it.has_curr(); it.next_ne())
323 typename GT::Arc *a = it.get_curr();
324 if (a->
state() != GT_Unprocessed)
327 if (tgt->
state() != GT_Processed)
334 return std::make_tuple(node_count, arc_count);
341 return std::make_tuple(node_count, arc_count);
345 template <
class Node_Op,
class Arc_Op>
347 Arc_Op &&arc_op = Arc_Op())
349 return (*
this)(start, node_op, arc_op);
353template <
class GT,
class Itor,
class Show_Arc = Dft_Show_Arc<GT>>
356template <
class GT,
class Itor,
class Show_Arc = Dft_Show_Arc<GT>>
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
unsigned int state() const noexcept
Return the state of arc.
unsigned int state() const noexcept
Return the state's value.
void set_state(unsigned int s) noexcept
Set the state to value s
void reset_arcs() const
Reset all the arcs of graph (the control bits, the state, the counter and the cookie)
Node * get_src_node(Arc *arc) const noexcept
Return the source node of arc (only for directed graphs)
Node * get_connected_node(Arc *arc, Node *node) const noexcept
Return the adjacent node to node through arc.
constexpr size_t vsize() const noexcept
See get_num_nodes().
void reset_nodes() const
Reset all the nodes of graph (the control bits, the state, the counter and the cookie)
Node * get_tgt_node(Arc *arc) const noexcept
Return the target node of arc (only for directed graphs)
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.
Default filter for filtered iterators on arcs.
Array-based graph implementation.
Dynamic queue implementation based on linked lists.
Dynamic stack implementation based on linked lists.