aGrUM 3.0.0
a C++ library for (probabilistic) graphical models
markovBlanket_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
48 template < GUM_DiGraphable G >
49 DAG markovBlanket(const G& g, NodeId node, int level) {
50 if (!g.exists(node))
51 GUM_ERROR(InvalidArgument, "Node " << node << " does not exist in the graph.")
52 if (level < 1) GUM_ERROR(InvalidArgument, "level must be >= 1")
53
54 DAG mb;
55
56 auto step = [&](NodeId n, NodeSet& next_frontier) {
57 for (const auto par: g.parents(n)) {
58 if (!mb.existsNode(par)) {
59 mb.addNodeWithId(par);
60 next_frontier.insert(par);
61 }
62 if (!mb.existsArc(par, n)) mb.addArc(par, n);
63 }
64 for (const auto chi: g.children(n)) {
65 if (!mb.existsNode(chi)) {
66 mb.addNodeWithId(chi);
67 next_frontier.insert(chi);
68 }
69 if (!mb.existsArc(n, chi)) mb.addArc(n, chi);
70 for (const auto opar: g.parents(chi)) {
71 if (opar == n) continue;
72 if (!mb.existsNode(opar)) {
73 mb.addNodeWithId(opar);
74 next_frontier.insert(opar);
75 }
76 if (!mb.existsArc(opar, chi)) mb.addArc(opar, chi);
77 }
78 }
79 };
80
81 mb.addNodeWithId(node);
82 NodeSet frontier;
83 step(node, frontier);
84
85 for (int lv = 1; lv < level && !frontier.empty(); ++lv) {
86 NodeSet next;
87 for (const auto n: frontier)
88 step(n, next);
89 frontier = std::move(next);
90 }
91
92 return mb;
93 }
94
95} // namespace gum::graph
bool existsArc(const Arc &arc) const
indicates whether a given arc exists
Base class for dag.
Definition DAG.h:121
void addArc(NodeId tail, NodeId head) final
insert a new arc into the directed graph
Definition DAG_inl.h:75
Exception: at least one argument passed to a function is not what was expected.
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 empty() const noexcept
Indicates whether the set is the empty set.
Definition set_tpl.h:613
#define GUM_ERROR(type, msg)
Definition exceptions.h:76
Size NodeId
Type for node ids.
Set< NodeId > NodeSet
Some typdefs and define for shortcuts ...
Generic Markov-blanket algorithm for directed graphs.
DAG markovBlanket(const G &g, NodeId node, int level=1)
Returns the level-level Markov blanket of node in g as a DAG.