Aleph-w 7.10.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
Tarjan.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
91#ifndef TARJAN_H
92#define TARJAN_H
93
94# include <ah-graph-concepts.H>
95
96# include <algorithm>
97# include <cstddef>
98# include <type_traits>
99
100#include <tpl_dfs_stack.H>
101#include <tpl_dynListStack.H>
102#include <tpl_dynSetTree.H>
103#include <htlist.H>
104#include <tpl_graph_utils.H>
105#include <tpl_find_path.H>
106#include <ah-errors.H>
107
108namespace Aleph {
172template <AlephGraph GT, template <typename, class> class Itor = Out_Iterator, ArcFilter<GT> SA = Dft_Show_Arc<GT>>
174{
175 SA sa_;
176
177 GT *g_ptr_ = nullptr;
178
180
181 long df_count_ = 0;
182 mutable size_t n_ = 0; // number of nodes in the graph
183 size_t num_blocks_ = 0; // that scc_by_blocks() built; blk_list.size() would count them
184
185 Path<GT> *path_ptr_ = nullptr;
186
187 // A node that waits in search() for the one that it visits, and the
188 // next arc of it to look at.
189 //
190 // The functions that search() calls for each node or arc are inlined
191 // with all that they call: GCC would leave out of line the iterators and
192 // the stack, which then cost more than a recursion.
193 struct Frame
194 {
195 typename GT::Node *v;
196 Itor<GT, SA> it;
197
198 [[gnu::always_inline]] ALEPH_FLATTEN
199 Frame(typename GT::Node *p, const SA &sa) : v(p), it(p, sa) {}
200
201 [[gnu::always_inline]] ALEPH_FLATTEN
202 void next_arc() noexcept(noexcept(it.next_ne())) { it.next_ne(); }
203 };
204
205public:
208
210
213
215
222 noexcept(std::is_nothrow_move_constructible_v<SA>) : sa_(std::move(__sa))
223 { /* empty */
224 }
225
226private:
232 {
233 void operator () (const GT &g, typename GT::Node *p) const noexcept
234 {
235 g.reset_bits(p);
236 g.reset_counter(p); // initialize df
237 low<GT>(p) = -1; // initialize low
238 }
239 };
240
245 static bool is_node_in_stack(typename GT::Node *p) noexcept
246 {
247 assert(p != nullptr);
248 return IS_NODE_VISITED(p, Aleph::Min);
249 }
250
258 [[gnu::always_inline]] ALEPH_FLATTEN
260 {
261 assert(not is_node_in_stack(p));
262
263 stack_.push(p);
264 NODE_BITS(p).set_bit(Aleph::Min, true);
265 NODE_BITS(p).set_bit(Aleph::Depth_First, true);
266 df<GT>(p) = low<GT>(p) = df_count_++;
267 }
268
272 [[gnu::always_inline]] ALEPH_FLATTEN
274 {
275 auto ret = stack_.pop();
276 NODE_BITS(ret).set_bit(Aleph::Min, false);
277
278 return ret;
279 }
280
281 // Moves the iterator of f past the arcs of f.v whose targets were
282 // visited, of which those still in the stack lower low(f.v), and gives the
283 // target of the arc where it stops: a node not visited or, if Loops, f.v
284 // itself for a loop. When the arcs of f.v are done, nullptr. low(f.v) is
285 // stored once: a store in the loop could be to the iterator, which would
286 // have to be read again.
287 template <bool Loops>
288 [[gnu::always_inline]] ALEPH_FLATTEN
289 typename GT::Node *next_target(Frame &f) noexcept(noexcept(f.it.next_ne()))
290 {
291 auto v = f.v;
292 auto g = g_ptr_;
293 long v_low = low<GT>(v);
294 typename GT::Node *next = nullptr;
295 for (; f.it.has_curr(); f.it.next_ne())
296 {
297 auto w = g->get_tgt_node(dfs_curr(f.it));
298 if constexpr (Loops)
299 if (w == v)
300 {
301 next = w;
302 break;
303 }
304
306 {
307 next = w;
308 break;
309 }
310
311 if (is_node_in_stack(w)) // if on stack ==> v was visited before w
312 v_low = std::min(v_low, df<GT>(w));
313 }
314 low<GT>(v) = v_low;
315 return next;
316 }
317
335 template <class On_Loop, class Finish>
336 bool search(typename GT::Node *root, On_Loop on_loop, Finish finish)
337 {
338 // a loop that does not stop the search lowers nothing: df(v) >= low(v)
339 constexpr bool loops = not std::is_null_pointer_v<On_Loop>;
340
341 // The node being visited keeps its iterator in here until it visits a
342 // node; then it waits in frames, where it goes on when it comes back.
343 // here is optional, so that the frame of each node is built in its
344 // place: a frame keeps the filter, which may not be assignable. The
345 // stack is of this search, so that a search that stops early leaves no
346 // frame, and the object keeps no room, as a member would.
347 Dfs_Stack<Frame> frames;
349 std::optional<Frame> here(std::in_place, root, sa_);
350 while (true)
351 {
352 auto w = next_target<loops>(*here);
353 if constexpr (loops)
354 if (w == here->v)
355 {
356 if (on_loop(w))
357 return true;
358 here->next_arc();
359 continue;
360 }
361
362 if (w != nullptr) // the iterator stays on the arc to w, as in a recursion
363 {
364 frames.push(std::move(*here));
366 here.emplace(w, sa_);
367 continue;
368 }
369
370 // here->v is done; finish() may reuse the cookie that holds its low
371 long v_low = low<GT>(here->v);
372 if (finish(here->v))
373 return true;
374
375 while (true) // back to the nodes that wait
376 {
377 if (frames.is_empty())
378 return false;
379
380 Frame &f = frames.top(); // the node from which the last one was visited
381 low<GT>(f.v) = std::min(low<GT>(f.v), v_low);
382 do
383 {
384 f.next_arc(); // past the arc to that node, or past a loop
385 w = next_target<loops>(f);
386 if constexpr (loops)
387 if (w == f.v and on_loop(w))
388 return true;
389 }
390 while (loops and w == f.v);
391
392 if (w != nullptr)
393 {
395 here.emplace(w, sa_);
396 break;
397 }
398
399 v_low = low<GT>(f.v);
400 auto v = f.v;
401 frames.pop();
402 if (finish(v))
403 return true;
404 }
405 }
406 }
407
408 // on_loop of search() when no loop stops it
409 static constexpr std::nullptr_t go_on_with_loops = nullptr;
410
419 void scc_by_blocks(typename GT::Node *root, DynList<GT> &blk_list)
420 {
421 search(root, go_on_with_loops, [this, &blk_list](typename GT::Node *v)
422 {
423 if (low<GT>(v) == df<GT>(v)) // first visited node of the block?
424 pop_block(v, blk_list);
425 return false;
426 });
427 }
428
429 // Pops from the stack the block whose first visited node is v, and
430 // appends to blk_list a mapped copy of its nodes. Out of line, as the
431 // other work done once per block, so that search() stays small.
432 [[gnu::noinline]] void pop_block(typename GT::Node *v, DynList<GT> &blk_list)
433 {
434 const size_t blk_idx = num_blocks_++;
435 GT &blk = blk_list.append(GT());
436
437 while (true) // remove block from stack until v is removed
438 {
439 auto p = pop_from_stack();
440 auto q = blk.insert_node(p->get_info());
441 *q = *p; // copy node content
442 NODE_COOKIE(p) = NODE_COOKIE(q) = nullptr;
443 GT::map_nodes(p, q);
444 NODE_COUNTER(p) = NODE_COUNTER(q) = static_cast<long>(blk_idx);
445 if (p == v)
446 break;
447 }
448 }
449
450 // What scc_by_lists() does when search() leaves the node v: if v is the
451 // first visited node of its block, it pops the block out of the stack
452 // into a new list of blks. A class, so that its call is inline: search()
453 // makes it for each node, and Clang left a lambda out of line.
455 {
458
459 [[gnu::always_inline]] bool operator () (typename GT::Node *v) const
460 {
461 if (low<GT>(v) == df<GT>(v)) // first visited node of the block?
462 { // yes pop out block nodes that are on stack
464 while (true) // remove block from stack until reaching v
465 {
466 auto p = tarjan->pop_from_stack();
467 l.append(p);
468 if (p == v)
469 break;
470 }
471 }
472 return false;
473 }
474 };
475
488
497 void scc_by_len(typename GT::Node *root, DynList<size_t> &sizes)
498 {
499 search(root, go_on_with_loops, [this, &sizes](typename GT::Node *v)
500 {
501 if (low<GT>(v) == df<GT>(v)) // first visited node of the block?
502 { // yes, pop out block nodes that are on the stack
503 size_t count = 0;
504 while (true) // remove block from the stack until reaching v
505 {
506 auto p = pop_from_stack();
507 ++count;
508
509 if (p == v)
510 break;
511 }
512 sizes.append(count);
513 }
514 return false;
515 });
516 }
517
529 void init_tarjan(const GT &g)
530 {
531 Operate_On_Nodes<GT, Init_Tarjan_Node>()(g); // initialize bits, df and low
532 df_count_ = 0; // visitor counter
533 stack_.empty();
534 n_ = g.get_num_nodes();
535
536 g_ptr_ = &const_cast<GT &>(g);
537 }
538
547 bool has_cycle(typename GT::Node *root)
548 {
549 return search(root, [](typename GT::Node *) { return true; }, // a loop
550 [this](typename GT::Node *v)
551 {
552 if (low<GT>(v) != df<GT>(v)) // not the first node of its block
553 return false;
554
555 size_t count = 0; // of the nodes of the block
556 while (true)
557 {
558 ++count;
559 if (pop_from_stack() == v)
560 break;
561 }
562
563 return count >= 2; // if count >= 2 ==> there is a cycle
564 });
565 }
566
576 {
577 // Search for a cycle in the block
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);
582
583 auto aux_path = Directed_Find_Path<GT, Itor, SA>(block, sa_).dfs(start, end);
584 assert(not aux_path.is_empty()); // since it's connected it must be found
585
586 // aux_path is about the mapped block. We need to translate it back
587 // to the original graph using the mapping table.
588 path_ptr_->empty();
589 for (typename Path<GT>::Iterator i(aux_path); i.has_curr(); i.next_ne())
590 path_ptr_->append_directed(table.find(i.get_current_node_ne()));
591
592 path_ptr_->append_directed(path_ptr_->get_first_node());
593 }
594
602 bool build_cycle(typename GT::Node *root)
603 {
604 return search(root, [this](typename GT::Node *v) { return build_loop(v); },
605 [this](typename GT::Node *v)
606 {
607 if (low<GT>(v) != df<GT>(v)) // not the first visited node of the block?
608 {
609 assert(path_ptr_->is_empty());
610 return false;
611 }
612
614 });
615 }
616
617 // Puts in path_ptr_ the loop v -> v
618 [[gnu::noinline]] bool build_loop(typename GT::Node *v)
619 {
620 path_ptr_->empty();
621 path_ptr_->init(v);
622 path_ptr_->append_directed(v);
623 return true;
624 }
625
626 // Pops from the stack the block whose first visited node is v and, if it
627 // has more than one node, puts in path_ptr_ a cycle of it
628 [[gnu::noinline]] bool pop_block_and_build_cycle(typename GT::Node *v)
629 {
630 GT blk; // auxiliary graph
631
632 // g node mapping to blk (cookies are busy)
634
635 // pop nodes from stack and insert them into auxiliary block
636 while (true) // pop the component and insert into blk
637 {
638 auto p = pop_from_stack();
639 auto q = blk.insert_node();
640 *q = *p; // copy node content
641 table.insert(q, p);
642 table.insert(p, q);
643 if (p == v)
644 break;
645 }
646
647 if (blk.get_num_nodes() == 1)
648 return false; // single node without self-loop ==> no cycle
649
650 // finish constructing the block with the arcs
651 for (typename GT::Node_Iterator j(blk); j.has_curr(); j.next_ne())
652 {
653 auto bsrc = j.get_curr();
654 auto gsrc = table.find(bsrc);
655
656 // traverse the arcs of gsrc
657 for (Itor<GT, SA> k(gsrc, sa_); k.has_curr(); k.next_ne())
658 {
659 auto ga = k.get_curr();
660 auto gtgt = g_ptr_->get_tgt_node(ga);
661 auto ptr = table.search(gtgt);
662 if (ptr == nullptr) // arc of the block?
663 continue;
664
665 auto ta = blk.insert_arc(bsrc, ptr->second);
666 *ta = *ga; // copy arc content
667 }
668 }
669
670 build_path(blk, table);
671
672 return true;
673 }
674
683 bool is_connected(typename GT::Node *root)
684 {
685 return not search(root, go_on_with_loops, [this](typename GT::Node *v)
686 {
687 if (low<GT>(v) != df<GT>(v)) // not the first visited node of the block?
688 return false;
689
690 // pop nodes from stack until v is found; a block that does not
691 // reach the bottom of the stack is not that of root
692 while (pop_from_stack() != v)
693 ;
694
695 return not stack_.is_empty();
696 });
697 }
698
699public:
722 {
723 init_tarjan(g);
724
725 const size_t held = blk_list.size(); // the blocks that it holds, which stay
726 num_blocks_ = 0;
727 for (typename GT::Node_Iterator it(g); df_count_ < n_; it.next_ne())
728 if (auto v = it.get_curr(); not IS_NODE_VISITED(v, Aleph::Depth_First))
729 scc_by_blocks(v, blk_list);
730
731 assert(stack_.is_empty());
732
733 // traverse each partial subgraph of g and add its arcs
734 typename DynList<GT>::Iterator i(blk_list);
735 for (size_t k = 0; k < held; ++k)
736 i.next_ne();
737 for (; i.has_curr(); i.next_ne())
738 { // traverse all nodes of the block
739 GT &blk = i.get_curr();
740 for (typename GT::Node_Iterator j(blk); j.has_curr(); j.next_ne())
741 {
742 auto bsrc = j.get_curr();
743 auto gsrc = mapped_node<GT>(bsrc);
744
745 // traverse arcs of gsrc
746 for (Itor<GT, SA> k(gsrc, sa_); k.has_curr(); k.next_ne())
747 {
748 auto ga = k.get_curr();
749 auto gtgt = g_ptr_->get_tgt_node(ga);
750 if (NODE_COUNTER(gsrc) != NODE_COUNTER(gtgt))
751 { // inter-block arc ==> add it to arc_list
752 arc_list.append(ga);
753 continue;
754 }
755
756 // insert and map the arc in the sub-block
757 auto btgt = mapped_node<GT>(gtgt);
758 auto ba = blk.insert_arc(bsrc, btgt);
759 *ba = *ga; // copy arc content
760 ARC_COOKIE(ga) = ARC_COOKIE(ba) = nullptr; // else it would compose with a former block
761 GT::map_arcs(ga, ba);
762 }
763 }
764 }
765 }
766
790 {
791 init_tarjan(g);
792 for (typename GT::Node_Iterator it(g); df_count_ < n_; it.next_ne())
793 if (auto v = it.get_curr(); not IS_NODE_VISITED(v, Aleph::Depth_First))
794 scc_by_lists(v, blks);
795 }
796
798 {
800 connected_components(g, blks);
801 return blks;
802 }
803
814 [[nodiscard]] size_t num_connected_components(const GT &g)
815 {
816 DynList<size_t> sizes;
817 connected_components(g, sizes);
818 return sizes.size();
819 }
820
835 {
836 init_tarjan(g);
837 for (typename GT::Node_Iterator it(g); df_count_ < n_; it.next_ne())
838 if (auto v = it.get_curr(); not IS_NODE_VISITED(v, Aleph::Depth_First))
839 scc_by_len(v, blks);
840 }
841
851 void operator () (const GT &g, DynList<GT> &blk_list, DynList<typename GT::Arc *> &arc_list)
852 {
853 connected_components(g, blk_list, arc_list);
854 }
855
864 {
865 connected_components(g, blks);
866 }
867
872
882 void operator () (const GT &g, DynDlist<GT> &blk_list, DynDlist<typename GT::Arc *> &arc_list)
883 {
884 DynList<GT> blist;
886 connected_components(g, blist, alist);
887
888 for (typename DynList<GT>::Iterator it(blist); it.has_curr(); it.next_ne())
889 {
890 GT &curr = it.get_curr();
891 GT &block = blk_list.append(GT());
892 curr.swap(block);
893 }
894
895 for (typename DynList<typename GT::Arc *>::Iterator it(alist); it.has_curr(); it.next_ne())
896 arc_list.append(it.get_curr());
897 }
898
908 {
911
912 for (typename DynList<DynList<typename GT::Node *>>::Iterator it(b); it.has_curr(); it.next_ne())
913 {
914 auto &tgt_list = blks.append(DynDlist<typename GT::Node *>());
915
916 auto &blk = it.get_curr();
917 while (not blk.is_empty())
918 tgt_list.append(blk.remove_first());
919 }
920 }
921
933 [[nodiscard]] bool has_cycle(const GT &g)
934 {
935 init_tarjan(g);
936 for (typename GT::Node_Iterator it(g); df_count_ < n_; it.next_ne())
937 if (auto v = it.get_curr(); not IS_NODE_VISITED(v, Aleph::Depth_First))
938 if (has_cycle(v))
939 return true;
940
941 return false;
942 }
943
954 [[nodiscard]] bool is_dag(const GT &g)
955 {
956 return not has_cycle(g);
957 }
958
972 bool compute_cycle(const GT &g, Path<GT> &path)
973 {
974 init_tarjan(g);
975 path_ptr_ = &path;
977
978 for (typename GT::Node_Iterator it(g); df_count_ < n_; it.next_ne())
979 if (auto v = it.get_curr(); not IS_NODE_VISITED(v, Aleph::Depth_First)) // p visited?
980 if (build_cycle(v))
981 return true;
982
983 path.empty();
984 return false;
985 }
986
1001 [[nodiscard]] bool compute_cycle(const GT &g, typename GT::Node *src, Path<GT> &path)
1002 {
1003 ah_domain_error_if(src == nullptr) << "compute_cycle: source node cannot be null";
1004
1005 init_tarjan(g);
1006 path_ptr_ = &path;
1007 path_ptr_->set_graph(g);
1008 return build_cycle(src);
1009 }
1010
1022 [[nodiscard]] bool test_connectivity(const GT &g)
1023 {
1024 init_tarjan(g);
1025
1026 // In a strongly connected graph, a single DFS from any node should reach
1027 // all nodes. If we have to start from a second unvisited node, it means
1028 // the graph has multiple disconnected components.
1029 bool started = false;
1030 for (typename GT::Node_Iterator it(g); df_count_ < n_; it.next_ne())
1031 if (auto v = it.get_curr(); not IS_NODE_VISITED(v, Aleph::Depth_First))
1032 {
1033 if (started) // Second DFS root → not strongly connected
1034 return false;
1035 started = true;
1036 if (not is_connected(v))
1037 return false;
1038 }
1039
1040 assert(stack_.is_empty());
1041
1042 return true;
1043 }
1044
1047
1051 SA &get_filter() noexcept
1052 {
1053 return sa_;
1054 }
1055
1059 const SA &get_filter() const noexcept
1060 {
1061 return sa_;
1062 }
1063
1067 [[nodiscard]] bool has_computation() const noexcept
1068 {
1069 return g_ptr_ != nullptr;
1070 }
1071
1075 [[nodiscard]] GT *get_graph() const noexcept
1076 {
1077 return g_ptr_;
1078 }
1079
1081};
1082
1103template <AlephGraph GT, template <typename, class> class Itor = Out_Iterator, ArcFilter<GT> SA = Dft_Show_Arc<GT>>
1105{
1107
1108public:
1114 Compute_Cycle_In_Digraph(SA __sa = SA()) : sa_(std::move(__sa))
1115 { /* empty */
1116 }
1117
1126 [[nodiscard]] bool operator () (const GT &g, Path<GT> &path) const
1127 {
1129
1130 return tarjan.compute_cycle(g, path);
1131 }
1132
1139 [[nodiscard]] Path<GT> operator () (const GT &g) const
1140 {
1141 Path<GT> ret(g);
1143 return ret;
1144 }
1145
1154 [[nodiscard]] Path<GT> operator () (const GT &g, typename GT::Node *src) const
1155 {
1156 Path<GT> ret(g);
1158 return ret;
1159 }
1160};
1161} // end namespace Aleph
1162
1163#endif // TARJAN_H
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:670
C++20 concepts for the protocol shared by graph algorithms.
List_Graph< Graph_Node< Node_Info >, Graph_Arc< Arc_Info > > GT
long double w
Definition btreepic.C:153
Determines if a digraph contains a cycle and constructs it.
Definition Tarjan.H:1105
bool operator()(const GT &g, Path< GT > &path) const
Invokes the computation of a cycle in a digraph.
Definition Tarjan.H:1126
Compute_Cycle_In_Digraph(SA __sa=SA())
Constructs a cycle computation instance with an arc filter.
Definition Tarjan.H:1114
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.
Definition htlist.H:1614
T & get_curr() const
Return the current item.
Definition htlist.H:1679
Doubly-linked list (defined in tpl_dynList.H).
Definition htlist.H:1231
T & append(const T &item)
Definition htlist.H:1362
size_t size() const noexcept
Return the number of items of the list, in constant time.
Definition htlist.H:1264
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.
Definition htlist.H:1012
bool has_curr() const noexcept
Definition htlist.H:969
virtual Node * insert_node(Node *node) noexcept
Insertion of a node already allocated.
Definition tpl_graph.H:535
void swap(List_Graph &g) noexcept
Swap in constant time this with g
Definition tpl_graph.H:1007
Arc * insert_arc(Node *src_node, Node *tgt_node, void *a)
Definition tpl_graph.H:618
Functor that traverses the nodes of a graph and performs an operation.
Definition tpl_graph.H:3698
Filtered iterator for outcoming arcs of a node.
Definition tpl_graph.H:1942
Iterator on nodes and arcs of a path.
Definition tpl_graph.H:4395
Path on a graph.
Definition tpl_graph.H:3832
void empty()
Clean the path: all the nodes and arc are removed.
Definition tpl_graph.H:3999
void set_graph(const GT &__g, Node *start_node=nullptr)
Set the graph of the path.
Definition tpl_graph.H:3977
Computes strongly connected components (SCCs) in a directed graph using Tarjan's algorithm.
Definition Tarjan.H:174
void init_tarjan(const GT &g)
Initialize internal state for a Tarjan traversal.
Definition Tarjan.H:529
bool has_computation() const noexcept
Check if a computation has been performed.
Definition Tarjan.H:1067
SA & get_filter() noexcept
Returns the arc filter used by this instance.
Definition Tarjan.H:1051
DynList< DynList< typename GT::Node * > > connected_components(const GT &g)
Definition Tarjan.H:797
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.
Definition Tarjan.H:259
bool is_connected(typename GT::Node *root)
Depth-first search that tests if the graph is strongly connected.
Definition Tarjan.H:683
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.
Definition Tarjan.H:547
void connected_components(const GT &g, DynList< DynList< typename GT::Node * > > &blks)
Computes the strongly connected components (SCCs) of a digraph.
Definition Tarjan.H:789
bool test_connectivity(const GT &g)
Tests whether the digraph is strongly connected.
Definition Tarjan.H:1022
static constexpr std::nullptr_t go_on_with_loops
Definition Tarjan.H:409
Tarjan_Connected_Components(SA __sa=SA()) noexcept(std::is_nothrow_move_constructible_v< SA >)
Constructs a Tarjan algorithm instance for computing strongly connected components.
Definition Tarjan.H:221
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.
Definition Tarjan.H:721
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.
Definition Tarjan.H:1001
void pop_block(typename GT::Node *v, DynList< GT > &blk_list)
Definition Tarjan.H:432
bool compute_cycle(const GT &g, Path< GT > &path)
Finds and constructs a cycle in the digraph, if one exists.
Definition Tarjan.H:972
void build_path(const GT &block, DynMapAvlTree< typename GT::Node *, typename GT::Node * > &table)
Build a cycle path from a strongly connected block.
Definition Tarjan.H:575
GT * get_graph() const noexcept
Get the graph of the last computation.
Definition Tarjan.H:1075
const SA & get_filter() const noexcept
Returns the arc filter used by this instance (const version).
Definition Tarjan.H:1059
void scc_by_blocks(typename GT::Node *root, DynList< GT > &blk_list)
Depth-first search that finds SCCs and builds mapped subgraphs.
Definition Tarjan.H:419
void scc_by_lists(typename GT::Node *root, DynList< DynList< typename GT::Node * > > &blks)
Depth-first search that finds SCCs and collects their nodes.
Definition Tarjan.H:484
bool build_cycle(typename GT::Node *root)
Depth-first search that finds a cycle reachable from root.
Definition Tarjan.H:602
bool has_cycle(const GT &g)
Determines whether the digraph contains at least one cycle.
Definition Tarjan.H:933
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).
Definition Tarjan.H:954
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.
Definition Tarjan.H:336
DynListStack< typename GT::Node * > stack_
Definition Tarjan.H:179
void scc_by_len(typename GT::Node *root, DynList< size_t > &sizes)
Depth-first search that finds SCCs and counts their sizes.
Definition Tarjan.H:497
bool build_loop(typename GT::Node *v)
Definition Tarjan.H:618
GT::Node * next_target(Frame &f) noexcept(noexcept(f.it.next_ne()))
Definition Tarjan.H:289
GT::Node * pop_from_stack()
Pop a node from the traversal stack.
Definition Tarjan.H:273
bool pop_block_and_build_cycle(typename GT::Node *v)
Definition Tarjan.H:628
static bool is_node_in_stack(typename GT::Node *p) noexcept
Check if a node is currently on the traversal stack.
Definition Tarjan.H:245
void connected_components(const GT &g, DynList< size_t > &blks)
Computes the sizes of the strongly connected components (SCCs).
Definition Tarjan.H:834
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...
Definition Tarjan.H:851
size_t num_connected_components(const GT &g)
Returns the number of strongly connected components in the graph.
Definition Tarjan.H:814
constexpr size_t get_num_nodes() const noexcept
Return the total of nodes of graph.
Definition graph-dry.H:975
static void map_arcs(A1 *p, A2 *q) noexcept
Map the arcs through their cookies.
Definition graph-dry.H:1312
Node * get_tgt_node(Arc *arc) const noexcept
Return the target node of arc (only for directed graphs)
Definition graph-dry.H:1023
static void map_nodes(N1 *p, N2 *q) noexcept
Map the nodes through their cookies.
Definition graph-dry.H:1281
__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)
Definition gmpfrxx.h:4138
#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.
@ Depth_First
Definition aleph-graph.H:73
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).
Definition ah-zip.H:236
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).
Definition ah-zip.H:252
Itor::difference_type count(const Itor &beg, const Itor &end, const T &value)
Count elements equal to a value.
Definition ahAlgo.H:127
STL namespace.
Default filter for filtered iterators on arcs.
Definition tpl_graph.H:1024
Frame(typename GT::Node *p, const SA &sa)
Definition Tarjan.H:199
void next_arc() noexcept(noexcept(it.next_ne()))
Definition Tarjan.H:202
Functor to initialize node metadata for Tarjan traversal.
Definition Tarjan.H:232
void operator()(const GT &g, typename GT::Node *p) const noexcept
Definition Tarjan.H:233
DynList< DynList< typename GT::Node * > > * blks
Definition Tarjan.H:457
bool operator()(typename GT::Node *v) const
Definition Tarjan.H:459
static int * k
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.
DynList< int > l