98# include <type_traits>
182 mutable size_t n_ = 0;
222 noexcept(
std::is_nothrow_move_constructible_v<SA>) :
sa_(
std::move(__sa))
247 assert(p !=
nullptr);
287 template <
bool Loops>
293 long v_low = low<GT>(v);
295 for (; f.it.has_curr(); f.it.next_ne())
297 auto w = g->get_tgt_node(
dfs_curr(f.it));
312 v_low = std::min(v_low, df<GT>(
w));
335 template <
class On_Loop,
class Finish>
339 constexpr bool loops = not std::is_null_pointer_v<On_Loop>;
349 std::optional<Frame> here(std::in_place,
root,
sa_);
352 auto w = next_target<loops>(*here);
364 frames.
push(std::move(*here));
366 here.emplace(
w,
sa_);
371 long v_low = low<GT>(here->v);
381 low<GT>(f.
v) = std::min(low<GT>(f.
v), v_low);
385 w = next_target<loops>(f);
387 if (
w == f.
v and on_loop(
w))
390 while (loops
and w == f.
v);
395 here.emplace(
w,
sa_);
399 v_low = low<GT>(f.
v);
423 if (low<GT>(v) == df<GT>(v))
461 if (low<GT>(v) == df<GT>(v))
501 if (low<GT>(v) == df<GT>(v))
552 if (low<GT>(v) != df<GT>(v))
578 auto a = block.get_first_arc();
579 auto start = block.get_tgt_node(a);
580 auto end = block.get_src_node(a);
581 assert(start != end);
584 assert(not aux_path.is_empty());
590 path_ptr_->append_directed(table.
find(i.get_current_node_ne()));
607 if (low<GT>(v) != df<GT>(v))
651 for (
typename GT::Node_Iterator j(blk); j.has_curr(); j.next_ne())
653 auto bsrc = j.get_curr();
654 auto gsrc = table.
find(bsrc);
657 for (Itor<GT, SA>
k(gsrc,
sa_);
k.has_curr();
k.next_ne())
659 auto ga =
k.get_curr();
661 auto ptr = table.
search(gtgt);
687 if (low<GT>(v) != df<GT>(v))
725 const size_t held = blk_list.
size();
727 for (
typename GT::Node_Iterator it(g);
df_count_ <
n_; it.next_ne())
735 for (
size_t k = 0;
k < held; ++
k)
740 for (
typename GT::Node_Iterator j(blk); j.has_curr(); j.next_ne())
742 auto bsrc = j.get_curr();
743 auto gsrc = mapped_node<GT>(bsrc);
746 for (Itor<GT, SA>
k(gsrc,
sa_);
k.has_curr();
k.next_ne())
748 auto ga =
k.get_curr();
757 auto btgt = mapped_node<GT>(gtgt);
792 for (
typename GT::Node_Iterator it(g);
df_count_ <
n_; it.next_ne())
837 for (
typename GT::Node_Iterator it(g);
df_count_ <
n_; it.next_ne())
890 GT &curr = it.get_curr();
896 arc_list.
append(it.get_curr());
916 auto &blk = it.get_curr();
917 while (not blk.is_empty())
918 tgt_list.append(blk.remove_first());
936 for (
typename GT::Node_Iterator it(g);
df_count_ <
n_; it.next_ne())
978 for (
typename GT::Node_Iterator it(g);
df_count_ <
n_; it.next_ne())
1029 bool started =
false;
1030 for (
typename GT::Node_Iterator it(g);
df_count_ <
n_; it.next_ne())
1069 return g_ptr_ !=
nullptr;
Exception handling system with formatted messages for Aleph-w.
#define ah_domain_error_if(C)
Throws std::domain_error if condition holds.
C++20 concepts for the protocol shared by graph algorithms.
List_Graph< Graph_Node< Node_Info >, Graph_Arc< Arc_Info > > GT
Determines if a digraph contains a cycle and constructs it.
bool operator()(const GT &g, Path< GT > &path) const
Invokes the computation of a cycle in a digraph.
Compute_Cycle_In_Digraph(SA __sa=SA())
Constructs a cycle computation instance with an arc filter.
auto has_curr() const noexcept(noexcept(std::declval< I & >().has_curr())) -> decltype(std::declval< I & >().has_curr())
true if there is a current item.
Stack of the frames of a depth-first search that does not recurse.
T & push(Args &&...args)
Builds a frame on top of the stack.
bool is_empty() const noexcept
Whether the stack has no frames.
T & top() noexcept
The top frame; the stack must not be empty.
void pop() noexcept
Destroys the top frame; the stack must not be empty.
Path search on directed graphs defined through a graph class (not a digraph).
Path< GT > dfs(typename GT::Node *start, Op &op)
Dynamic doubly linked list with O(1) size and bidirectional access.
T & append(const T &item)
Append a copied item at the end of the list.
Dynamic stack of elements of generic type T based on a singly linked list.
bool is_empty() const noexcept
Check if the stack is empty.
T pop()
Remove and return the top item of the stack.
T & push(const T &data)
Push an item by copy onto the top of the stack.
void empty() noexcept
Remove all elements from the stack.
Iterator on the items of list.
T & get_curr() const
Return the current item.
Doubly-linked list (defined in tpl_dynList.H).
T & append(const T &item)
size_t size() const noexcept
Return the number of items of the list, in constant time.
Dynamic map implemented with an AVL tree.
Pair * search(const Key &key) noexcept(nothrow_search)
Collect all keys.
Pair * insert(const Key &key, const Data &data)
Insert a key-value pair.
Data & find(const Key &key)
Find the value associated with key.
void next_ne() noexcept
Move the iterator one position forward guaranteeing no exception.
bool has_curr() const noexcept
virtual Node * insert_node(Node *node) noexcept
Insertion of a node already allocated.
void swap(List_Graph &g) noexcept
Swap in constant time this with g
Arc * insert_arc(Node *src_node, Node *tgt_node, void *a)
Functor that traverses the nodes of a graph and performs an operation.
Filtered iterator for outcoming arcs of a node.
Iterator on nodes and arcs of a path.
void empty()
Clean the path: all the nodes and arc are removed.
void set_graph(const GT &__g, Node *start_node=nullptr)
Set the graph of the path.
Computes strongly connected components (SCCs) in a directed graph using Tarjan's algorithm.
void init_tarjan(const GT &g)
Initialize internal state for a Tarjan traversal.
bool has_computation() const noexcept
Check if a computation has been performed.
SA & get_filter() noexcept
Returns the arc filter used by this instance.
DynList< DynList< typename GT::Node * > > connected_components(const GT &g)
Tarjan_Connected_Components(const Tarjan_Connected_Components &)=delete
Tarjan instances should not be copied (they hold internal traversal state)
void init_node_and_push_in_stack(typename GT::Node *p)
Initialize a node and push it onto the traversal stack.
bool is_connected(typename GT::Node *root)
Depth-first search that tests if the graph is strongly connected.
Tarjan_Connected_Components & operator=(const Tarjan_Connected_Components &)=delete
bool has_cycle(typename GT::Node *root)
Depth-first search that detects a cycle reachable from root.
void connected_components(const GT &g, DynList< DynList< typename GT::Node * > > &blks)
Computes the strongly connected components (SCCs) of a digraph.
bool test_connectivity(const GT &g)
Tests whether the digraph is strongly connected.
static constexpr std::nullptr_t go_on_with_loops
Tarjan_Connected_Components(SA __sa=SA()) noexcept(std::is_nothrow_move_constructible_v< SA >)
Constructs a Tarjan algorithm instance for computing strongly connected components.
void connected_components(const GT &g, DynList< GT > &blk_list, DynList< typename GT::Arc * > &arc_list)
Computes the strongly connected components (SCCs) of a digraph.
bool compute_cycle(const GT &g, typename GT::Node *src, Path< GT > &path)
Finds and constructs a cycle starting from a specific node, if one exists.
void pop_block(typename GT::Node *v, DynList< GT > &blk_list)
bool compute_cycle(const GT &g, Path< GT > &path)
Finds and constructs a cycle in the digraph, if one exists.
void build_path(const GT &block, DynMapAvlTree< typename GT::Node *, typename GT::Node * > &table)
Build a cycle path from a strongly connected block.
GT * get_graph() const noexcept
Get the graph of the last computation.
const SA & get_filter() const noexcept
Returns the arc filter used by this instance (const version).
void scc_by_blocks(typename GT::Node *root, DynList< GT > &blk_list)
Depth-first search that finds SCCs and builds mapped subgraphs.
void scc_by_lists(typename GT::Node *root, DynList< DynList< typename GT::Node * > > &blks)
Depth-first search that finds SCCs and collects their nodes.
bool build_cycle(typename GT::Node *root)
Depth-first search that finds a cycle reachable from root.
bool has_cycle(const GT &g)
Determines whether the digraph contains at least one cycle.
Tarjan_Connected_Components(Tarjan_Connected_Components &&)=default
Move is allowed.
bool is_dag(const GT &g)
Determines whether the directed graph is acyclic (a DAG).
bool search(typename GT::Node *root, On_Loop on_loop, Finish finish)
Depth-first search of Tarjan from root, with a stack of its own.
DynListStack< typename GT::Node * > stack_
void scc_by_len(typename GT::Node *root, DynList< size_t > &sizes)
Depth-first search that finds SCCs and counts their sizes.
bool build_loop(typename GT::Node *v)
GT::Node * next_target(Frame &f) noexcept(noexcept(f.it.next_ne()))
GT::Node * pop_from_stack()
Pop a node from the traversal stack.
bool pop_block_and_build_cycle(typename GT::Node *v)
static bool is_node_in_stack(typename GT::Node *p) noexcept
Check if a node is currently on the traversal stack.
void connected_components(const GT &g, DynList< size_t > &blks)
Computes the sizes of the strongly connected components (SCCs).
void operator()(const GT &g, DynList< GT > &blk_list, DynList< typename GT::Arc * > &arc_list)
This is an overloaded member function, provided for convenience. It differs from the above function o...
size_t num_connected_components(const GT &g)
Returns the number of strongly connected components in the graph.
constexpr size_t get_num_nodes() const noexcept
Return the total of nodes of graph.
static void map_arcs(A1 *p, A2 *q) noexcept
Map the arcs through their cookies.
Node * get_tgt_node(Arc *arc) const noexcept
Return the target node of arc (only for directed graphs)
static void map_nodes(N1 *p, N2 *q) noexcept
Map the nodes through their cookies.
__gmp_expr< T, __gmp_binary_expr< __gmp_expr< T, U >, unsigned long int, __gmp_root_function > > root(const __gmp_expr< T, U > &expr, unsigned long int l)
#define ARC_COOKIE(p)
Return the arc cookie
#define NODE_COUNTER(p)
Get the counter of a node.
#define IS_NODE_VISITED(p, bit)
Determine whether the control bit is set or not to one.
#define NODE_COOKIE(p)
Return the node cookie
decltype(auto) dfs_curr(const It &it)
The current item of an iterator that has one.
#define NODE_BITS(p)
Get the control bits of a node.
#define ALEPH_FLATTEN
[[gnu::flatten]], or nothing where it would only make the compilation slower.
Singly linked list implementations with head-tail access.
Main namespace for Aleph-w library functions.
and
Check uniqueness with explicit hash + equality functors.
void next()
Advance all underlying iterators (bounds-checked).
void next_ne() noexcept(noexcept(std::declval< Aleph::const_iterator_t< HeadC > & >().next_ne()) and noexcept(std::declval< ZipIterator< TailC... > & >().next_ne()))
Advance all underlying iterators (unchecked variant).
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.
Frame(typename GT::Node *p, const SA &sa)
void next_arc() noexcept(noexcept(it.next_ne()))
Functor to initialize node metadata for Tarjan traversal.
void operator()(const GT &g, typename GT::Node *p) const noexcept
DynList< DynList< typename GT::Node * > > * blks
Tarjan_Connected_Components * tarjan
bool operator()(typename GT::Node *v) const
Stack of the frames of a depth-first search that does not recurse.
Dynamic stack implementation based on linked lists.
Dynamic set implementations based on balanced binary search trees.
Path finding algorithms in graphs.
Utility algorithms and operations for graphs.