Aleph-w 3.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 <tpl_graph_utils.H>
43# include <ah-errors.H>
44
45namespace Aleph
46{
65 template <class GT, class SA = Dft_Show_Arc<GT>>
67 {
68 SA & sa;
69
70 bool is_acyclique(typename GT::Node *curr)
71 {
72 if (IS_NODE_VISITED(curr, Test_Cycle))
73 return false;
74
75 NODE_BITS(curr).set_bit(Test_Cycle, true); // mark node
76
77 for (Node_Arc_Iterator<GT, SA> i(curr, sa); i.has_curr(); i.next_ne())
78 {
79 typename GT::Arc *arc = i.get_current_arc_ne();
80 if (IS_ARC_VISITED(arc, Test_Cycle))
81 continue;
82
83 ARC_BITS(arc).set_bit(Test_Cycle, true);
84
85 if (not is_acyclique(i.get_tgt_node()))
86 return false;
87 }
88
89 // all arcs traversed without finding a cycle ==>
90 // the graph is acyclic through curr_node
91 return true;
92 }
93
94 bool is_acyclique(GT & g, size_t num_arcs)
95 {
97 << "is_graph_acyclique() does not work for digraphs";
98
99 if (num_arcs >= g.get_num_nodes())
100 return false;
101
104
105 for (typename GT::Node_Iterator it(g); it.has_curr(); it.next_ne())
106 {
107 typename GT::Node *curr = it.get_current_node_ne();
108 if (IS_NODE_VISITED(curr, Test_Cycle))
109 continue;
110
111 if (not is_acyclique(curr))
112 return false;
113 }
114
115 return true;
116 }
117
118 public:
120 { /* empty */
121 }
122
124 { /* empty */
125 }
126
146 bool operator ()(GT & g, size_t num_arcs)
147 {
148 return is_acyclique(g, num_arcs);
149 }
150
166 bool operator ()(GT & g)
167 {
168 return is_acyclique(g, g.get_num_arcs());
169 }
170 };
171
189 template <class GT, class SA = Dft_Show_Arc<GT>>
191 {
193
194 public:
196 { /* empty */
197 }
198
200 { /* empty */
201 }
202
218 bool operator ()(GT & g) const
219 {
221 }
222
239 bool operator ()(GT & g, size_t num_arcs) const
240 {
241 return not Is_Graph_Acyclique<GT, SA>(sa)(g, num_arcs);
242 }
243 };
244} // end namespace Aleph
245
246# 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:522
void next_ne() noexcept
Advances the iterator to the next filtered element (noexcept version).
Determines whether a graph contains cycles.
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 operator()(GT &g, size_t num_arcs)
Invokes the acyclicity test.
void reset_bit_nodes(int bit) const noexcept
Reset bit to zero for all the nodes of graph.
Definition graph-dry.H:1040
constexpr size_t get_num_nodes() const noexcept
Return the total of nodes of graph.
Definition graph-dry.H:695
bool is_digraph() const noexcept
Return true if the graph this is directed.
Definition graph-dry.H:657
void reset_bit_arcs(int bit) const noexcept
Reset bit to zero for all the arcs of graph.
Definition graph-dry.H:1046
constexpr size_t get_num_arcs() const noexcept
Definition graph-dry.H:778
#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.
Definition ah-arena.H:89
DynList< T > maps(const C &c, Op op)
Classic map operation.
Arc of graph implemented with double-linked adjacency lists.
Definition tpl_graph.H:222
Filtered iterator of adjacent arcs of a node.
Definition tpl_graph.H:1119
Utility algorithms and operations for graphs.