aGrUM 3.1.1
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-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#pragma once
42
43
49
50#include <agrum/base/graphs/cliqueGraph.h> // to ease IDE parser
51#ifndef DOXYGEN_SHOULD_SKIP_THIS
52
53// to ease parser in IDE
55
56namespace gum {
57
60 if (this != &g) {
62 _cliques_ = g._cliques_;
63 _separators_ = g._separators_;
64 GUM_OP_CPY(CliqueGraph);
65 }
66
67 return *this;
68 }
69
72 if (this != &g) {
73 UndiGraph::operator=(std::move(g));
74 _cliques_ = std::move(g._cliques_);
75 _separators_ = std::move(g._separators_);
76 GUM_OP_MOV(CliqueGraph);
77 }
78
79 return *this;
80 }
81
82 INLINE void CliqueGraph::addEdge(NodeId first, NodeId second) {
83 Edge edge(first, second);
84
85 if (!existsEdge(edge)) {
86 // create the edge in the graph
87 UndiGraph::addEdge(first, second);
88
89 // create the separator
90 _separators_.insert(edge, _cliques_[first] * _cliques_[second]);
91 }
92 }
93
95
96 INLINE void CliqueGraph::eraseEdge(const Edge& edge) {
97 if (existsEdge(edge)) {
98 _separators_.erase(edge);
100 }
101 }
102
104
105 INLINE NodeId CliqueGraph::addNode(const NodeSet& clique) {
106 // create the new node in the graph
107 NodeId new_node = UndiGraph::addNode();
108
109 // update the set of nodes of the clique
110 _cliques_.insert(new_node, clique);
111 return new_node;
112 }
113
114 INLINE NodeId CliqueGraph::addNode() { return addNode(NodeSet()); }
115
117
118 INLINE void CliqueGraph::addNodeWithId(const NodeId id, const NodeSet& clique) {
119 // create the new node in the graph
121
122 // update the set of nodes of the clique
123 _cliques_.insert(id, clique);
124 }
125
126 INLINE void CliqueGraph::addNodeWithId(const NodeId id) { addNodeWithId(id, NodeSet()); }
127
129
130 INLINE void CliqueGraph::eraseNode(const NodeId id) {
131 // check if the node belongs to the graph
132 if (!exists(id)) return;
133
134 // remove the separators
135 auto nei = neighbours(id);
136 for (auto iter = nei.beginSafe(); iter != nei.endSafe(); ++iter) // safe iterator needed here
137 eraseEdge(Edge(*iter, id));
138
139 // erase the clique set
140 _cliques_.erase(id);
141
142 // erase the node and its neighbours from the graph
144 }
145
147
148 INLINE const NodeSet& CliqueGraph::clique(const NodeId clique) const { return _cliques_[clique]; }
149
152
153 INLINE NodeId CliqueGraph::container(const NodeId id) const {
154 for (const auto& elt: _cliques_)
155 if (elt.second.contains(id)) return elt.first;
156
157 GUM_ERROR(NotFound, "This node belongs to no clique")
158 }
159
161
162 INLINE void CliqueGraph::_updateSeparators_(const NodeId id1) {
163 for (const auto nei: neighbours(id1))
164 _separators_[Edge(nei, id1)] = _cliques_[id1] * _cliques_[nei];
165 }
166
169
170 INLINE void CliqueGraph::setClique(const NodeId id, const NodeSet& new_clique) {
171 // get the current clique set
172 _cliques_[id] = new_clique;
174 }
175
177
178 INLINE const NodeSet& CliqueGraph::separator(const Edge& edge) const {
179 return _separators_[edge];
180 }
181
183
184 INLINE const NodeSet& CliqueGraph::separator(const NodeId node1, const NodeId node2) const {
185 return separator(Edge(node1, node2));
186 }
187
189
190 INLINE bool CliqueGraph::isJoinTree() const {
192 }
193
196
197 INLINE void CliqueGraph::clear() {
199 _cliques_.clear();
200 _separators_.clear();
201 }
202
204
205 INLINE void CliqueGraph::clearEdges() {
207 _separators_.clear();
208 }
209
210} /* namespace gum */
211
212#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
void eraseNode(const NodeId node) override
removes a given clique from the clique graph
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
void addNodeWithId(const NodeId id, const NodeSet &clique)
try to add a new clique to the graph
void addEdge(NodeId first, NodeId second) override
inserts a new edge between two cliques
void clearEdges() override
removes all edges and their separators
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
void eraseEdge(const Edge &edge) override
removes an edge (and its separator) from the clique graph
void clear() override
removes all the cliques and separators from the graph (as well as their adjacent edges)
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
NodeId addNode() override
adds a new clique to the graph
CliqueGraph(Size nodes_size=HashTableConst::default_size, bool nodes_resize_policy=true, Size edges_size=HashTableConst::default_size, bool edges_resize_policy=true)
basic constructor: creates an empty clique graph
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
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
void addEdge(NodeId first, NodeId second) override
insert a new edge into the undirected graph
bool hasUndirectedCycle() const
checks whether the graph contains cycles
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:76
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