aGrUM  0.20.3
a C++ library for (probabilistic) graphical models
gum::EssentialGraph Class Reference

Class building the essential graph from a BN. More...

#include <agrum/BN/algorithms/essentialGraph.h>

+ Collaboration diagram for gum::EssentialGraph:

Public Member Functions

 EssentialGraph ()=default
 
 EssentialGraph (const DAGmodel &m)
 
 EssentialGraph (const DAGmodel &m, const MixedGraph &mg)
 
 EssentialGraph (const EssentialGraph &g)
 
EssentialGraphoperator= (const EssentialGraph &g)
 
 ~EssentialGraph ()
 
MixedGraph mixedGraph ()
 
std::string toDot () const
 
const NodeSetparents (NodeId id) const
 wrapping MixedGraph::parents(id) More...
 
const NodeSetchildren (NodeId id) const
 wrapping MixedGraph::parents(id) More...
 
NodeSet parents (const NodeSet &ids) const
 wrapping MixedGraph::parents(ids) More...
 
NodeSet children (const NodeSet &ids) const
 wrapping MixedGraph::parents(ids) More...
 
const NodeSetneighbours (NodeId id) const
 wrapping MixedGraph::parents(id) More...
 
Size sizeArcs () const
 wrapping MixedGraph::sizeArcs() More...
 
const ArcSetarcs () const
 wrapping MixedGraph::arcs() More...
 
Size sizeEdges () const
 wrapping MixedGraph::sizeEdges() More...
 
const EdgeSetedges () const
 wrapping MixedGraph::edges() More...
 
Size sizeNodes () const
 wrapping MixedGraph::sizeNodes() More...
 
Size size () const
 wrapping MixedGraph::size() More...
 
UndiGraph skeleton () const
 
const NodeGraphPartnodes () const
 wrapping MixedGraph::nodes() More...
 

Detailed Description

Class building the essential graph from a BN.

Essential graph is a mixed graph (Chain Graph) that represents the class of markov equivalent Bayesian networks (with the same independence model).

The main goal of this class is to nest the algorithm to build the essential graph from a BN and to encapsulate the representation (as a MixedGraph) of the essential graph.

gum::operator<<(std::ostream&, const BayesNet<GUM_SCALAR>&).

Definition at line 54 of file essentialGraph.h.

Constructor & Destructor Documentation

◆ EssentialGraph() [1/4]

gum::EssentialGraph::EssentialGraph ( )
default

◆ EssentialGraph() [2/4]

gum::EssentialGraph::EssentialGraph ( const DAGmodel m)
explicit

Definition at line 36 of file essentialGraph.cpp.

References gum::Set< Key, Alloc >::emplace().

const DAGmodel * _dagmodel_
+ Here is the call graph for this function:

◆ EssentialGraph() [3/4]

gum::EssentialGraph::EssentialGraph ( const DAGmodel m,
const MixedGraph mg 
)

Definition at line 38 of file essentialGraph.cpp.

References gum::Set< Key, Alloc >::emplace().

38  :
39  _dagmodel_(&m), _mg_(mg) {}
const DAGmodel * _dagmodel_
+ Here is the call graph for this function:

◆ EssentialGraph() [4/4]

gum::EssentialGraph::EssentialGraph ( const EssentialGraph g)

Definition at line 40 of file essentialGraph.cpp.

References gum::Set< Key, Alloc >::emplace().

40  {
41  _dagmodel_ = g._dagmodel_;
43  }
const DAGmodel * _dagmodel_
+ Here is the call graph for this function:

◆ ~EssentialGraph()

gum::EssentialGraph::~EssentialGraph ( )
default

Member Function Documentation

◆ _buildEssentialGraph_()

void gum::EssentialGraph::_buildEssentialGraph_ ( )
private

Definition at line 54 of file essentialGraph.cpp.

References gum::Set< Key, Alloc >::emplace().

