Aleph-w 7.10.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
tpl_graph_utils.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
32
44# ifndef TPL_GRAPH_UTILS_H
45# define TPL_GRAPH_UTILS_H
46
47# include <ah-graph-concepts.H>
48
49
50# include <cassert>
51# include <cstddef>
52# include <limits>
53# include <memory>
54# include <tuple>
55# include <type_traits>
56# include <utility>
57# include <vector>
58# include <tpl_agraph.H>
59# include <tpl_dynListQueue.H>
60# include <tpl_dfs_stack.H>
61# include <ah-errors.H>
62
63using namespace Aleph;
64
65namespace Aleph {
66
82 template <AlephGraph GT> inline static bool
83 __depth_first_traversal(const GT & g, typename GT::Node * node,
84 typename GT::Arc * arc,
85 bool (*visit)(const GT & g, typename GT::Node *,
86 typename GT::Arc *),
87 size_t & count);
88
113 template <AlephGraph GT> inline size_t
114 depth_first_traversal(const GT & g, typename GT::Node * start_node,
115 bool (*visit)(const GT & g, typename GT::Node *,
116 typename GT::Arc *))
117 {
120 size_t counter = 0;
121
122 __depth_first_traversal(g, start_node, nullptr, visit, counter);
123
124 return counter;
125 }
126
128 template <AlephGraph GT> inline
129 size_t depth_first_traversal(const GT & g,
130 bool (*visit)(const GT &, typename GT::Node *,
131 typename GT::Arc *))
132 {
133 return depth_first_traversal(g, g.get_first_node(), visit);
134 }
135
140 template <class GT>
142 {
144 bool operator () (const GT &, typename GT::Node *, typename GT::Arc *)
145 {
146 return false;
147 }
148 };
149
170 template <AlephGraph GT,
171 class Operation = Default_Visit_Op<GT>,
172 ArcFilter<GT> SA = Dft_Show_Arc<GT>>
174 {
175 Operation * op_ptr = nullptr;
176 SA sa;
177 size_t count = 0;
178 const GT * g_ptr = nullptr;
179
180 private:
181
183
184 // Marks node, reached through arc, counts it and visits it; tells
185 // whether the traversal must stop
186 bool enter(typename GT::Node * node, typename GT::Arc * arc)
187 {
188 NODE_BITS(node).set_bit(Depth_First, true);
189 count++;
190
191 if ((*op_ptr)(*g_ptr, node, arc))
192 return true;
193
194 return count == g_ptr->get_num_nodes();
195 }
196
197 // A path can have as many nodes as the graph, too many for recursion,
198 // so that the search keeps its own stack; the order of the visits is
199 // that of a recursion.
200 bool __dft(typename GT::Node * node, typename GT::Arc * arc = nullptr)
201 {
202 if (IS_NODE_VISITED(node, Depth_First))
203 return false;
204
205 if (enter(node, arc))
206 return true;
207
208 // The node being visited keeps its iterator in here until it visits
209 // a node; then it waits in frames, where it goes on when it comes
210 // back. here is optional, so that the frame of each node is built in
211 // its place: a frame keeps the filter, which may not be assignable
212 Dfs_Stack<Frame> frames;
213 std::optional<Frame> here(std::in_place, node, sa);
214 while (true)
215 {
216 if (auto tgt = here->template next_target<Depth_First>())
217 { // the iterator stays on the arc to tgt, as in a recursion
218 if (enter(tgt, here->it.get_current_arc_ne()))
219 return true;
220
221 frames.push(std::move(*here));
222 here.emplace(tgt, sa);
223 continue;
224 }
225
226 while (true) // back to the nodes that wait
227 {
228 if (frames.is_empty())
229 return false;
230
231 Frame & f = frames.top();
232 f.next_arc(); // past the arc to the node that was visited
233 if (auto tgt = f.template next_target<Depth_First>())
234 {
235 if (enter(tgt, f.it.get_current_arc_ne()))
236 return true;
237
238 here.emplace(tgt, sa);
239 break;
240 }
241 frames.pop();
242 }
243 }
244 }
245
246 size_t dft(const GT & g, typename GT::Node * start_node, Operation & __op)
247 {
248 op_ptr = &__op;
249 g_ptr = &g;
250
253
254 count = 0;
255
256 __dft(start_node);
257
258 return count;
259 }
260
261 public:
262
264 Depth_First_Traversal(SA __sa = SA()) : sa(__sa) { /* empty */ }
265
280 size_t operator () (const GT & g, Operation op = Operation())
281 {
282 return dft(g, g.get_first_node(), op);
283 }
284
300 size_t operator () (const GT & g, typename GT::Node * sn,
301 Operation op = Operation())
302 {
303 return dft(g, sn, op);
304 }
305 };
306
307
308 template <AlephGraph GT> inline static bool
309 __depth_first_traversal(const GT & g, typename GT::Node * node,
310 typename GT::Arc * arc,
311 bool (*visit)(const GT & g, typename GT::Node *,
312 typename GT::Arc *),
313 size_t & count)
314 {
315 if (IS_NODE_VISITED(node, Depth_First))
316 return false;
317
318 // Marks p, reached through a, counts it and visits it; tells whether
319 // the traversal must stop
320 struct Visit
321 {
322 [[gnu::always_inline]] static bool
323 enter(const GT & gr, typename GT::Node * p, typename GT::Arc * a,
324 bool (*v)(const GT &, typename GT::Node *, typename GT::Arc *),
325 size_t & c)
326 {
327 NODE_BITS(p).set_bit(Depth_First, true); // mark node visited
328 c++;
329
330 if (v != nullptr) // invoke callback if provided
331 if ((*v)(gr, p, a))
332 return true;
333
334 return c == gr.get_num_nodes(); // all nodes discovered?
335 }
336 };
337
338 if (Visit::enter(g, node, arc, visit, count))
339 return true;
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
344 Dfs_Stack<Frame> frames;
345 Frame here(g.get_arc_it(node));
346 while (true)
347 {
348 if (auto tgt = here.template next_target<Depth_First>())
349 { // the iterator stays on the arc to tgt, as in a recursion
350 if (Visit::enter(g, tgt, here.it.get_current_arc_ne(), visit, count))
351 return true;
352
353 frames.push(std::move(here));
354 here = Frame(g.get_arc_it(tgt));
355 continue;
356 }
357
358 while (true) // back to the nodes that wait
359 {
360 if (frames.is_empty())
361 return false; // keep exploring
362
363 Frame & f = frames.top();
364 f.next_arc(); // past the arc to the node that was visited
365 if (auto tgt = f.template next_target<Depth_First>())
366 {
367 if (Visit::enter(g, tgt, f.it.get_current_arc_ne(), visit, count))
368 return true;
369
370 here = Frame(g.get_arc_it(tgt));
371 break;
372 }
373 frames.pop();
374 }
375 }
376 }
377
378
404 template <AlephGraph GT> inline size_t
405 breadth_first_traversal(const GT & g, typename GT::Node * start,
406 bool (*visit)(const GT &, typename GT::Node *,
407 typename GT::Arc *) )
408 {
412
413 for (auto it = g.get_arc_it(start); it.has_curr(); it.next_ne())
414 q.put(it.get_current_arc_ne());
415
416 NODE_BITS(start).set_bit(Breadth_First, true);
417 size_t node_counter = 1;
418
419 if (visit != nullptr)
420 if ((*visit)(g, start, nullptr))
421 return 1;
422
423 while (not q.is_empty() and node_counter < g.get_num_nodes())
424 {
425 auto arc = q.get();
426 ARC_BITS(arc).set_bit(Breadth_First, true);
427
428 auto src = g.get_src_node(arc);
429 auto tgt = g.get_tgt_node(arc);
432 continue;
433
434 auto visit_node = IS_NODE_VISITED(src, Breadth_First) ? tgt : src;
435 if (visit != nullptr)
436 if ((*visit)(g, visit_node, arc))
437 break;
438
439 NODE_BITS(visit_node).set_bit(Breadth_First, true);
440 node_counter++;
441
442 for (auto it = g.get_arc_it(visit_node); it.has_curr(); it.next_ne())
443 {
444 auto curr_arc = it.get_current_arc_ne();
445 if (IS_ARC_VISITED(curr_arc, Breadth_First))
446 continue;
447
450 continue; // both endpoints already visited
451
452 q.put(curr_arc);
453 }
454 }
455
456 return node_counter;
457 }
458
460 template <AlephGraph GT> inline size_t
462 bool (*visit)(const GT &, typename GT::Node *,
463 typename GT::Arc *))
464 {
465 return breadth_first_traversal(g, g.get_first_node(), visit);
466 }
467
468
490 template <AlephGraph GT,
491 class Operation = Default_Visit_Op<GT>,
492 ArcFilter<GT> SA = Dft_Show_Arc<GT>>
494 {
495 SA sa;
496 size_t count = 0;
497
498 size_t bft(const GT & g, typename GT::Node * start, Operation & op)
499 {
502 DynListQueue<typename GT::Arc*> q; // pending arcs queue
503
504 for (Node_Arc_Iterator<GT, SA> it(start, sa); it.has_curr(); it.next_ne())
505 q.put(it.get_current_arc_ne());
506
507 NODE_BITS(start).set_bit(Breadth_First, true);
508 count = 1;
509
510 if (op (g, start, nullptr))
511 return 1;
512
513 while (not q.is_empty() and count < g.get_num_nodes())
514 {
515 auto arc = q.get();
516 ARC_BITS(arc).set_bit(Breadth_First, true);
517
518 auto src = g.get_src_node(arc);
519 auto tgt = g.get_tgt_node(arc);
520
523 continue;
524
525 auto curr = IS_NODE_VISITED(src, Breadth_First) ? tgt : src;
526 if (op (g, curr, arc))
527 break;
528
529 NODE_BITS(curr).set_bit(Breadth_First, true);
530 count++;
531
532 for (Node_Arc_Iterator<GT, SA> it(curr, sa); it.has_curr(); it.next_ne())
533 {
534 auto curr_arc = it.get_current_arc_ne();
535 if (IS_ARC_VISITED(curr_arc, Breadth_First))
536 continue;
537
540 continue;
541
542 q.put(curr_arc);
543 }
544 }
545
546 return count;
547 }
548
549 public:
550
552 Breadth_First_Traversal(SA __sa = SA()) : sa(__sa) { /* empty */ }
553
568 size_t operator () (const GT & g, Operation op)
569 {
570 return bft (g, g.get_first_node(), op);
571 }
572
588 size_t operator () (const GT & g, typename GT::Node * p,
589 Operation && op = Operation())
590 {
591 return bft(g, p, op);
592 }
593
604 size_t operator () (const GT & g, typename GT::Node * p, Operation & op)
605 {
606 return bft(g, p, op);
607 }
608 };
609
632 template <AlephGraph GT> inline
633 Path<GT> find_path_breadth_first(const GT & g, typename GT::Node * start,
634 typename GT::Node * end)
635 {
636 ah_invalid_argument_if(start == nullptr or end == nullptr)
637 << "find_path_breadth_first(): start and end must be non-null";
638
639 if (start == end)
640 return Path<GT>(g, start);
641
642 g.reset_nodes();
643 g.reset_arcs();
644
646
647 for (auto it = g.get_arc_it(start); it.has_curr(); it.next_ne())
648 q.put(it.get_current_arc_ne());
649
650 NODE_BITS(start).set_bit(Find_Path, true);
651
652 bool path_found = false;
653
654 while (not q.is_empty())
655 {
656 auto arc = q.get();
657 auto src = g.get_src_node(arc);
658 auto tgt = g.get_tgt_node(arc);
659
661 continue;
662
663 if (IS_NODE_VISITED(tgt, Find_Path))
664 std::swap(src, tgt);
665
666 ARC_BITS(arc).set_bit(Find_Path, true);
667 NODE_BITS(tgt).set_bit(Find_Path, true);
668 NODE_COOKIE(tgt) = src;
669
670 if (tgt == end)
671 {
672 path_found = true;
673 break;
674 }
675
676 for (auto it = g.get_arc_it(tgt); it.has_curr(); it.next_ne())
677 {
678 auto a = it.get_current_arc_ne();
680 continue;
681
684 continue;
685
686 q.put(a);
687 }
688 }
689
690 if (not path_found)
691 return Path<GT>(g);
692
693 q.empty(); // free queue memory for eventually saving the required for path
694
695 Path<GT> path(g, end);
696 auto p = end;
697 while (p != start)
698 {
699 p = (typename GT::Node *) NODE_COOKIE(p);
700 path.insert(p);
701 }
702
703 return path;
704 }
705
706
719 template <AlephGraph GT> inline
720 bool test_connectivity(const GT & g)
721 {
723 << "test_connectivity() does not work on digraphs";
724
725 const auto num_nodes = g.get_num_nodes();
726 if (num_nodes == 0)
727 return false;
728
729 if (g.get_num_arcs() + 1 < num_nodes)
730 return false;
731
732 return depth_first_traversal<GT>(g, nullptr) == num_nodes;
733 }
734
735
737 template <AlephGraph GT> inline static
738 bool __test_cycle(const GT & g, typename GT::Node *, typename GT::Node *);
739
740
757 template <AlephGraph GT> inline
758 bool test_for_cycle(const GT & g, typename GT::Node * src)
759 {
760 ah_invalid_argument_if(src == nullptr)
761 << "test_for_cycle(): src must be non-null";
762
765 for (auto it = g.get_arc_it(src); it.has_curr(); it.next_ne())
766 {
767 auto arc = it.get_curr();
768 if (IS_ARC_VISITED(arc, Test_Cycle))
769 continue;
770
771 ARC_BITS(arc).set_bit(Test_Cycle, true);
772 if (__test_cycle(g, src, it.get_tgt_node_ne()))
773 return true;
774 }
775
776 return false;
777 }
778
779
780 template <AlephGraph GT> inline static bool
781 __test_cycle(const GT & g, typename GT::Node * src, typename GT::Node * curr)
782 {
783 if (src == curr)
784 return true; // detected cycle
785
786 if (IS_NODE_VISITED(curr, Test_Cycle))
787 return false;
788
789 NODE_BITS(curr).set_bit(Test_Cycle, true);
790
791 // A path can have as many nodes as the graph, too many for recursion,
792 // so that the search keeps its own stack; the order of the visits is
793 // that of a recursion. The node being visited keeps its iterator in
794 // here until it visits a node; then it waits in frames, where it goes
795 // on when it comes back.
797 Dfs_Stack<Frame> frames;
798 Frame here(g.get_arc_it(curr));
799 while (true)
800 {
801 if (auto tgt = here.template next_target<Test_Cycle>(src))
802 { // the iterator stays on the arc to tgt, as in a recursion
803 if (tgt == src)
804 return true; // detected cycle
805
806 NODE_BITS(tgt).set_bit(Test_Cycle, true);
807 frames.push(std::move(here));
808 here = Frame(g.get_arc_it(tgt));
809 continue;
810 }
811
812 while (true) // back to the nodes that wait
813 {
814 if (frames.is_empty())
815 return false;
816
817 Frame & f = frames.top();
818 f.next_arc(); // past the arc to the node that was visited
819 if (auto tgt = f.template next_target<Test_Cycle>(src))
820 {
821 if (tgt == src)
822 return true; // detected cycle
823
824 NODE_BITS(tgt).set_bit(Test_Cycle, true);
825 here = Frame(g.get_arc_it(tgt));
826 break;
827 }
828 frames.pop();
829 }
830 }
831 }
832
839 template <AlephGraph GT> inline static
840 bool __is_graph_acyclique(const GT & g, typename GT::Node * curr_node)
841 {
842 if (IS_NODE_VISITED(curr_node, Test_Cycle))
843 return false;
844
845 NODE_BITS(curr_node).set_bit(Test_Cycle, true);
846
847 // The node being visited keeps its iterator in here until it visits a
848 // node; then it waits in frames, where it goes on when it comes back
850 Dfs_Stack<Frame> frames;
851 Frame here(g.get_arc_it(curr_node));
852 while (true)
853 {
854 if (auto tgt = here.template next_target<Test_Cycle, true>())
855 { // the iterator stays on the arc to tgt, as in a recursion
856 if (IS_NODE_VISITED(tgt, Test_Cycle)) // reached again
857 return false;
858
859 NODE_BITS(tgt).set_bit(Test_Cycle, true);
860 frames.push(std::move(here));
861 here = Frame(g.get_arc_it(tgt));
862 continue;
863 }
864
865 while (true) // back to the nodes that wait
866 {
867 if (frames.is_empty())
868 return true; // all the arcs explored without a back edge
869
870 Frame & f = frames.top();
871 f.next_arc(); // past the arc to the node that was visited
872 if (auto tgt = f.template next_target<Test_Cycle, true>())
873 {
874 if (IS_NODE_VISITED(tgt, Test_Cycle)) // reached again
875 return false;
876
877 NODE_BITS(tgt).set_bit(Test_Cycle, true);
878 here = Frame(g.get_arc_it(tgt));
879 break;
880 }
881 frames.pop();
882 }
883 }
884 }
885
886
906 template <AlephGraph GT> inline
907 bool is_graph_acyclique(const GT & g, typename GT::Node * start_node)
908 {
910 << "is_graph_acyclique() does not work for digraphs";
911
912 const auto num_nodes = g.get_num_nodes();
913 if (num_nodes == 0)
914 return true;
915
916 ah_invalid_argument_if(start_node == nullptr)
917 << "is_graph_acyclique(): start_node must be non-null";
918
919 // as many arcs as nodes give a cycle somewhere in g, perhaps not in the
920 // component of start_node
923
924 return __is_graph_acyclique(g, start_node);
925 }
926
943 template <AlephGraph GT> inline
944 bool is_graph_acyclique(const GT & g)
945 {
947 << "is_graph_acyclique() does not work for digraphs";
948
949 const auto num_nodes = g.get_num_nodes();
950 if (num_nodes == 0)
951 return true;
952
953 if (g.get_num_arcs() >= num_nodes) // a forest has fewer arcs than nodes
954 return false;
955
958
959 for (auto it = g.get_node_it(); it.has_curr(); it.next_ne())
960 {
961 auto current_node = it.get_current_node_ne();
962 if (IS_NODE_VISITED(current_node, Test_Cycle))
963 continue;
964
965 if (not __is_graph_acyclique(g, current_node))
966 return false;
967 }
968
969 return true;
970 }
971
972
981 template <AlephGraph GT>
982 inline bool has_cycle(const GT & g)
983 {
984 return not is_graph_acyclique(g);
985 }
986
987
1003 template <AlephGraph GT> inline
1004 bool test_for_path(const GT & g, typename GT::Node * start_node,
1005 typename GT::Node * end_node)
1006 {
1007 ah_invalid_argument_if(start_node == nullptr or end_node == nullptr)
1008 << "test_for_path(): start_node and end_node must be non-null";
1009
1010 if (start_node == end_node)
1011 return true;
1012
1015
1016 for (auto it = g.get_arc_it(start_node); it.has_curr(); it.next_ne())
1017 {
1018 auto arc = it.get_current_arc_ne();
1019 ARC_BITS(arc).set_bit(Find_Path, true);
1020 if (__test_for_path(g, it.get_tgt_node_ne(), end_node))
1021 return true;
1022 }
1023
1024 return false;
1025 }
1026
1027
1034 template <AlephGraph GT> inline static
1035 bool __test_for_path(const GT & g, typename GT::Node * curr_node,
1036 typename GT::Node * end_node)
1037 {
1038 if (curr_node == end_node)
1039 return true;
1040
1041 if (IS_NODE_VISITED(curr_node, Find_Path))
1042 return false;
1043
1044 NODE_BITS(curr_node).set_bit(Find_Path, true);
1045
1046 // The node being visited keeps its iterator in here until it visits a
1047 // node; then it waits in frames, where it goes on when it comes back
1049 Dfs_Stack<Frame> frames;
1050 Frame here(g.get_arc_it(curr_node));
1051 while (true)
1052 {
1053 if (auto tgt = here.template next_target<Find_Path>(end_node))
1054 { // the iterator stays on the arc to tgt, as in a recursion
1055 if (tgt == end_node)
1056 return true;
1057
1058 NODE_BITS(tgt).set_bit(Find_Path, true);
1059 frames.push(std::move(here));
1060 here = Frame(g.get_arc_it(tgt));
1061 continue;
1062 }
1063
1064 while (true) // back to the nodes that wait
1065 {
1066 if (frames.is_empty())
1067 return false;
1068
1069 Frame & f = frames.top();
1070 f.next_arc(); // past the arc to the node that was visited
1071 if (auto tgt = f.template next_target<Find_Path>(end_node))
1072 {
1073 if (tgt == end_node)
1074 return true;
1075
1076 NODE_BITS(tgt).set_bit(Find_Path, true);
1077 here = Frame(g.get_arc_it(tgt));
1078 break;
1079 }
1080 frames.pop();
1081 }
1082 }
1083 }
1084
1086 template <AlephGraph GT> inline
1088
1090 template <AlephGraph GT> inline
1091 void build_subgraph(const GT & g, GT & sg,
1092 typename GT::Node * g_src, size_t & node_count);
1093
1094
1117 template <AlephGraph GT> inline
1119 {
1121 << "inconnected_components() does not work on digraphs";
1122 g.reset_nodes();
1123 g.reset_arcs();
1124
1125 DynList<GT> list;
1126 size_t count = 0; // visited node counter
1127 for (auto it = g.get_node_it(); count < g.get_num_nodes() and it.has_curr();
1128 it.next_ne())
1129 {
1130 auto curr = it.get_current_node_ne();
1131 if (IS_NODE_VISITED(curr, Build_Subtree))
1132 continue;
1133
1134 list.append(GT());
1135 GT & subgraph = list.get_last();
1136 build_subgraph(g, subgraph, curr, count);
1137 }
1138
1139 return list;
1140 }
1141
1142
1163 template <AlephGraph GT> inline
1164 void build_subgraph(const GT & g, GT & sg,
1165 typename GT::Node * g_src, size_t & node_count)
1166 {
1167 if (IS_NODE_VISITED(g_src, Build_Subtree))
1168 return;
1169
1170 // A node that waits for the one that it visits, its image in sg, and
1171 // the next arc of it to look at
1172 struct Frame
1173 {
1174 typename GT::Node * sg_src;
1175 typename GT::Node_Arc_Iterator it;
1176
1177 [[gnu::always_inline]] ALEPH_FLATTEN
1178 Frame(const GT & gr, typename GT::Node * p, typename GT::Node * image)
1179 : sg_src(image), it(gr.get_arc_it(p)) {}
1180
1181 [[gnu::always_inline]] ALEPH_FLATTEN
1182 void next_arc() noexcept(noexcept(it.next_ne())) { it.next_ne(); }
1183
1184 // Marks p, counts it and gives its frame, with its image in sg,
1185 // which it inserts if p has none
1186 [[gnu::always_inline]] static Frame
1187 visit(const GT & gr, GT & sgr, typename GT::Node * p, size_t & count)
1188 {
1189 NODE_BITS(p).set_bit(Build_Subtree, true);
1190 ++count;
1191
1192 auto sg_p = mapped_node<GT>(p);
1193 if (sg_p == nullptr) // not mapped yet
1194 {
1195 sg_p = sgr.insert_node(p->get_info());
1196 GT::map_nodes(p, sg_p);
1197 }
1198 return Frame(gr, p, sg_p);
1199 }
1200
1201 // Copies into sg the arcs that were not visited, with their targets,
1202 // up to one whose target was not visited, which it gives, with the
1203 // iterator on that arc; nullptr when the arcs are done. Once every
1204 // node is visited, the arcs that are left still go into sg.
1205 [[gnu::always_inline]] typename GT::Node * next_to_visit(GT & sgr)
1206 {
1207 for (; it.has_curr(); next_arc())
1208 {
1209 auto arc = it.get_current_arc_ne();
1210 if (IS_ARC_VISITED(arc, Build_Subtree))
1211 continue;
1212
1213 // the target before the mark, which the compiler takes for a
1214 // store that could change the iterator
1215 auto g_tgt = it.get_tgt_node_ne();
1216 ARC_BITS(arc).set_bit(Build_Subtree, true);
1217 auto sg_tgt = mapped_node<GT>(g_tgt);
1218 if (sg_tgt == nullptr) // sg_tgt mapped in sg?
1219 {
1220 sg_tgt = sgr.insert_node(g_tgt->get_info());
1221 GT::map_nodes(g_tgt, sg_tgt);
1222 }
1223
1224 auto sg_arc = sgr.insert_arc(sg_src, sg_tgt, arc->get_info());
1225 GT::map_arcs(arc, sg_arc);
1226
1227 if (not IS_NODE_VISITED(g_tgt, Build_Subtree))
1228 return g_tgt;
1229 }
1230 return nullptr;
1231 }
1232 };
1233
1234 // A path can have as many nodes as the graph, too many for recursion,
1235 // so that the search keeps its own stack; the order of the visits is
1236 // that of a recursion. The node being visited keeps its iterator in
1237 // here until it visits a node; then it waits in frames, where it goes
1238 // on when it comes back.
1239 Dfs_Stack<Frame> frames;
1240 Frame here = Frame::visit(g, sg, g_src, node_count);
1241 while (true)
1242 {
1243 if (auto g_tgt = here.next_to_visit(sg))
1244 { // the iterator stays on the arc to g_tgt, as in a recursion
1245 frames.push(std::move(here));
1246 here = Frame::visit(g, sg, g_tgt, node_count);
1247 continue;
1248 }
1249
1250 while (true) // back to the nodes that wait
1251 {
1252 if (frames.is_empty())
1253 return;
1254
1255 Frame & f = frames.top();
1256 f.next_arc(); // past the arc to the node that was visited
1257 if (auto g_tgt = f.next_to_visit(sg))
1258 {
1259 here = Frame::visit(g, sg, g_tgt, node_count);
1260 break;
1261 }
1262 frames.pop();
1263 }
1264 }
1265 }
1266
1267
1269 template <AlephGraph GT> inline static
1270 bool __find_depth_first_spanning_tree(const GT & g,
1271 typename GT::Node * gnode,
1272 typename GT::Arc * garc,
1273 GT & tree,
1274 typename GT::Node * tnode);
1275
1295 template <AlephGraph GT> inline
1296 GT find_depth_first_spanning_tree(const GT & g, typename GT::Node * gnode)
1297 {
1298 g.reset_nodes();
1299 g.reset_arcs();
1300
1301 GT tree;
1302
1303 NODE_BITS(gnode).set_bit(Spanning_Tree, true);
1304
1305 auto tnode = tree.insert_node(gnode->get_info());
1306 GT::map_nodes(gnode, tnode);
1307
1308 for (auto it = g.get_arc_it(gnode); it.has_curr(); it.next_ne())
1309 {
1310 auto arc = it.get_current_arc_ne();
1311 if (IS_ARC_VISITED(arc, Spanning_Tree))
1312 continue;
1313
1314 auto arc_tgt_node = it.get_tgt_node_ne();
1315 if (IS_NODE_VISITED(arc_tgt_node, Spanning_Tree))
1316 continue;
1317
1318 if (__find_depth_first_spanning_tree(g, arc_tgt_node, arc, tree, tnode))
1319 return tree;
1320 }
1321
1322 return tree;
1323 }
1324
1326 template <AlephGraph GT> inline
1328 {
1330 }
1331
1332
1333 template <AlephGraph GT> inline static
1334 bool __find_depth_first_spanning_tree(const GT & g, typename GT::Node * gnode,
1335 typename GT::Arc * garc,
1336 GT & tree, typename GT::Node * tnode)
1337 {
1338 // A node of the tree that waits for the one that it visits, its image
1339 // in the tree, and the next arc of it to look at
1340 struct Frame
1341 {
1342 typename GT::Node * tnode;
1343 typename GT::Node_Arc_Iterator it;
1344
1345 [[gnu::always_inline]] ALEPH_FLATTEN
1346 Frame(const GT & gr, typename GT::Node * p, typename GT::Node * image)
1347 : tnode(image), it(gr.get_arc_it(p)) {}
1348
1349 [[gnu::always_inline]] ALEPH_FLATTEN
1350 void next_arc() noexcept(noexcept(it.next_ne())) { it.next_ne(); }
1351
1352 // Moves the iterator to the next arc not in the tree whose target is
1353 // not in the tree either, and gives that target, or nullptr when the
1354 // arcs are done
1355 [[gnu::always_inline]] ALEPH_FLATTEN typename GT::Node * next_target()
1356 noexcept(noexcept(it.next_ne()))
1357 {
1358 for (; it.has_curr(); it.next_ne())
1359 {
1360 auto arc = it.get_current_arc_ne();
1361 if (IS_ARC_VISITED(arc, Spanning_Tree))
1362 continue;
1363
1364 auto arc_tgt_node = it.get_tgt_node_ne();
1365 if (not IS_NODE_VISITED(arc_tgt_node, Spanning_Tree))
1366 return arc_tgt_node;
1367 }
1368 return nullptr;
1369 }
1370 };
1371
1372 // Puts in the tree p, reached through a from the node whose image is
1373 // t, and gives the image of p, or nullptr if the tree is complete
1374 struct Tree
1375 {
1376 [[gnu::always_inline]] static typename GT::Node *
1377 put(const GT & gr, GT & tr, typename GT::Node * p, typename GT::Arc * a,
1378 typename GT::Node * t)
1379 {
1380 NODE_BITS(p).set_bit(Spanning_Tree, true);
1381 ARC_BITS(a).set_bit(Spanning_Tree, true);
1382
1383 auto tree_tgt_node = tr.insert_node(p->get_info());
1384 GT::map_nodes(p, tree_tgt_node);
1385
1386 auto tarc = tr.insert_arc(t, tree_tgt_node, a->get_info());
1387 GT::map_arcs(a, tarc);
1388
1389 if (tr.get_num_nodes() == gr.get_num_nodes())
1390 return nullptr;
1391
1392 assert(tr.get_num_nodes() > tr.get_num_arcs()); // tree invariant
1393 return tree_tgt_node;
1394 }
1395 };
1396
1397 // A path can have as many nodes as the graph, too many for recursion,
1398 // so that the search keeps its own stack; the order of the visits is
1399 // that of a recursion. The node being visited keeps its iterator in
1400 // here until it visits a node; then it waits in frames, where it goes
1401 // on when it comes back.
1402 auto image = Tree::put(g, tree, gnode, garc, tnode);
1403 if (image == nullptr)
1404 return true;
1405
1406 Dfs_Stack<Frame> frames;
1407 Frame here(g, gnode, image);
1408 while (true)
1409 {
1410 if (auto tgt = here.next_target())
1411 { // the iterator stays on the arc to tgt, as in a recursion
1412 image = Tree::put(g, tree, tgt, here.it.get_current_arc_ne(), here.tnode);
1413 if (image == nullptr)
1414 return true; // spanning tree completed
1415
1416 frames.push(std::move(here));
1417 here = Frame(g, tgt, image);
1418 continue;
1419 }
1420
1421 while (true) // back to the nodes that wait
1422 {
1423 if (frames.is_empty())
1424 return false;
1425
1426 Frame & f = frames.top();
1427 f.next_arc(); // past the arc to the node that was visited
1428 if (auto tgt = f.next_target())
1429 {
1430 image = Tree::put(g, tree, tgt, f.it.get_current_arc_ne(), f.tnode);
1431 if (image == nullptr)
1432 return true; // spanning tree completed
1433
1434 here = Frame(g, tgt, image);
1435 break;
1436 }
1437 frames.pop();
1438 }
1439 }
1440 }
1441
1460 template <AlephGraph GT> inline
1462 {
1465
1466 GT tree;
1467
1468 // insert_node() frees the node if it cannot take it
1469 auto tp = tree.insert_node(new typename GT::Node(gp));
1470 NODE_COOKIE(gp) = nullptr; // else it would compose with a former tree
1471 GT::map_nodes(gp, tp);
1472 NODE_BITS(gp).set_bit(Spanning_Tree, true);
1473
1475 for (auto it = g.get_arc_it(gp); it.has_curr(); it.next_ne())
1476 q.put(it.get_curr());
1477
1478 while (not q.is_empty())
1479 {
1480 auto garc = q.get();
1481 ARC_BITS(garc).set_bit(Spanning_Tree, true);
1482 auto gsrc = g.get_src_node(garc);
1483 auto gtgt = g.get_tgt_node(garc);
1484
1487 continue;
1488
1489 if (IS_NODE_VISITED(gtgt, Spanning_Tree)) // gtgt visited?
1490 std::swap(gsrc, gtgt); // make gsrc the visited endpoint
1491
1492 auto tsrc = mapped_node<GT>(gsrc);
1493 NODE_BITS(gtgt).set_bit(Spanning_Tree, true);
1494
1495 // Create and map the new tree node.
1496 auto ttgt = tree.insert_node(new typename GT::Node(gtgt));
1497 NODE_COOKIE(gtgt) = nullptr; // else it would compose with a former tree
1498 GT::map_nodes(gtgt, ttgt);
1499
1500 // Insert and map the new tree arc.
1501 auto tarc = tree.insert_arc(tsrc, ttgt, garc->get_info());
1502 ARC_COOKIE(garc) = nullptr;
1503 GT::map_arcs(garc, tarc);
1504 if (tree.get_num_nodes() == g.get_num_nodes())
1505 break;
1506
1507 // Enqueue arcs incident to the newly visited node.
1508 for (auto it = g.get_arc_it(gtgt); it.has_curr(); it.next_ne())
1509 {
1510 auto current_arc = it.get_current_arc_ne();
1511 if (IS_ARC_VISITED(current_arc, Spanning_Tree))
1512 continue;
1513
1514 if (IS_NODE_VISITED(g.get_src_node(current_arc),Spanning_Tree) and
1516 continue;
1517 q.put(current_arc);
1518 }
1519 }
1520
1521 return tree;
1522 }
1523
1542 template <AlephGraph GT>
1544 {
1545 using Node = typename GT::Node;
1546 using Arc = typename GT::Arc;
1547
1548 GT ret;
1550 arcs.for_each([&table, &ret] (Arc * ga)
1551 {
1552 if (ga == nullptr)
1553 return;
1554
1555 Node * gsrc = static_cast<Node *>(ga->src_node);
1556 Node * gtgt = static_cast<Node *>(ga->tgt_node);
1557
1558 Node * tsrc;
1559 auto * pair_ptr = table.search(gsrc);
1560 if (pair_ptr)
1561 tsrc = pair_ptr->second;
1562 else
1563 {
1564 tsrc = ret.insert_node(gsrc->get_info());
1565 table.insert(gsrc, tsrc);
1566 NODE_COOKIE(tsrc) = gsrc;
1567 }
1568
1569 Node * ttgt;
1570 pair_ptr = table.search(gtgt);
1571 if (pair_ptr)
1572 ttgt = pair_ptr->second;
1573 else
1574 {
1575 ttgt = ret.insert_node(gtgt->get_info());
1576 table.insert(gtgt, ttgt);
1577 NODE_COOKIE(ttgt) = gtgt;
1578 }
1579
1580 Arc * ta = ret.insert_arc(tsrc, ttgt);
1581 *ta = *ga;
1582 ARC_COOKIE(ta) = ga;
1583 });
1584
1585 return ret;
1586 }
1587
1589 template <AlephGraph GT> inline static
1590 long & df(typename GT::Node * p)
1591 {
1592 return NODE_COUNTER(p);
1593 }
1594
1597 template <AlephGraph GT> inline static
1598 long & low(typename GT::Node * p)
1599 {
1600 return reinterpret_cast<long&>(NODE_COOKIE(p));
1601 }
1602
1603 namespace graph_utils_detail
1604 {
1610 template <class GT>
1612 {
1613 typename GT::Node * p;
1614 typename GT::Arc * a;
1615 typename GT::Node_Arc_Iterator it;
1616 long low;
1617 bool p_is_cut_node = false;
1618
1619 [[gnu::always_inline]] ALEPH_FLATTEN
1620 Cut_Frame(const GT & g, typename GT::Node * n, typename GT::Arc * arc,
1621 const long n_low)
1622 : p(n), a(arc), it(g.get_arc_it(n)), low(n_low) {}
1623
1624 [[gnu::always_inline]] ALEPH_FLATTEN
1625 void next_arc() noexcept(noexcept(it.next_ne())) { it.next_ne(); }
1626 };
1627
1628 // Visits n, reached through arc, and pushes its frame
1629 template <class GT>
1630 [[gnu::always_inline]] inline Cut_Frame<GT> *
1631 visit_cut(const GT & g, Dfs_Stack<Cut_Frame<GT>> & frames,
1632 typename GT::Node * n, typename GT::Arc * arc, long & curr_df)
1633 {
1634 NODE_BITS(n).set_bit(Depth_First, true);
1635 const long n_df = df<GT>(n) = curr_df++;
1636 return &frames.push(g, n, arc, n_df); // its low value starts as it
1637 }
1638
1639 // Moves the iterator of f to the next arc of f.p that goes to a node
1640 // not visited, which it marks, and gives that node, or nullptr when
1641 // the arcs of f.p are done. The arcs to nodes visited lower f.low.
1642 template <class GT>
1643 [[gnu::always_inline]] ALEPH_FLATTEN inline typename GT::Node *
1644 next_cut_child(Cut_Frame<GT> & f) noexcept(noexcept(f.it.next_ne()))
1645 {
1646 for (; f.it.has_curr(); f.it.next_ne())
1647 {
1648 auto arc = f.it.get_current_arc_ne();
1649 if (arc == f.a)
1650 continue; // parent arc
1651
1652 auto tgt = f.it.get_tgt_node_ne();
1653 if (IS_NODE_VISITED(tgt, Depth_First))
1654 {
1655 if (not IS_ARC_VISITED(arc, Depth_First))
1656 if (df<GT>(tgt) < f.low)
1657 f.low = df<GT>(tgt);
1658
1659 continue;
1660 }
1661
1662 if (IS_ARC_VISITED(arc, Depth_First))
1663 continue;
1664
1665 ARC_BITS(arc).set_bit(Depth_First, true);
1666 return tgt;
1667 }
1668 return nullptr;
1669 }
1670 } // namespace graph_utils_detail
1671
1673 template <AlephGraph GT> inline static
1676 typename GT::Node * p, typename GT::Arc * a,
1677 long & curr_df);
1678
1703 template <AlephGraph GT>
1705 compute_cut_nodes(const GT & g, typename GT::Node * start)
1706 {
1708
1710 << "compute_cut_nodes() does not work on digraphs";
1711
1712 if (g.get_num_nodes() == 0)
1713 return list;
1714
1715 ah_invalid_argument_if(start == nullptr)
1716 << "compute_cut_nodes(): start must be non-null";
1717
1718 g.for_each_node([](auto p)
1719 {
1720 NODE_COUNTER(p) = 0;
1721 NODE_BITS(p).reset();
1722 NODE_COOKIE(p) = nullptr;
1723 });
1724
1725 g.reset_arcs();
1726 long current_df = 0;
1727 NODE_BITS(start).set_bit(Depth_First, true);
1728 df<GT>(start) = current_df++;
1729 int call_counter = 0;
1731
1732 // Traverse arcs incident to `start`.
1733 for (auto it = g.get_arc_it(start);
1734 it.has_curr() and current_df < g.get_num_nodes(); it.next_ne())
1735 {
1736 auto tgt = it.get_tgt_node_ne();
1737 if (IS_NODE_VISITED(tgt, Depth_First))
1738 continue;
1739
1740 auto arc = it.get_current_arc_ne();
1741 if (IS_ARC_VISITED(arc, Depth_First))
1742 continue;
1743
1744 ARC_BITS(arc).set_bit(Depth_First, true);
1745 __compute_cut_nodes(g, list, frames, tgt, arc, current_df);
1746 ++call_counter;
1747 }
1748
1749 // Root is an articulation point iff it has more than one DFS child.
1750 if (call_counter > 1)
1751 {
1752 NODE_BITS(start).set_bit(Cut, true);
1753 list.append(start);
1754 }
1755
1756 return list;
1757 }
1758
1760 template <AlephGraph GT>
1762 {
1763 return compute_cut_nodes(g, g.get_node());
1764 }
1765
1773 template <AlephGraph GT> inline static
1776 typename GT::Node * p, typename GT::Arc * a,
1777 long & curr_df)
1778 {
1782
1783 Frame * f = visit_cut(g, frames, p, a, curr_df); // the top one
1784 while (true)
1785 {
1786 if (auto tgt = next_cut_child(*f))
1787 { // the iterator of f stays on the arc to tgt, as in a recursion
1788 f = visit_cut(g, frames, tgt, f->it.get_current_arc_ne(), curr_df);
1789 continue;
1790 }
1791
1792 auto child = f->p;
1793 const long child_low = f->low;
1794 if (f->p_is_cut_node)
1795 {
1796 NODE_BITS(child).set_bit(Cut, true);
1797 list.append(child);
1798 }
1799 frames.pop();
1800 if (frames.is_empty())
1801 return;
1802
1803 f = &frames.top(); // the node from which child was visited
1804 if (child_low < f->low)
1805 f->low = child_low;
1806
1807 if (child_low >= df<GT>(f->p) and df<GT>(child) != 0)
1808 f->p_is_cut_node = true;
1809
1810 f->next_arc(); // past the arc to child
1811 }
1812 }
1813
1818 constexpr long Cross_Arc = -1;
1819
1821 template <AlephGraph GT> inline static
1822 bool is_a_cross_arc(typename GT::Arc * a)
1823 {
1824 return ARC_COUNTER(a) == Cross_Arc;
1825 }
1826
1828 template <AlephGraph GT> inline static
1829 bool is_a_cut_node(typename GT::Node * p)
1830 {
1831 return NODE_BITS(p).get_bit(Cut);
1832 }
1833
1835 template <AlephGraph GT> inline static
1836 bool is_an_cut_arc(typename GT::Arc * a)
1837 {
1838 return ARC_BITS(a).get_bit(Cut);
1839 }
1840
1842 template <AlephGraph GT> inline static
1843 bool is_node_painted(typename GT::Node * p)
1844 {
1845 return NODE_COUNTER(p) > 0;
1846 }
1847
1849 template <AlephGraph GT> inline static
1850 bool is_arc_painted(typename GT::Arc * arc)
1851 {
1852 return ARC_COUNTER(arc) > 0;
1853 }
1854
1856 template <AlephGraph GT> inline static
1857 void paint_node(typename GT::Node * p, const long & color)
1858 {
1859 NODE_COUNTER(p) = color;
1860 }
1861
1863 template <AlephGraph GT> inline static
1864 void paint_arc(typename GT::Arc * a, const long & color)
1865 {
1866 ARC_COUNTER(a) = color;
1867 }
1868
1870 template <AlephGraph GT> inline static
1871 const long & get_color(typename GT::Node * p)
1872 {
1873 return NODE_COUNTER(p);
1874 }
1875
1877 template <AlephGraph GT> inline static
1878 const long & get_color(typename GT::Arc * a)
1879 {
1880 return ARC_COUNTER(a);
1881 }
1882
1883 namespace graph_utils_detail
1884 {
1886 template <class GT>
1888 {
1889 typename GT::Node_Arc_Iterator it;
1890
1891 [[gnu::always_inline]] ALEPH_FLATTEN
1892 Paint_Frame(const GT & g, typename GT::Node * n) : it(g.get_arc_it(n)) {}
1893
1894 [[gnu::always_inline]] ALEPH_FLATTEN
1895 void next_arc() noexcept(noexcept(it.next_ne())) { it.next_ne(); }
1896
1897 // Moves the iterator to the next arc that goes to a node that is not
1898 // a cut node and was not painted, and paints both; gives that node,
1899 // or nullptr when the arcs are done
1900 [[gnu::always_inline]] ALEPH_FLATTEN
1901 typename GT::Node * next_to_paint(const long color) noexcept(noexcept(it.next_ne()))
1902 {
1903 for (; it.has_curr(); it.next_ne())
1904 {
1905 auto arc = it.get_current_arc_ne();
1906 if (is_arc_painted <GT> (arc))
1907 continue;
1908
1909 auto tgt = it.get_tgt_node_ne();
1910 if (is_a_cut_node <GT> (tgt))
1911 continue;
1912
1913 paint_arc <GT> (arc, color);
1914 if (is_node_painted <GT> (tgt))
1915 continue;
1916
1917 paint_node <GT> (tgt, color);
1918 return tgt;
1919 }
1920 return nullptr;
1921 }
1922 };
1923 } // namespace graph_utils_detail
1924
1931 template <AlephGraph GT> inline static
1932 void __paint_subgraph(const GT & g,
1934 typename GT::Node * p, long current_color)
1935 {
1936 assert(not is_a_cut_node <GT> (p));
1937
1938 if (is_node_painted <GT> (p))
1939 return;
1940
1941 paint_node <GT> (p, current_color);
1942
1944
1945 // The node being visited keeps its iterator in here until it visits a
1946 // node; then it waits in frames, where it goes on when it comes back
1947 Frame here(g, p);
1948 while (true)
1949 {
1950 if (auto tgt = here.next_to_paint(current_color))
1951 { // the iterator stays on the arc to tgt, as in a recursion
1952 frames.push(std::move(here));
1953 here = Frame(g, tgt);
1954 continue;
1955 }
1956
1957 while (true) // back to the nodes that wait
1958 {
1959 if (frames.is_empty())
1960 return;
1961
1962 Frame & f = frames.top();
1963 f.next_arc(); // past the arc to the node that was visited
1964 if (auto tgt = f.next_to_paint(current_color))
1965 {
1966 here = Frame(g, tgt);
1967 break;
1968 }
1969 frames.pop();
1970 }
1971 }
1972 }
1973
1975 template <AlephGraph GT> inline static
1976 void
1979 typename GT::Node * p, long & current_color)
1980 {
1981 assert(is_a_cut_node <GT> (p));
1982
1983 // Paint each adjacent non-cut block with a new color.
1984 for (auto it = g.get_arc_it(p); it.has_curr(); it.next_ne())
1985 {
1986 auto arc = it.get_current_arc_ne();
1987
1988 assert(not is_arc_painted <GT> (arc));
1989
1990 auto tgt_node = it.get_tgt_node_ne();
1991 if (is_a_cut_node <GT> (tgt_node)) // cut-to-cut arc
1992 {
1993 ARC_BITS(arc).set_bit(Cut, true);
1994 continue;
1995 }
1996 paint_arc<GT>(arc, Cross_Arc);
1997 if (is_node_painted<GT>(tgt_node))
1998 continue;
1999
2000 __paint_subgraph(g, frames, tgt_node, current_color);
2001
2002 ++current_color;
2003
2004 assert(not is_arc_painted <GT> (arc));
2005 }
2006 }
2007
2043 template <AlephGraph GT> inline long
2044 paint_subgraphs(const GT & g, const DynList<typename GT::Node*> & cut_node_list)
2045 {
2048 long current_color = 1;
2049
2051 for (auto it = cut_node_list.get_it(); it.has_curr(); it.next_ne())
2052 __paint_from_cut_node(g, frames, it.get_curr(), current_color);
2053
2054 return current_color;
2055 }
2056
2065 template <AlephGraph GT> inline static
2066 void __map_subgraph(const GT & g, GT & sg, typename GT::Node * gsrc,
2067 const long color)
2068 {
2069 assert(get_color <GT> (gsrc) == color);
2070
2071 // A node that waits for the one that the copy goes to, its image in
2072 // sg, and the next arc of it to look at
2073 struct Frame
2074 {
2075 typename GT::Node * tsrc;
2076 typename GT::Node_Arc_Iterator it;
2077
2078 [[gnu::always_inline]] ALEPH_FLATTEN
2079 Frame(const GT & gr, typename GT::Node * n)
2080 : tsrc(mapped_node<GT>(n)), it(gr.get_arc_it(n)) {}
2081
2082 [[gnu::always_inline]] ALEPH_FLATTEN
2083 void next_arc() noexcept(noexcept(it.next_ne())) { it.next_ne(); }
2084
2085 // Moves the iterator to the next arc of the color that was not
2086 // copied, and gives it, or nullptr when the arcs are done
2087 [[gnu::always_inline]] ALEPH_FLATTEN
2088 typename GT::Arc * next_to_map(const long c) noexcept(noexcept(it.next_ne()))
2089 {
2090 for (; it.has_curr(); it.next_ne())
2091 {
2092 auto garc = it.get_current_arc_ne();
2093 if (get_color<GT>(garc) != c or IS_ARC_VISITED(garc, Build_Subtree))
2094 continue;
2095
2096 return garc;
2097 }
2098 return nullptr;
2099 }
2100 };
2101
2102 // The node being visited keeps its iterator in here until the copy
2103 // goes on from a target of it; then it waits in frames until the copy
2104 // comes back to it, and goes on in here, where the iterator can stay
2105 // in registers
2106 Dfs_Stack<Frame> frames;
2107 Frame here(g, gsrc);
2108 while (true)
2109 {
2110 // Traverse arcs and copy those with the requested color.
2111 auto garc = here.next_to_map(color);
2112 if (garc == nullptr)
2113 {
2114 if (frames.is_empty())
2115 return;
2116
2117 here = std::move(frames.top()); // back to the node that waits
2118 frames.pop();
2119 here.next_arc(); // past the arc that was copied
2120 continue;
2121 }
2122
2123 // the target before the mark, which the compiler takes for a
2124 // store that could change the iterator
2125 auto gtgt = here.it.get_tgt_node_ne();
2126 ARC_BITS(garc).set_bit(Build_Subtree, true);
2127
2128 assert(get_color <GT> (gtgt) == color);
2129
2130 typename GT::Node * ttgt = nullptr; // image of gtgt in sg
2131 if (IS_NODE_VISITED(gtgt, Build_Subtree))
2132 ttgt = mapped_node<GT> (gtgt);
2133 else
2134 { // gtgt not in sg yet: copy & map it
2135 // insert_node() frees the node if it cannot take it
2136 ttgt = sg.insert_node(new typename GT::Node(gtgt));
2137 NODE_COOKIE(gtgt) = nullptr; // else it would compose with a former copy
2138 GT::map_nodes(gtgt, ttgt);
2139 NODE_BITS(gtgt).set_bit(Build_Subtree, true);
2140 }
2141
2142 auto tarc = sg.insert_arc(here.tsrc, ttgt, garc->get_info());
2143 ARC_COOKIE(garc) = nullptr; // else it would compose with a former copy
2144 GT::map_arcs(garc, tarc);
2145
2146 // the iterator stays on garc, as in a recursion
2147 frames.push(std::move(here));
2148 here = Frame(g, gtgt);
2149 }
2150 }
2151
2171 template <AlephGraph GT>
2172 GT map_subgraph(const GT & g, const long color)
2173 {
2176
2177 typename GT::Node * first = nullptr;
2178 for (auto it = g.get_node_it(); it.has_curr(); it.next_ne())
2179 if (get_color <GT> (it.get_current_node_ne()) == color)
2180 {
2181 first = it.get_current_node_ne();
2182 break;
2183 }
2184
2185 ah_domain_error_if(first == nullptr)
2186 << "Color does not exist in the graph";
2187
2188 GT sg;
2189 // insert_node() frees the node if it cannot take it
2190 auto tsrc = sg.insert_node(new typename GT::Node(first));
2191 NODE_COOKIE(first) = nullptr; // else it would compose with a former copy
2192 GT::map_nodes(first, tsrc);
2193 NODE_BITS(first).set_bit(Build_Subtree, true);
2194
2195 __map_subgraph(g, sg, first, color);
2196
2197 return sg;
2198 }
2199
2220 template <AlephGraph GT>
2221 std::tuple<GT, DynList<typename GT::Arc*>>
2222 map_cut_graph(const GT & g, const DynList<typename GT::Node*> & cut_node_list)
2223 {
2224 GT cut_graph;
2225 DynList<typename GT::Arc*> cross_arc_list;
2226
2227 for (auto it = cut_node_list.get_it(); it.has_curr(); it.next_ne())
2228 {
2229 auto gp = it.get_curr();
2230
2231 assert(is_a_cut_node <GT> (gp));
2232
2233 // insert_node() frees the node if it cannot take it
2234 auto tp = cut_graph.insert_node(new typename GT::Node(gp));
2235 NODE_COOKIE(gp) = nullptr; // else it would compose with a former copy
2236 GT::map_nodes(gp, tp);
2237 }
2238
2239 // cut_graph contains cut-to-cut arcs; cross_arc_list collects cut-to-noncut arcs.
2240 for (auto it = g.get_arc_it(); it.has_curr(); it.next_ne())
2241 {
2242 auto garc = it.get_current_arc_ne();
2243 if (is_a_cross_arc <GT> (garc))
2244 {
2245 cross_arc_list.append(garc);
2246 continue;
2247 }
2248
2249 if (not is_an_cut_arc <GT> (garc))
2250 continue;
2251
2252 auto src = mapped_node<GT>(g.get_src_node(garc));
2253 auto tgt = mapped_node<GT>(g.get_tgt_node(garc));
2254
2255 assert(src != nullptr and tgt != nullptr);
2256
2257 auto arc = cut_graph.insert_arc(src, tgt, garc->get_info());
2258 ARC_COOKIE(garc) = nullptr; // else it would compose with a former copy
2259 GT::map_arcs(garc, arc);
2260 }
2261
2262 // moved, so that the nodes and arcs that g maps to are those returned
2263 return { std::move(cut_graph), std::move(cross_arc_list) };
2264 }
2265
2266
2276 template <class GT, class Distance>
2278 {
2280
2281 Distance_Compare(Distance __dist = Distance()) : dist(__dist) { /* empty */ }
2282
2283 bool operator () (typename GT::Arc * a1, typename GT::Arc * a2) const
2284 {
2285 return dist(a1) < dist(a2);
2286 }
2287 };
2288
2289
2307 template <AlephGraph GT>
2309 {
2311 << "invert_digraph() requires a digraph (or a graph temporarily treated as a digraph)";
2312
2313 g.reset_nodes();
2314 g.reset_arcs();
2315 GT gi;
2316
2317 // Copy all nodes first so isolated vertices are preserved.
2318 for (auto it = g.get_node_it(); it.has_curr(); it.next_ne())
2319 {
2320 auto gp = it.get_curr();
2321 auto ip = gi.insert_node(gp->get_info());
2322 GT::map_nodes(gp, ip);
2323 }
2324
2325 for (auto it = g.get_arc_it(); it.has_curr(); it.next_ne())
2326 {
2327 auto arc = it.get_curr();
2328
2329 auto ssrc = g.get_src_node(arc);
2330 auto stgt = g.get_tgt_node(arc);
2331
2332 auto rsrc = mapped_node<GT>(ssrc);
2333 auto rtgt = mapped_node<GT>(stgt);
2334
2335 assert(rsrc != nullptr and rtgt != nullptr);
2336
2337 typename GT::Arc * ai = gi.insert_arc(rtgt, rsrc, arc->get_info());
2338 GT::map_arcs(arc, ai);
2339 }
2340
2341 assert(g.get_num_arcs() == gi.get_num_arcs() and
2342 g.get_num_nodes() == gi.get_num_nodes());
2343
2344 return gi;
2345 }
2346
2347
2357 template <AlephGraph GT, ArcFilter<GT> SA = Dft_Show_Arc<GT>>
2359 {
2360 SA sa;
2361
2362 public:
2363
2365 Invert_Digraph(SA __sa) : sa(__sa) { /* empty */ }
2366
2374 GT operator () (const GT & g) const
2375 {
2377 << "Invert_Digraph requires a digraph (or a graph temporarily treated as a digraph)";
2378
2379 g.reset_nodes();
2380 g.reset_arcs();
2381 GT gi;
2382
2383 // Copy all nodes first so isolated vertices are preserved.
2384 for (auto it = g.get_node_it(); it.has_curr(); it.next_ne())
2385 {
2386 auto gp = it.get_curr();
2387 auto ip = gi.insert_node(gp->get_info());
2388 GT::map_nodes(gp, ip);
2389 }
2390
2391 for (Arc_Iterator<GT, SA> it(g, sa); it.has_curr(); it.next_ne())
2392 {
2393 auto arc = it.get_curr();
2394
2395 auto ssrc = g.get_src_node(arc);
2396 auto stgt = g.get_tgt_node(arc);
2397
2398 auto rsrc = mapped_node<GT>(ssrc);
2399 auto rtgt = mapped_node<GT>(stgt);
2400
2401 assert(rsrc != nullptr and rtgt != nullptr);
2402
2403 typename GT::Arc * ai = gi.insert_arc(rtgt, rsrc, arc->get_info());
2404 GT::map_arcs(arc, ai);
2405 }
2406
2407 assert(g.get_num_nodes() == gi.get_num_nodes());
2408
2409 return gi;
2410 }
2411 };
2412
2421 template <class GT>
2423 {
2424 public:
2425
2426 typedef typename GT::Arc_Type Distance_Type;
2427
2429
2431
2432 Distance_Type & operator () (typename GT::Arc * a) const
2433 {
2434 return a->get_info();
2435 }
2436
2437 Distance_Type & operator () (typename GT::Arc * a, typename GT::Node*) const
2438 {
2439 return a->get_info();
2440 }
2441
2442 static void set_zero(typename GT::Arc * a) { a->get_info() = 0; }
2443 };
2444
2445 template <class GT>
2447 std::numeric_limits<typename Dft_Dist<GT>::Distance_Type>::max();
2448
2449 template <class GT>
2451 typename Dft_Dist<GT>::Distance_Type{};
2452
2453
2466 template <AlephGraph GT, ArcFilter<GT> SA = Dft_Show_Arc<GT>>
2467 typename GT::Arc *
2469 typename GT::Node *src, typename GT::Node *tgt,
2470 SA sa = SA()) noexcept(nothrow_arc_filter_v<GT, SA>)
2471 {
2472 assert(src != nullptr and tgt != nullptr);
2473
2474 // For efficiency, iterate over the node with fewer arcs
2475 if (not g.is_digraph() and tgt->num_arcs < src->num_arcs)
2476 std::swap(tgt, src);
2477
2478 typename GT::Arc * first_match = nullptr;
2479 for (Node_Arc_Iterator<GT, SA> it(src, sa); it.has_curr(); it.next_ne())
2480 {
2481 if (it.get_tgt_node_ne() != tgt)
2482 continue;
2483
2484 auto arc = it.get_current_arc_ne();
2485 // Prefer arc marked as spanning tree
2487 return arc;
2488
2489 // Keep first match as fallback
2490 if (first_match == nullptr)
2491 first_match = arc;
2492 }
2493
2494 return first_match; // Return first match if no spanning tree arc found
2495 }
2496
2513 template <AlephGraph GT, ArcDistance<GT> Distance = Dft_Dist<GT>>
2515 get_min_path(typename GT::Node * s, typename GT::Node * end, Path<GT> & path,
2516 Distance distance = Distance())
2517 {
2518 using Distance_Type = typename Distance::Distance_Type;
2519
2520 ah_invalid_argument_if(s == nullptr or end == nullptr)
2521 << "get_min_path(): s and end must be non-null";
2522
2523 Distance_Type dist{};
2524 path.empty();
2525
2526 if (s == end)
2527 {
2528 path.init(end);
2529 return dist;
2530 }
2531
2532 // Build path from end to start following cookies, collecting arcs
2534
2535 auto curr = end;
2536 while (curr != s)
2537 {
2538 auto prev = static_cast<typename GT::Node *>(NODE_COOKIE(curr));
2539 ah_domain_error_if(prev == nullptr)
2540 << "get_min_path(): broken cookie chain (nullptr)";
2541
2542 // Find the spanning tree arc between prev and curr
2543 auto arc = search_spanning_tree_arc<GT>(path.get_graph(), prev, curr);
2544 ah_domain_error_if(arc == nullptr)
2545 << "get_min_path(): no arc connecting nodes in path";
2546
2547 node_arc_list.insert(std::make_pair(curr, arc));
2548 curr = prev;
2549 }
2550
2551 // Now build path from start to end. The cost is added from the start
2552 // too, so that each partial sum is the distance of a node of the path:
2553 // with negative weights, a sum from the end could overflow although
2554 // every distance fits.
2555 path.init(s);
2556 for (auto it = node_arc_list.get_it(); it.has_curr(); it.next())
2557 {
2558 auto & [node, arc] = it.get_curr();
2559 (void)node; // suppress warning
2560 path.append(arc);
2561 dist += distance(arc);
2562 }
2563
2564 return dist;
2565 }
2566
2583 template <AlephGraph GT,
2584 ArcDistance<GT> Distance = Dft_Dist<GT>,
2585 ArcFilter<GT> SA = Dft_Show_Arc<GT>>
2587 {
2589 SA sa;
2591
2592 public:
2593
2594 Total_Cost(Distance __dist = Distance(), SA __sa = SA())
2595 : dist(__dist), sa(__sa)
2596 {
2597 // empty
2598 }
2599
2602 {
2603 sum = typename Distance::Distance_Type{};
2604
2605 // Traverse all arcs and sum their weights
2606 for (Arc_Iterator <GT, SA> it(g, sa); it.has_curr(); it.next_ne())
2607 sum += dist(it.get_current_arc_ne());
2608
2609 return sum;
2610 }
2611
2614 {
2615 return total_cost (g);
2616 }
2617
2619 void reset() noexcept
2620 {
2621 sum = typename Distance::Distance_Type{};
2622 }
2623
2625 typename Distance::Distance_Type value() const noexcept
2626 {
2627 return sum;
2628 }
2629
2630 bool operator () (typename GT::Arc * a)
2631 {
2632 if (not sa(a))
2633 return true; // skip but keep traversing
2634
2635 sum += dist(a);
2636 return true; // keep traversing
2637 }
2638 };
2639
2640
2641
2642} // end namespace Aleph
2643
2644# endif // TPL_GRAPH_UTILS_H
Exception handling system with formatted messages for Aleph-w.
#define ah_domain_error_unless(C)
Throws std::domain_error if condition does NOT hold.
Definition ah-errors.H:686
#define ah_domain_error_if(C)
Throws std::domain_error if condition holds.
Definition ah-errors.H:670
#define ah_invalid_argument_if(C)
Throws std::invalid_argument if condition holds.
Definition ah-errors.H:787
C++20 concepts for the protocol shared by graph algorithms.
WeightedDigraph::Node Node
WeightedDigraph::Arc Arc
List_Graph< Graph_Node< Node_Info >, Graph_Arc< Arc_Info > > GT
int num_nodes
Definition btreepic.C:410
Stateful breadth-first traversal functor.
Breadth_First_Traversal(SA __sa=SA())
Construct a traversal functor using the arc filter __sa.
size_t operator()(const GT &g, Operation op)
Traverse starting from the first node of the graph.
size_t bft(const GT &g, typename GT::Node *start, Operation &op)
Stateful depth-first traversal functor.
bool __dft(typename GT::Node *node, typename GT::Arc *arc=nullptr)
Depth_First_Traversal(SA __sa=SA())
Construct a traversal functor using the arc filter __sa.
bool enter(typename GT::Node *node, typename GT::Arc *arc)
size_t operator()(const GT &g, Operation op=Operation())
Traverse starting from the first node of the graph.
size_t dft(const GT &g, typename GT::Node *start_node, Operation &__op)
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.
Default distance accessor for arc weights.
static const Distance_Type Max_Distance
Distance_Type & operator()(typename GT::Arc *a) const
GT::Arc_Type Distance_Type
static const Distance_Type Zero_Distance
static void set_zero(typename GT::Arc *a)
Dynamic queue of elements of generic type T based on single linked list.
T & put(const T &data)
The type of element.
T get()
Remove the oldest item of the queue.
void empty() noexcept
Empty the queue.
bool is_empty() const noexcept
Return true if this is empty.
Doubly-linked list (defined in tpl_dynList.H).
Definition htlist.H:1231
T & append(const T &item)
Definition htlist.H:1362
T & insert(const T &item)
Definition htlist.H:1308
T & get_last()
Return the last item of the list.
Definition htlist.H:1529
Generic key-value map implemented on top of a binary search 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.
void next_ne() noexcept(shows_ne)
Advances the iterator to the next filtered element, as next() does.
Functor for computing the transposed digraph, filtering arcs.
Invert_Digraph(SA __sa)
Construct a functor using the arc filter __sa.
GT operator()(const GT &g) const
Compute the transposed graph.
Node * get_first_node() const
Return any node in the graph.
Definition tpl_graph.H:590
virtual Node * insert_node(Node *node) noexcept
Insertion of a node already allocated.
Definition tpl_graph.H:535
Arc * insert_arc(Node *src_node, Node *tgt_node, void *a)
Definition tpl_graph.H:618
typename Arc::Arc_Type Arc_Type
The type of data stored in the arc.
Definition tpl_graph.H:450
Path on a graph.
Definition tpl_graph.H:3832
void insert(Arc *arc)
Insert an arc as the first of a path.
Definition tpl_graph.H:4195
void empty()
Clean the path: all the nodes and arc are removed.
Definition tpl_graph.H:3999
void init(Node *start_node)
Set the first node of a path.
Definition tpl_graph.H:3947
void append(Arc *arc)
Append an arc to the path.
Definition tpl_graph.H:4052
const GT & get_graph() const noexcept
Get a constant reference to the graph.
Definition tpl_graph.H:3921
Compute the total cost (sum of arc weights) of a graph.
Distance::Distance_Type sum
Distance::Distance_Type operator()(GT &g)
See total_cost.
Distance::Distance_Type total_cost(GT &g)
Compute the total cost.
Total_Cost(Distance __dist=Distance(), SA __sa=SA())
Distance::Distance_Type value() const noexcept
Return the accumulated value (after using operator()(Arc*)).
void reset() noexcept
Reset the internal accumulator used by operator()(Arc*).
void for_each(Operation &operation) const
Traverse all the container and performs an operation on each element.
Definition ah-dry.H:1475
ArcInfo & get_info() noexcept
Return a modifiable reference to the arc data.
Definition graph-dry.H:854
NodeInfo & get_info() noexcept
Return a modifiable reference to the data contained in the node.
Definition graph-dry.H:753
size_t num_arcs
data associated to the node. Access it with get_info()
Definition graph-dry.H:713
void reset_bit_nodes(int bit) const noexcept
Reset bit to zero for all the nodes of graph.
Definition graph-dry.H:1326
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
constexpr size_t get_num_nodes() const noexcept
Return the total of nodes of graph.
Definition graph-dry.H:975
auto get_arc_it() const noexcept
Obtains an iterator to the arc of graph.
Definition graph-dry.H:3144
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_node() const
Return any node in the graph.
Definition graph-dry.H:994
void set_bit(Node *node, int bit, int value) const noexcept
Set the control bit of node to value
Definition graph-dry.H:1105
bool is_digraph() const noexcept
Return true if the graph this is directed.
Definition graph-dry.H:937
static void map_arcs(A1 *p, A2 *q) noexcept
Map the arcs through their cookies.
Definition graph-dry.H:1312
void reset_counter_nodes() const noexcept
Reset all the counters to zero for all the nodes of graph.
Definition graph-dry.H:1350
void reset_bit_arcs(int bit) const noexcept
Reset bit to zero for all the arcs of graph.
Definition graph-dry.H:1332
void for_each_node(Operation &operation) const
Unconditionally traverse all the nodes of graph and on each one perform an operation.
Definition graph-dry.H:1765
constexpr size_t get_num_arcs() const noexcept
Return the number of arcs of the graph.
Definition graph-dry.H:1064
void reset_counter_arcs() const noexcept
Reset all the counters to zero for all the arcs of graph.
Definition graph-dry.H:1356
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
auto get_node_it() const noexcept
Obtains an iterator to the nodes of graph.
Definition graph-dry.H:3122
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
auto get_it()
Return a properly initialized iterator positioned at the first item on the container.
Definition ah-dry.H:663
QuadTree - Hierarchical spatial index for 2D points.
Definition quadtree.H:126
__gmp_expr< typename __gmp_resolve_expr< T, V >::value_type, __gmp_binary_expr< __gmp_expr< T, U >, __gmp_expr< V, W >, __gmp_max_function > > max(const __gmp_expr< T, U > &expr1, const __gmp_expr< V, W > &expr2)
Definition gmpfrxx.h:4188
DynArray< Graph::Arc * > arcs
Definition graphpic.C:408
size_t depth_first_traversal(const GT &g, typename GT::Node *start_node, bool(*visit)(const GT &g, typename GT::Node *, typename GT::Arc *))
Depth-first traversal starting from a given node.
constexpr bool nothrow_arc_filter_v
Whether a filter of type SA of the arcs of GT is copied, moved and called without exceptions: then an...
Definition tpl_graph.H:1043
GT build_spanning_tree(const DynArray< typename GT::Arc * > &arcs)
Build a graph from a list of arcs (typically a spanning tree).
#define ARC_COOKIE(p)
Return the arc cookie
bool has_cycle(const GT &g)
Return true if an undirected graph has at least one cycle.
#define NODE_COUNTER(p)
Get the counter of a node.
DynList< GT > inconnected_components(const GT &g)
Forward declaration for inconnected_components().
bool test_for_cycle(const GT &g, typename GT::Node *src)
Search for a cycle reachable from a given node.
GT find_breadth_first_spanning_tree(GT &g, typename GT::Node *gp)
Build a breadth-first spanning tree (mapped to the original graph).
#define ARC_COUNTER(p)
Return the counter of arc p.
#define IS_NODE_VISITED(p, bit)
Determine whether the control bit is set or not to one.
GT invert_digraph(const GT &g)
Compute the transpose (arc-reversed) digraph.
bool test_connectivity(const GT &g)
Connectivity test for undirected graphs.
#define ARC_BITS(p)
Return the control bits of arc p.
#define NODE_COOKIE(p)
Return the node cookie
long paint_subgraphs(const GT &g, const DynList< typename GT::Node * > &cut_node_list)
Paint connected blocks around articulation points.
DynList< typename GT::Node * > compute_cut_nodes(const GT &g, typename GT::Node *start)
Compute articulation points (cut vertices) of an undirected graph.
Path< GT > find_path_breadth_first(const GT &g, typename GT::Node *start, typename GT::Node *end)
Breadth-first search of a (shortest-by-edges) path between two nodes.
bool test_for_path(const GT &g, typename GT::Node *start_node, typename GT::Node *end_node)
Return true if there is a path between two nodes.
size_t breadth_first_traversal(const GT &g, typename GT::Node *start, bool(*visit)(const GT &, typename GT::Node *, typename GT::Arc *))
Breadth-first traversal starting from a given node.
void build_subgraph(const GT &g, GT &sg, typename GT::Node *g_src, size_t &node_count)
Forward declaration for build_subgraph().
#define IS_ARC_VISITED(p, bit)
Determine whether the bit field is or not set to one.
GT find_depth_first_spanning_tree(const GT &g, typename GT::Node *gnode)
Build a depth-first spanning tree (mapped to the original graph).
bool is_graph_acyclique(const GT &g, typename GT::Node *start_node)
Return true if an undirected graph is acyclic.
std::tuple< GT, DynList< typename GT::Arc * > > map_cut_graph(const GT &g, const DynList< typename GT::Node * > &cut_node_list)
Extract the cut graph and cross-arc list.
#define NODE_BITS(p)
Get the control bits of a node.
GT map_subgraph(const GT &g, const long color)
Extract a mapped subgraph containing a given color.
#define ALEPH_FLATTEN
[[gnu::flatten]], or nothing where it would only make the compilation slower.
@ Depth_First
Definition aleph-graph.H:73
@ Build_Subtree
Definition aleph-graph.H:80
@ Breadth_First
Definition aleph-graph.H:74
@ Test_Cycle
Definition aleph-graph.H:75
@ Spanning_Tree
Definition aleph-graph.H:79
@ Find_Path
Definition aleph-graph.H:76
Cut_Frame< GT > * visit_cut(const GT &g, Dfs_Stack< Cut_Frame< GT > > &frames, typename GT::Node *n, typename GT::Arc *arc, long &curr_df)
GT::Node * next_cut_child(Cut_Frame< GT > &f) noexcept(noexcept(f.it.next_ne()))
Main namespace for Aleph-w library functions.
static bool __test_cycle(const GT &g, typename GT::Node *, typename GT::Node *)
Internal helper used by test_for_cycle().
static void __paint_subgraph(const GT &g, Dfs_Stack< graph_utils_detail::Paint_Frame< GT > > &frames, typename GT::Node *p, long current_color)
Internal DFS that paints a non-cut block with current_color.
static void paint_arc(typename GT::Arc *a, const long &color)
Set the arc color (stored in ARC_COUNTER).
static long & low(typename GT::Node *p)
Internal helper: low-link value stored directly in NODE_COOKIE(p).
static bool is_node_painted(typename GT::Node *p)
Return true if the node has a positive color.
static const long & get_color(typename GT::Node *p)
Return the node color (stored in NODE_COUNTER).
static bool __test_for_path(const GT &g, typename GT::Node *curr_node, typename GT::Node *end_node)
Internal DFS used by test_for_path().
static void paint_node(typename GT::Node *p, const long &color)
Set the node color (stored in NODE_COUNTER).
static bool is_a_cross_arc(typename GT::Arc *a)
Return true if the arc is marked as a cross-arc by paint_subgraphs().
static void __paint_from_cut_node(const GT &g, Dfs_Stack< graph_utils_detail::Paint_Frame< GT > > &frames, typename GT::Node *p, long &current_color)
Internal step that paints all blocks adjacent to a cut node.
and
Check uniqueness with explicit hash + equality functors.
static bool is_arc_painted(typename GT::Arc *arc)
Return true if the arc has a positive color.
static long & df(typename GT::Node *p)
Internal helper: DFS discovery time stored in NODE_COUNTER(p).
static bool is_an_cut_arc(typename GT::Arc *a)
Return true if the arc is marked as a cut-arc (between cut nodes).
constexpr long Cross_Arc
Special marker for arcs connecting a cut node to a non-cut block.
static bool __is_graph_acyclique(const GT &g, typename GT::Node *curr_node)
Internal DFS used by is_graph_acyclique().
Distance::Distance_Type get_min_path(typename GT::Node *s, typename GT::Node *end, Path< GT > &path, Distance distance=Distance())
Extracts a shortest path from a graph painted by a shortest paths algorithm.
static void __map_subgraph(const GT &g, GT &sg, typename GT::Node *gsrc, const long color)
Internal step used by map_subgraph().
static bool __depth_first_traversal(const GT &g, typename GT::Node *node, typename GT::Arc *arc, bool(*visit)(const GT &g, typename GT::Node *, typename GT::Arc *), size_t &count)
Internal DFS used by depth_first_traversal().
static bool is_a_cut_node(typename GT::Node *p)
Return true if the node is marked as an articulation point.
GT::Arc * search_spanning_tree_arc(const GT &g, typename GT::Node *src, typename GT::Node *tgt, SA sa=SA()) noexcept(nothrow_arc_filter_v< GT, SA >)
Helper to find the spanning tree arc between two nodes in a multigraph.
static void __compute_cut_nodes(const GT &g, DynList< typename GT::Node * > &list, Dfs_Stack< graph_utils_detail::Cut_Frame< GT > > &frames, typename GT::Node *p, typename GT::Arc *a, long &curr_df)
Internal DFS step used by compute_cut_nodes().
static bool __find_depth_first_spanning_tree(const GT &g, typename GT::Node *gnode, typename GT::Arc *garc, GT &tree, typename GT::Node *tnode)
Internal DFS used by find_depth_first_spanning_tree().
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
static long & color(typename GT::Node *p)
Itor::difference_type count(const Itor &beg, const Itor &end, const T &value)
Count elements equal to a value.
Definition ahAlgo.H:127
Filtered iterator on all the arcs of a graph.
Definition tpl_graph.H:1200
Default visit operation for traversals (never stops).
bool operator()(const GT &, typename GT::Node *, typename GT::Arc *)
Always continues the traversal (never stops early).
The next arc of a node to look at, for a depth-first search that keeps only that in a frame.
It it
The arcs of the node, on the next one to look at.
void next_arc() noexcept(noexcept(it.next_ne()))
Moves to the next arc; inlined, with all it calls.
Default filter for filtered iterators on arcs.
Definition tpl_graph.H:1024
Comparison functor for arc weights/distances.
Distance_Compare(Distance __dist=Distance())
bool operator()(typename GT::Arc *a1, typename GT::Arc *a2) const
Filtered iterator of adjacent arcs of a node.
Definition tpl_graph.H:1155
A node that waits for the one that it visits, the arc through which it was visited,...
Cut_Frame(const GT &g, typename GT::Node *n, typename GT::Arc *arc, const long n_low)
void next_arc() noexcept(noexcept(it.next_ne()))
The next arc of a node of a block being painted.
void next_arc() noexcept(noexcept(it.next_ne()))
Paint_Frame(const GT &g, typename GT::Node *n)
GT::Node * next_to_paint(const long color) noexcept(noexcept(it.next_ne()))
Distance accessor.
static long counter
Definition test-splice.C:40
Array-based graph implementation.
Stack of the frames of a depth-first search that does not recurse.
Dynamic queue implementation based on linked lists.