aGrUM 3.1.1
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
57 template < typename DEST_GRAPH, typename SRC_GRAPH >
58 inline void _copyNodeWithName_(DEST_GRAPH& dest, const SRC_GRAPH& src, NodeId id) {
59 dest.addNodeWithId(id);
60 if (src.hasName(id)) dest.setName(id, src.nameFromId(id));
61 }
62
64
65 template < GUM_DiGraphable G >
66 UndiGraph moralGraph(const G& g) {
67 UndiGraph moral;
68 for (const auto node: g.nodes()) {
69 _copyNodeWithName_(moral, g, node);
70 }
71
72 if constexpr (GUM_MixedGraphable< G >) {
73 for (const auto node: g.nodes()) {
74 for (const auto p: g.parents(node))
75 moral.addEdge(node, p);
76 for (const auto n: g.neighbours(node))
77 moral.addEdge(node, n);
78 }
79
80 NodeSet already;
81 for (const auto node: g.nodes()) {
82 if (already.contains(node)) continue;
83 already.insert(node);
84
85 NodeSet par = g.parents(node);
86 NodeSet frontier = g.neighbours(node);
87 while (!frontier.empty()) {
88 const NodeId nei = frontier.popFirst();
89 if (already.contains(nei)) continue;
90 already.insert(nei);
91 par += g.parents(nei);
92 frontier += g.neighbours(nei) - already;
93 }
94
95 _marryParents_(par, moral);
96 }
97 } else {
98 for (const auto node: g.nodes()) {
99 const auto& par = g.parents(node);
100 for (const auto p: par)
101 moral.addEdge(node, p);
102 _marryParents_(par, moral);
103 }
104 }
105 return moral;
106 }
107
108 template < GUM_DiGraphable G >
109 UndiGraph moralizedAncestralGraph(const G& g, const NodeSet& query) {
110 if constexpr (GUM_MixedGraphable< G >) {
111 MixedGraph ancestral;
112 NodeSet frontier{query};
113 for (const auto n: query) {
114 _copyNodeWithName_(ancestral, g, n);
115 }
116
117 while (!frontier.empty()) {
118 const NodeId current = *frontier.begin();
119 frontier.erase(current);
120
121 for (const auto p: g.parents(current)) {
122 if (!ancestral.existsNode(p)) {
123 _copyNodeWithName_(ancestral, g, p);
124 frontier.insert(p);
125 }
126 ancestral.addArc(p, current);
127 }
128 for (const auto n: g.neighbours(current)) {
129 if (!ancestral.existsNode(n)) {
130 _copyNodeWithName_(ancestral, g, n);
131 frontier.insert(n);
132 }
133 ancestral.addEdge(n, current);
134 }
135 }
136 return moralGraph(ancestral);
137
138 } else {
139 UndiGraph res;
140 NodeSet frontier{query};
141
142 while (!frontier.empty()) {
143 const NodeId current = *frontier.begin();
144 frontier.erase(current);
145 _copyNodeWithName_(res, g, current);
146 for (const auto p: g.parents(current))
147 if (!res.existsNode(p) && !frontier.contains(p)) frontier.insert(p);
148 }
149
150 for (const auto node: res.nodes()) {
151 const auto& par = g.parents(node);
152 for (const auto p: par)
153 res.addEdge(node, p);
154 _marryParents_(par, res);
155 }
156 return res;
157 }
158 }
159
160} // 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
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.