Aleph-w 7.10.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
tpl_test_cycle.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 TPL_TEST_CYCLE_H
40# define TPL_TEST_CYCLE_H
41
42# include <ah-graph-concepts.H>
43
44# include <tpl_dfs_stack.H>
45# include <tpl_graph.H>
46# include <ah-errors.H>
47
48namespace Aleph {
49
111template <AlephGraph GT, ArcFilter<GT> SA = Dft_Show_Arc<GT>>
113{
114 mutable typename GT::Node * src = nullptr;
115 SA sa;
116
117 // DFS helper to detect if a path from curr returns to src. A path can
118 // have as many nodes as the graph, too many for recursion, so that the
119 // search keeps its own stack; the order of the visits is that of a
120 // recursion.
121 bool test_cycle(typename GT::Node * curr) const
122 {
123 // Base case: found a path back to source -> cycle detected
124 if (src == curr)
125 return true;
126
127 // Already visited this node in current DFS -> no cycle via this path
128 if (IS_NODE_VISITED(curr, Test_Cycle))
129 return false;
130
131 // Mark current node as visited
132 NODE_BITS(curr).set_bit(Test_Cycle, true);
133
134 // Explore the arcs not visited, which it marks. The node being visited
135 // keeps its iterator in here until it visits a node; then it waits in
136 // frames, where it goes on when it comes back. here is optional, so
137 // that the frame of each node is built in its place: a frame keeps the
138 // filter, which may not be assignable.
140 Dfs_Stack<Frame> frames;
141 std::optional<Frame> here(std::in_place, curr, sa);
142 while (true)
143 {
144 if (auto tgt = here->template next_target<Test_Cycle>(src))
145 { // the iterator stays on the arc to tgt, as in a recursion
146 if (tgt == src)
147 return true;
148
149 NODE_BITS(tgt).set_bit(Test_Cycle, true);
150 frames.push(std::move(*here));
151 here.emplace(tgt, sa);
152 continue;
153 }
154
155 while (true) // back to the nodes that wait
156 {
157 if (frames.is_empty())
158 return false; // all paths from curr explored without finding src
159
160 Frame & f = frames.top();
161 f.next_arc(); // past the arc to the node that was visited
162 if (auto tgt = f.template next_target<Test_Cycle>(src))
163 {
164 if (tgt == src)
165 return true;
166
167 NODE_BITS(tgt).set_bit(Test_Cycle, true);
168 here.emplace(tgt, sa);
169 break;
170 }
171 frames.pop();
172 }
173 }
174 }
175
176 // Main algorithm entry point
177 bool test_cycle(const GT & g, typename GT::Node * s) const
178 {
179 ah_invalid_argument_if(s == nullptr)
180 << "Test_For_Cycle: source node cannot be null";
181
182 src = s;
183
184 // Reset control bits for a fresh traversal
187
188 // Explore all arcs adjacent to source node
189 for (Node_Arc_Iterator<GT, SA> it(src, sa); it.has_curr(); it.next_ne())
190 {
191 typename GT::Arc * arc = it.get_current_arc_ne();
192
193 // Skip already-visited arcs (shouldn't happen in first iteration)
194 if (IS_ARC_VISITED(arc, Test_Cycle))
195 continue;
196
197 // Mark arc as visited
198 ARC_BITS(arc).set_bit(Test_Cycle, true);
199
200 // Check if target node leads back to source
201 if (test_cycle(it.get_tgt_node()))
202 return true;
203 }
204
205 // All paths explored without finding a cycle
206 return false;
207 }
208
209public:
210
216 Test_For_Cycle(SA arc_filter = SA()) : sa(arc_filter) { /* empty */ }
217
241 bool operator () (const GT & g, typename GT::Node * src_node) const
242 {
243 return this->test_cycle(g, src_node);
244 }
245};
246
247} // end namespace Aleph
248
249
250# endif // TPL_TEST_CYCLE_H
Exception handling system with formatted messages for Aleph-w.
#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.
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.
Test whether a cycle is reachable from a given node.
bool test_cycle(typename GT::Node *curr) const
bool test_cycle(const GT &g, typename GT::Node *s) const
Test_For_Cycle(SA arc_filter=SA())
Construct a cycle tester with optional arc filter.
bool operator()(const GT &g, typename GT::Node *src_node) const
Test whether a cycle is reachable from the given source node.
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 IS_ARC_VISITED(p, bit)
Determine whether the bit field is or not set to one.
#define NODE_BITS(p)
Get the control bits of a node.
@ Test_Cycle
Definition aleph-graph.H:75
Main namespace for Aleph-w library functions.
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.