2 #ifndef ANDRES_GRAPH_MINIMUM_SPANNING_TREE_HXX
3 #define ANDRES_GRAPH_MINIMUM_SPANNING_TREE_HXX
9 #include "andres/functional.hxx"
10 #include "subgraph.hxx"
15 template<
typename GRAPH,
typename ECA,
typename PRED,
typename FUNC = Identity<
typename ECA::value_type>>
16 inline typename ECA::value_type
24 template<
typename GRAPH,
typename ECA,
typename SUBGRAPH,
typename PRED,
typename FUNC = Identity<
typename ECA::value_type>>
25 inline typename ECA::value_type
34 template<
typename GRAPH,
typename ECA,
typename SUBGRAPH,
typename PRED,
typename FUNC = Identity<
typename ECA::value_type>>
35 inline typename ECA::value_type
45 template<
typename GRAPH,
typename ECA,
typename PRED,
typename FUNC = Identity<
typename ECA::value_type>>
46 inline typename ECA::value_type
54 template<
typename GRAPH,
typename ECA,
typename SUBGRAPH,
typename PRED,
typename FUNC = Identity<
typename ECA::value_type>>
55 inline typename ECA::value_type
64 template<
typename GRAPH,
typename ECA,
typename SUBGRAPH,
typename PRED,
typename FUNC = Identity<
typename ECA::value_type>>
65 inline typename ECA::value_type
87 template<
typename GRAPH,
typename ECA,
typename PRED,
typename FUNC>
88 inline typename ECA::value_type
91 const ECA& edge_weights,
112 template<
typename GRAPH,
typename ECA,
typename SUBGRAPH,
typename PRED,
typename FUNC>
113 inline typename ECA::value_type
116 const ECA& edge_weights,
117 const SUBGRAPH& subgraph_mask,
122 typedef typename ECA::value_type value_type;
124 std::fill(std::begin(predecessor), std::end(predecessor), graph.numberOfEdges());
126 value_type mst_value = value_type();
127 for (std::size_t i = 0; i < graph.numberOfVertices(); ++i)
128 if (predecessor[i] == graph.numberOfEdges() && subgraph_mask.vertex(i))
129 mst_value +=
findMSTPrim(graph, edge_weights, subgraph_mask, i, predecessor, f);
148 template<
typename GRAPH,
typename ECA,
typename SUBGRAPH,
typename PRED,
typename FUNC>
149 inline typename ECA::value_type
152 const ECA& edge_weights,
153 const SUBGRAPH& subgraph_mask,
154 std::size_t starting_vertex,
159 typedef typename ECA::value_type value_type;
163 element(value_type weight, std::size_t vertex_index) :
164 weight_(weight), vertex_index_(vertex_index)
167 bool operator<(
const element& other)
const
170 return weight_ > other.weight_;
174 std::size_t vertex_index_;
177 std::vector<value_type> min_edge(graph.numberOfVertices(), std::numeric_limits<value_type>::max());
178 std::vector<char> visited(graph.numberOfVertices());
180 std::priority_queue<element> Q;
181 Q.push(element(value_type(), starting_vertex));
183 predecessor[starting_vertex] = graph.numberOfEdges();
185 value_type mst_value = value_type();
192 if (visited[v.vertex_index_])
195 visited[v.vertex_index_] = 1;
196 mst_value += v.weight_;
198 auto e = graph.edgesFromVertexBegin(v.vertex_index_);
199 for (
auto w = graph.verticesFromVertexBegin(v.vertex_index_); w != graph.verticesFromVertexEnd(v.vertex_index_); ++w, ++e)
200 if (subgraph_mask.vertex(*w) &&
201 subgraph_mask.edge(*e) &&
203 *w != v.vertex_index_ &&
204 f(edge_weights[*e]) < min_edge[*w]
207 min_edge[*w] = f(edge_weights[*e]);
208 predecessor[*w] = *e;
209 Q.push(element(f(edge_weights[*e]), *w));
225 template<
typename GRAPH,
typename ECA,
typename PRED,
typename FUNC>
226 inline typename ECA::value_type
229 const ECA& edge_weights,
247 template<
typename GRAPH,
typename ECA,
typename SUBGRAPH,
typename PRED,
typename FUNC>
248 inline typename ECA::value_type
251 const ECA& edge_weights,
252 const SUBGRAPH& subgraph_mask,
257 typedef typename ECA::value_type value_type;
259 std::fill(std::begin(predecessor), std::end(predecessor), graph.numberOfEdges());
261 value_type mst_value = value_type();
262 for (std::size_t i = 0; i < graph.numberOfVertices(); ++i)
263 if (predecessor[i] == graph.numberOfEdges() && subgraph_mask.vertex(i))
280 template<
typename GRAPH,
typename ECA,
typename SUBGRAPH,
typename PRED,
typename FUNC>
281 inline typename ECA::value_type
284 const ECA& edge_weights,
285 const SUBGRAPH& subgraph_mask,
286 std::size_t starting_vertex,
291 typedef typename ECA::value_type value_type;
293 std::vector<value_type> min_edge(graph.numberOfVertices(), std::numeric_limits<value_type>::max());
294 std::vector<char> visited(graph.numberOfVertices());
296 min_edge[starting_vertex] = value_type();
297 predecessor[starting_vertex] = graph.numberOfEdges();
299 value_type mst_value = value_type();
301 for (std::size_t i = 0; i < graph.numberOfVertices(); ++i)
305 for (std::size_t j = 0; j < graph.numberOfVertices(); ++j)
306 if (subgraph_mask.vertex(j) &&
308 (v == -1 || min_edge[j] < min_edge[v])
315 mst_value += min_edge[v];
318 auto e = graph.edgesFromVertexBegin(v);
319 for (
auto w = graph.verticesFromVertexBegin(v); w != graph.verticesFromVertexEnd(v); ++w, ++e)
320 if (subgraph_mask.vertex(*w) &&
321 subgraph_mask.edge(*e) &&
324 f(edge_weights[*e]) < min_edge[*w]
327 min_edge[*w] = f(edge_weights[*e]);
328 predecessor[*w] = *e;