54  {
55  _mg_.clear();
56  if (_dagmodel_ == nullptr) return;
57 
58  for (const auto& node: _dagmodel_->nodes()) {
59  _mg_.addNodeWithId(node);
60  }
61  for (const auto& arc: _dagmodel_->arcs()) {
62  _mg_.addArc(arc.tail(), arc.head());
63  }
64 
65  std::vector< Arc > v;
66  do {
67  v.clear();
68  for (const auto x: _dagmodel_->topologicalOrder())
69  for (const auto y: _mg_.children(x))
70  if (!_strongly_protected_(x, y)) v.emplace_back(x, y);
71 
72  for (const auto& arc: v) {
73  _mg_.eraseArc(arc);
74  _mg_.addEdge(arc.tail(), arc.head());
75  }
76  } while (!v.empty());
77  }
const ArcSet & arcs() const
return true if the arc tail->head exists in the DAGmodel
Definition: DAGmodel_inl.h:43
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:34
const NodeGraphPart & nodes() const final
Returns a constant reference to the dag of this Bayes Net.
Definition: DAGmodel_inl.h:84
virtual void eraseArc(const Arc &arc)
removes an arc from the ArcGraphPart
NodeSet children(const NodeSet &ids) const
returns the set of children of a set of nodes
bool _strongly_protected_(NodeId a, NodeId b)
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_inl.h:86
const DAGmodel * _dagmodel_
void addEdge(NodeId first, NodeId second) override
insert a new edge into the undirected graph
Definition: undiGraph_inl.h:34
+ Here is the call graph for this function:

◆ _strongly_protected_()

bool gum::EssentialGraph::_strongly_protected_ ( NodeId  a,
NodeId  b 
)
private

Definition at line 79 of file essentialGraph.cpp.

References gum::Set< Key, Alloc >::emplace().

79  {
80  // testing a->b from
81  // A Characterization of Markov Equivalence Classes for Acyclic Digraphs (2001)
82  // Steen A. Andersson, David Madigan, and Michael D. Perlman*
83 
84  // condition (a)
85  for (const auto& c: _mg_.parents(a)) {
86  if (!_mg_.existsArc(c, b)) { return true; }
87  }
88 
89 
90  for (const auto& c: _mg_.parents(b)) {
91  if (c == a) { continue; }
92  // condition (c)
93  if (_mg_.existsArc(a, c)) { return true; }
94 
95  // condition (b) knowing that a can not be a parent of c (condition below)
96  if (!_mg_.existsEdge(a, c) && !_mg_.existsArc(c, a)) { return true; }
97  }
98 
99  // condition (d)
100  bool oneFound = false;
101  for (const auto& c: _mg_.parents(b)) {
102  if (c == a) { continue; }
103  // condition (d)
104  if (_mg_.existsEdge(c, a)) {
105  if (oneFound) { // this is the second found
106  return true;
107  }
108  oneFound = true;
109  }
110  }
111 
112  return false;
113  }
const NodeSet & parents(NodeId id) const
returns the set of nodes with arc ingoing to a given node
bool existsEdge(const Edge &edge) const
indicates whether a given edge exists
bool existsArc(const Arc &arc) const
indicates whether a given arc exists
+ Here is the call graph for this function:

◆ arcs()

INLINE const ArcSet & gum::EssentialGraph::arcs ( ) const

wrapping MixedGraph::arcs()

Definition at line 49 of file essentialGraph_inl.h.

References gum::Set< Key, Alloc >::emplace().

49 { return _mg_.arcs(); }
const ArcSet & arcs() const
returns the set of arcs stored within the ArcGraphPart
+ Here is the call graph for this function:

◆ children() [1/2]

INLINE const NodeSet & gum::EssentialGraph::children ( NodeId  id) const

wrapping MixedGraph::parents(id)

Definition at line 38 of file essentialGraph_inl.h.

References gum::Set< Key, Alloc >::emplace().

38 { return _mg_.children(id); }
NodeSet children(const NodeSet &ids) const
returns the set of children of a set of nodes
+ Here is the call graph for this function:

