Aleph-w 7.10.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
tpl_test_acyclique.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_ACYCLIQUE_H
40# define TPL_TEST_ACYCLIQUE_H
41
42# include <type_traits>
43
44# include <ah-graph-concepts.H>
45
46# include <tpl_dfs_stack.H>
47# include <tpl_graph_utils.H>
48# include <ah-errors.H>
49
50namespace Aleph
51{
70 template <AlephGraph GT, ArcFilter<GT> SA = Dft_Show_Arc<GT>>
72 {
74
75 // DFS from curr that tells whether no node is reached twice. A path
76 // can have as many nodes as the graph, too many for recursion, so that
77 // the search keeps its own stack; the order of the visits is that of a
78 // recursion.
79 bool is_acyclique(typename GT::Node *curr)
80 {
81 if (IS_NODE_VISITED(curr, Test_Cycle))
82 return false;
83
84 NODE_BITS(curr).set_bit(Test_Cycle, true); // mark node
85
86 // The node being visited keeps its iterator in here until it visits
87 // a node; then it waits in frames, where it goes on when it comes
88 // back. here is optional, so that the frame of each node is built in
89 // its place: a frame keeps the filter, which may not be assignable
91 Dfs_Stack<Frame> frames;
92 std::optional<Frame> here(std::in_place, curr, sa);
93 while (true)
94 {
95 if (auto tgt = here->template next_target<Test_Cycle, true>())
96 { // the iterator stays on the arc to tgt, as in a recursion
97 if (IS_NODE_VISITED(tgt, Test_Cycle)) // reached again
98 return false;
99
100 NODE_BITS(tgt).set_bit(Test_Cycle, true);
101 frames.push(std::move(*here));
102 here.emplace(tgt, sa);
103 continue;
104 }
105
106 while (true) // back to the nodes that wait
107 {
108 // all arcs traversed without finding a cycle ==>
109 // the graph is acyclic through curr_node
110 if (frames.is_empty())
111 return true;
112
113 Frame & f = frames.top();
114 f.next_arc(); // past the arc to the node that was visited
115 if (auto tgt = f.template next_target<Test_Cycle, true>())
116 {
117 if (IS_NODE_VISITED(tgt, Test_Cycle)) // reached again
118 return false;
119
120 NODE_BITS(tgt).set_bit(Test_Cycle, true);
121 here.emplace(tgt, sa);
122 break;
123 }
124 frames.pop();
125 }
126 }
127 }
128
129 // Whether g has no cycle; num_arcs, if known, is the number of arcs
130 // that sa shows
131 bool is_acyclique(GT & g, size_t num_arcs, bool num_arcs_known = true)
132 {
134 << "is_graph_acyclique() does not work for digraphs";
135
136 // a forest with nodes has fewer arcs than nodes
137 if (num_arcs_known and g.get_num_nodes() > 0
138 and num_arcs >= g.get_num_nodes())
139 return false;
140
143
144 for (typename GT::Node_Iterator it(g); it.has_curr(); it.next_ne())
145 {
146 typename GT::Node *curr = it.get_current_node_ne();
147 if (IS_NODE_VISITED(curr, Test_Cycle))
148 continue;
149
150 if (not is_acyclique(curr))
151 return false;
152 }
153
154 return true;
155 }
156
157 public:
158 Is_Graph_Acyclique(SA && __sa = SA()) : sa(std::move(__sa))
159 { /* empty */
160 }
161
162 Is_Graph_Acyclique(SA & __sa) : sa(__sa)
163 { /* empty */
164 }
165
185 bool operator ()(GT & g, size_t num_arcs)
186 {
187 return is_acyclique(g, num_arcs);
188 }
189
206 bool operator ()(GT & g)
207 {
208 // the number of arcs of g is that of those that sa shows only if sa
209 // shows them all
210 return is_acyclique(g, g.get_num_arcs(),
211 std::is_same_v<SA, Dft_Show_Arc<GT>>);
212 }
213 };
214
232 template <AlephGraph GT, ArcFilter<GT> SA = Dft_Show_Arc<GT>>
234 {
236
237 public:
238 Has_Cycle(SA && __sa = SA()) : sa(std::move(__sa))
239 { /* empty */
240 }
241
242 Has_Cycle(SA & __sa) : sa(__sa)
243 { /* empty */
244 }
245
262 bool operator ()(GT & g) const
263 {
264 return not Is_Graph_Acyclique<GT, SA>(sa)(g);
265 }
266
284 bool operator ()(GT & g, size_t num_arcs) const
285 {
286 return not Is_Graph_Acyclique<GT, SA>(sa)(g, num_arcs);
287 }
288 };
289} // end namespace Aleph
290
291# endif // TPL_TEST_ACYCLIQUE_H
Exception handling system with formatted messages for Aleph-w.
#define ah_domain_error_if(C)
Throws std::domain_error if condition holds.
Definition ah-errors.H:670
C++20 concepts for the protocol shared by graph algorithms.
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.
Determines whether a graph contains cycles.
Kept_Filter< SA > sa
Has_Cycle(SA &&__sa=SA())
bool operator()(GT &g) const
Invokes the cycle-existence test.
Determines whether a graph is acyclic (contains no cycles).
bool is_acyclique(typename GT::Node *curr)
bool is_acyclique(GT &g, size_t num_arcs, bool num_arcs_known=true)
bool operator()(GT &g, size_t num_arcs)
Invokes the acyclicity test, with the number of arcs that the filter shows.
A filter that an algorithm object keeps: one given as an lvalue is used where it is,...
Definition tpl_graph.H:1249
void reset_bit_nodes(int bit) const noexcept
Reset bit to zero for all the nodes of graph.
Definition graph-dry.H:1326
constexpr size_t get_num_nodes() const noexcept
Return the total of nodes of graph.
Definition graph-dry.H:975
bool is_digraph() const noexcept
Return true if the graph this is directed.
Definition graph-dry.H:937
void reset_bit_arcs(int bit) const noexcept
Reset bit to zero for all the arcs of graph.
Definition graph-dry.H:1332
constexpr size_t get_num_arcs() const noexcept
Return the number of arcs of the graph.
Definition graph-dry.H:1064
#define IS_NODE_VISITED(p, bit)
Determine whether the control bit is set or not 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.
and
Check uniqueness with explicit hash + equality functors.
STL namespace.
The next arc of a node to look at, for a depth-first search that keeps only that in a frame.
Default filter for filtered iterators on arcs.
Definition tpl_graph.H:1024
Stack of the frames of a depth-first search that does not recurse.
Utility algorithms and operations for graphs.