Aleph-w 7.10.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
tpl_test_path.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
39# ifndef TEST_PATH_H
40# define TEST_PATH_H
41
42# include <ah-graph-concepts.H>
43
44# include <tpl_dfs_stack.H>
45# include <tpl_graph.H>
46
47namespace Aleph {
48
49
70 template <AlephGraph GT, ArcFilter<GT> SA = Dft_Show_Arc<GT> >
72{
73 SA sa;
74 typename GT::Node * tgt = nullptr;
75
76 // DFS from curr that tells whether tgt is reached. A path can have as
77 // many nodes as the graph, too many for recursion, so that the search
78 // keeps its own stack; the order of the visits is that of a recursion.
79 bool test_path(typename GT::Node * curr)
80 {
81 if (curr == tgt)
82 return true; // tgt was reached
83
84 if (IS_NODE_VISITED(curr, Find_Path)) // was curr_node visited?
85 return false; // yes, do not explore
86
87 NODE_BITS(curr).set_bit(Find_Path, true); // paint curr_node
88
89 // search through the arcs not painted, which it paints. The node being
90 // visited keeps its iterator in here until it visits a node; then it
91 // waits in frames, where it goes on when it comes back. here is
92 // optional, so that the frame of each node is built in its place: a
93 // frame keeps the filter, which may not be assignable.
95 Dfs_Stack<Frame> frames;
96 std::optional<Frame> here(std::in_place, curr, sa);
97 while (true)
98 {
99 if (auto next = here->template next_target<Find_Path>(tgt))
100 { // the iterator stays on the arc to next, as in a recursion
101 if (next == tgt)
102 return true;
103
104 NODE_BITS(next).set_bit(Find_Path, true);
105 frames.push(std::move(*here));
106 here.emplace(next, sa);
107 continue;
108 }
109
110 while (true) // back to the nodes that wait
111 {
112 // all of curr_node's adjacent arcs were explored without
113 // finding end_node ==> there is no path through curr_node
114 if (frames.is_empty())
115 return false;
116
117 Frame & f = frames.top();
118 f.next_arc(); // past the arc to the node that was visited
119 if (auto next = f.template next_target<Find_Path>(tgt))
120 {
121 if (next == tgt)
122 return true;
123
124 NODE_BITS(next).set_bit(Find_Path, true);
125 here.emplace(next, sa);
126 break;
127 }
128 frames.pop();
129 }
130 }
131 }
132
133 // Whether a path goes from src to dest: from a node to itself, the path
134 // of no arc, as in test_for_path(); otherwise a search in depth from
135 // each arc of src
136 bool test_path(const GT & g, typename GT::Node * src, typename GT::Node * dest)
137 {
138 if (src == dest)
139 return true;
140
143
144 tgt = dest;
145
146 // search through src's adjacent arcs
147 for (Node_Arc_Iterator<GT, SA> i(src, sa); i.has_curr(); i.next_ne())
148 {
149 typename GT::Arc * arc = i.get_current_arc_ne();
150 ARC_BITS(arc).set_bit(Find_Path, true); // mark arc
151 if (test_path(i.get_tgt_node()))
152 return true;
153 }
154
155 // all of start_node's arcs have been explored without
156 // finding a path to end_node ==> no path exists
157 return false;
158 }
159
160public:
161
162 Test_For_Path(SA __sa = SA()) : sa(__sa) { /* empty */ }
163
172 bool operator () (const GT& g, typename GT::Node * start_node,
173 typename GT::Node * end_node)
174 {
175 return test_path(g, start_node, end_node);
176 }
177};
178
179
180
181} // end namespace Aleph
182
183# endif // TEST_PAT_H
C++20 concepts for the protocol shared by graph algorithms.
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.
void next_ne() noexcept(shows_ne)
Advances the iterator to the next filtered element, as next() does.
Checks whether a path exists between two nodes.
bool operator()(const GT &g, typename GT::Node *start_node, typename GT::Node *end_node)
Invokes the test for a path's existence between two nodes.
bool test_path(typename GT::Node *curr)
Test_For_Path(SA __sa=SA())
bool test_path(const GT &g, typename GT::Node *src, typename GT::Node *dest)
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_bit_arcs(int bit) const noexcept
Reset bit to zero for all the arcs of graph.
Definition graph-dry.H:1332
#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_BITS(p)
Get the control bits of a node.
@ Find_Path
Definition aleph-graph.H:76
Main namespace for Aleph-w library functions.
void next()
Advance all underlying iterators (bounds-checked).
Definition ah-zip.H:236
The next arc of a node to look at, for a depth-first search that keeps only that in a frame.
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.
Generic graph and digraph implementations.