◆ children() [2/2]

INLINE NodeSet gum::EssentialGraph::children ( const NodeSet ids) const

wrapping MixedGraph::parents(ids)

Definition at line 42 of file essentialGraph_inl.h.

References gum::Set< Key, Alloc >::emplace().

42 { return _mg_.children(ids); }
NodeSet children(const NodeSet &ids) const
returns the set of children of a set of nodes
+ Here is the call graph for this function:

◆ edges()

INLINE const EdgeSet & gum::EssentialGraph::edges ( ) const

wrapping MixedGraph::edges()

Definition at line 53 of file essentialGraph_inl.h.

References gum::Set< Key, Alloc >::emplace().

53 { return _mg_.edges(); }
const EdgeSet & edges() const
returns the set of edges stored within the EdgeGraphPart
+ Here is the call graph for this function:

◆ mixedGraph()

INLINE MixedGraph gum::EssentialGraph::mixedGraph ( )
Returns
a copy of the mixed graph

Definition at line 34 of file essentialGraph_inl.h.

References gum::Set< Key, Alloc >::emplace().

34 { return _mg_; }
+ Here is the call graph for this function:

◆ neighbours()

INLINE const NodeSet & gum::EssentialGraph::neighbours ( NodeId  id) const

wrapping MixedGraph::parents(id)

Definition at line 44 of file essentialGraph_inl.h.

References gum::Set< Key, Alloc >::emplace().

44 { return _mg_.neighbours(id); }
const NodeSet & neighbours(const NodeId id) const
returns the set of node neighbours to a given node
+ Here is the call graph for this function:

◆ nodes()

INLINE const NodeGraphPart & gum::EssentialGraph::nodes ( ) const

wrapping MixedGraph::nodes()

Definition at line 59 of file essentialGraph_inl.h.

References gum::Set< Key, Alloc >::emplace().

59 { return _mg_.nodes(); }
const NodeGraphPart & nodes() const
return *this as a NodeGraphPart
+ Here is the call graph for this function:

◆ operator=()

EssentialGraph & gum::EssentialGraph::operator= ( const EssentialGraph g)

Definition at line 44 of file essentialGraph.cpp.

References gum::Set< Key, Alloc >::emplace().

44  {
45  if (&g != this) {
46  _dagmodel_ = g._dagmodel_;
48  }
49  return *this;
50  }
const DAGmodel * _dagmodel_
+ Here is the call graph for this function:

◆ parents() [1/2]

INLINE const NodeSet & gum::EssentialGraph::parents ( NodeId  id) const

wrapping MixedGraph::parents(id)

Definition at line 36 of file essentialGraph_inl.h.

References gum::Set< Key, Alloc >::emplace().

36 { return _mg_.parents(id); }
const NodeSet & parents(NodeId id) const
returns the set of nodes with arc ingoing to a given node
+ Here is the call graph for this function:

◆ parents() [2/2]

INLINE NodeSet gum::EssentialGraph::parents ( const NodeSet ids) const

wrapping MixedGraph::parents(ids)

Definition at line 40 of file essentialGraph_inl.h.

References gum::Set< Key, Alloc >::emplace().

40 { return _mg_.parents(ids); }
const NodeSet & parents(NodeId id) const
returns the set of nodes with arc ingoing to a given node
+ Here is the call graph for this function:

◆ size()

INLINE Size gum::EssentialGraph::size ( ) const

wrapping MixedGraph::size()

Definition at line 57 of file essentialGraph_inl.h.

References gum::Set< Key, Alloc >::emplace().

57 { return _mg_.size(); }
Size size() const
alias for sizeNodes
+ Here is the call graph for this function:

◆ sizeArcs()

INLINE Size gum::EssentialGraph::sizeArcs ( ) const

wrapping MixedGraph::sizeArcs()

Definition at line 47 of file essentialGraph_inl.h.

References gum::Set< Key, Alloc >::emplace().

