aGrUM 3.0.0
a C++ library for (probabilistic) graphical models
moralization_tpl.h
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
42#pragma once
43
45
46namespace gum::graph {
47
49 inline void _marryParents_(const NodeSet& parents, UndiGraph& g) {
50 for (auto it1 = parents.begin(); it1 != parents.end(); ++it1) {
51 auto it2 = it1;
52 for (++it2; it2 != parents.end(); ++it2)
53 g.addEdge(*it1, *it2);
54 }
55 }
56
58
59 template < GUM_DiGraphable G >
60 UndiGraph moralGraph(const G& g) {
61 UndiGraph moral;
62 for (const auto node: g.nodes())
63 moral.addNodeWithId(node);
64
65 if constexpr (GUM_MixedGraphable< G >) {
66 for (const auto node: g.nodes()) {
67 for (const auto p: g.parents(node))
68 moral.addEdge(node, p);
69 for (const auto n: g.neighbours(node))
70 moral.addEdge(node, n);
71 }
72
73 NodeSet already;
74 for (const auto node: g.nodes()) {
75 if (already.contains(node)) continue;
76 already.insert(node);
77
78 NodeSet par = g.parents(node);
79 NodeSet frontier = g.neighbours(node);
80 while (!frontier.empty()) {
81 const NodeId nei = frontier.popFirst();
82 if (already.contains(nei)) continue;
83 already.insert(nei);
84 par += g.parents(nei);
85 frontier += g.neighbours(nei) - already;
86 }
87
88 _marryParents_(par, moral);
89 }
90 } else {
91 for (const auto node: g.nodes()) {
92 const auto& par = g.parents(node);
93 for (const auto p: par)
94 moral.addEdge(node, p);
95 _marryParents_(par, moral);
96 }
97 }
98 return moral;
99 }
100
101 template < GUM_DiGraphable G >
102 UndiGraph moralizedAncestralGraph(const G& g, const NodeSet& query) {
103 if constexpr (GUM_MixedGraphable< G >) {
104 MixedGraph ancestral;
105 NodeSet frontier{query};
106 for (const auto n: query)
107 ancestral.addNodeWithId(n);
108
109 while (!frontier.empty()) {
110 const NodeId current = *frontier.begin();
111 frontier.erase(current);
112
113 for (const auto p: g.parents(current)) {
114 if (!ancestral.existsNode(p)) {
115 ancestral.addNodeWithId(p);
116 frontier.insert(p);
117 }
118 ancestral.addArc(p, current);
119 }
120 for (const auto n: g.neighbours(current)) {
121 if (!ancestral.existsNode(n)) {
122 ancestral.addNodeWithId(n);
123 frontier.insert(n);
124 }
125 ancestral.addEdge(n, current);
126 }
127 }
128 return moralGraph(ancestral);
129
130 } else {
131 UndiGraph res;
132 NodeSet frontier{query};
133
134 while (!frontier.empty()) {
135 const NodeId current = *frontier.begin();
136 frontier.erase(current);
137 res.addNodeWithId(current);
138 for (const auto p: g.parents(current))
139 if (!res.existsNode(p) && !frontier.contains(p)) frontier.insert(p);
140 }
141
142 for (const auto node: res.nodes()) {
143 const auto& par = g.parents(node);
144 for (const auto p: par)
145 res.addEdge(node, p);
146 _marryParents_(par, res);
147 }
148 return res;
149 }
150 }
151
152} // namespace gum::graph
void addArc(const NodeId tail, const NodeId head) override
insert a new arc into the directed graph
Definition diGraph_inl.h:59
Base class for mixed graphs.
Definition mixedGraph.h:146
const NodeGraphPart & nodes() const
return *this as a NodeGraphPart
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
bool contains(const Key &k) const
Indicates whether a given elements belong to the set.
Definition set_tpl.h:468
Key popFirst()
Removes and returns an arbitrary element from the set.
Definition set_tpl.h:564
bool empty() const noexcept
Indicates whether the set is the empty set.
Definition set_tpl.h:613
iterator begin() const
The usual unsafe begin iterator to parse the set.
Definition set_tpl.h:409
void insert(const Key &k)
Inserts a new element into the set.
Definition set_tpl.h:510
void erase(const Key &k)
Erases an element from the set.
Definition set_tpl.h:553
Base class for undirected graphs.
Definition undiGraph.h:130
void addEdge(NodeId first, NodeId second) override
insert a new edge into the undirected graph
Concept for mixed graphs (both arcs and edges).
Size NodeId
Type for node ids.
Set< NodeId > NodeSet
Some typdefs and define for shortcuts ...
Moralization algorithms for aGrUM directed and mixed graphs.
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.