aGrUM 3.1.1
a C++ library for (probabilistic) graphical models
spanningForestPrim.cpp
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
47
48#include <agrum/agrum.h>
49
51
52namespace gum {
53
56 const EdgeProperty< float >* cost) :
59 if (!graph || !cost) { GUM_ERROR(GraphError, "invalid null graph or edge cost pointer") }
60
61 // for debugging purposes
62 GUM_CONSTRUCTOR(SpanningForestPrim);
63 }
64
65 // copy constructor
74
83
84 // destructor
86 // for debugging purposes
87 GUM_DESTRUCTOR(SpanningForestPrim);
88 }
89
96
103
110
113 _spanning_tree_.clear();
114 _edgesToExplore_.clear();
116
117 // compute a spanning tree in every connected component
118 for (const auto node: _graph_.nodes()) {
119 if (!_spanning_tree_.existsNode(node)) { _computeInAComponent_(node); }
120 }
121
122 // indicate that everything was computed
123 _require_computation_ = false;
124 }
125
128 // add the node to the spanning tree
129 _spanning_tree_.addNodeWithId(id);
130
131 // explore its neighborhood
132 _exploreNode_(id);
133
134 // get the next nodes to link to the current spanning tree nodes
135
136 while (!_edgesToExplore_.empty()) {
137 const Edge edge = _edgesToExplore_.pop();
138 const NodeId first = edge.first();
139 const NodeId second = edge.second();
140
141 // consider only the edges that have one extremal node not in the spanning
142 // tree as those that can be added to the tree
143
144 if (!_spanning_tree_.existsNode(first)) {
145 // add the edge to the spanning tree
146 _spanning_tree_.addNodeWithId(first);
147 _spanning_tree_.addEdge(first, second);
149
150 // We must explore the first node's neighborhood
151 _exploreNode_(first);
152 } else if (!_spanning_tree_.existsNode(second)) {
153 // add the edge to the spanning tree
154 _spanning_tree_.addNodeWithId(second);
155 _spanning_tree_.addEdge(first, second);
157
158 // We must explore the second node
159 _exploreNode_(second);
160 }
161 }
162 }
163
166 // add its neighbors _edgesToExplore_ to indicate that they are
167 // tensor next nodes to explore
168 for (const auto node: _graph_.neighbours(id)) {
169 if (!_spanning_tree_.existsNode(node)) {
170 Edge edge(node, id);
171 if (!_costTable_.exists(edge)) {
173 "no cost defined for edge (" << edge.first() << "," << edge.second() << ")")
174 }
175 _edgesToExplore_.insert(edge, _costTable_[edge]);
176 }
177 }
178 }
179
180} /* namespace gum */
The base class for all undirected edges.
GUM_NODISCARD NodeId first() const
returns one extremal node ID (whichever one it is is unspecified)
GUM_NODISCARD NodeId second() const
returns the node ID of the other extremal node ID
Exception base for graph error.
Exception : the element we looked for cannot be found.
UndiGraph _spanning_tree_
the computed spanning tree
SpanningForestPrim(const UndiGraph *graph, const EdgeProperty< float > *costTable)
Default constructor.
float costOfSpanningForest() override
Returns the cost of the spanning forest.
const UndiGraph & spanningForest() override
Construct the spanning forest.
const EdgeSet & edgesInSpanningForest() override
Returns the edges in a min cost spanning forest.
void _compute_()
Computes the spanning forest.
const EdgeProperty< float > & _costTable_
the costs of the edges
bool _require_computation_
a Boolean indicating whether we need recompute the spanning tree
void _computeInAComponent_(const NodeId id)
compute a spanning tree in a given connected component of graph
float _spanning_tree_cost_
the cost of the spanning tree
void _exploreNode_(const NodeId id)
explore the neighborhood of a node belonging to the spanning tree
~SpanningForestPrim() override
Destructor.
PriorityQueue< Edge, float > _edgesToExplore_
the next edges that may be added to the spanning tree
const UndiGraph & _graph_
the graph the spanning tree of which we wish to compute
SpanningForest()
default constructor
Base class for undirected graphs.
Definition undiGraph.h:130
#define GUM_ERROR(type, msg)
Definition exceptions.h:76
Set< Edge > EdgeSet
Some typdefs and define for shortcuts ...
Size NodeId
Type for node ids.
HashTable< Edge, VAL > EdgeProperty
Property on graph elements.
gum is the global namespace for all aGrUM entities
Definition agrum.h:46
STL namespace.
The Prim algorithm for computing min cost spanning trees or forests.