aGrUM  0.16.0
diGraph.cpp
Go to the documentation of this file.
1 
29 #include <agrum/graphs/diGraph.h>
30 
31 #ifdef GUM_NO_INLINE
33 #endif // GUM_NOINLINE
34 
35 namespace gum {
36 
37  DiGraph::DiGraph(Size nodes_size,
38  bool nodes_resize_policy,
39  Size arcs_size,
40  bool arcs_resize_policy) :
41  NodeGraphPart(nodes_size, nodes_resize_policy),
42  ArcGraphPart(arcs_size, arcs_resize_policy),
43  __mutableTopologicalOrder(nullptr) {
44  GUM_CONSTRUCTOR(DiGraph);
45  }
46 
49  GUM_CONS_CPY(DiGraph);
50  if (g.__mutableTopologicalOrder != nullptr) {
53  }
54  }
55 
57  GUM_DESTRUCTOR(DiGraph);
58  if (__mutableTopologicalOrder != nullptr) { delete __mutableTopologicalOrder; }
59  }
60 
61  const std::string DiGraph::toString() const {
62  std::string s = NodeGraphPart::toString();
63  s += " , ";
65  return s;
66  }
67 
68  const std::string DiGraph::toDot() const {
69  std::stringstream strBuff;
70  std::string tab = " ";
71  strBuff << "digraph {" << std::endl;
72 
73  for (const auto node : nodes())
74  strBuff << tab << node << ";" << std::endl;
75 
76  strBuff << std::endl;
77 
78  for (const auto& arc : arcs())
79  strBuff << tab << arc.tail() << " -> " << arc.head() << ";" << std::endl;
80 
81  strBuff << "}" << std::endl << std::endl;
82  return strBuff.str();
83  }
84 
86  std::ostream& operator<<(std::ostream& stream, const DiGraph& g) {
87  stream << g.toString();
88  return stream;
89  }
90 
92  if (clear
94  == nullptr)) { // we have to call _topologicalOrder
95  if (__mutableTopologicalOrder == nullptr) {
97  } else {
98  // clear is True
100  }
101 
103  }
104 
106  }
107 
109  auto dag = *this;
110  auto roots = std::vector< NodeId >();
111 
112  for (const auto node : dag.nodes()) {
113  if (dag.parents(node).empty()) { roots.push_back(node); }
114  }
115 
116  while (roots.size()) {
117  if (__mutableTopologicalOrder->exists(roots.back())) {
119  "cycles prevent the creation of a topological ordering.");
120  }
121  __mutableTopologicalOrder->insert(roots.back());
122  roots.pop_back();
123 
124  while (dag.children(__mutableTopologicalOrder->back()).size()) {
125  auto back = __mutableTopologicalOrder->back();
126  auto child = *(dag.children(back).begin());
127  dag.eraseArc(Arc(back, child));
128 
129  if (dag.parents(child).empty()) { roots.push_back(child); }
130  }
131  }
132 
133  GUM_ASSERT(dag.sizeArcs() == (gum::Size)(0));
134  GUM_ASSERT(__mutableTopologicalOrder->size() == dag.size());
135  }
136 
137  bool DiGraph::hasDirectedPath(const NodeId from, const NodeId to) {
138  if (!exists(from)) return false;
139 
140  // not recursive version => use a FIFO for simulating the recursion
141  List< NodeId > nodeFIFO;
142  nodeFIFO.pushBack(from);
143 
144  NodeSet marked;
145  marked.insert(from);
146 
147  NodeId new_one;
148 
149  while (!nodeFIFO.empty()) {
150  new_one = nodeFIFO.front();
151  // std::cout<<new_one<<std::endl;
152  nodeFIFO.popFront();
153 
154  for (const auto chi : children(new_one)) {
155  if (chi == to) return true;
156 
157  if (!marked.contains(chi)) {
158  nodeFIFO.pushBack(chi);
159  marked.insert(chi);
160  }
161  }
162  }
163 
164  return false;
165  }
166 
167 } /* namespace gum */
bool contains(const Key &k) const
Indicates whether a given elements belong to the set.
Definition: set_tpl.h:581
bool empty() const noexcept
Returns a boolean indicating whether the chained list is empty.
Definition: list_tpl.h:1970
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
const std::string toString() const
to friendly display the content of the ArcGraphPart
void clear()
Clear the sequence.
Definition: sequence_tpl.h:271
DiGraph(Size nodes_size=HashTableConst::default_size, bool nodes_resize_policy=true, Size arcs_size=HashTableConst::default_size, bool arcs_resize_policy=true)
default constructor
Definition: diGraph.cpp:37
Size size() const noexcept
Returns the size of the sequence.
Definition: sequence_tpl.h:38
Classes for directed edge sets.
Definition: arcGraphPart.h:79
virtual void clear()
removes all the nodes and arcs from the graph
Definition: diGraph_inl.h:43
Size size() const
alias for sizeNodes
virtual const std::string toString() const
to friendly display the content of the graph
Definition: diGraph.cpp:61
virtual ~DiGraph()
destructor
Definition: diGraph.cpp:56
bool exists(const NodeId id) const
alias for existsNode
Generic doubly linked lists.
Definition: list.h:372
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
Definition: agrum.h:25
void popFront()
Removes the first element of a List, if any.
Definition: list_tpl.h:1964
Sequence< NodeId > * __mutableTopologicalOrder
The topology sequence of this Directed Graphical Model.
Definition: diGraph.h:212
std::ostream & operator<<(std::ostream &output, const BayesNet< GUM_SCALAR > &bn)
Prints map&#39;s DAG in output using the Graphviz-dot format.
Definition: BayesNet_tpl.h:605
The base class for all directed edgesThis class is used as a basis for manipulating all directed edge...
Val & pushBack(const Val &val)
Inserts a new element (a copy) at the end of the chained list.
Definition: list_tpl.h:1592
const NodeGraphPart & nodes() const
return *this as a NodeGraphPart
Base class for all oriented graphs.
Definition: diGraph.h:111
bool exists(const Key &k) const
Check the existence of k in the sequence.
Definition: sequence_tpl.h:402
std::string toString() const
a function to display the set of nodes
virtual const std::string toDot() const
to friendly display the content of the graph in the DOT syntax
Definition: diGraph.cpp:68
Class for node sets in graph.
const Sequence< NodeId > & topologicalOrder(bool clear=true) const
The topological order stays the same as long as no variable or arcs are added or erased src the topol...
Definition: diGraph.cpp:91
Val & front() const
Returns a reference to first element of a list, if any.
Definition: list_tpl.h:1831
const NodeSet & children(const NodeId id) const
returns the set of nodes with arc outgoing from a given node
const ArcSet & arcs() const
returns the set of arcs stored within the ArcGraphPart
bool hasDirectedPath(const NodeId from, const NodeId to)
checks whether there exists a directed path from from to to
Definition: diGraph.cpp:137
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:48
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
Size NodeId
Type for node ids.
Definition: graphElements.h:98
void insert(const Key &k)
Inserts a new element into the set.
Definition: set_tpl.h:613
void __topologicalOrder() const
Returns a topological order of this DAGModel.
Definition: diGraph.cpp:108
const Key & back() const
Returns the last element of the sequence.
Definition: sequence_tpl.h:568
#define GUM_ERROR(type, msg)
Definition: exceptions.h:55
void insert(const Key &k)
Insert an element at the end of the sequence.
Definition: sequence_tpl.h:408