aGrUM  0.20.3
a C++ library for (probabilistic) graphical models
spanningForestPrim.cpp
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 
28 #include <agrum/agrum.h>
29 
30 #include <agrum/tools/graphs/algorithms/spanningForestPrim.h>
31 
32 namespace gum {
33 
34  /// Default constructor
36  const EdgeProperty< float >* cost) :
39  if (!graph || !cost) { GUM_ERROR(GraphError, "invalid null graph or edge cost pointer") }
40 
41  // for debugging purposes
43  }
44 
45  // copy constructor
51  // for debugging purposes
53  }
54 
55  // destructor
57  // for debugging purposes
59  }
60 
61  /// Returns the cost of the spanning forest
64 
65  return _spanning_tree_cost_;
66  }
67 
68  /// Returns the edges in a min cost spanning forest
71 
72  return _spanning_tree_.edges();
73  }
74 
75  /// Construct the spanning forest
78 
79  return _spanning_tree_;
80  }
81 
82  /// compute the spanning forest
84  // compute a spanning tree in every connected component
85  for (const auto node: _graph_.nodes()) {
87  }
88 
89  // indicate that everything was computed
90  _require_computation_ = false;
91  }
92 
93  /// compute a spanning tree
95  // add the node to the spanning tree
97 
98  // explore its neighborhood
100 
101  // get the next nodes to link to the current spanning tree nodes
102 
103  while (!_edgesToExplore_.empty()) {
104  const Edge edge = _edgesToExplore_.pop();
105  const NodeId first = edge.first();
106  const NodeId second = edge.second();
107 
108  // consider only the edges that have one extremal node not in the spanning
109  // tree as those that can be added to the tree
110 
112  // add the edge to the spanning tree
116 
117  // We must explore the first node's neighborhood
119  } else if (!_spanning_tree_.existsNode(second)) {
120  // add the edge to the spanning tree
124 
125  // We must explore the second node
127  }
128  }
129  }
130 
131  /// explore the neighborhood of a node belonging to the spanning tree
133  // add its neighbors _edgesToExplore_ to indicate that they are
134  // potential next nodes to explore
135  for (const auto node: _graph_.neighbours(id)) {
137  Edge edge(node, id);
139  }
140  }
141  }
142 
143 } /* namespace gum */
INLINE void emplace(Args &&... args)
Definition: set_tpl.h:643