aGrUM 2.3.2
a C++ library for (probabilistic) graphical models
DAGCycleDetector.h
Go to the documentation of this file.
1/****************************************************************************
2 * This file is part of the aGrUM/pyAgrum library. *
3 * *
4 * Copyright (c) 2005-2025 by *
5 * - Pierre-Henri WUILLEMIN(_at_LIP6) *
6 * - Christophe GONZALES(_at_AMU) *
7 * *
8 * The aGrUM/pyAgrum library is free software; you can redistribute it *
9 * and/or modify it under the terms of either : *
10 * *
11 * - the GNU Lesser General Public License as published by *
12 * the Free Software Foundation, either version 3 of the License, *
13 * or (at your option) any later version, *
14 * - the MIT license (MIT), *
15 * - or both in dual license, as here. *
16 * *
17 * (see https://agrum.gitlab.io/articles/dual-licenses-lgplv3mit.html) *
18 * *
19 * This aGrUM/pyAgrum library is distributed in the hope that it will be *
20 * useful, but WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, *
21 * INCLUDING BUT NOT LIMITED TO THE WARRANTIES MERCHANTABILITY or FITNESS *
22 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE *
23 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *
24 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, *
25 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR *
26 * OTHER DEALINGS IN THE SOFTWARE. *
27 * *
28 * See LICENCES for more details. *
29 * *
30 * SPDX-FileCopyrightText: Copyright 2005-2025 *
31 * - Pierre-Henri WUILLEMIN(_at_LIP6) *
32 * - Christophe GONZALES(_at_AMU) *
33 * SPDX-License-Identifier: LGPL-3.0-or-later OR MIT *
34 * *
35 * Contact : info_at_agrum_dot_org *
36 * homepage : http://agrum.gitlab.io *
37 * gitlab : https://gitlab.com/agrumery/agrum *
38 * *
39 ****************************************************************************/
40
41
54#ifndef GUM_DAG_CYCLE_DETECTOR_H
55#define GUM_DAG_CYCLE_DETECTOR_H
56
57#include <vector>
58
60
61namespace gum {
62
63 /* ===========================================================================
64 */
65 // DAG CYCLE DETECTOR
66 /* ===========================================================================
67 */
82 public:
83 // the type of modification that can be applied to the graph
85
87 class Change {
88 public:
90 Change(const Change& from) noexcept;
91 Change(Change&& from) noexcept;
92 virtual ~Change() noexcept;
93
94 protected:
95 Change& operator=(const Change& from) noexcept;
96 Change& operator=(Change&& from) noexcept;
97
98 public:
99 // ##########################################################################
101 // ##########################################################################
103
105 ChangeType type() const noexcept;
106
108 NodeId tail() const noexcept;
109
111 NodeId head() const noexcept;
112
114
115 private:
118
121
124 };
125
132 class ArcAdd: public Change {
133 public:
134 // ##########################################################################
136 // ##########################################################################
139 ArcAdd(NodeId tail, NodeId head) noexcept;
140
142 ArcAdd(const ArcAdd& from) noexcept;
143
145 ArcAdd(ArcAdd&& from) noexcept;
146
148 ~ArcAdd() noexcept;
149
151
152 // ##########################################################################
154 // ##########################################################################
156
158 ArcAdd& operator=(const ArcAdd& from) noexcept;
159
161 ArcAdd& operator=(ArcAdd&& from) noexcept;
162
164 };
165
172 class ArcDel: public Change {
173 public:
174 // ##########################################################################
176 // ##########################################################################
179 ArcDel(NodeId tail, NodeId head) noexcept;
180
182 ArcDel(const ArcDel& from) noexcept;
183
185 ArcDel(ArcDel&& from) noexcept;
186
188 ~ArcDel() noexcept;
189
191
192 // ##########################################################################
194 // ##########################################################################
196
198 ArcDel& operator=(const ArcDel& from) noexcept;
199
201 ArcDel& operator=(ArcDel&& from) noexcept;
202
204 };
205
211 class ArcReverse: public Change {
212 public:
213 // ##########################################################################
215 // ##########################################################################
218 ArcReverse(NodeId tail, NodeId head) noexcept;
219
221 ArcReverse(const ArcReverse& from) noexcept;
222
224 ArcReverse(ArcReverse&& from) noexcept;
225
227 ~ArcReverse() noexcept;
228
230
231 // ##########################################################################
233 // ##########################################################################
235
237 ArcReverse& operator=(const ArcReverse& from) noexcept;
238
240 ArcReverse& operator=(ArcReverse&& from) noexcept;
241
243 };
244
245 // ############################################################################
247 // ############################################################################
249
251 DAGCycleDetector() noexcept;
252
255
258
261
263
264 // ############################################################################
266 // ############################################################################
268
270 DAGCycleDetector& operator=(const DAGCycleDetector& from);
271
273 DAGCycleDetector& operator=(DAGCycleDetector&& from);
274
276
277 bool operator==(const DAGCycleDetector& from) const;
278
280
281 bool operator!=(const DAGCycleDetector& from) const;
282
284
285 // ############################################################################
287 // ############################################################################
289
291 void setDAG(const DAG& dag);
292
294
297 void addArc(NodeId x, NodeId y);
298
300
301 void eraseArc(NodeId x, NodeId y);
302
304
307 void reverseArc(NodeId x, NodeId y);
308
310
311 bool hasCycleFromAddition(NodeId x, NodeId y) const noexcept;
312
314
315 bool hasCycleFromReversal(NodeId x, NodeId y) const noexcept;
316
318
319 bool hasCycleFromDeletion(NodeId x, NodeId y) const noexcept;
320
322
329 bool hasCycleFromModifications(const std::vector< Change >& modifs) const;
330
332
333 private:
336
338
340
342
344
346 void _addWeightedSet_(NodeProperty< Size >& nodeset,
347 const NodeProperty< Size >& set_to_add,
348 Size multiplier) const;
349
351 void _delWeightedSet_(NodeProperty< Size >& nodeset,
352 const NodeProperty< Size >& set_to_del,
353 Size multiplier) const;
354
358 void _restrictWeightedSet_(NodeProperty< Size >& result_set,
359 const NodeProperty< Size >& set_to_restrict,
360 const NodeSet& extrmities) const;
361 };
362
363} /* namespace gum */
364
365#ifndef GUM_NO_INLINE
367#endif // GU%_NO_INLINE
368
369#endif // GUM_DAG_CYCLE_DETECTOR_H
A class for detecting directed cycles in DAGs when trying to apply many changes to the graph.
Base classes for directed acyclic graphs.
ArcAdd & operator=(const ArcAdd &from) noexcept
copy operator
ArcAdd(NodeId tail, NodeId head) noexcept
default constructor
ArcDel(NodeId tail, NodeId head) noexcept
default constructor
ArcDel & operator=(const ArcDel &from) noexcept
copy operator
ArcReverse & operator=(const ArcReverse &from) noexcept
copy operator
ArcReverse(NodeId tail, NodeId head) noexcept
default constructor
the base class indicating the possible changes
ChangeType type() const noexcept
returns the type of the operation
NodeId tail() const noexcept
indicates the tail of the arc involved in the modification
NodeId _head_
the head of the arc to be modified
Change & operator=(const Change &from) noexcept
ChangeType _type_
the type of modification
Change(ChangeType type, NodeId tail, NodeId head) noexcept
NodeId _tail_
the tail of the arc to be modified
NodeId head() const noexcept
indicates the head of the arc involved in the modification
DAGCycleDetector() noexcept
default constructor
bool hasCycleFromModifications(const std::vector< Change > &modifs) const
indicates whether a set of modifications would create a cycle
bool hasCycleFromDeletion(NodeId x, NodeId y) const noexcept
indicates whether an arc deletion would create a cycle
NodeProperty< NodeProperty< Size > > _descendants_
the set of descendants of each node in the dag
void _addWeightedSet_(NodeProperty< Size > &nodeset, const NodeProperty< Size > &set_to_add, Size multiplier) const
adds a weighted nodeset to another (weights are added)
void addArc(NodeId x, NodeId y)
adds a new arc to the current DAG
void setDAG(const DAG &dag)
sets the initial DAG from which changes shall be applied
bool hasCycleFromReversal(NodeId x, NodeId y) const noexcept
indicates wether an arc reversal would create a cycle
NodeProperty< NodeProperty< Size > > _ancestors_
the set of ancestors of each node in the dag
void _delWeightedSet_(NodeProperty< Size > &nodeset, const NodeProperty< Size > &set_to_del, Size multiplier) const
removes a weighted nodeset from another (weights are subtracted)
void reverseArc(NodeId x, NodeId y)
reverses an arc from the DAG
void eraseArc(NodeId x, NodeId y)
removes an arc from the current DAG
DiGraph _dag_
the initial dag from which modifications are applied
void _restrictWeightedSet_(NodeProperty< Size > &result_set, const NodeProperty< Size > &set_to_restrict, const NodeSet &extrmities) const
put into a weighted nodeset the nodes of another weighted set that belong to a set of arc extremities
bool hasCycleFromAddition(NodeId x, NodeId y) const noexcept
indicates whether an arc addition would create a cycle
Base class for dag.
Definition DAG.h:121
Base class for all oriented graphs.
Definition diGraph.h:130
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition types.h:74
Size NodeId
Type for node ids.
HashTable< NodeId, VAL > NodeProperty
Property on graph elements.
Set< NodeId > NodeSet
Some typdefs and define for shortcuts ...
gum is the global namespace for all aGrUM entities
Definition agrum.h:46
STL namespace.