aGrUM 2.3.2
a C++ library for (probabilistic) graphical models
DAGCycleDetector_inl.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#pragma once
41
48
49namespace gum {
50
51 /* ===========================================================================
52 */
53 // CHANGES
54 /* ===========================================================================
55 */
56
57 // default constructor
59 _type_{type}, _tail_{tail}, _head_{head} {
60 GUM_CONSTRUCTOR(DAGCycleDetector::Change);
61 }
62
63 // copy constructor
65 _type_{from._type_}, _tail_{from._tail_}, _head_{from._head_} {
66 GUM_CONS_CPY(DAGCycleDetector::Change);
67 }
68
69 // move constructor
71 _type_{from._type_}, _tail_{from._tail_}, _head_{from._head_} {
72 GUM_CONS_MOV(DAGCycleDetector::Change);
73 }
74
75 // destructor
76 INLINE DAGCycleDetector::Change::~Change() noexcept { GUM_DESTRUCTOR(DAGCycleDetector::Change); }
77
78 // copy operator
81 _type_ = from._type_;
82 _tail_ = from._tail_;
83 _head_ = from._head_;
84 return *this;
85 }
86
87 // move operator
90 _type_ = from._type_;
91 _tail_ = from._tail_;
92 _head_ = from._head_;
93 return *this;
94 }
95
98 return _type_;
99 }
100
102 INLINE NodeId DAGCycleDetector::Change::tail() const noexcept { return _tail_; }
103
105 INLINE NodeId DAGCycleDetector::Change::head() const noexcept { return _head_; }
106
107 /* ===========================================================================
108 */
109 // ArcAdd
110 /* ===========================================================================
111 */
112
118
121 DAGCycleDetector::Change(from.type(), from.tail(), from.head()) {
122 GUM_CONS_CPY(DAGCycleDetector::ArcAdd);
123 }
124
127 DAGCycleDetector::Change(std::move(from.type()),
128 std::move(from.tail()),
129 std::move(from.head())) {
130 GUM_CONS_MOV(DAGCycleDetector::ArcAdd);
131 }
132
134 INLINE DAGCycleDetector::ArcAdd::~ArcAdd() noexcept { GUM_DESTRUCTOR(DAGCycleDetector::ArcAdd); }
135
142
147 return *this;
148 }
149
150 /* ===========================================================================
151 */
152 // ArcDel
153 /* ===========================================================================
154 */
155
161
164 DAGCycleDetector::Change(from.type(), from.tail(), from.head()) {
165 GUM_CONS_CPY(DAGCycleDetector::ArcDel);
166 }
167
170 DAGCycleDetector::Change(std::move(from.type()),
171 std::move(from.tail()),
172 std::move(from.head())) {
173 GUM_CONS_MOV(DAGCycleDetector::ArcDel);
174 }
175
177 INLINE DAGCycleDetector::ArcDel::~ArcDel() noexcept { GUM_DESTRUCTOR(DAGCycleDetector::ArcDel); }
178
185
190 return *this;
191 }
192
193 /* ===========================================================================
194 */
195 // ArcReverse
196 /* ===========================================================================
197 */
198
204
207 : DAGCycleDetector::Change(from.type(), from.tail(), from.head()) {
208 GUM_CONS_CPY(DAGCycleDetector::ArcReverse);
209 }
210
213 DAGCycleDetector::Change(std::move(from.type()),
214 std::move(from.tail()),
215 std::move(from.head())) {
216 GUM_CONS_MOV(DAGCycleDetector::ArcReverse);
217 }
218
221 GUM_DESTRUCTOR(DAGCycleDetector::ArcReverse);
222 }
223
230
237
238 /* ===========================================================================
239 */
240 // DAGCycleDetector
241 /* ===========================================================================
242 */
243
245 INLINE DAGCycleDetector::DAGCycleDetector() noexcept { GUM_CONSTRUCTOR(DAGCycleDetector); }
246
252
255 _dag_(std::move(from._dag_)), _ancestors_(std::move(from._ancestors_)),
256 _descendants_(std::move(from._descendants_)) {
257 GUM_CONS_MOV(DAGCycleDetector);
258 }
259
262
264 INLINE
266 if (this != &from) {
267 _dag_ = from._dag_;
270 }
271
272 return *this;
273 }
274
277 if (this != &from) {
278 _dag_ = std::move(from._dag_);
279 _ancestors_ = std::move(from._ancestors_);
280 _descendants_ = std::move(from._descendants_);
281 }
282
283 return *this;
284 }
285
287 INLINE bool DAGCycleDetector::hasCycleFromAddition(NodeId x, NodeId y) const noexcept {
288 return _descendants_[y].exists(x);
289 }
290
292 INLINE bool DAGCycleDetector::hasCycleFromReversal(NodeId x, NodeId y) const noexcept {
293 return (_ancestors_[y][x] > 1);
294 }
295
297 INLINE bool DAGCycleDetector::hasCycleFromDeletion(NodeId x, NodeId y) const noexcept {
298 return false;
299 }
300
302 INLINE
304 const NodeProperty< Size >& set_to_add,
305 Size multiplier) const {
306 for (auto iter = set_to_add.cbegin(); iter != set_to_add.cend(); ++iter) {
307 if (nodeset.exists(iter.key())) {
308 nodeset[iter.key()] += iter.val() * multiplier;
309 } else {
310 nodeset.insert(iter.key(), iter.val() * multiplier);
311 }
312 }
313 }
314
316 INLINE
318 const NodeProperty< Size >& set_to_del,
319 Size multiplier) const {
320 for (auto iter = set_to_del.cbegin(); iter != set_to_del.cend(); ++iter) {
321 if (nodeset.exists(iter.key())) {
322 Size& weight = nodeset[iter.key()];
323 weight -= iter.val() * multiplier;
324
325 if (!weight) { nodeset.erase(iter.key()); }
326 }
327 }
328 }
329
332 INLINE
334 const NodeProperty< Size >& set_to_restrict,
335 const NodeSet& extremities) const {
336 for (auto iter = set_to_restrict.cbegin(); iter != set_to_restrict.cend(); ++iter) {
337 if (extremities.exists(iter.key())) { result_set.insert(iter.key(), iter.val()); }
338 }
339 }
340
343 if (hasCycleFromReversal(tail, head)) {
344 GUM_ERROR(InvalidDirectedCycle, "the arc would create a directed into a DAG")
345 }
346
347 eraseArc(tail, head);
348 addArc(head, tail);
349 }
350
352 INLINE bool DAGCycleDetector::operator==(const DAGCycleDetector& from) const {
353 return ( //( _dagmodel_ == from. _dagmodel_ ) &&
354 (_ancestors_ == from._ancestors_) && (_descendants_ == from._descendants_));
355 }
356
358 INLINE bool DAGCycleDetector::operator!=(const DAGCycleDetector& from) const {
359 return !operator==(from);
360 }
361
362} /* namespace gum */
the class to indicate that we wish to add a new arc
ArcAdd & operator=(const ArcAdd &from) noexcept
copy operator
ArcAdd(NodeId tail, NodeId head) noexcept
default constructor
the class to indicate that we wish to remove an arc
ArcDel(NodeId tail, NodeId head) noexcept
default constructor
ArcDel & operator=(const ArcDel &from) noexcept
copy operator
the class to indicate that we wish to reverse an arc
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 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
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
DAGCycleDetector & operator=(const DAGCycleDetector &from)
copy operator
void _delWeightedSet_(NodeProperty< Size > &nodeset, const NodeProperty< Size > &set_to_del, Size multiplier) const
removes a weighted nodeset from another (weights are subtracted)
bool operator!=(const DAGCycleDetector &from) const
check the inequality between two DAGCycleDetectors
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
bool operator==(const DAGCycleDetector &from) const
check the equality between two DAGCycleDetectors
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
void erase(const Key &key)
Removes a given element from the hash table.
bool exists(const Key &key) const
Checks whether there exists an element with a given key in the hashtable.
const_iterator cbegin() const
Returns an unsafe const_iterator pointing to the beginning of the hashtable.
const const_iterator & cend() const noexcept
Returns the unsafe const_iterator pointing to the end of the hashtable.
value_type & insert(const Key &key, const Val &val)
Adds a new element (actually a copy of this element) into the hash table.
Exception : existence of a directed cycle in a graph.
bool exists(const Key &k) const
Indicates whether a given elements belong to the set.
Definition set_tpl.h:533
#define GUM_ERROR(type, msg)
Definition exceptions.h:72
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.