aGrUM  0.16.0
essentialGraph.cpp
Go to the documentation of this file.
1 
31 
32 #ifdef GUM_NO_INLINE
34 #endif // GUM_NOINLINE
35 
36 namespace gum {
37  EssentialGraph::EssentialGraph(const DAGmodel& m) : __dagmodel(&m) {
39  }
40 
42  __dagmodel(&m), __mg(mg) {}
46  }
48  if (&g != this) {
51  }
52  return *this;
53  }
54 
56 
58  __mg.clear();
59  if (__dagmodel == nullptr) return;
60 
61  for (const auto& node : __dagmodel->nodes()) {
62  __mg.addNodeWithId(node);
63  }
64  for (const auto& arc : __dagmodel->arcs()) {
65  __mg.addArc(arc.tail(), arc.head());
66  }
67 
68  std::vector< Arc > v;
69  do {
70  v.clear();
71  for (const auto x : __dagmodel->topologicalOrder())
72  for (const auto y : __mg.children(x))
73  if (!__strongly_protected(x, y)) v.push_back(Arc(x, y));
74  for (const auto& arc : v) {
75  __mg.eraseArc(arc);
76  __mg.addEdge(arc.tail(), arc.head());
77  }
78  } while (v.size() > 0);
79  }
80 
82  // testing a->b from
83  // A Characterization of Markov Equivalence Classes for Acyclic Digraphs (2001)
84  // Steen A. Andersson, David Madigan, and Michael D. Perlman*
85 
86  // condition (c)
87  if ((__mg.parents(a) - __mg.parents(b)).size() > 0) { return true; }
88 
89  NodeSet cs;
90  for (const auto& c : __mg.parents(b)) {
91  // condition (b) & (c)
92  if (c == a) { continue; }
93  if (!__mg.existsEdge(c, a)) {
94  return true;
95  } else {
96  cs.insert(c);
97  }
98  }
99 
100  // condition (d)
101  NodeSet ss(cs);
102  if (cs.size() < 2) {
103  return false;
104  } else {
105  for (const auto& i : cs) {
106  ss = ss - __mg.neighbours(i);
107  if (ss.size() < 2) { return false; }
108  }
109  return true;
110  }
111  }
112 
113  std::string EssentialGraph::toDot() const {
114  std::stringstream output;
115  std::stringstream nodeStream;
116  std::stringstream edgeStream;
117  List< NodeId > treatedNodes;
118  output << "digraph \""
119  << "no_name\" {" << std::endl;
120  nodeStream << "node [shape = ellipse];" << std::endl;
121  std::string tab = " ";
122  if (__dagmodel != nullptr) {
123  for (const auto node : __mg.nodes()) {
124  nodeStream << tab << node << "[label=\""
125  << __dagmodel->variable(node).name() << "\"];";
126 
127  for (const auto nei : __mg.neighbours(node))
128  if (!treatedNodes.exists(nei))
129  edgeStream << tab << node << " -> " << nei << " [dir=none];"
130  << std::endl;
131 
132  for (const auto chi : __mg.children(node))
133  edgeStream << tab << node << " -> " << chi << " [color=red];"
134  << std::endl;
135 
136  treatedNodes.insert(node);
137  }
138  }
139  output << nodeStream.str() << std::endl
140  << edgeStream.str() << std::endl
141  << "}" << std::endl;
142 
143  return output.str();
144  }
145 
147  UndiGraph skel;
148  for (const auto& n : nodes())
149  skel.addNodeWithId(n);
150  for (const auto& edge : edges())
151  skel.addEdge(edge.first(), edge.second());
152  for (const auto& arc : arcs())
153  skel.addEdge(arc.tail(), arc.head());
154  return skel;
155  }
156 } // namespace gum
const ArcSet & arcs() const
returns the set of nodes with arc ingoing to a given node
Definition: DAGmodel_inl.h:104
virtual void clear()
removes all the nodes, arcs and edges from the graph
virtual void addNodeWithId(const NodeId id)
try to insert a node with the given id
virtual void addArc(const NodeId tail, const NodeId head)
insert a new arc into the directed graph
Definition: diGraph_inl.h:35
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
Virtual base class for PGMs using a DAG.
Definition: DAGmodel.h:48
const NodeGraphPart & nodes() const
wrapping MixedGraph::nodes()
EssentialGraph & operator=(const EssentialGraph &g)
virtual void eraseArc(const Arc &arc)
removes an arc from the ArcGraphPart
virtual void addEdge(const NodeId first, const NodeId second)
insert a new edge into the undirected graph
Definition: undiGraph_inl.h:35
std::string toDot() const
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
bool __strongly_protected(NodeId a, NodeId b)
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
const NodeSet & neighbours(const NodeId id) const
returns the set of edges adjacent to a given node
Size size() const
wrapping MixedGraph::size()
const DAGmodel * __dagmodel
UndiGraph skeleton() const
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: DAGmodel.cpp:117
bool existsEdge(const Edge &edge) const
indicates whether a given edge exists
The base class for all directed edgesThis class is used as a basis for manipulating all directed edge...
const NodeGraphPart & nodes() const
Returns a constant reference to the dag of this Bayes Net.
Definition: DAGmodel_inl.h:115
const EdgeSet & edges() const
wrapping MixedGraph::edges()
const NodeSet & parents(const NodeId id) const
returns the set of nodes with arc ingoing to a given node
const NodeGraphPart & nodes() const
return *this as a NodeGraphPart
virtual const DiscreteVariable & variable(NodeId id) const =0
Returns a constant reference over a variabe given it&#39;s node id.
bool exists(const Val &val) const
Checks whether there exists a given element in the list.
Definition: list_tpl.h:1857
Val & insert(const Val &val)
Inserts a new element at the end of the chained list (alias of pushBack).
Definition: list_tpl.h:1619
const NodeSet & children(const NodeId id) const
returns the set of nodes with arc outgoing from a given node
Class building the essential graph from a BN.
Base class for undirected graphs.
Definition: undiGraph.h:109
Size size() const noexcept
Returns the number of elements in the set.
Definition: set_tpl.h:701
const std::string & name() const
returns the name of the variable
EssentialGraph()=default
const ArcSet & arcs() const
wrapping MixedGraph::arcs()
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
Base class for mixed graphs.
Definition: mixedGraph.h:127