aGrUM 2.3.2
a C++ library for (probabilistic) graphical models
PDAG.cpp
Go to the documentation of this file.
1/****************************************************************************
2 * This file is part of the aGrUM/pyAgrum library. *
3 * *
4 * Copyright (c) 2005-2025 by *
5 * - Pierre-Henri WUILLEMIN(_at_LIP6) *
6 * - Christophe GONZALES(_at_AMU) *
7 * *
8 * The aGrUM/pyAgrum library is free software; you can redistribute it *
9 * and/or modify it under the terms of either : *
10 * *
11 * - the GNU Lesser General Public License as published by *
12 * the Free Software Foundation, either version 3 of the License, *
13 * or (at your option) any later version, *
14 * - the MIT license (MIT), *
15 * - or both in dual license, as here. *
16 * *
17 * (see https://agrum.gitlab.io/articles/dual-licenses-lgplv3mit.html) *
18 * *
19 * This aGrUM/pyAgrum library is distributed in the hope that it will be *
20 * useful, but WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, *
21 * INCLUDING BUT NOT LIMITED TO THE WARRANTIES MERCHANTABILITY or FITNESS *
22 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE *
23 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *
24 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, *
25 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR *
26 * OTHER DEALINGS IN THE SOFTWARE. *
27 * *
28 * See LICENCES for more details. *
29 * *
30 * SPDX-FileCopyrightText: Copyright 2005-2025 *
31 * - Pierre-Henri WUILLEMIN(_at_LIP6) *
32 * - Christophe GONZALES(_at_AMU) *
33 * SPDX-License-Identifier: LGPL-3.0-or-later OR MIT *
34 * *
35 * Contact : info_at_agrum_dot_org *
36 * homepage : http://agrum.gitlab.io *
37 * gitlab : https://gitlab.com/agrumery/agrum *
38 * *
39 ****************************************************************************/
40
41
49
50#ifdef GUM_NO_INLINE
52#endif // GUM_NO_INLINE
53
54namespace gum {
55
56 // diamond structure require to explicitly initialize NodeGraphPart
57 PDAG::PDAG(Size nodes_size,
58 bool nodes_resize_policy,
59 Size arcs_size,
60 bool arcs_resize_policy,
61 Size edges_size,
62 bool edges_resize_policy) :
63 NodeGraphPart(nodes_size, nodes_resize_policy), MixedGraph(nodes_size,
64 nodes_resize_policy,
65 arcs_size,
66 arcs_resize_policy,
67 edges_size,
68 edges_resize_policy) {
69 GUM_CONSTRUCTOR(PDAG);
70 }
71
72 PDAG::PDAG(const UndiGraph& g) : NodeGraphPart(g), MixedGraph(g) { GUM_CONSTRUCTOR(PDAG); }
73
74 PDAG::PDAG(const DAG& g) : NodeGraphPart(g), MixedGraph(g) { GUM_CONSTRUCTOR(PDAG); }
75
76 PDAG::PDAG(const MixedGraph& g) : NodeGraphPart(g), MixedGraph(g) { GUM_CONS_CPY(PDAG); }
77
78 PDAG::PDAG(const PDAG& g) : NodeGraphPart(g), MixedGraph(g) { GUM_CONS_CPY(PDAG); }
79
80 PDAG::~PDAG() { GUM_DESTRUCTOR(PDAG); }
81
83 UndiGraph mg;
84 mg.populateNodes(*this);
85 // transform the arcs into edges
86 for (const auto& arc: edges())
87 mg.addEdge(arc.first(), arc.second());
88 for (const auto& arc: arcs())
89 mg.addEdge(arc.first(), arc.second());
90
91 // marry the parents
92 NodeSet already;
93 for (const auto node: nodes()) {
94 if (already.contains(node)) continue;
95
96 already.insert(node);
97 auto par = parents(node);
98 auto cc = neighbours(node);
99 while (!cc.empty()) {
100 const auto nei = cc.popFirst();
101 if (already.contains(nei)) continue;
102
103 already.insert(nei);
104 par += parents(nei);
105 cc += neighbours(nei) - already;
106 }
107
108 for (auto it1 = par.begin(); it1 != par.end(); ++it1) {
109 auto it2 = it1;
110 for (++it2; it2 != par.end(); ++it2) {
111 // will automatically check if this edge already exists
112 mg.addEdge(*it1, *it2);
113 }
114 }
115 }
116 return mg;
117 }
118
120 NodeSet& marked,
121 NodeId node,
122 NodeId goal,
123 bool alreadyOriented) {
124 if (node == goal) return alreadyOriented;
125 if (marked.contains(node)) return false;
126 marked.insert(node);
127 for (const auto nod: gr.children(node))
128 if (rec_hasMixedReallyOrientedPath(gr, marked, nod, goal, true)) return true;
129 for (const auto nod: gr.neighbours(node))
130 if (rec_hasMixedReallyOrientedPath(gr, marked, nod, goal, alreadyOriented)) return true;
131 return false;
132 }
133
135 if (n1 == n2) return false;
136 NodeSet marked; // marked as already explored
137 for (const auto nod: this->children(n1))
138 if (rec_hasMixedReallyOrientedPath(*this, marked, nod, n2, true)) return true;
139
140 for (const auto nod: this->neighbours(n1))
141 if (rec_hasMixedReallyOrientedPath(*this, marked, nod, n2, false)) return true;
142
143 return false;
144 }
145
146 void rec_ancestral(const PDAG& graph, PDAG& ancestral, NodeId nod) {
147 for (const auto par: graph.parents(nod)) {
148 if (!ancestral.existsNode(par)) {
149 ancestral.addNodeWithId(par);
150 rec_ancestral(graph, ancestral, par);
151 }
152 ancestral.addArc(par, nod);
153 }
154 for (const auto nei: graph.neighbours(nod)) {
155 if (!ancestral.existsNode(nei)) {
156 ancestral.addNodeWithId(nei);
157 rec_ancestral(graph, ancestral, nei);
158 }
159 ancestral.addEdge(nei, nod);
160 }
161 }
162
164 PDAG ancestral;
165 for (const auto n: nodes) {
166 if (!ancestral.existsNode(n)) { ancestral.addNodeWithId(n); }
167 rec_ancestral(*this, ancestral, n);
168 }
169
170 return ancestral.moralGraph();
171 }
172
173 bool PDAG::cSeparation(NodeId X, NodeId Y, const NodeSet& Z) const {
174 NodeSet cumul{Z};
175 cumul << X << Y;
176 auto g = moralizedAncestralGraph(cumul);
177
178 for (auto node: Z)
179 g.eraseNode(node);
180
181 return !g.hasUndirectedPath(X, Y);
182 }
183
184 bool PDAG::cSeparation(const NodeSet& X, const NodeSet& Y, const NodeSet& Z) const {
185 if (!(X * Y).empty())
186 GUM_ERROR(InvalidArgument, "NodeSets " << X << ", " << Y << " should have no intersection")
187
188 NodeSet cumul{Z};
189 cumul += X;
190 cumul += Y;
191 auto g = moralizedAncestralGraph(cumul);
192 for (auto node: Z)
193 g.eraseNode(node);
194 auto cc = g.nodes2ConnectedComponent();
195
196 NodeSet Xcc;
197 NodeSet Ycc;
198 for (const auto node: X)
199 if (g.existsNode(node) && !Xcc.exists(cc[node])) // it may be in Z too
200 Xcc.insert(cc[node]);
201 for (const auto node: Y)
202 if (g.existsNode(node) && !Ycc.exists(cc[node])) // it may be in Z too
203 Ycc.insert(cc[node]);
204 return (Xcc * Ycc).empty();
205 }
206
207 std::string PDAG::toDot() const {
208 std::stringstream output;
209 List< NodeId > treatedNodes;
210 output << "digraph \""
211 << "no_name\" {" << std::endl;
212
213 std::string tab = " ";
214 output << tab << "rankdir = TD;" << std::endl;
215 output << tab << "node [style=filled,fillcolor=white,color=black];" << std::endl;
216 output << tab << "graph [style=filled,color=\"#F5F5F5\"];" << std::endl;
217
218 output << std::endl;
219 for (const auto node: nodes()) {
220 if (neighbours(node).empty()) {
221 output << tab << node << ";" << std::endl;
222 treatedNodes.insert(node);
223 }
224 }
225 output << std::endl;
226
227 int cluster = 0;
228 for (const auto node: nodes()) {
229 if (!treatedNodes.exists(node)) {
230 output << tab << "subgraph cluster_" << cluster++ << "{{" << std::endl;
231 output << tab << tab << "rank=same;" << std::endl << tab << tab;
232 for (const auto cc: chainComponent(node)) {
233 output << cc << ";";
234 treatedNodes.insert(cc);
235 }
236 output << std::endl << tab << "}}" << std::endl << std::endl;
237 }
238 }
239
240 for (const auto node: nodes()) {
241 for (const auto child: children(node)) {
242 output << tab << node << "->" << child << ";\n";
243 }
244 }
245 output << std::endl << tab << "edge [dir=none];" << std::endl;
246
247 for (const auto node: nodes()) {
248 for (const auto other: neighbours(node))
249 if (other > node) output << tab << node << "->" << other << ";\n";
250 }
251 output << "}\n";
252 return output.str();
253 }
254
255} /* namespace gum */
Base classes for partially directed acyclic graphs.
Inline implementation of Base classes for directed acylic graphs.
const NodeSet & parents(NodeId id) const
returns the set of nodes with arc ingoing to a given node
NodeSet children(const NodeSet &ids) const
returns the set of children of a set of nodes
const ArcSet & arcs() const
returns the set of arcs stored within the ArcGraphPart
Base class for dag.
Definition DAG.h:121
const EdgeSet & edges() const
returns the set of edges stored within the EdgeGraphPart
const NodeSet & neighbours(NodeId id) const
returns the set of node neighbours to a given node
Exception: at least one argument passed to a function is not what was expected.
Generic doubly linked lists.
Definition list.h:379
bool exists(const Val &val) const
Checks whether there exists a given element in the list.
Definition list_tpl.h:1725
Val & insert(const Val &val)
Inserts a new element at the end of the chained list (alias of pushBack).
Definition list_tpl.h:1515
NodeSet chainComponent(NodeId node) const
returns the set of nodes reachable by undirected path
MixedGraph(Size nodes_size=HashTableConst::default_size, bool nodes_resize_policy=true, Size arcs_size=HashTableConst::default_size, bool arcs_resize_policy=true, Size edges_size=HashTableConst::default_size, bool edges_resize_policy=true)
default constructor
Class for node sets in graph.
void populateNodes(const NodeGraphPart &s)
populateNodes clears *this and fills it with the same nodes as "s"
const NodeGraphPart & nodes() const
return *this as a NodeGraphPart
bool empty() const
alias for emptyNodes
bool existsNode(const NodeId id) const
returns true iff the NodeGraphPart contains the given nodeId
virtual void addNodeWithId(const NodeId id)
try to insert a node with the given id
Base class for partially directed acyclic graphs.
Definition PDAG.h:130
void addEdge(NodeId first, NodeId second) final
insert a new edge into the partially directed graph
Definition PDAG_inl.h:81
std::string toDot() const override
to friendly display mixed graph in DOT format
Definition PDAG.cpp:207
void addArc(NodeId tail, NodeId head) final
insert a new arc into the directed graph
Definition PDAG_inl.h:63
UndiGraph moralizedAncestralGraph(const NodeSet &nodes) const
build a UndiGraph by moralizing the Ancestral Graph of a set of Nodes
Definition PDAG.cpp:163
PDAG(Size nodes_size=HashTableConst::default_size, bool nodes_resize_policy=true, Size arcs_size=HashTableConst::default_size, bool arcs_resize_policy=true, Size edges_size=HashTableConst::default_size, bool edges_resize_policy=true)
default constructor
Definition PDAG.cpp:57
UndiGraph moralGraph() const
build a UndiGraph by moralizing the PDAG
Definition PDAG.cpp:82
bool hasMixedReallyOrientedPath(NodeId n1, NodeId n2) const
returns true if a mixed edge/directed arc path from node1 to node2 in the arc/edge set exists with at...
Definition PDAG.cpp:134
bool cSeparation(NodeId X, NodeId Y, const NodeSet &Z) const
check if node X and node Y are independent given nodes Z (in the sense of c-separation)
Definition PDAG.cpp:173
virtual ~PDAG()
destructor
Definition PDAG.cpp:80
bool exists(const Key &k) const
Indicates whether a given elements belong to the set.
Definition set_tpl.h:533
bool contains(const Key &k) const
Indicates whether a given elements belong to the set.
Definition set_tpl.h:497
void insert(const Key &k)
Inserts a new element into the set.
Definition set_tpl.h:539
Base class for undirected graphs.
Definition undiGraph.h:128
void addEdge(NodeId first, NodeId second) override
insert a new edge into the undirected graph
#define GUM_ERROR(type, msg)
Definition exceptions.h:72
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition types.h:74
Size NodeId
Type for node ids.
Set< NodeId > NodeSet
Some typdefs and define for shortcuts ...
gum is the global namespace for all aGrUM entities
Definition agrum.h:46
void rec_ancestral(const PDAG &graph, PDAG &ancestral, NodeId nod)
Definition PDAG.cpp:146
bool rec_hasMixedReallyOrientedPath(const PDAG &gr, NodeSet &marked, NodeId node, NodeId goal, bool alreadyOriented)
Definition PDAG.cpp:119