aGrUM 2.3.2
a C++ library for (probabilistic) graphical models
cliqueGraph_inl.h
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#pragma once
41
42
48
49#ifndef DOXYGEN_SHOULD_SKIP_THIS
50
51// to ease parser in IDE
53
54namespace gum {
55
58 if (this != &g) {
60 _cliques_ = g._cliques_;
61 _separators_ = g._separators_;
62 }
63
64 return *this;
65 }
66
67 INLINE void CliqueGraph::addEdge(NodeId first, NodeId second) {
68 Edge edge(first, second);
69
70 if (!existsEdge(edge)) {
71 // create the edge in the graph
72 UndiGraph::addEdge(first, second);
73
74 // create the separator
75 _separators_.insert(edge, _cliques_[first] * _cliques_[second]);
76 }
77 }
78
80
81 INLINE void CliqueGraph::eraseEdge(const Edge& edge) {
82 if (existsEdge(edge)) {
83 _separators_.erase(edge);
85 }
86 }
87
89
90 INLINE NodeId CliqueGraph::addNode(const NodeSet& clique) {
91 // create the new node in the graph
92 NodeId new_node = UndiGraph::addNode();
93
94 // update the set of nodes of the clique
95 _cliques_.insert(new_node, clique);
96 return new_node;
97 }
98
99 INLINE NodeId CliqueGraph::addNode() { return addNode(NodeSet()); }
100
102
103 INLINE void CliqueGraph::addNodeWithId(const NodeId id, const NodeSet& clique) {
104 // create the new node in the graph
106
107 // update the set of nodes of the clique
108 _cliques_.insert(id, clique);
109 }
110
111 INLINE void CliqueGraph::addNodeWithId(const NodeId id) { addNodeWithId(id, NodeSet()); }
112
114
115 INLINE void CliqueGraph::eraseNode(const NodeId id) {
116 // check if the node belongs to the graph
117 if (!exists(id)) return;
118
119 // remove the separators
120 auto nei = neighbours(id);
121 for (auto iter = nei.beginSafe(); iter != nei.endSafe(); ++iter) // safe iterator needed here
122 eraseEdge(Edge(*iter, id));
123
124 // erase the clique set
125 _cliques_.erase(id);
126
127 // erase the node and its neighbours from the graph
129 }
130
132
133 INLINE const NodeSet& CliqueGraph::clique(const NodeId clique) const { return _cliques_[clique]; }
134
137
138 INLINE NodeId CliqueGraph::container(const NodeId id) const {
139 for (const auto& elt: _cliques_)
140 if (elt.second.contains(id)) return elt.first;
141
142 GUM_ERROR(NotFound, "This node belongs to no clique")
143 }
144
146
147 INLINE void CliqueGraph::_updateSeparators_(const NodeId id1) {
148 for (const auto nei: neighbours(id1))
149 _separators_[Edge(nei, id1)] = _cliques_[id1] * _cliques_[nei];
150 }
151
154
155 INLINE void CliqueGraph::setClique(const NodeId id, const NodeSet& new_clique) {
156 // get the current clique set
157 _cliques_[id] = new_clique;
159 }
160
162
163 INLINE const NodeSet& CliqueGraph::separator(const Edge& edge) const {
164 return _separators_[edge];
165 }
166
168
169 INLINE const NodeSet& CliqueGraph::separator(const NodeId node1, const NodeId node2) const {
170 return separator(Edge(node1, node2));
171 }
172
174
175 INLINE bool CliqueGraph::isJoinTree() const {
177 }
178
181
182 INLINE void CliqueGraph::clear() {
184 _cliques_.clear();
185 _separators_.clear();
186 }
187
189
190 INLINE void CliqueGraph::clearEdges() {
192 _separators_.clear();
193 }
194
195} /* namespace gum */
196
197#endif /* DOXYGEN_SHOULD_SKIP_THIS */
Basic graph of cliques.
Definition cliqueGraph.h:77
const NodeSet & separator(const Edge &edge) const
returns the separator included in a given edge
bool hasRunningIntersection() const
indicates whether the running intersection property holds
void _updateSeparators_(const NodeId clique1)
function used to update the separators when a clique is modified
virtual void addEdge(NodeId first, NodeId second)
inserts a new edge between two cliques
virtual void addNodeWithId(const NodeId id, const NodeSet &clique)
try to add a new clique to the graph
virtual void setClique(const NodeId idClique, const NodeSet &new_clique)
changes the set of nodes included into a given clique and returns the new set
virtual void clear()
removes all the cliques and separators from the graph (as well as their adjacent edges)
virtual NodeId addNode()
adds a new clique to the graph
virtual void eraseEdge(const Edge &edge)
removes an edge (and its separator) from the clique graph
virtual void eraseNode(const NodeId node)
removes a given clique from the clique graph
NodeId container(const NodeId idNode) const
returns the id of a clique containing the node the id of which is in argument
EdgeProperty< NodeSet > _separators_
the set of nodes contained into the separators
NodeProperty< NodeSet > _cliques_
the set of nodes contained into the cliques
const NodeSet & clique(const NodeId idClique) const
returns the set of nodes included into a given clique
virtual void clearEdges()
removes all edges and their separators
CliqueGraph & operator=(const CliqueGraph &from)
copy operator
bool isJoinTree() const
indicates whether the graph is a join tree
virtual void eraseEdge(const Edge &edge)
removes an edge from the EdgeGraphPart
virtual void clearEdges()
removes all the edges from the EdgeGraphPart
bool existsEdge(const Edge &edge) const
indicates whether a given edge exists
const NodeSet & neighbours(NodeId id) const
returns the set of node neighbours to a given node
The base class for all undirected edges.
bool exists(const NodeId id) const
alias for existsNode
virtual NodeId addNode()
insert a new node and return its id
virtual void addNodeWithId(const NodeId id)
try to insert a node with the given id
UndiGraph & operator=(const UndiGraph &g)
copy operator
void clear() override
removes all the nodes and edges from the graph
bool hasUndirectedCycle() const
checks whether the graph contains cycles
Definition undiGraph.cpp:90
void addEdge(NodeId first, NodeId second) override
insert a new edge into the undirected graph
void eraseNode(NodeId id) override
remove a node and its adjacent edges from the graph
Basic class for all graphs of cliques (join trees, etc).
#define GUM_ERROR(type, msg)
Definition exceptions.h:72
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