aGrUM 3.1.1
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-2026 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-2026 *
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
51
52#ifdef GUM_NO_INLINE
54#endif // GUM_NO_INLINE
55
56namespace gum {
57
58 // diamond structure require to explicitly initialize NodeGraphPart
59 PDAG::PDAG(Size nodes_size,
60 bool nodes_resize_policy,
61 Size arcs_size,
62 bool arcs_resize_policy,
63 Size edges_size,
64 bool edges_resize_policy) :
65 NodeGraphPart(nodes_size, nodes_resize_policy), MixedGraph(nodes_size,
66 nodes_resize_policy,
67 arcs_size,
68 arcs_resize_policy,
69 edges_size,
70 edges_resize_policy) {
71 GUM_CONSTRUCTOR(PDAG);
72 }
73
74 PDAG::PDAG(const UndiGraph& g) : NodeGraphPart(g), MixedGraph(g) { GUM_CONSTRUCTOR(PDAG); }
75
76 PDAG::PDAG(const DAG& g) : NodeGraphPart(g), MixedGraph(g) { GUM_CONSTRUCTOR(PDAG); }
77
78 PDAG::PDAG(const MixedGraph& g) : NodeGraphPart(g), MixedGraph(g) { GUM_CONS_CPY(PDAG); }
79
80 PDAG::PDAG(const PDAG& g) : NodeGraphPart(g), MixedGraph(g) { GUM_CONS_CPY(PDAG); }
81
82 PDAG::PDAG(PDAG&& g) : NodeGraphPart(std::move(g)), MixedGraph(std::move(g)) {
83 GUM_CONS_MOV(PDAG);
84 }
85
86 PDAG::~PDAG() { GUM_DESTRUCTOR(PDAG); }
87
88 UndiGraph PDAG::moralGraph() const { return graph::moralGraph(*this); }
89
91 NodeSet& marked,
92 NodeId node,
93 NodeId goal,
94 bool alreadyOriented) {
95 if (node == goal) return alreadyOriented;
96 if (marked.contains(node)) return false;
97 marked.insert(node);
98 for (const auto nod: gr.children(node))
99 if (rec_hasMixedReallyOrientedPath(gr, marked, nod, goal, true)) return true;
100 for (const auto nod: gr.neighbours(node))
101 if (rec_hasMixedReallyOrientedPath(gr, marked, nod, goal, alreadyOriented)) return true;
102 return false;
103 }
104
106 if (n1 == n2) return false;
107 NodeSet marked; // marked as already explored
108 for (const auto nod: this->children(n1))
109 if (rec_hasMixedReallyOrientedPath(*this, marked, nod, n2, true)) return true;
110
111 for (const auto nod: this->neighbours(n1))
112 if (rec_hasMixedReallyOrientedPath(*this, marked, nod, n2, false)) return true;
113
114 return false;
115 }
116
120
121 bool PDAG::cSeparation(NodeId X, NodeId Y, const NodeSet& Z) const {
122 return graph::cSeparated(*this, X, Y, Z);
123 }
124
125 bool PDAG::cSeparation(const NodeSet& X, const NodeSet& Y, const NodeSet& Z) const {
126 return graph::cSeparated(*this, X, Y, Z);
127 }
128
129 std::string PDAG::toDot() const {
130 std::string output;
131 List< NodeId > treatedNodes;
132 output = "digraph \"no_name\" {\n";
133
134 std::string tab = " ";
135 output += tab + "rankdir = TD;\n";
136 output += tab + "node [style=filled,fillcolor=white,color=black];\n";
137 output += tab + "graph [style=filled,color=\"#F5F5F5\"];\n";
138
139 output += '\n';
140 auto nodeDecl = [&](NodeId n) -> std::string { return std::to_string(n) + dotNodeLabel(n); };
141
142 for (const auto node: nodes()) {
143 if (neighbours(node).empty()) {
144 output += tab + nodeDecl(node) + ";\n";
145 treatedNodes.insert(node);
146 }
147 }
148 output += '\n';
149
150 int cluster = 0;
151 for (const auto node: nodes()) {
152 if (!treatedNodes.exists(node)) {
153 output += tab + "subgraph cluster_" + std::to_string(cluster++) + "{{\n";
154 output += tab + tab + "rank=same;\n" + tab + tab;
155 for (const auto cc: chainComponent(node)) {
156 output += nodeDecl(cc) + ';';
157 treatedNodes.insert(cc);
158 }
159 output += '\n' + tab + "}}\n\n";
160 }
161 }
162
163 for (const auto node: nodes()) {
164 for (const auto child: children(node)) {
165 output += std::format("{}{}->{};", tab, node, child);
166 output += '\n';
167 }
168 }
169 output += '\n' + tab + "edge [dir=none];\n";
170
171 for (const auto node: nodes()) {
172 for (const auto other: neighbours(node))
173 if (other > node) {
174 output += std::format("{}{}->{};", tab, node, other);
175 output += '\n';
176 }
177 }
178 output += "}\n";
179 return output;
180 }
181
182} /* namespace gum */
Base classes for partially directed acyclic graphs.
Inline implementation of Base classes for directed acylic graphs.
d-Separation and c-Separation tests for aGrUM graphs.
NodeSet children(const NodeSet &ids) const
returns the set of nodes which consists in the node and its parents returns the set of children of a ...
Base class for dag.
Definition DAG.h:121
const NodeSet & neighbours(NodeId id) const
returns the set of node neighbours to a given node
Generic doubly linked lists.
Definition list.h:378
Val & insert(const Val &val)
Inserts a new element at the end of the chained list (alias of pushBack).
Definition list_tpl.h:1508
bool exists(const Val &val) const
Checks whether there exists a given element in the list.
Definition list_tpl.h:1716
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.
const NodeGraphPart & nodes() const
return *this as a NodeGraphPart
bool empty() const
alias for emptyNodes
std::string dotNodeLabel(NodeId id) const
returns " [label=\"...\"]" with DOT-escaped name, or "" if no name
Base class for partially directed acyclic graphs.
Definition PDAG.h:130
std::string toDot() const override
to friendly display mixed graph in DOT format
Definition PDAG.cpp:129
UndiGraph moralizedAncestralGraph(const NodeSet &nodes) const
build a UndiGraph by moralizing the Ancestral Graph of a set of Nodes
Definition PDAG.cpp:117
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:59
UndiGraph moralGraph() const
build a UndiGraph by moralizing the PDAG
Definition PDAG.cpp:88
~PDAG() override
destructor
Definition PDAG.cpp:86
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:105
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:121
bool contains(const Key &k) const
Indicates whether a given elements belong to the set.
Definition set_tpl.h:468
void insert(const Key &k)
Inserts a new element into the set.
Definition set_tpl.h:510
Base class for undirected graphs.
Definition undiGraph.h:130
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 ...
Moralization algorithms for aGrUM directed and mixed graphs.
bool cSeparated(const G &g, NodeId X, NodeId Y, const NodeSet &Z)
Returns true iff X and Y are c-separated by Z in g.
UndiGraph moralizedAncestralGraph(const G &g, const NodeSet &query)
Returns the moralized ancestral graph of query in g.
UndiGraph moralGraph(const G &g)
Returns the moral graph of g.
gum is the global namespace for all aGrUM entities
Definition agrum.h:46
bool rec_hasMixedReallyOrientedPath(const PDAG &gr, NodeSet &marked, NodeId node, NodeId goal, bool alreadyOriented)
Definition PDAG.cpp:90
STL namespace.