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 <ah-graph-concepts.H>
43
44# include <tpl_graph_utils.H>
45# include <ah-errors.H>
46
47namespace Aleph
48{
67 template <AlephGraph GT, ArcFilter<GT> SA = Dft_Show_Arc<GT>>
69 {
70 SA & sa;
71
72 bool is_acyclique(typename GT::Node *curr)
73 {
74 if (IS_NODE_VISITED(curr, Test_Cycle))
75 return false;
76
77 NODE_BITS(curr).set_bit(Test_Cycle, true); // mark node
78
79 for (Node_Arc_Iterator<GT, SA> i(curr, sa); i.has_curr(); i.next_ne())
80 {
81 typename GT::Arc *arc = i.get_current_arc_ne();
82 if (IS_ARC_VISITED(arc, Test_Cycle))
83 continue;
84
85 ARC_BITS(arc).set_bit(Test_Cycle, true);
86
87 if (not is_acyclique(i.get_tgt_node()))
88 return false;
89 }
90
91 // all arcs traversed without finding a cycle ==>
92 // the graph is acyclic through curr_node
93 return true;
94 }
95
96 bool is_acyclique(GT & g, size_t num_arcs)
97 {
99 << "is_graph_acyclique() does not work for digraphs";
100
101 if (num_arcs >= g.get_num_nodes())
102 return false;
103
106
107 for (typename GT::Node_Iterator it(g); it.has_curr(); it.next_ne())
108 {
109 typename GT::Node *curr = it.get_current_node_ne();
110 if (IS_NODE_VISITED(curr, Test_Cycle))
111 continue;
112
113 if (not is_acyclique(curr))
114 return false;
115 }
116
117 return true;
118 }
119
120 public:
122 { /* empty */
123 }
124
126 { /* empty */
127 }
128
148 bool operator ()(GT & g, size_t num_arcs)
149 {
150 return is_acyclique(g, num_arcs);
151 }
152
168 bool operator ()(GT & g)
169 {
170 return is_acyclique(g, g.get_num_arcs());
171 }
172 };
173
191 template <AlephGraph GT, ArcFilter<GT> SA = Dft_Show_Arc<GT>>
193 {
194 SA & sa;
195
196 public:
197 Has_Cycle(SA && __sa = SA()) : sa(__sa)
198 { /* empty */
199 }
200
202 { /* empty */
203 }
204
220 bool operator ()(GT & g) const
221 {
223 }
224
241 bool operator ()(GT & g, size_t num_arcs) const
242 {
243 return not Is_Graph_Acyclique<GT, SA>(sa)(g, num_arcs);
244 }
245 };
246} // end namespace Aleph
247
248# 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:527
C++20 concepts for the protocol shared by graph algorithms.
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:1088
constexpr size_t get_num_nodes() const noexcept
Return the total of nodes of graph.
Definition graph-dry.H:737
bool is_digraph() const noexcept
Return true if the graph this is directed.
Definition graph-dry.H:699
void reset_bit_arcs(int bit) const noexcept
Reset bit to zero for all the arcs of graph.
Definition graph-dry.H:1094
constexpr size_t get_num_arcs() const noexcept
Definition graph-dry.H:826
#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.
size_t blossom_maximum_cardinality_matching(const GT &g, DynDlist< typename GT::Arc * > &matching, SA sa=SA())
Alias of compute_maximum_cardinality_general_matching().
Definition Blossom.H:466
#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
Filtered iterator of adjacent arcs of a node.
Definition tpl_graph.H:1120
Utility algorithms and operations for graphs.