aGrUM 2.3.2
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-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
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
75 // destructor
77 // for debugging purposes
78 GUM_DESTRUCTOR(SpanningForestPrim);
79 }
80
87
94
101
104 // compute a spanning tree in every connected component
105 for (const auto node: _graph_.nodes()) {
106 if (!_spanning_tree_.existsNode(node)) { _computeInAComponent_(node); }
107 }
108
109 // indicate that everything was computed
110 _require_computation_ = false;
111 }
112
115 // add the node to the spanning tree
116 _spanning_tree_.addNodeWithId(id);
117
118 // explore its neighborhood
119 _exploreNode_(id);
120
121 // get the next nodes to link to the current spanning tree nodes
122
123 while (!_edgesToExplore_.empty()) {
124 const Edge edge = _edgesToExplore_.pop();
125 const NodeId first = edge.first();
126 const NodeId second = edge.second();
127
128 // consider only the edges that have one extremal node not in the spanning
129 // tree as those that can be added to the tree
130
131 if (!_spanning_tree_.existsNode(first)) {
132 // add the edge to the spanning tree
133 _spanning_tree_.addNodeWithId(first);
134 _spanning_tree_.addEdge(first, second);
136
137 // We must explore the first node's neighborhood
138 _exploreNode_(first);
139 } else if (!_spanning_tree_.existsNode(second)) {
140 // add the edge to the spanning tree
141 _spanning_tree_.addNodeWithId(second);
142 _spanning_tree_.addEdge(first, second);
144
145 // We must explore the second node
146 _exploreNode_(second);
147 }
148 }
149 }
150
153 // add its neighbors _edgesToExplore_ to indicate that they are
154 // tensor next nodes to explore
155 for (const auto node: _graph_.neighbours(id)) {
156 if (!_spanning_tree_.existsNode(node)) {
157 Edge edge(node, id);
158 _edgesToExplore_.insert(edge, _costTable_[edge]);
159 }
160 }
161 }
162
163} /* 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.
UndiGraph _spanning_tree_
the computed spanning tree
SpanningForestPrim(const UndiGraph *graph, const EdgeProperty< float > *costTable)
Default constructor.
float costOfSpanningForest()
Returns the cost of the spanning forest.
void _compute_()
Computes the spanning forest.
const EdgeProperty< float > & _costTable_
the costs of the edges
const EdgeSet & edgesInSpanningForest()
Returns the edges in a min cost spanning forest.
bool _require_computation_
a Boolean indicating whether we need recompute the spanning tree
const UndiGraph & spanningForest()
Construct the spanning forest.
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
virtual ~SpanningForestPrim()
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:128
#define GUM_ERROR(type, msg)
Definition exceptions.h:72
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
The Prim algorithm for computing min cost spanning trees or forests.