aGrUM  0.20.2
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().

36  : dagmodel__(&m) {
38  }
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 40 of file essentialGraph.cpp.

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

40  :
41  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 42 of file essentialGraph.cpp.

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

42  {
43  dagmodel__ = g.dagmodel__;
45  }
const DAGmodel * dagmodel__
+ Here is the call graph for this function:

◆ ~EssentialGraph()

gum::EssentialGraph::~EssentialGraph ( )
default

Member Function Documentation

◆ arcs()

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

wrapping MixedGraph::arcs()

Definition at line 59 of file essentialGraph_inl.h.

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

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

◆ buildEssentialGraph__()

void gum::EssentialGraph::buildEssentialGraph__ ( )
private

Definition at line 56 of file essentialGraph.cpp.

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

56  {
57  mg__.clear();
58  if (dagmodel__ == nullptr) return;
59 
60  for (const auto& node: dagmodel__->nodes()) {
61  mg__.addNodeWithId(node);
62  }
63  for (const auto& arc: dagmodel__->arcs()) {
64  mg__.addArc(arc.tail(), arc.head());
65  }
66 
67  std::vector< Arc > v;
68  do {
69  v.clear();
70  for (const auto x: dagmodel__->topologicalOrder())
71  for (const auto y: mg__.children(x))
72  if (!strongly_protected__(x, y)) v.emplace_back(x, y);
73 
74  for (const auto& arc: v) {
75  mg__.eraseArc(arc);
76  mg__.addEdge(arc.tail(), arc.head());
77  }
78  } while (!v.empty());
79  }
const DAGmodel * dagmodel__
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:96
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
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:100
void addEdge(NodeId first, NodeId second) override
insert a new edge into the undirected graph
Definition: undiGraph_inl.h:34
bool strongly_protected__(NodeId a, NodeId b)
+ 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 40 of file essentialGraph_inl.h.

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

40  {
41  return mg__.children(id);
42  }
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 48 of file essentialGraph_inl.h.

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

48  {
49  return mg__.children(ids);
50  }
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 63 of file essentialGraph_inl.h.

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

63 { 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 52 of file essentialGraph_inl.h.

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

52  {
53  return mg__.neighbours(id);
54  }
const NodeSet & neighbours(const NodeId id) const
returns the set of edges adjacent 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 69 of file essentialGraph_inl.h.

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

69  {
70  return mg__.nodes();
71  }
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 46 of file essentialGraph.cpp.

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

46  {
47  if (&g != this) {
48  dagmodel__ = g.dagmodel__;
50  }
51  return *this;
52  }
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  {
37  return mg__.parents(id);
38  }
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 44 of file essentialGraph_inl.h.

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

44  {
45  return mg__.parents(ids);
46  }
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 67 of file essentialGraph_inl.h.

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

67 { 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 57 of file essentialGraph_inl.h.

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

57 { 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 61 of file essentialGraph_inl.h.

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

61 { 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 65 of file essentialGraph_inl.h.

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

65 { 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 160 of file essentialGraph.cpp.

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

160  {
161  UndiGraph skel;
162  for (const auto& n: nodes())
163  skel.addNodeWithId(n);
164  for (const auto& edge: edges())
165  skel.addEdge(edge.first(), edge.second());
166  for (const auto& arc: arcs())
167  skel.addEdge(arc.tail(), arc.head());
168  return skel;
169  }
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:

◆ strongly_protected__()

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

Definition at line 81 of file essentialGraph.cpp.

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

81  {
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 (a)
87  for (const auto& c: mg__.parents(a)) {
88  if (!mg__.existsArc(c, b)) {
89  // GUM_TRACE(a << "->" << b << " with "<<c<<" : (a)")
90  return true;
91  }
92  }
93 
94 
95  for (const auto& c: mg__.parents(b)) {
96  if (c == a) { continue; }
97  // condition (c)
98  if (mg__.existsArc(a, c)) {
99  // GUM_TRACE(a << "->" << b << " with "<<c<<" : (c)")
100  return true;
101  }
102 
103  // condition (b) knowing that a can not be a parent of c (condition below)
104  if (!mg__.existsEdge(a, c) && !mg__.existsArc(c, a)) {
105  // GUM_TRACE(a << "->" << b << " with "<<c<<" : (b)")
106  return true;
107  }
108  }
109 
110  // condition (d)
111  bool oneFound = false;
112  for (const auto& c: mg__.parents(b)) {
113  if (c == a) { continue; }
114  // condition (d)
115  if (mg__.existsEdge(c, a)) {
116  if (oneFound) { // this is the second found
117  // GUM_TRACE(a << "->" << b << " : (d)")
118  return true;
119  }
120  oneFound = true;
121  }
122  }
123 
124  return false;
125  }
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:

◆ toDot()

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

Definition at line 127 of file essentialGraph.cpp.

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

127  {
128  std::stringstream output;
129  std::stringstream nodeStream;
130  std::stringstream edgeStream;
131  List< NodeId > treatedNodes;
132  output << "digraph \""
133  << "no_name\" {" << std::endl;
134  nodeStream << "node [shape = ellipse];" << std::endl;
135  std::string tab = " ";
136  if (dagmodel__ != nullptr) {
137  for (const auto node: mg__.nodes()) {
138  nodeStream << tab << node << "[label=\""
139  << dagmodel__->variable(node).name() << "\"];";
140 
141  for (const auto nei: mg__.neighbours(node))
142  if (!treatedNodes.exists(nei))
143  edgeStream << tab << node << " -> " << nei << " [dir=none];"
144  << std::endl;
145 
146  for (const auto chi: mg__.children(node))
147  edgeStream << tab << node << " -> " << chi << " [color=red];"
148  << std::endl;
149 
150  treatedNodes.insert(node);
151  }
152  }
153  output << nodeStream.str() << std::endl
154  << edgeStream.str() << std::endl
155  << "}" << std::endl;
156 
157  return output.str();
158  }
const DAGmodel * dagmodel__
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 edges adjacent 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 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: