aGrUM  0.20.3
a C++ library for (probabilistic) graphical models
spanningForestPrim.h
Go to the documentation of this file.
1 /**
2  *
3  * Copyright (c) 2005-2021 by Pierre-Henri WUILLEMIN(@LIP6) & Christophe GONZALES(@AMU)
4  * info_at_agrum_dot_org
5  *
6  * This library is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU Lesser General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public License
17  * along with this library. If not, see <http://www.gnu.org/licenses/>.
18  *
19  */
20 
21 
22 /** @file
23  * @brief The Prim algorithm for computing min cost spanning trees or forests
24  *
25  * @author Jean-Philippe DUBUS & Christophe GONZALES(@AMU)
26  */
27 #ifndef GUM_SPANNING_FOREST_PRIM_H
28 #define GUM_SPANNING_FOREST_PRIM_H
29 
30 #include <agrum/tools/core/priorityQueue.h>
31 #include <agrum/tools/graphs/algorithms/spanningForest.h>
32 
33 namespace gum {
34 
35  /* ===========================================================================
36  */
37  /** @class SpanningForestPrim
38  * @brief The Prim algorithm for computing min cost spanning trees or forests
39  *
40  * Binary heap implementation : O(E log(V)) */
41  /* ===========================================================================
42  */
44  public:
45  // ############################################################################
46  /// @name Constructors / Destructors
47  // ############################################################################
48  /// @{
49 
50  /// Default constructor
51  /** Note that this algorithm takes into account the fact that the graph
52  * given
53  * in input is not connected (that, is, it may contain several connected
54  * components)
55  * @param graph the graph the spanning forest of which we look for
56  * @param costTable the cost for each edge of graph
57  * @warning note that, by aGrUM's rule, the graph and the costs are not
58  * copied but only referenced by the elimination sequence algorithm.
59  * @throws GraphError if the grand and/or the cost table are null pointers
60  */
61  SpanningForestPrim(const UndiGraph* graph, const EdgeProperty< float >* costTable);
62 
63  /// Copy constructor
65 
66  /// Destructor
67  virtual ~SpanningForestPrim();
68 
69  /// @}
70 
71  // ############################################################################
72  /// @name Accessors / Modifiers
73  // ############################################################################
74  /// @{
75 
76  /// Returns the edges in a min cost spanning forest
77  /** @returns edges in the spanning forest */
79 
80  /// Construct the spanning forest
81  /** @return the spanning forest */
82  const UndiGraph& spanningForest();
83 
84  /// Returns the cost of the spanning forest
85  /** @return cost of the spanning forest */
86  float costOfSpanningForest();
87 
88  /// @}
89 
90  private:
91  /// the graph the spanning tree of which we wish to compute
93 
94  /// the costs of the edges
95  const EdgeProperty< float >& _costTable_;
96 
97  /// the next edges that may be added to the spanning tree
99 
100  /// the computed spanning tree
102 
103  /// the cost of the spanning tree
105 
106  /// a Boolean indicating whether we need recompute the spanning tree
108 
109  /// Computes the spanning forest
110  void _compute_();
111 
112  /// compute a spanning tree in a given connected component of _graph_
113  void _computeInAComponent_(const NodeId id);
114 
115  /// explore the neighborhood of a node belonging to the spanning tree
116  void _exploreNode_(const NodeId id);
117 
118  /// Copy operator: private to prevent using it
120  };
121 
122 } /* namespace gum */
123 
124 #endif /* GUM_SPANNING_FOREST_PRIM_H */
bool _require_computation_
a Boolean indicating whether we need recompute the spanning tree
UndiGraph _spanning_tree_
the computed spanning tree
SpanningForestPrim(const SpanningForestPrim &toCopy)
Copy constructor.
float _spanning_tree_cost_
the cost of the spanning tree
void _compute_()
Computes the spanning forest.
INLINE void emplace(Args &&... args)
Definition: set_tpl.h:643
SpanningForestPrim(const UndiGraph *graph, const EdgeProperty< float > *costTable)
Default constructor.
const EdgeProperty< float > & _costTable_
the costs of the edges
const UndiGraph & spanningForest()
Construct the spanning forest.
The Prim algorithm for computing min cost spanning trees or forests.
const EdgeSet & edgesInSpanningForest()
Returns the edges in a min cost spanning forest.
float costOfSpanningForest()
Returns the cost of the spanning forest.
PriorityQueue< Edge, float > _edgesToExplore_
the next edges that may be added to the spanning tree
virtual ~SpanningForestPrim()
Destructor.
SpanningForestPrim & operator=(const SpanningForestPrim &toCopy)
Copy operator: private to prevent using it.
void _exploreNode_(const NodeId id)
explore the neighborhood of a node belonging to the spanning tree
void _computeInAComponent_(const NodeId id)
compute a spanning tree in a given connected component of graph
const UndiGraph & _graph_
the graph the spanning tree of which we wish to compute