![]() |
aGrUM 2.3.2
a C++ library for (probabilistic) graphical models
|
A class for detecting directed cycles in DAGs when trying to apply many changes to the graph. More...
#include <DAGCycleDetector.h>
Classes | |
| class | Change |
| the base class indicating the possible changes More... | |
| class | ArcAdd |
| the class to indicate that we wish to add a new arc More... | |
| class | ArcDel |
| the class to indicate that we wish to remove an arc More... | |
| class | ArcReverse |
| the class to indicate that we wish to reverse an arc More... | |
Public Types | |
| enum class | ChangeType { ARC_ADDITION , ARC_DELETION , ARC_REVERSAL } |
Public Member Functions | |
Constructors / Destructors | |
| DAGCycleDetector () noexcept | |
| default constructor | |
| DAGCycleDetector (const DAGCycleDetector &from) | |
| copy constructor | |
| DAGCycleDetector (DAGCycleDetector &&from) | |
| move constructor | |
| ~DAGCycleDetector () | |
| destructor | |
Operators | |
| DAGCycleDetector & | operator= (const DAGCycleDetector &from) |
| copy operator | |
| DAGCycleDetector & | operator= (DAGCycleDetector &&from) |
| move operator | |
| bool | operator== (const DAGCycleDetector &from) const |
| check the equality between two DAGCycleDetectors | |
| bool | operator!= (const DAGCycleDetector &from) const |
| check the inequality between two DAGCycleDetectors | |
Accessors/Modifiers | |
| void | setDAG (const DAG &dag) |
| sets the initial DAG from which changes shall be applied | |
| void | addArc (NodeId x, NodeId y) |
| adds a new arc to the current DAG | |
| void | eraseArc (NodeId x, NodeId y) |
| removes an arc from the current DAG | |
| void | reverseArc (NodeId x, NodeId y) |
| reverses an arc from the DAG | |
| bool | hasCycleFromAddition (NodeId x, NodeId y) const noexcept |
| indicates whether an arc addition would create a cycle | |
| bool | hasCycleFromReversal (NodeId x, NodeId y) const noexcept |
| indicates wether an arc reversal would create a cycle | |
| bool | hasCycleFromDeletion (NodeId x, NodeId y) const noexcept |
| indicates whether an arc deletion would create a cycle | |
| bool | hasCycleFromModifications (const std::vector< Change > &modifs) const |
| indicates whether a set of modifications would create a cycle | |
Private Member Functions | |
| void | _addWeightedSet_ (NodeProperty< Size > &nodeset, const NodeProperty< Size > &set_to_add, Size multiplier) const |
| adds a weighted nodeset to another (weights are added) | |
| void | _delWeightedSet_ (NodeProperty< Size > &nodeset, const NodeProperty< Size > &set_to_del, Size multiplier) const |
| removes a weighted nodeset from another (weights are subtracted) | |
| 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 | |
Private Attributes | |
| DiGraph | _dag_ |
| the initial dag from which modifications are applied | |
| NodeProperty< NodeProperty< Size > > | _ancestors_ |
| the set of ancestors of each node in the dag | |
| NodeProperty< NodeProperty< Size > > | _descendants_ |
| the set of descendants of each node in the dag | |
A class for detecting directed cycles in DAGs when trying to apply many changes to the graph.
When trying to assess whether multiple changes applied to a given DAG would induce cycles, use class DAGCycleDetector instead of trying to apply the modifications to the DAG itself and check whether and exception is raised: the class is designed to be fast for such modifications. However, the number of modifications checked should be higher than at least 3 for this class to be competitive.
Definition at line 81 of file DAGCycleDetector.h.
|
strong |
|
noexcept |
default constructor
Definition at line 245 of file DAGCycleDetector_inl.h.
References DAGCycleDetector().
Referenced by DAGCycleDetector(), DAGCycleDetector(), DAGCycleDetector(), ~DAGCycleDetector(), operator!=(), operator=(), operator=(), and operator==().
| INLINE gum::DAGCycleDetector::DAGCycleDetector | ( | const DAGCycleDetector & | from | ) |
copy constructor
Definition at line 248 of file DAGCycleDetector_inl.h.
References DAGCycleDetector(), _ancestors_, _dag_, and _descendants_.
| INLINE gum::DAGCycleDetector::DAGCycleDetector | ( | DAGCycleDetector && | from | ) |
move constructor
Definition at line 254 of file DAGCycleDetector_inl.h.
References DAGCycleDetector(), _ancestors_, _dag_, and _descendants_.
| INLINE gum::DAGCycleDetector::~DAGCycleDetector | ( | ) |
destructor
Definition at line 261 of file DAGCycleDetector_inl.h.
References DAGCycleDetector().
|
private |
adds a weighted nodeset to another (weights are added)
adds a nodeset to another (nodes are weighted, so weights are added)
Definition at line 303 of file DAGCycleDetector_inl.h.
References gum::HashTable< Key, Val >::cbegin(), gum::HashTable< Key, Val >::cend(), gum::HashTable< Key, Val >::exists(), and gum::HashTable< Key, Val >::insert().
Referenced by addArc(), hasCycleFromModifications(), and setDAG().
|
private |
removes a weighted nodeset from another (weights are subtracted)
Definition at line 317 of file DAGCycleDetector_inl.h.
References gum::HashTable< Key, Val >::cbegin(), gum::HashTable< Key, Val >::cend(), gum::HashTable< Key, Val >::erase(), and gum::HashTable< Key, Val >::exists().
Referenced by eraseArc(), and hasCycleFromModifications().
|
private |
put into a weighted nodeset the nodes of another weighted set that belong to a set of arc extremities
Definition at line 333 of file DAGCycleDetector_inl.h.
References gum::HashTable< Key, Val >::cbegin(), gum::HashTable< Key, Val >::cend(), gum::Set< Key >::exists(), and gum::HashTable< Key, Val >::insert().
Referenced by hasCycleFromModifications().
adds a new arc to the current DAG
worst case complexity: O(h^2) where h is the height of the DAG
| InvalidDirectedCycle | if the arc would create a cycle in the dag |
Definition at line 310 of file DAGCycleDetector.cpp.
References _addWeightedSet_(), _ancestors_, _dag_, _descendants_, gum::HashTable< Key, Val >::cbegin(), gum::HashTable< Key, Val >::cend(), GUM_ERROR, hasCycleFromAddition(), and gum::HashTable< Key, Val >::insert().
Referenced by reverseArc().
removes an arc from the current DAG
worst case complexity: O(h^2) where h is the height of the DAG
Definition at line 346 of file DAGCycleDetector.cpp.
References _ancestors_, _dag_, _delWeightedSet_(), _descendants_, gum::HashTable< Key, Val >::cbegin(), gum::HashTable< Key, Val >::cend(), and gum::HashTable< Key, Val >::insert().
Referenced by reverseArc().
indicates whether an arc addition would create a cycle
worst case complexity: O(1)
Definition at line 287 of file DAGCycleDetector_inl.h.
References _descendants_.
Referenced by addArc().
indicates whether an arc deletion would create a cycle
worst case complexity: O(1)
Definition at line 297 of file DAGCycleDetector_inl.h.
indicates whether a set of modifications would create a cycle
the Boolean returned corresponds to the existence (or not) of a cycle on the graph resulting from the whole sequence of modifications. This means, for instance, that if, among modifs, there exists an arc (X,Y) addition involving a cycle but also the same arc (X,Y) deletion, the final graph obtained does not contains a cycle (due to the deletion) and the method will thus return false.
Definition at line 134 of file DAGCycleDetector.cpp.
References _addWeightedSet_(), _ancestors_, _delWeightedSet_(), _descendants_, _restrictWeightedSet_(), ARC_ADDITION, ARC_DELETION, ARC_REVERSAL, gum::HashTable< Key, Val >::beginSafe(), gum::HashTable< Key, Val >::cbegin(), gum::HashTable< Key, Val >::cend(), gum::HashTable< Key, Val >::endSafe(), gum::HashTable< Key, Val >::erase(), gum::HashTable< Key, Val >::exists(), GUM_ERROR, gum::Arc::head(), gum::HashTable< Key, Val >::insert(), gum::Set< Key >::insert(), and gum::Arc::tail().
indicates wether an arc reversal would create a cycle
worst case complexity: O(1)
Definition at line 292 of file DAGCycleDetector_inl.h.
References _ancestors_.
Referenced by reverseArc().
| INLINE bool gum::DAGCycleDetector::operator!= | ( | const DAGCycleDetector & | from | ) | const |
check the inequality between two DAGCycleDetectors
used essentally for debugging purposes
Definition at line 358 of file DAGCycleDetector_inl.h.
References DAGCycleDetector(), and operator==().
| INLINE DAGCycleDetector & gum::DAGCycleDetector::operator= | ( | const DAGCycleDetector & | from | ) |
copy operator
Definition at line 265 of file DAGCycleDetector_inl.h.
References DAGCycleDetector(), _ancestors_, _dag_, and _descendants_.
| INLINE DAGCycleDetector & gum::DAGCycleDetector::operator= | ( | DAGCycleDetector && | from | ) |
move operator
Definition at line 276 of file DAGCycleDetector_inl.h.
References DAGCycleDetector(), _ancestors_, _dag_, and _descendants_.
| INLINE bool gum::DAGCycleDetector::operator== | ( | const DAGCycleDetector & | from | ) | const |
check the equality between two DAGCycleDetectors
used essentally for debugging purposes
Definition at line 352 of file DAGCycleDetector_inl.h.
References DAGCycleDetector(), _ancestors_, and _descendants_.
Referenced by operator!=().
reverses an arc from the DAG
worst case complexity: O(h^2) where h is the height of the DAG
| InvalidDirectedCycle | if the arc reversal would create a cycle in the dag |
Definition at line 342 of file DAGCycleDetector_inl.h.
References addArc(), eraseArc(), GUM_ERROR, and hasCycleFromReversal().
| void gum::DAGCycleDetector::setDAG | ( | const DAG & | dag | ) |
sets the initial DAG from which changes shall be applied
Definition at line 65 of file DAGCycleDetector.cpp.
References _addWeightedSet_(), _ancestors_, _dag_, _descendants_, gum::ArcGraphPart::children(), gum::List< Val >::empty(), gum::List< Val >::front(), gum::HashTable< Key, Val >::insert(), gum::List< Val >::insert(), gum::ArcGraphPart::parents(), gum::List< Val >::popFront(), and gum::Set< Key >::size().
|
private |
the set of ancestors of each node in the dag
for each ancestor, we keep track of the number of paths leading to it
Definition at line 339 of file DAGCycleDetector.h.
Referenced by DAGCycleDetector(), DAGCycleDetector(), addArc(), eraseArc(), hasCycleFromModifications(), hasCycleFromReversal(), operator=(), operator=(), operator==(), and setDAG().
|
private |
the initial dag from which modifications are applied
Definition at line 335 of file DAGCycleDetector.h.
Referenced by DAGCycleDetector(), DAGCycleDetector(), addArc(), eraseArc(), operator=(), operator=(), and setDAG().
|
private |
the set of descendants of each node in the dag
for each ancestor, we keep track of the number of paths leading to it
Definition at line 343 of file DAGCycleDetector.h.
Referenced by DAGCycleDetector(), DAGCycleDetector(), addArc(), eraseArc(), hasCycleFromAddition(), hasCycleFromModifications(), operator=(), operator=(), operator==(), and setDAG().