aGrUM 3.1.1
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-2026 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-2026 *
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 */
81 public:
82 // the type of modification that can be applied to the graph
84
86 class Change {
87 public:
89 Change(const Change& from) noexcept;
90 Change(Change&& from) noexcept;
91 virtual ~Change() noexcept;
92
93 protected:
94 Change& operator=(const Change& from) noexcept;
95 Change& operator=(Change&& from) noexcept;
96
97 public:
98 // ##########################################################################
100 // ##########################################################################
102
104 ChangeType type() const noexcept;
105
107 NodeId tail() const noexcept;
108
110 NodeId head() const noexcept;
111
113
114 private:
117
120
123 };
124
131 class ArcAdd: public Change {
132 public:
133 // ##########################################################################
135 // ##########################################################################
138 ArcAdd(NodeId tail, NodeId head) noexcept;
139
141 ArcAdd(const ArcAdd& from) noexcept;
142
144 ArcAdd(ArcAdd&& from) noexcept;
145
147 ~ArcAdd() noexcept override;
148
150
151 // ##########################################################################
153 // ##########################################################################
155
157 ArcAdd& operator=(const ArcAdd& from) noexcept;
158
160 ArcAdd& operator=(ArcAdd&& from) noexcept;
161
163 };
164
171 class ArcDel: public Change {
172 public:
173 // ##########################################################################
175 // ##########################################################################
178 ArcDel(NodeId tail, NodeId head) noexcept;
179
181 ArcDel(const ArcDel& from) noexcept;
182
184 ArcDel(ArcDel&& from) noexcept;
185
187 ~ArcDel() noexcept override;
188
190
191 // ##########################################################################
193 // ##########################################################################
195
197 ArcDel& operator=(const ArcDel& from) noexcept;
198
200 ArcDel& operator=(ArcDel&& from) noexcept;
201
203 };
204
210 class ArcReverse: public Change {
211 public:
212 // ##########################################################################
214 // ##########################################################################
217 ArcReverse(NodeId tail, NodeId head) noexcept;
218
220 ArcReverse(const ArcReverse& from) noexcept;
221
223 ArcReverse(ArcReverse&& from) noexcept;
224
226 ~ArcReverse() noexcept override;
227
229
230 // ##########################################################################
232 // ##########################################################################
234
236 ArcReverse& operator=(const ArcReverse& from) noexcept;
237
239 ArcReverse& operator=(ArcReverse&& from) noexcept;
240
242 };
243
244 // ############################################################################
246 // ############################################################################
248
250 DAGCycleDetector() noexcept;
251
254
257
260
262
263 // ############################################################################
265 // ############################################################################
267
269 DAGCycleDetector& operator=(const DAGCycleDetector& from);
270
272 DAGCycleDetector& operator=(DAGCycleDetector&& from);
273
275
276 bool operator==(const DAGCycleDetector& from) const;
277
279
280 bool operator!=(const DAGCycleDetector& from) const;
281
283
284 // ############################################################################
286 // ############################################################################
288
290 void setDAG(const DAG& dag);
291
293
296 void addArc(NodeId x, NodeId y);
297
299
300 void eraseArc(NodeId x, NodeId y);
301
303
306 void reverseArc(NodeId x, NodeId y);
307
309
310 bool hasCycleFromAddition(NodeId x, NodeId y) const noexcept;
311
313
314 bool hasCycleFromReversal(NodeId x, NodeId y) const noexcept;
315
317
318 bool hasCycleFromDeletion(NodeId x, NodeId y) const noexcept;
319
321
328 bool hasCycleFromModifications(const std::vector< Change >& modifs) const;
329
331
332 private:
335
337
339
341
343
345 void _addWeightedSet_(NodeProperty< Size >& nodeset,
346 const NodeProperty< Size >& set_to_add,
347 Size multiplier) const;
348
350 void _delWeightedSet_(NodeProperty< Size >& nodeset,
351 const NodeProperty< Size >& set_to_del,
352 Size multiplier) const;
353
357 void _restrictWeightedSet_(NodeProperty< Size >& result_set,
358 const NodeProperty< Size >& set_to_restrict,
359 const NodeSet& extrmities) const;
360 };
361
362} /* namespace gum */
363
364#ifndef GUM_NO_INLINE
366#endif // GU%_NO_INLINE
367
368#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
void _delWeightedSet_(NodeProperty< Size > &nodeset, const NodeProperty< Size > &set_to_del, Size multiplier) const
removes a weighted nodeset from another (weights are subtracted)
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 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:132
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.