47 { return _mg_.sizeArcs(); }
Size sizeArcs() const
indicates the number of arcs stored within the ArcGraphPart
+ Here is the call graph for this function:

◆ sizeEdges()

INLINE Size gum::EssentialGraph::sizeEdges ( ) const

wrapping MixedGraph::sizeEdges()

Definition at line 51 of file essentialGraph_inl.h.

References gum::Set< Key, Alloc >::emplace().

51 { return _mg_.sizeEdges(); }
Size sizeEdges() const
indicates the number of edges stored within the EdgeGraphPart
+ Here is the call graph for this function:

◆ sizeNodes()

INLINE Size gum::EssentialGraph::sizeNodes ( ) const

wrapping MixedGraph::sizeNodes()

Definition at line 55 of file essentialGraph_inl.h.

References gum::Set< Key, Alloc >::emplace().

55 { return _mg_.sizeNodes(); }
Size sizeNodes() const
returns the number of nodes in the NodeGraphPart
+ Here is the call graph for this function:

◆ skeleton()

UndiGraph gum::EssentialGraph::skeleton ( ) const

Definition at line 143 of file essentialGraph.cpp.

References gum::Set< Key, Alloc >::emplace().

143  {
144  UndiGraph skel;
145  for (const auto& n: nodes())
146  skel.addNodeWithId(n);
147  for (const auto& edge: edges())
148  skel.addEdge(edge.first(), edge.second());
149  for (const auto& arc: arcs())
150  skel.addEdge(arc.tail(), arc.head());
151  return skel;
152  }
const NodeGraphPart & nodes() const
wrapping MixedGraph::nodes()
const EdgeSet & edges() const
wrapping MixedGraph::edges()
const ArcSet & arcs() const
wrapping MixedGraph::arcs()
+ Here is the call graph for this function:

◆ toDot()

std::string gum::EssentialGraph::toDot ( ) const
Returns
a dot representation of this essentialGraph

Definition at line 115 of file essentialGraph.cpp.

References gum::Set< Key, Alloc >::emplace().

115  {
116  std::stringstream output;
117  std::stringstream nodeStream;
118  std::stringstream edgeStream;
119  List< NodeId > treatedNodes;
120  output << "digraph \""
121  << "no_name\" {" << std::endl;
122  nodeStream << "node [shape = ellipse];" << std::endl;
123  std::string tab = " ";
124  if (_dagmodel_ != nullptr) {
125  for (const auto node: _mg_.nodes()) {
126  nodeStream << tab << node << "[label=\"" << _dagmodel_->variable(node).name() << "\"];";
127 
128  for (const auto nei: _mg_.neighbours(node))
129  if (!treatedNodes.exists(nei))
130  edgeStream << tab << node << " -> " << nei << " [dir=none];" << std::endl;
131 
132  for (const auto chi: _mg_.children(node))
133  edgeStream << tab << node << " -> " << chi << " [color=red];" << std::endl;
134 
135  treatedNodes.insert(node);
136  }
137  }
138  output << nodeStream.str() << std::endl << edgeStream.str() << std::endl << "}" << std::endl;
139 
140  return output.str();
141  }
NodeSet children(const NodeSet &ids) const
returns the set of children of a set of nodes
const NodeSet & neighbours(const NodeId id) const
returns the set of node neighbours to a given node
const NodeGraphPart & nodes() const
return *this as a NodeGraphPart
const DAGmodel * _dagmodel_
virtual const DiscreteVariable & variable(NodeId id) const =0
Returns a constant reference over a variable given it&#39;s node id.
const std::string & name() const
returns the name of the variable
+ Here is the call graph for this function:

Member Data Documentation

◆ _dagmodel_

const DAGmodel* gum::EssentialGraph::_dagmodel_
private

Definition at line 112 of file essentialGraph.h.

◆ _mg_

MixedGraph gum::EssentialGraph::_mg_
private

Definition at line 113 of file essentialGraph.h.


The documentation for this class was generated from the following files: