aGrUM  0.14.2
diGraph.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  * Copyright (C) 2005 by Christophe GONZALES and Pierre-Henri WUILLEMIN *
3  * {prenom.nom}_at_lip6.fr *
4  * *
5  * This program is free software; you can redistribute it and/or modify *
6  * it under the terms of the GNU General Public License as published by *
7  * the Free Software Foundation; either version 2 of the License, or *
8  * (at your option) any later version. *
9  * *
10  * This program is distributed in the hope that it will be useful, *
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13  * GNU General Public License for more details. *
14  * *
15  * You should have received a copy of the GNU General Public License *
16  * along with this program; if not, write to the *
17  * Free Software Foundation, Inc., *
18  * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19  ***************************************************************************/
26 #include <agrum/graphs/diGraph.h>
27 
28 #ifdef GUM_NO_INLINE
30 #endif // GUM_NOINLINE
31 
32 namespace gum {
33 
34  DiGraph::DiGraph(Size nodes_size,
35  bool nodes_resize_policy,
36  Size arcs_size,
37  bool arcs_resize_policy) :
38  NodeGraphPart(nodes_size, nodes_resize_policy),
39  ArcGraphPart(arcs_size, arcs_resize_policy),
40  __mutableTopologicalOrder(nullptr) {
41  GUM_CONSTRUCTOR(DiGraph);
42  }
43 
46  GUM_CONS_CPY(DiGraph);
47  if (g.__mutableTopologicalOrder != nullptr) {
50  }
51  }
52 
54  GUM_DESTRUCTOR(DiGraph);
55  if (__mutableTopologicalOrder != nullptr) { delete __mutableTopologicalOrder; }
56  }
57 
58  const std::string DiGraph::toString() const {
59  std::string s = NodeGraphPart::toString();
60  s += " , ";
62  return s;
63  }
64 
65  const std::string DiGraph::toDot() const {
66  std::stringstream strBuff;
67  std::string tab = " ";
68  strBuff << "digraph {" << std::endl;
69 
70  for (const auto node : nodes())
71  strBuff << tab << node << ";" << std::endl;
72 
73  strBuff << std::endl;
74 
75  for (const auto& arc : arcs())
76  strBuff << tab << arc.tail() << " -> " << arc.head() << ";" << std::endl;
77 
78  strBuff << "}" << std::endl << std::endl;
79  return strBuff.str();
80  }
81 
83  std::ostream& operator<<(std::ostream& stream, const DiGraph& g) {
84  stream << g.toString();
85  return stream;
86  }
87 
89  if (clear
91  == nullptr)) { // we have to call _topologicalOrder
92  if (__mutableTopologicalOrder == nullptr) {
94  } else {
95  // clear is True
97  }
98 
100  }
101 
103  }
104 
106  auto dag = *this;
107  auto roots = std::vector< NodeId >();
108 
109  for (const auto node : dag.nodes()) {
110  if (dag.parents(node).empty()) { roots.push_back(node); }
111  }
112 
113  while (roots.size()) {
114  if (__mutableTopologicalOrder->exists(roots.back())) {
116  "cycles prevent the creation of a topological ordering.");
117  }
118  __mutableTopologicalOrder->insert(roots.back());
119  roots.pop_back();
120 
121  while (dag.children(__mutableTopologicalOrder->back()).size()) {
122  auto back = __mutableTopologicalOrder->back();
123  auto child = *(dag.children(back).begin());
124  dag.eraseArc(Arc(back, child));
125 
126  if (dag.parents(child).empty()) { roots.push_back(child); }
127  }
128  }
129 
130  GUM_ASSERT(dag.sizeArcs() == (gum::Size)(0));
131  GUM_ASSERT(__mutableTopologicalOrder->size() == dag.size());
132  }
133 } /* namespace gum */
Base classes for oriented graphs.
const std::string toString() const
to friendly display the content of the ArcGraphPart
void clear()
Clear the sequence.
Definition: sequence_tpl.h:268
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:34
Size size() const noexcept
Returns the size of the sequence.
Definition: sequence_tpl.h:35
Classes for directed edge sets.
Definition: arcGraphPart.h:76
virtual void clear()
removes all the nodes and arcs from the graph
Definition: diGraph_inl.h:40
Size size() const
alias for sizeNodes
virtual const std::string toString() const
to friendly display the content of the graph
Definition: diGraph.cpp:58
virtual ~DiGraph()
destructor
Definition: diGraph.cpp:53
gum is the global namespace for all aGrUM entities
Definition: agrum.h:25
Sequence< NodeId > * __mutableTopologicalOrder
The topology sequence of this Directed Graphical Model.
Definition: diGraph.h:198
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:583
The base class for all directed edgesThis class is used as a basis for manipulating all directed edge...
const NodeGraphPart & nodes() const
return *this as a NodeGraphPart
Base class for all oriented graphs.
Definition: diGraph.h:108
bool exists(const Key &k) const
Check the existence of k in the sequence.
Definition: sequence_tpl.h:399
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:65
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:88
const ArcSet & arcs() const
returns the set of arcs stored within the ArcGraphPart
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:45
Inline implementation of Base classes for oriented graphs.
void __topologicalOrder() const
Returns a topological order of this DAGModel.
Definition: diGraph.cpp:105
const Key & back() const
Returns the last element of the sequence.
Definition: sequence_tpl.h:565
#define GUM_ERROR(type, msg)
Definition: exceptions.h:52
void insert(const Key &k)
Insert an element at the end of the sequence.
Definition: sequence_tpl.h:405