Aleph-w 7.10.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
tpl_cut_nodes.H
Go to the documentation of this file.
1/*
2 Aleph_w
3
4 Data structures & Algorithms
5 version 2.0.0b
6 https://github.com/lrleon/Aleph-w
7
8 This file is part of Aleph-w library
9
10 Copyright (c) 2002-2026 Leandro Rabindranath Leon
11
12 Permission is hereby granted, free of charge, to any person obtaining a copy
13 of this software and associated documentation files (the "Software"), to deal
14 in the Software without restriction, including without limitation the rights
15 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
16 copies of the Software, and to permit persons to whom the Software is
17 furnished to do so, subject to the following conditions:
18
19 The above copyright notice and this permission notice shall be included in all
20 copies or substantial portions of the Software.
21
22 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
25 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
27 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
28 SOFTWARE.
29*/
30
31
87# ifndef TPL_CUT_NODES_H
88# define TPL_CUT_NODES_H
89
90# include <ah-graph-concepts.H>
91
92# include <algorithm>
93
94# include <tpl_dfs_stack.H>
95# include <tpl_graph_utils.H>
96
97namespace Aleph
98{
155 template <AlephGraph GT, ArcFilter<GT> SA = Dft_Show_Arc<GT>>
157 {
158 SA sa;
159 GT *gptr = nullptr;
161 long curr_df = 0;
162 long curr_color = 1;
163
165
166 // Whether a copy made since the cut nodes were computed marked nodes
167 // and arcs with Build_Subtree
168 bool copies_marked = false;
169
170 // A node that waits for the one that it visits, the arc through which
171 // it was visited, the next arc of it to look at, its low value, and
172 // whether it is a cut node of what was seen. The low value is needed
173 // only while the node is in the stack, so it is kept here instead of
174 // in the cookie of the node.
176 {
177 typename GT::Node *p;
178 typename GT::Arc *a;
180 long low;
181 bool p_is_cut_node = false;
182
183 [[gnu::always_inline]] ALEPH_FLATTEN
184 Cut_Frame(typename GT::Node *n, typename GT::Arc *arc, SA &sa,
185 const long n_low)
186 : p(n), a(arc), it(n, sa), low(n_low) {}
187
188 [[gnu::always_inline]] ALEPH_FLATTEN
189 void next_arc() noexcept(noexcept(it.next_ne())) { it.next_ne(); }
190 };
191
192 // Visits p, reached through a, and pushes its frame
193 [[gnu::always_inline]]
195 typename GT::Node *p, typename GT::Arc *a)
196 {
197 NODE_BITS(p).set_bit(Depth_First, true); // mark p as visited
198 const long p_df = df<GT>(p) = curr_df++; // assign df number
199 return &frames.push(p, a, sa, p_df); // its low value starts as it
200 }
201
202 // Moves the iterator of f to the next arc of f.p that goes to a node
203 // not visited, which it marks, and gives that node, or nullptr when the
204 // arcs of f.p are done. The arcs to nodes visited lower f.low, which is
205 // kept in a local and stored once.
206 [[gnu::always_inline]] ALEPH_FLATTEN
207 typename GT::Node *next_child(Cut_Frame &f) noexcept(noexcept(f.it.next_ne()))
208 {
209 long p_low = f.low;
210 typename GT::Node *child = nullptr;
211 for (; f.it.has_curr(); f.it.next_ne())
212 {
213 auto arc = f.it.get_curr_ne();
214 if (arc == f.a)
215 continue; // a is the parent arc ==> ignore it
216
217 auto tgt = f.it.get_tgt_node_ne();
219 {
220 if (not IS_ARC_VISITED(arc, Depth_First)) // non-tree arc?
221 p_low = std::min(df<GT>(tgt), p_low);
222 continue;
223 }
224
225 if (IS_ARC_VISITED(arc, Depth_First))
226 continue;
227
228 ARC_BITS(arc).set_bit(Depth_First, true); // mark arc
229 child = tgt;
230 break;
231 }
232 f.low = p_low;
233 return child;
234 }
235
236 // Depth-first search from p, reached through a, that appends to the
237 // list the cut nodes below the root. A path can have as many nodes as
238 // the graph, too many for recursion, so that the search keeps its own
239 // stack; the order of the visits is that of a recursion.
241 typename GT::Node *p, typename GT::Arc *a)
242 {
243 Cut_Frame *f = visit(frames, p, a); // the top one
244 while (true)
245 {
246 if (auto tgt = next_child(*f))
247 { // the iterator of f stays on the arc to tgt, as in a recursion
248 f = visit(frames, tgt, f->it.get_curr_ne());
249 continue;
250 }
251
252 // at this point, f->p has already been explored
253 auto child = f->p;
254 const long child_low = f->low;
255 if (f->p_is_cut_node)
256 {
257 NODE_BITS(child).set_bit(Cut, true);
258 list_ptr->append(child);
259 }
260 frames.pop();
261 if (frames.is_empty())
262 return;
263
264 f = &frames.top(); // the node from which child was visited
265 f->low = std::min(child_low, f->low);
266 if (child_low >= df<GT>(f->p) and df<GT>(child) != 0) // cut node?
267 f->p_is_cut_node = true;
268 f->next_arc(); // past the arc to child
269 }
270 }
271
272 public:
285 void cut_nodes(typename GT::Node *start,
287 {
288 curr_df = 0; // global visit counter
289 list_ptr = &list;
290
291 list_ptr->empty();
292
293 gptr->for_each_node([](auto p) // initialize nodes
294 {
295 NODE_COUNTER(p) = 0;
296 NODE_BITS(p).reset();
297 NODE_COOKIE(p) = nullptr;
298 });
299 gptr->reset_arcs();
300 copies_marked = false; // the bits were reset
301
302 NODE_BITS(start).set_bit(Depth_First, true); // mark start
303 df<GT>(start) = curr_df++;
304
305 int call_counter = 0; // number of children of start
307
308 // Traverse arcs from start while the graph is not fully spanned
309 for (Node_Arc_Iterator<GT, SA> it(start, sa);
310 it.has_curr() and curr_df < gptr->get_num_nodes(); it.next_ne())
311 {
312 auto tgt = it.get_tgt_node();
314 continue;
315
316 auto arc = it.get_curr();
317 if (IS_ARC_VISITED(arc, Depth_First))
318 continue;
319
320 ARC_BITS(arc).set_bit(Depth_First, true);
321 cut_nodes(frames, tgt, arc);
322 ++call_counter;
323 }
324
325 if (call_counter > 1) // is the root an articulation point?
326 { // yes ==> append it to the list
327 NODE_BITS(start).set_bit(Cut, true);
328 list_ptr->append(start);
329 }
330
332 }
333
334 private:
335 // The next arc of a node to look at, in the searches that only need it
337 {
339
340 [[gnu::always_inline]] ALEPH_FLATTEN
341 Arc_Frame(typename GT::Node *p, SA &sa) : it(p, sa) {}
342
343 [[gnu::always_inline]] ALEPH_FLATTEN
344 void next_arc() noexcept(noexcept(it.next_ne())) { it.next_ne(); }
345 };
346
347 // Moves the iterator of f to the next arc that goes to a node that is
348 // not a cut node and was not painted, and paints both; gives that
349 // node, or nullptr when the arcs are done
350 [[gnu::always_inline]] ALEPH_FLATTEN
351 typename GT::Node *next_to_paint(Arc_Frame &f) noexcept(noexcept(f.it.next_ne()))
352 {
353 for (; f.it.has_curr(); f.it.next_ne())
354 {
355 auto arc = f.it.get_curr_ne();
356 if (is_arc_painted<GT>(arc))
357 continue;
358
359 auto tgt = f.it.get_tgt_node_ne();
360 if (is_a_cut_node<GT>(tgt))
361 continue;
362
363 paint_arc<GT>(arc, curr_color);
364 if (is_node_painted<GT>(tgt))
365 continue;
366
367 paint_node<GT>(tgt, curr_color);
368 return tgt;
369 }
370 return nullptr;
371 }
372
373 // Paints with curr_color the block of p, which is not a cut node. A
374 // path can have as many nodes as the graph, too many for recursion, so
375 // that the search keeps its own stack; the order of the visits is that
376 // of a recursion.
377 void paint_subgraph(typename GT::Node *p)
378 {
379 assert(not is_a_cut_node <GT> (p));
380
381 if (is_node_painted<GT>(p))
382 return;
383
384 paint_node<GT>(p, curr_color);
385
387 Arc_Frame *f = &frames.push(p, sa); // the top one
388 while (true)
389 {
390 if (auto tgt = next_to_paint(*f))
391 { // the iterator of f stays on the arc to tgt, as in a recursion
392 f = &frames.push(tgt, sa);
393 continue;
394 }
395
396 frames.pop();
397 if (frames.is_empty())
398 return;
399
400 f = &frames.top();
401 f->next_arc();
402 }
403 }
404
406 {
407 assert(is_a_cut_node <GT> (p));
408
409 // Paint connected blocks adjacent to p with different colors
410 for (Node_Arc_Iterator<GT, SA> it(p, sa); it.has_curr(); it.next_ne())
411 {
412 auto arc = it.get_curr();
413
414 assert(not is_arc_painted <GT> (arc));
415
416 auto tgt_node = it.get_tgt_node();
417 if (is_a_cut_node<GT>(tgt_node)) // is it a cut arc?
418 {
419 ARC_BITS(arc).set_bit(Cut, true); // mark as cut arc
420 continue; // move to next arc
421 }
422 paint_arc<GT>(arc, Cross_Arc); // mark as cross arc
423 if (is_node_painted<GT>(tgt_node))
424 continue;
425
426 // paint the block reachable through this arc
427 paint_subgraph(tgt_node);
428
429 curr_color++; // next color (next arc belongs to a different block)
430
431 assert(not is_arc_painted <GT> (arc));
432 }
433 }
434
435 typename GT::Node * create_and_map_node(typename GT::Node *gp,
436 const long & color,
437 GT & sg)
438 {
439 (void) color;
440 assert(get_color<GT>(gp) == color);
441 assert(not IS_NODE_VISITED(gp, Build_Subtree));
442
443 // insert_node() frees the node if it cannot take it
444 auto tp = sg.insert_node(new typename GT::Node(gp));
445 NODE_COOKIE(gp) = nullptr; // else it would compose with a former copy
446 GT::map_nodes(gp, tp);
447 NODE_BITS(gp).set_bit(Build_Subtree, true);
448
449 return tp;
450 }
451
452 // A node of the block being copied that waits for the one that the
453 // copy goes to, its image, and the next arc of it to look at
455 {
456 typename GT::Node *tsrc;
458
459 [[gnu::always_inline]] ALEPH_FLATTEN
460 Map_Frame(typename GT::Node *gsrc, SA &sa)
461 : tsrc(mapped_node<GT>(gsrc)), it(gsrc, sa) {}
462
463 [[gnu::always_inline]] ALEPH_FLATTEN
464 void next_arc() noexcept(noexcept(it.next_ne())) { it.next_ne(); }
465 };
466
467 // Moves the iterator of f to the next arc of the color that was not
468 // copied, and gives it, or nullptr when the arcs are done
469 [[gnu::always_inline]] ALEPH_FLATTEN
470 typename GT::Arc *next_to_map(Map_Frame &f, const long & color)
471 noexcept(noexcept(f.it.next_ne()))
472 {
473 for (; f.it.has_curr(); f.it.next_ne())
474 {
475 auto garc = f.it.get_curr_ne();
476 if (get_color<GT>(garc) != color or IS_ARC_VISITED(garc, Build_Subtree))
477 continue; // arc has a different color or was already visited
478
479 return garc;
480 }
481 return nullptr;
482 }
483
484 // Copies into sg, from gsrc, whose image is in sg, the arcs of the
485 // given color not copied yet, with the nodes that they reach; the
486 // search goes on from the target of each arc that it copies. A path
487 // can have as many nodes as the graph, too many for recursion, so that
488 // the search keeps its own stack; the order of the visits is that of a
489 // recursion.
490 void map_subgraph(GT & sg, typename GT::Node *gsrc, const long & color)
491 {
492 assert(get_color <GT> (gsrc) == color);
493
494 // Copies garc, an arc of f.it, and gtgt, its target, if gtgt was not
495 // copied; gives gtgt
496 auto copy = [&](Map_Frame &f, typename GT::Arc *garc)
497 {
498 // the target before the mark, which the compiler takes for a
499 // store that could change the iterator
500 auto gtgt = f.it.get_tgt_node_ne();
501 ARC_BITS(garc).set_bit(Build_Subtree, true);
502
503 assert(get_color <GT> (gtgt) == color);
504
505 typename GT::Node *ttgt = nullptr; // gtgt mapped into sg
506 if (IS_NODE_VISITED(gtgt, Build_Subtree)) // already in sg?
507 ttgt = mapped_node<GT>(gtgt);
508 else
509 ttgt = create_and_map_node(gtgt, color, sg);
510
511 auto tarc = sg.insert_arc(f.tsrc, ttgt, garc->get_info());
512 ARC_COOKIE(garc) = nullptr; // else it would compose with a former copy
513 GT::map_arcs(garc, tarc);
514 return gtgt;
515 };
516
517 // The node being visited keeps its iterator in here until the copy
518 // goes on from a target of it; then it waits in frames, where it goes
519 // on when the copy comes back. here is optional, so that the frame of
520 // each node is built in its place: a frame keeps the filter, which
521 // may not be assignable
523 std::optional<Map_Frame> here(std::in_place, gsrc, sa);
524 while (true)
525 {
526 if (auto garc = next_to_map(*here, color))
527 { // the iterator stays on garc, as in a recursion
528 auto gtgt = copy(*here, garc);
529 frames.push(std::move(*here));
530 here.emplace(gtgt, sa);
531 continue;
532 }
533
534 while (true) // back to the nodes that wait
535 {
536 if (frames.is_empty())
537 return;
538
539 Map_Frame &f = frames.top();
540 f.next_arc(); // past the arc that was copied
541 if (auto garc = next_to_map(f, color))
542 {
543 here.emplace(copy(f, garc), sa);
544 break;
545 }
546 frames.pop();
547 }
548 }
549 }
550
551 public:
557 Compute_Cut_Nodes(const GT & g, SA __sa = SA())
558 : sa(__sa), gptr(&const_cast<GT &>(g)), state(Init)
559 {
560 /* empty */
561 }
562
573
581 void operator ()(typename GT::Node *start,
583 {
584 cut_nodes(start, list);
585 }
586
618 {
620 << "Cut nodes have not been computed or the class is in another phase";
621
624 curr_color = 1;
625
626 // Traverse each cut node and paint its adjacent blocks
628 i.has_curr(); i.next_ne())
629 paint_from_cut_node(i.get_curr());
630
631 state = Painted;
632
633 return curr_color;
634 }
635
646 void map_subgraph(GT & sg, const long & color)
647 {
648 ah_logic_error_if(state != Painted) << "Graph is not painted";
649
650 clear_graph(sg);
651
652 typename GT::Node *first = nullptr; // find the first node with the requested color
653
654 // A former copy of the color marked its nodes and its arcs with
655 // Build_Subtree, and some node of the color has the mark then
656 bool copied_before = false;
657 for (typename GT::Node_Iterator it(*gptr); it.has_curr(); it.next_ne())
658 if (get_color<GT>(it.get_curr()) == color)
659 {
660 first = it.get_curr();
661 copied_before = copied_before or IS_NODE_VISITED(first, Build_Subtree);
662 }
663
664 if (first == nullptr) // did we find the color?
665 {
666 ah_domain_error_if(first == nullptr) << "Color does not exist in the graph";
667 }
668
669 if (copied_before)
670 {
673 }
674 copies_marked = true;
675
676 // create first, insert it into sg, and map it
677 create_and_map_node(first, color, sg);
678 try
679 {
680 map_subgraph(sg, first, color); // map the component
681 }
682 catch (...)
683 {
684 clear_graph(sg);
685 throw;
686 }
687 }
688
707 void map_cut_graph(GT & cut_graph,
708 DynDlist<typename GT::Arc *> & cross_arc_list)
709 {
710 ah_logic_error_if(state != Painted) << "Graph is not painted";
711
712 clear_graph(cut_graph);
713
714 // Traverse the cut-node list and insert them into cut_graph
716 it.has_curr(); it.next_ne())
717 {
718 auto gp = it.get_curr();
719
720 assert(is_a_cut_node <GT> (gp));
721
722 // insert_node() frees the node if it cannot take it
723 auto tp = cut_graph.insert_node(new typename GT::Node(gp));
724 NODE_COOKIE(gp) = nullptr; // else it would compose with a former copy
725 GT::map_nodes(gp, tp);
726 }
727
728 // Traverse arcs of g:
729 // - cut_graph will contain cut arcs (between cut nodes)
730 // - cross_arc_list will contain cross arcs (from cut nodes to blocks)
731 for (Arc_Iterator<GT, SA> it(*gptr, sa); it.has_curr(); it.next_ne())
732 {
733 auto garc = it.get_curr();
734 if (is_a_cross_arc<GT>(garc))
735 {
736 cross_arc_list.append(garc);
737 continue;
738 }
739
740 if (not is_an_cut_arc<GT>(garc))
741 continue;
742
743 typename GT::Node *src = mapped_node<GT>(gptr->get_src_node(garc));
744 typename GT::Node *tgt = mapped_node<GT>(gptr->get_tgt_node(garc));
745
746 assert(src != nullptr and tgt != nullptr);
747
748 typename GT::Arc *arc =
749 cut_graph.insert_arc(src, tgt, garc->get_info());
750 ARC_COOKIE(garc) = nullptr; // else it would compose with a former copy
751 GT::map_arcs(garc, arc);
752 }
753 }
754
778 void compute_blocks(DynDlist<GT> & block_list,
779 GT & cut_graph,
780 DynDlist<typename GT::Arc *> & cross_arc_list)
781 {
782 ah_logic_error_if(state < Cut_Nodes_Computed) << "Cut nodes have not been computed";
783
786
787 if (copies_marked) // by a former copy
788 {
791 }
792 copies_marked = true;
793
794 // curr_color is the next unused color, so the colors of the blocks
795 // go from 1 to curr_color - 1, and the block of color c is the
796 // (c - 1)-th. Without cut nodes nothing was painted: the graph is one
797 // block, whose nodes and arcs have color 0.
798 const long num_blocks = curr_color > 1 ? curr_color - 1 : 1;
799
800 DynArray<GT *> blocks; // blocks in an array for fast access
801 blocks.reserve(num_blocks);
802
803 // Create an ordered list of empty components by color i
804 for (long i = 0; i < num_blocks; ++i)
805 blocks.access(i) = &block_list.append(GT());
806
807 // Traverse nodes and copy/map according to color
808 for (typename GT::Node_Iterator it(*gptr); it.has_curr(); it.next_ne())
809 {
810 auto p = it.get_curr();
812 continue;
813
814 if (is_a_cut_node<GT>(p))
815 continue;
816
817 if (not IS_NODE_VISITED(p, Depth_First))
818 continue; // in a component that cut_nodes() did not reach
819
820 const long color = get_color<GT>(p);
821
822 GT & sg = *blocks.access(color == 0 ? 0 : color - 1);
823
825
826 map_subgraph(sg, p, color);
827 }
828
829 map_cut_graph(cut_graph, cross_arc_list);
830 }
831 };
832
833 // =========================================================================
834 // Bridge finding (Tarjan low-link algorithm)
835 // =========================================================================
836
873 template <AlephGraph GT, ArcFilter<GT> SA = Dft_Show_Arc<GT>>
875 {
876 SA sa;
878
879 // A node that waits for the one that it visits, the arc through which
880 // it was visited, and the next arc of it to look at
881 struct Frame
882 {
883 typename GT::Node *p;
886
887 [[gnu::always_inline]] ALEPH_FLATTEN
888 Frame(typename GT::Node *n, typename GT::Arc *a, SA &sa)
889 : p(n), parent_arc(a), it(n, sa) {}
890
891 [[gnu::always_inline]] ALEPH_FLATTEN
892 void next_arc() noexcept(noexcept(it.next_ne())) { it.next_ne(); }
893 };
894
895 // Moves the iterator of f to the next arc of f.p that goes to a node
896 // not visited, which it marks, and gives that node, or nullptr when the
897 // arcs of f.p are done. The back-edges lower low(f.p), which is stored
898 // once: a store in the loop could be to the iterator, which would have
899 // to be read again.
900 [[gnu::always_inline]] ALEPH_FLATTEN
901 typename GT::Node *next_child(Frame &f) noexcept(noexcept(f.it.next_ne()))
902 {
903 long p_low = low<GT>(f.p);
904 typename GT::Node *child = nullptr;
905 for (; f.it.has_curr(); f.it.next_ne())
906 {
907 auto arc = f.it.get_curr_ne();
908 if (arc == f.parent_arc)
909 continue; // skip the arc we came from
910
911 auto tgt = f.it.get_tgt_node_ne();
912
914 {
915 // Back-edge: update low if this arc has not been traversed yet
916 if (not IS_ARC_VISITED(arc, Depth_First))
917 p_low = std::min(df<GT>(tgt), p_low);
918 continue;
919 }
920
921 if (IS_ARC_VISITED(arc, Depth_First))
922 continue;
923
924 ARC_BITS(arc).set_bit(Depth_First, true); // mark tree arc
925 child = tgt;
926 break;
927 }
928 low<GT>(f.p) = p_low;
929 return child;
930 }
931
932 // Depth-first search from p that appends to bridges those below p. A
933 // path can have as many nodes as the graph, too many for recursion, so
934 // that the search keeps its own stack; the order of the visits is that
935 // of a recursion.
936 void __dfs(Dfs_Stack<Frame> & frames, typename GT::Node *p,
937 long & curr_df, DynList<typename GT::Arc *> & bridges)
938 {
939 auto visit = [&frames, &curr_df, this](typename GT::Node *n,
940 typename GT::Arc *a)
941 {
942 NODE_BITS(n).set_bit(Depth_First, true);
943 low<GT>(n) = df<GT>(n) = curr_df++;
944 return &frames.push(n, a, sa);
945 };
946
947 Frame *f = visit(p, nullptr); // the top one
948 while (true)
949 {
950 if (auto tgt = next_child(*f))
951 { // the iterator of f stays on the arc to tgt, as in a recursion
952 f = visit(tgt, f->it.get_curr_ne());
953 continue;
954 }
955
956 auto child = f->p;
957 auto arc = f->parent_arc;
958 frames.pop();
959 if (frames.is_empty())
960 return;
961
962 f = &frames.top(); // the node from which child was visited
963 low<GT>(f->p) = std::min(low<GT>(child), low<GT>(f->p));
964
965 if (low<GT>(child) > df<GT>(f->p)) // bridge condition
966 bridges.append(arc);
967
968 f->next_arc(); // past the arc to child
969 }
970 }
971
972 public:
978 Compute_Bridges(const GT & g, SA sa = SA())
979 : sa(sa), gptr(&const_cast<GT &>(g))
980 { /* empty */
981 }
982
999 {
1000 ah_domain_error_if(gptr->is_digraph()) << "Compute_Bridges: does not work on digraphs";
1001
1003
1004 if (gptr->get_num_nodes() == 0)
1005 return bridges;
1006
1007 ah_invalid_argument_if(start == nullptr) << "Compute_Bridges::find_bridges(): start must be non-null";
1008
1009 gptr->for_each_node([](auto p)
1010 {
1011 NODE_COUNTER(p) = 0;
1012 NODE_BITS(p).reset();
1013 });
1014 gptr->reset_arcs();
1015
1016 long curr_df = 0;
1017 Dfs_Stack<Frame> frames;
1018 __dfs(frames, start, curr_df, bridges);
1019
1020 // Cover disconnected components: any node not reached from start.
1021 gptr->for_each_node([&](auto p)
1022 {
1023 if (not IS_NODE_VISITED(p, Depth_First))
1024 __dfs(frames, p, curr_df, bridges);
1025 });
1026
1027 // NODE_COOKIE holds low-link values (stored as longs); clear them.
1028 gptr->for_each_node([](auto p) { NODE_COOKIE(p) = nullptr; });
1029
1030 return bridges;
1031 }
1032
1044
1050 {
1051 return find_bridges(start);
1052 }
1053
1062 };
1063
1085 template <AlephGraph GT, ArcFilter<GT> SA = Dft_Show_Arc<GT>>
1087 find_bridges(const GT & g, typename GT::Node *start, SA sa = SA())
1088 {
1089 return Compute_Bridges<GT, SA>(g, sa).find_bridges(start);
1090 }
1091
1096 template <AlephGraph GT>
1101} // end namespace Aleph
1102
1103# endif // TPL_CUT_NODES_H
#define ah_domain_error_if(C)
Throws std::domain_error if condition holds.
Definition ah-errors.H:670
#define ah_logic_error_if(C)
Throws std::logic_error if condition holds.
Definition ah-errors.H:474
#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.
List_Graph< Graph_Node< Node_Info >, Graph_Arc< Arc_Info > > GT
Find bridge edges (isthmuses) of a connected undirected graph.
DynList< typename GT::Arc * > operator()(typename GT::Node *start)
This is an overloaded member function, provided for convenience. It differs from the above function o...
GT::Node * next_child(Frame &f) noexcept(noexcept(f.it.next_ne()))
Compute_Bridges(const GT &g, SA sa=SA())
Constructor.
void __dfs(Dfs_Stack< Frame > &frames, typename GT::Node *p, long &curr_df, DynList< typename GT::Arc * > &bridges)
DynList< typename GT::Arc * > find_bridges()
This is an overloaded member function, provided for convenience. It differs from the above function o...
DynList< typename GT::Arc * > find_bridges(typename GT::Node *start)
Find all bridge edges in the graph, starting from start.
DynList< typename GT::Arc * > operator()()
This is an overloaded member function, provided for convenience. It differs from the above function o...
Computation of cut nodes (articulation points) of a graph.
void map_cut_graph(GT &cut_graph, DynDlist< typename GT::Arc * > &cross_arc_list)
Computes the mapped cut graph of a graph.
Cut_Frame * visit(Dfs_Stack< Cut_Frame > &frames, typename GT::Node *p, typename GT::Arc *a)
void map_subgraph(GT &sg, typename GT::Node *gsrc, const long &color)
GT::Node * create_and_map_node(typename GT::Node *gp, const long &color, GT &sg)
enum Aleph::Compute_Cut_Nodes::State state
void map_subgraph(GT &sg, const long &color)
Obtains a mapped copy of the component with the given color.
void operator()(DynDlist< typename GT::Node * > &list)
This is an overloaded member function, provided for convenience. It differs from the above function o...
GT::Arc * next_to_map(Map_Frame &f, const long &color) noexcept(noexcept(f.it.next_ne()))
void paint_subgraph(typename GT::Node *p)
GT::Node * next_to_paint(Arc_Frame &f) noexcept(noexcept(f.it.next_ne()))
void cut_nodes(Dfs_Stack< Cut_Frame > &frames, typename GT::Node *p, typename GT::Arc *a)
DynDlist< typename GT::Node * > * list_ptr
GT::Node * next_child(Cut_Frame &f) noexcept(noexcept(f.it.next_ne()))
Compute_Cut_Nodes(const GT &g, SA __sa=SA())
Constructor for cut nodes calculator.
void compute_blocks(DynDlist< GT > &block_list, GT &cut_graph, DynDlist< typename GT::Arc * > &cross_arc_list)
Build mapped graphs around cut nodes.
void cut_nodes(typename GT::Node *start, DynDlist< typename GT::Node * > &list)
Computes the cut nodes.
long paint_subgraphs()
Paints the connected components around the cut nodes.
void paint_from_cut_node(typename GT::Node *p)
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.
T & access(const size_t i) noexcept
Fast access without checking allocation and bound_min_clock checking.
void reserve(const size_t l, const size_t r)
Allocate a range of entries.
Iterator dynamic list.
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.
void empty() noexcept
@brief Empties the container.
Doubly-linked list (defined in tpl_dynList.H).
Definition htlist.H:1231
void next_ne() noexcept(shows_ne)
Advances the iterator to the next filtered element, as next() does.
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
NodeInfo & get_info() noexcept
Return a modifiable reference to the data contained in the node.
Definition graph-dry.H:753
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
Node * get_src_node(Arc *arc) const noexcept
Return the source node of arc (only for directed graphs)
Definition graph-dry.H:1017
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
void reset_counter_arcs() const noexcept
Reset all the counters to zero for all the arcs of graph.
Definition graph-dry.H:1356
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
GT::Node * mapped_node(typename GT::Node *p) noexcept
Return the mapped node through the cookie of p
Definition tpl_graph.H:3622
#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 ARC_BITS(p)
Return the control bits of arc p.
#define NODE_COOKIE(p)
Return the node cookie
void clear_graph(GT &g)
Clean a graph: all its nodes and arcs are removed and freed.
Definition tpl_graph.H:4776
#define IS_ARC_VISITED(p, bit)
Determine whether the bit field is or not set to one.
DynList< typename GT::Arc * > find_bridges(const GT &g, typename GT::Node *start, SA sa=SA())
Find all bridge edges in a connected undirected graph.
#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
@ Build_Subtree
Definition aleph-graph.H:80
Main namespace for Aleph-w library functions.
Itor2 copy(Itor1 sourceBeg, const Itor1 &sourceEnd, Itor2 destBeg)
Copy elements from one range to another.
Definition ahAlgo.H:584
and
Check uniqueness with explicit hash + equality functors.
constexpr long Cross_Arc
Special marker for arcs connecting a cut node to a non-cut block.
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)
Filtered iterator on all the arcs of a graph.
Definition tpl_graph.H:1200
Node_Arc_Iterator< GT, SA > it
void next_arc() noexcept(noexcept(it.next_ne()))
Frame(typename GT::Node *n, typename GT::Arc *a, SA &sa)
Node_Arc_Iterator< GT, SA > it
Arc_Frame(typename GT::Node *p, SA &sa)
void next_arc() noexcept(noexcept(it.next_ne()))
Node_Arc_Iterator< GT, SA > it
void next_arc() noexcept(noexcept(it.next_ne()))
Cut_Frame(typename GT::Node *n, typename GT::Arc *arc, SA &sa, const long n_low)
Map_Frame(typename GT::Node *gsrc, SA &sa)
void next_arc() noexcept(noexcept(it.next_ne()))
Node_Arc_Iterator< GT, SA > it
Filtered iterator of adjacent arcs of a node.
Definition tpl_graph.H:1155
Stack of the frames of a depth-first search that does not recurse.
Utility algorithms and operations for graphs.