aGrUM  0.20.2
a C++ library for (probabilistic) graphical models
spanningForestPrim.cpp
Go to the documentation of this file.
1 /**
2  *
3  * Copyright 2005-2020 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  require_computation__(true) {
40  if (!graph || !cost) {
41  GUM_ERROR(GraphError, "invalid null graph or edge cost pointer");
42  }
43 
44  // for debugging purposes
46  }
47 
48  // copy constructor
55  // for debugging purposes
57  }
58 
59  // destructor
61  // for debugging purposes
63  }
64 
65  /// Returns the cost of the spanning forest
68 
69  return spanning_tree_cost__;
70  }
71 
72  /// Returns the edges in a min cost spanning forest
75 
76  return spanning_tree__.edges();
77  }
78 
79  /// Construct the spanning forest
82 
83  return spanning_tree__;
84  }
85 
86  /// compute the spanning forest
88  // compute a spanning tree in every connected component
89  for (const auto node: graph__.nodes()) {
91  }
92 
93  // indicate that everything was computed
94  require_computation__ = false;
95  }
96 
97  /// compute a spanning tree
99  // add the node to the spanning tree
101 
102  // explore its neighborhood
103  exploreNode__(id);
104 
105  // get the next nodes to link to the current spanning tree nodes
106 
107  while (!edgesToExplore__.empty()) {
108  const Edge edge = edgesToExplore__.pop();
109  const NodeId first = edge.first();
110  const NodeId second = edge.second();
111 
112  // consider only the edges that have one extremal node not in the spanning
113  // tree as those that can be added to the tree
114 
116  // add the edge to the spanning tree
120 
121  // We must explore the first node's neighborhood
123  } else if (!spanning_tree__.existsNode(second)) {
124  // add the edge to the spanning tree
128 
129  // We must explore the second node
131  }
132  }
133  }
134 
135  /// explore the neighborhood of a node belonging to the spanning tree
137  // add its neighbors edgesToExplore__ to indicate that they are
138  // potential next nodes to explore
139  for (const auto node: graph__.neighbours(id)) {
141  Edge edge(node, id);
143  }
144  }
145  }
146 
147 } /* namespace gum */
INLINE void emplace(Args &&... args)
Definition: set_tpl.h:669