![]() |
aGrUM
0.16.0
|
A class for detecting directed cycles in DAGs when trying to apply many changes to the graph. More...
#include <DAGCycleDetector.h>
Public Member Functions | |
Constructors / Destructors | |
DAGCycleDetector () noexcept | |
default constructor More... | |
DAGCycleDetector (const DAGCycleDetector &from) | |
copy constructor More... | |
DAGCycleDetector (DAGCycleDetector &&from) | |
move constructor More... | |
~DAGCycleDetector () | |
destructor More... | |
Operators | |
DAGCycleDetector & | operator= (const DAGCycleDetector &from) |
copy operator More... | |
DAGCycleDetector & | operator= (DAGCycleDetector &&from) |
move operator More... | |
bool | operator== (const DAGCycleDetector &from) const |
check the equality between two DAGCycleDetectors More... | |
bool | operator!= (const DAGCycleDetector &from) const |
check the inequality between two DAGCycleDetectors More... | |
Accessors/Modifiers | |
void | setDAG (const DAG &dag) |
sets the initial DAG from which changes shall be applied More... | |
void | addArc (NodeId x, NodeId y) |
adds a new arc to the current DAG More... | |
void | eraseArc (NodeId x, NodeId y) |
removes an arc from the current DAG More... | |
void | reverseArc (NodeId x, NodeId y) |
reverses an arc from the DAG More... | |
bool | hasCycleFromAddition (NodeId x, NodeId y) const noexcept |
indicates whether an arc addition would create a cycle More... | |
bool | hasCycleFromReversal (NodeId x, NodeId y) const noexcept |
indicates wether an arc reversal would create a cycle More... | |
bool | hasCycleFromDeletion (NodeId x, NodeId y) const noexcept |
indicates whether an arc deletion would create a cycle More... | |
bool | hasCycleFromModifications (const std::vector< Change > &modifs) const |
indicates whether a set of modifications would create a cycle More... | |
Public Types | |
enum | ChangeType { ChangeType::ARC_ADDITION, ChangeType::ARC_DELETION, ChangeType::ARC_REVERSAL } |
Classes | |
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... | |
class | Change |
the base class indicating the possible changes More... | |
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 62 of file DAGCycleDetector.h.
|
strong |
Enumerator | |
---|---|
ARC_ADDITION | |
ARC_DELETION | |
ARC_REVERSAL |
Definition at line 65 of file DAGCycleDetector.h.
|
noexcept |
default constructor
Definition at line 245 of file DAGCycleDetector_inl.h.
INLINE gum::DAGCycleDetector::DAGCycleDetector | ( | const DAGCycleDetector & | from | ) |
copy constructor
Definition at line 250 of file DAGCycleDetector_inl.h.
INLINE gum::DAGCycleDetector::DAGCycleDetector | ( | DAGCycleDetector && | from | ) |
move constructor
Definition at line 257 of file DAGCycleDetector_inl.h.
INLINE gum::DAGCycleDetector::~DAGCycleDetector | ( | ) |
destructor
Definition at line 264 of file DAGCycleDetector_inl.h.
References operator=().
|
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 311 of file DAGCycleDetector_inl.h.
References gum::HashTable< Key, Val, Alloc >::cbegin(), gum::HashTable< Key, Val, Alloc >::cend(), gum::HashTable< Key, Val, Alloc >::exists(), and gum::HashTable< Key, Val, Alloc >::insert().
Referenced by addArc(), hasCycleFromModifications(), and setDAG().
|
private |
removes a weighted nodeset from another (weights are subtracted)
Definition at line 325 of file DAGCycleDetector_inl.h.
References gum::HashTable< Key, Val, Alloc >::cbegin(), gum::HashTable< Key, Val, Alloc >::cend(), gum::HashTable< Key, Val, Alloc >::erase(), and gum::HashTable< Key, Val, Alloc >::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 341 of file DAGCycleDetector_inl.h.
References gum::HashTable< Key, Val, Alloc >::cbegin(), gum::HashTable< Key, Val, Alloc >::cend(), gum::Set< Key, Alloc >::exists(), and gum::HashTable< Key, Val, Alloc >::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 306 of file DAGCycleDetector.cpp.
References __addWeightedSet(), __ancestors, __dag, __descendants, gum::DiGraph::addArc(), gum::HashTable< Key, Val, Alloc >::cbegin(), gum::HashTable< Key, Val, Alloc >::cend(), gum::ArcGraphPart::existsArc(), GUM_ERROR, hasCycleFromAddition(), and gum::HashTable< Key, Val, Alloc >::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 345 of file DAGCycleDetector.cpp.
References __ancestors, __dag, __delWeightedSet(), __descendants, gum::HashTable< Key, Val, Alloc >::cbegin(), gum::HashTable< Key, Val, Alloc >::cend(), gum::ArcGraphPart::eraseArc(), gum::ArcGraphPart::existsArc(), and gum::HashTable< Key, Val, Alloc >::insert().
Referenced by reverseArc().
indicates whether an arc addition would create a cycle
worst case complexity: O(1)
Definition at line 292 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 304 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 115 of file DAGCycleDetector.cpp.
References __addWeightedSet(), __ancestors, __delWeightedSet(), __descendants, __restrictWeightedSet(), ARC_ADDITION, ARC_DELETION, ARC_REVERSAL, gum::HashTable< Key, Val, Alloc >::beginSafe(), gum::HashTable< Key, Val, Alloc >::cbegin(), gum::HashTable< Key, Val, Alloc >::cend(), gum::HashTable< Key, Val, Alloc >::endSafe(), gum::HashTable< Key, Val, Alloc >::erase(), gum::HashTable< Key, Val, Alloc >::exists(), GUM_ERROR, gum::Arc::head(), gum::Set< Key, Alloc >::insert(), gum::HashTable< Key, Val, Alloc >::insert(), and gum::Arc::tail().
indicates wether an arc reversal would create a cycle
worst case complexity: O(1)
Definition at line 298 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 371 of file DAGCycleDetector_inl.h.
References operator==().
INLINE DAGCycleDetector & gum::DAGCycleDetector::operator= | ( | const DAGCycleDetector & | from | ) |
copy operator
Definition at line 270 of file DAGCycleDetector_inl.h.
References __ancestors, __dag, and __descendants.
Referenced by ~DAGCycleDetector().
INLINE DAGCycleDetector & gum::DAGCycleDetector::operator= | ( | DAGCycleDetector && | from | ) |
move operator
Definition at line 281 of file DAGCycleDetector_inl.h.
References __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 365 of file DAGCycleDetector_inl.h.
References __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 354 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 46 of file DAGCycleDetector.cpp.
References __addWeightedSet(), __ancestors, __dag, __descendants, gum::List< Val, Alloc >::empty(), gum::List< Val, Alloc >::front(), gum::List< Val, Alloc >::insert(), gum::HashTable< Key, Val, Alloc >::insert(), gum::List< Val, Alloc >::popFront(), and gum::HashTable< Key, Val, Alloc >::size().
Referenced by gum::learning::StructuralConstraintDAG::StructuralConstraintDAG().
|
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 320 of file DAGCycleDetector.h.
Referenced by addArc(), eraseArc(), hasCycleFromModifications(), hasCycleFromReversal(), operator=(), operator==(), and setDAG().
|
private |
the initial dag from which modifications are applied
Definition at line 316 of file DAGCycleDetector.h.
Referenced by addArc(), eraseArc(), 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 324 of file DAGCycleDetector.h.
Referenced by addArc(), eraseArc(), hasCycleFromAddition(), hasCycleFromModifications(), operator=(), operator==(), and setDAG().