/Src/Dependencies/Boost/boost/graph/distributed/dehne_gotz_min_spanning_tree.hpp

http://hadesmem.googlecode.com/ · C++ Header · 938 lines · 574 code · 121 blank · 243 comment · 59 complexity · f2dedf7719ffb8142aeb0e0f588028ab MD5 · raw file

  1. // Copyright (C) 2004-2006 The Trustees of Indiana University.
  2. // Use, modification and distribution is subject to the Boost Software
  3. // License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  4. // http://www.boost.org/LICENSE_1_0.txt)
  5. // Authors: Douglas Gregor
  6. // Andrew Lumsdaine
  7. /**
  8. * This header implements four distributed algorithms to compute
  9. * the minimum spanning tree (actually, minimum spanning forest) of a
  10. * graph. All of the algorithms were implemented as specified in the
  11. * paper by Dehne and Gotz:
  12. *
  13. * Frank Dehne and Silvia Gotz. Practical Parallel Algorithms for Minimum
  14. * Spanning Trees. In Symposium on Reliable Distributed Systems,
  15. * pages 366--371, 1998.
  16. *
  17. * There are four algorithm variants implemented.
  18. */
  19. #ifndef BOOST_DEHNE_GOTZ_MIN_SPANNING_TREE_HPP
  20. #define BOOST_DEHNE_GOTZ_MIN_SPANNING_TREE_HPP
  21. #ifndef BOOST_GRAPH_USE_MPI
  22. #error "Parallel BGL files should not be included unless <boost/graph/use_mpi.hpp> has been included"
  23. #endif
  24. #include <boost/graph/graph_traits.hpp>
  25. #include <boost/property_map/property_map.hpp>
  26. #include <vector>
  27. #include <boost/graph/parallel/algorithm.hpp>
  28. #include <boost/limits.hpp>
  29. #include <utility>
  30. #include <boost/pending/disjoint_sets.hpp>
  31. #include <boost/pending/indirect_cmp.hpp>
  32. #include <boost/property_map/parallel/caching_property_map.hpp>
  33. #include <boost/graph/vertex_and_edge_range.hpp>
  34. #include <boost/graph/kruskal_min_spanning_tree.hpp>
  35. #include <boost/iterator/counting_iterator.hpp>
  36. #include <boost/iterator/transform_iterator.hpp>
  37. #include <boost/graph/parallel/container_traits.hpp>
  38. #include <boost/graph/parallel/detail/untracked_pair.hpp>
  39. #include <cmath>
  40. namespace boost { namespace graph { namespace distributed {
  41. namespace detail {
  42. /**
  43. * Binary function object type that selects the (edge, weight) pair
  44. * with the minimum weight. Used within a Boruvka merge step to select
  45. * the candidate edges incident to each supervertex.
  46. */
  47. struct smaller_weighted_edge
  48. {
  49. template<typename Edge, typename Weight>
  50. std::pair<Edge, Weight>
  51. operator()(const std::pair<Edge, Weight>& x,
  52. const std::pair<Edge, Weight>& y) const
  53. { return x.second < y.second? x : y; }
  54. };
  55. /**
  56. * Unary predicate that determines if the source and target vertices
  57. * of the given edge have the same representative within a disjoint
  58. * sets data structure. Used to indicate when an edge is now a
  59. * self-loop because of supervertex merging in Boruvka's algorithm.
  60. */
  61. template<typename DisjointSets, typename Graph>
  62. class do_has_same_supervertex
  63. {
  64. public:
  65. typedef typename graph_traits<Graph>::edge_descriptor edge_descriptor;
  66. do_has_same_supervertex(DisjointSets& dset, const Graph& g)
  67. : dset(dset), g(g) { }
  68. bool operator()(edge_descriptor e)
  69. { return dset.find_set(source(e, g)) == dset.find_set(target(e, g)); }
  70. private:
  71. DisjointSets& dset;
  72. const Graph& g;
  73. };
  74. /**
  75. * Build a @ref do_has_same_supervertex object.
  76. */
  77. template<typename DisjointSets, typename Graph>
  78. inline do_has_same_supervertex<DisjointSets, Graph>
  79. has_same_supervertex(DisjointSets& dset, const Graph& g)
  80. { return do_has_same_supervertex<DisjointSets, Graph>(dset, g); }
  81. /** \brief A single distributed Boruvka merge step.
  82. *
  83. * A distributed Boruvka merge step involves computing (globally)
  84. * the minimum weight edges incident on each supervertex and then
  85. * merging supervertices along these edges. Once supervertices are
  86. * merged, self-loops are eliminated.
  87. *
  88. * The set of parameters passed to this algorithm is large, and
  89. * considering this algorithm in isolation there are several
  90. * redundancies. However, the more asymptotically efficient
  91. * distributed MSF algorithms require mixing Boruvka steps with the
  92. * merging of local MSFs (implemented in
  93. * merge_local_minimum_spanning_trees_step): the interaction of the
  94. * two algorithms mandates the addition of these parameters.
  95. *
  96. * \param pg The process group over which communication should be
  97. * performed. Within the distributed Boruvka algorithm, this will be
  98. * equivalent to \code process_group(g); however, in the context of
  99. * the mixed MSF algorithms, the process group @p pg will be a
  100. * (non-strict) process subgroup of \code process_group(g).
  101. *
  102. * \param g The underlying graph on which the MSF is being
  103. * computed. The type of @p g must model DistributedGraph, but there
  104. * are no other requirements because the edge and (super)vertex
  105. * lists are passed separately.
  106. *
  107. * \param weight_map Property map containing the weights of each
  108. * edge. The type of this property map must model
  109. * ReadablePropertyMap and must support caching.
  110. *
  111. * \param out An output iterator that will be written with the set
  112. * of edges selected to build the MSF. Every process within the
  113. * process group @p pg will receive all edges in the MSF.
  114. *
  115. * \param dset Disjoint sets data structure mapping from vertices in
  116. * the graph @p g to their representative supervertex.
  117. *
  118. * \param supervertex_map Mapping from supervertex descriptors to
  119. * indices.
  120. *
  121. * \param supervertices A vector containing all of the
  122. * supervertices. Will be modified to include only the remaining
  123. * supervertices after merging occurs.
  124. *
  125. * \param edge_list The list of edges that remain in the graph. This
  126. * list will be pruned to remove self-loops once MSF edges have been
  127. * found.
  128. */
  129. template<typename ProcessGroup, typename Graph, typename WeightMap,
  130. typename OutputIterator, typename RankMap, typename ParentMap,
  131. typename SupervertexMap, typename Vertex, typename EdgeList>
  132. OutputIterator
  133. boruvka_merge_step(ProcessGroup pg, const Graph& g, WeightMap weight_map,
  134. OutputIterator out,
  135. disjoint_sets<RankMap, ParentMap>& dset,
  136. SupervertexMap supervertex_map,
  137. std::vector<Vertex>& supervertices,
  138. EdgeList& edge_list)
  139. {
  140. typedef typename graph_traits<Graph>::vertex_descriptor
  141. vertex_descriptor;
  142. typedef typename graph_traits<Graph>::vertices_size_type
  143. vertices_size_type;
  144. typedef typename graph_traits<Graph>::edge_descriptor edge_descriptor;
  145. typedef typename EdgeList::iterator edge_iterator;
  146. typedef typename property_traits<WeightMap>::value_type
  147. weight_type;
  148. typedef boost::parallel::detail::untracked_pair<edge_descriptor,
  149. weight_type> w_edge;
  150. typedef typename property_traits<SupervertexMap>::value_type
  151. supervertex_index;
  152. smaller_weighted_edge min_edge;
  153. weight_type inf = (std::numeric_limits<weight_type>::max)();
  154. // Renumber the supervertices
  155. for (std::size_t i = 0; i < supervertices.size(); ++i)
  156. put(supervertex_map, supervertices[i], i);
  157. // BSP-B1: Find local minimum-weight edges for each supervertex
  158. std::vector<w_edge> candidate_edges(supervertices.size(),
  159. w_edge(edge_descriptor(), inf));
  160. for (edge_iterator ei = edge_list.begin(); ei != edge_list.end(); ++ei) {
  161. weight_type w = get(weight_map, *ei);
  162. supervertex_index u =
  163. get(supervertex_map, dset.find_set(source(*ei, g)));
  164. supervertex_index v =
  165. get(supervertex_map, dset.find_set(target(*ei, g)));
  166. if (u != v) {
  167. candidate_edges[u] = min_edge(candidate_edges[u], w_edge(*ei, w));
  168. candidate_edges[v] = min_edge(candidate_edges[v], w_edge(*ei, w));
  169. }
  170. }
  171. // BSP-B2 (a): Compute global minimum edges for each supervertex
  172. all_reduce(pg,
  173. &candidate_edges[0],
  174. &candidate_edges[0] + candidate_edges.size(),
  175. &candidate_edges[0], min_edge);
  176. // BSP-B2 (b): Use the edges to compute sequentially the new
  177. // connected components and emit the edges.
  178. for (vertices_size_type i = 0; i < candidate_edges.size(); ++i) {
  179. if (candidate_edges[i].second != inf) {
  180. edge_descriptor e = candidate_edges[i].first;
  181. vertex_descriptor u = dset.find_set(source(e, g));
  182. vertex_descriptor v = dset.find_set(target(e, g));
  183. if (u != v) {
  184. // Emit the edge, but cache the weight so everyone knows it
  185. cache(weight_map, e, candidate_edges[i].second);
  186. *out++ = e;
  187. // Link the two supervertices
  188. dset.link(u, v);
  189. // Whichever vertex was reparented will be removed from the
  190. // list of supervertices.
  191. vertex_descriptor victim = u;
  192. if (dset.find_set(u) == u) victim = v;
  193. supervertices[get(supervertex_map, victim)] =
  194. graph_traits<Graph>::null_vertex();
  195. }
  196. }
  197. }
  198. // BSP-B3: Eliminate self-loops
  199. edge_list.erase(std::remove_if(edge_list.begin(), edge_list.end(),
  200. has_same_supervertex(dset, g)),
  201. edge_list.end());
  202. // TBD: might also eliminate multiple edges between supervertices
  203. // when the edges do not have the best weight, but this is not
  204. // strictly necessary.
  205. // Eliminate supervertices that have been absorbed
  206. supervertices.erase(std::remove(supervertices.begin(),
  207. supervertices.end(),
  208. graph_traits<Graph>::null_vertex()),
  209. supervertices.end());
  210. return out;
  211. }
  212. /**
  213. * An edge descriptor adaptor that reroutes the source and target
  214. * edges to different vertices, but retains the original edge
  215. * descriptor for, e.g., property maps. This is used when we want to
  216. * turn a set of edges in the overall graph into a set of edges
  217. * between supervertices.
  218. */
  219. template<typename Graph>
  220. struct supervertex_edge_descriptor
  221. {
  222. typedef supervertex_edge_descriptor self_type;
  223. typedef typename graph_traits<Graph>::vertex_descriptor Vertex;
  224. typedef typename graph_traits<Graph>::edge_descriptor Edge;
  225. Vertex source;
  226. Vertex target;
  227. Edge e;
  228. operator Edge() const { return e; }
  229. friend inline bool operator==(const self_type& x, const self_type& y)
  230. { return x.e == y.e; }
  231. friend inline bool operator!=(const self_type& x, const self_type& y)
  232. { return x.e != y.e; }
  233. };
  234. template<typename Graph>
  235. inline typename supervertex_edge_descriptor<Graph>::Vertex
  236. source(supervertex_edge_descriptor<Graph> se, const Graph&)
  237. { return se.source; }
  238. template<typename Graph>
  239. inline typename supervertex_edge_descriptor<Graph>::Vertex
  240. target(supervertex_edge_descriptor<Graph> se, const Graph&)
  241. { return se.target; }
  242. /**
  243. * Build a supervertex edge descriptor from a normal edge descriptor
  244. * using the given disjoint sets data structure to identify
  245. * supervertex representatives.
  246. */
  247. template<typename Graph, typename DisjointSets>
  248. struct build_supervertex_edge_descriptor
  249. {
  250. typedef typename graph_traits<Graph>::vertex_descriptor Vertex;
  251. typedef typename graph_traits<Graph>::edge_descriptor Edge;
  252. typedef Edge argument_type;
  253. typedef supervertex_edge_descriptor<Graph> result_type;
  254. build_supervertex_edge_descriptor() : g(0), dsets(0) { }
  255. build_supervertex_edge_descriptor(const Graph& g, DisjointSets& dsets)
  256. : g(&g), dsets(&dsets) { }
  257. result_type operator()(argument_type e) const
  258. {
  259. result_type result;
  260. result.source = dsets->find_set(source(e, *g));
  261. result.target = dsets->find_set(target(e, *g));
  262. result.e = e;
  263. return result;
  264. }
  265. private:
  266. const Graph* g;
  267. DisjointSets* dsets;
  268. };
  269. template<typename Graph, typename DisjointSets>
  270. inline build_supervertex_edge_descriptor<Graph, DisjointSets>
  271. make_supervertex_edge_descriptor(const Graph& g, DisjointSets& dsets)
  272. { return build_supervertex_edge_descriptor<Graph, DisjointSets>(g, dsets); }
  273. template<typename T>
  274. struct identity_function
  275. {
  276. typedef T argument_type;
  277. typedef T result_type;
  278. result_type operator()(argument_type x) const { return x; }
  279. };
  280. template<typename Graph, typename DisjointSets, typename EdgeMapper>
  281. class is_not_msf_edge
  282. {
  283. typedef typename graph_traits<Graph>::vertex_descriptor Vertex;
  284. typedef typename graph_traits<Graph>::edge_descriptor Edge;
  285. public:
  286. is_not_msf_edge(const Graph& g, DisjointSets dset, EdgeMapper edge_mapper)
  287. : g(g), dset(dset), edge_mapper(edge_mapper) { }
  288. bool operator()(Edge e)
  289. {
  290. Vertex u = dset.find_set(source(edge_mapper(e), g));
  291. Vertex v = dset.find_set(target(edge_mapper(e), g));
  292. if (u == v) return true;
  293. else {
  294. dset.link(u, v);
  295. return false;
  296. }
  297. }
  298. private:
  299. const Graph& g;
  300. DisjointSets dset;
  301. EdgeMapper edge_mapper;
  302. };
  303. template<typename Graph, typename ForwardIterator, typename EdgeList,
  304. typename EdgeMapper, typename RankMap, typename ParentMap>
  305. void
  306. sorted_mutating_kruskal(const Graph& g,
  307. ForwardIterator first_vertex,
  308. ForwardIterator last_vertex,
  309. EdgeList& edge_list, EdgeMapper edge_mapper,
  310. RankMap rank_map, ParentMap parent_map)
  311. {
  312. typedef disjoint_sets<RankMap, ParentMap> DisjointSets;
  313. // Build and initialize disjoint-sets data structure
  314. DisjointSets dset(rank_map, parent_map);
  315. for (ForwardIterator v = first_vertex; v != last_vertex; ++v)
  316. dset.make_set(*v);
  317. is_not_msf_edge<Graph, DisjointSets, EdgeMapper>
  318. remove_non_msf_edges(g, dset, edge_mapper);
  319. edge_list.erase(std::remove_if(edge_list.begin(), edge_list.end(),
  320. remove_non_msf_edges),
  321. edge_list.end());
  322. }
  323. /**
  324. * Merge local minimum spanning forests from p processes into
  325. * minimum spanning forests on p/D processes (where D is the tree
  326. * factor, currently fixed at 3), eliminating unnecessary edges in
  327. * the process.
  328. *
  329. * As with @ref boruvka_merge_step, this routine has many
  330. * parameters, not all of which make sense within the limited
  331. * context of this routine. The parameters are required for the
  332. * Boruvka and local MSF merging steps to interoperate.
  333. *
  334. * \param pg The process group on which local minimum spanning
  335. * forests should be merged. The top (D-1)p/D processes will be
  336. * eliminated, and a new process subgroup containing p/D processors
  337. * will be returned. The value D is a constant factor that is
  338. * currently fixed to 3.
  339. *
  340. * \param g The underlying graph whose MSF is being computed. It must model
  341. * the DistributedGraph concept.
  342. *
  343. * \param first_vertex Iterator to the first vertex in the graph
  344. * that should be considered. While the local MSF merging algorithm
  345. * typically operates on the entire vertex set, within the hybrid
  346. * distributed MSF algorithms this will refer to the first
  347. * supervertex.
  348. *
  349. * \param last_vertex The past-the-end iterator for the vertex list.
  350. *
  351. * \param edge_list The list of local edges that will be
  352. * considered. For the p/D processes that remain, this list will
  353. * contain edges in the MSF known to the vertex after other
  354. * processes' edge lists have been merged. The edge list must be
  355. * sorted in order of increasing weight.
  356. *
  357. * \param weight Property map containing the weights of each
  358. * edge. The type of this property map must model
  359. * ReadablePropertyMap and must support caching.
  360. *
  361. * \param global_index Mapping from vertex descriptors to a global
  362. * index. The type must model ReadablePropertyMap.
  363. *
  364. * \param edge_mapper A function object that can remap edge descriptors
  365. * in the edge list to any alternative edge descriptor. This
  366. * function object will be the identity function when a pure merging
  367. * of local MSFs is required, but may be a mapping to a supervertex
  368. * edge when the local MSF merging occurs on a supervertex
  369. * graph. This function object saves us the trouble of having to
  370. * build a supervertex graph adaptor.
  371. *
  372. * \param already_local_msf True when the edge list already
  373. * constitutes a local MSF. If false, Kruskal's algorithm will first
  374. * be applied to the local edge list to select MSF edges.
  375. *
  376. * \returns The process subgroup containing the remaining p/D
  377. * processes. If the size of this process group is greater than one,
  378. * the MSF edges contained in the edge list do not constitute an MSF
  379. * for the entire graph.
  380. */
  381. template<typename ProcessGroup, typename Graph, typename ForwardIterator,
  382. typename EdgeList, typename WeightMap, typename GlobalIndexMap,
  383. typename EdgeMapper>
  384. ProcessGroup
  385. merge_local_minimum_spanning_trees_step(ProcessGroup pg,
  386. const Graph& g,
  387. ForwardIterator first_vertex,
  388. ForwardIterator last_vertex,
  389. EdgeList& edge_list,
  390. WeightMap weight,
  391. GlobalIndexMap global_index,
  392. EdgeMapper edge_mapper,
  393. bool already_local_msf)
  394. {
  395. typedef typename ProcessGroup::process_id_type process_id_type;
  396. typedef typename EdgeList::value_type edge_descriptor;
  397. typedef typename property_traits<WeightMap>::value_type weight_type;
  398. typedef typename graph_traits<Graph>::vertex_descriptor vertex_descriptor;
  399. // The tree factor, often called "D"
  400. process_id_type const tree_factor = 3;
  401. process_id_type num_procs = num_processes(pg);
  402. process_id_type id = process_id(pg);
  403. process_id_type procs_left = (num_procs + tree_factor - 1) / tree_factor;
  404. std::size_t n = std::size_t(last_vertex - first_vertex);
  405. if (!already_local_msf) {
  406. // Compute local minimum spanning forest. We only care about the
  407. // edges in the MSF, because only edges in the local MSF can be in
  408. // the global MSF.
  409. std::vector<std::size_t> ranks(n);
  410. std::vector<vertex_descriptor> parents(n);
  411. detail::sorted_mutating_kruskal
  412. (g, first_vertex, last_vertex,
  413. edge_list, edge_mapper,
  414. make_iterator_property_map(ranks.begin(), global_index),
  415. make_iterator_property_map(parents.begin(), global_index));
  416. }
  417. typedef std::pair<edge_descriptor, weight_type> w_edge;
  418. // Order edges based on their weights.
  419. indirect_cmp<WeightMap, std::less<weight_type> > cmp_edge_weight(weight);
  420. if (id < procs_left) {
  421. // The p/D processes that remain will receive local MSF edges from
  422. // D-1 other processes.
  423. synchronize(pg);
  424. for (process_id_type from_id = procs_left + id; from_id < num_procs;
  425. from_id += procs_left) {
  426. std::size_t num_incoming_edges;
  427. receive(pg, from_id, 0, num_incoming_edges);
  428. if (num_incoming_edges > 0) {
  429. std::vector<w_edge> incoming_edges(num_incoming_edges);
  430. receive(pg, from_id, 1, &incoming_edges[0], num_incoming_edges);
  431. edge_list.reserve(edge_list.size() + num_incoming_edges);
  432. for (std::size_t i = 0; i < num_incoming_edges; ++i) {
  433. cache(weight, incoming_edges[i].first, incoming_edges[i].second);
  434. edge_list.push_back(incoming_edges[i].first);
  435. }
  436. std::inplace_merge(edge_list.begin(),
  437. edge_list.end() - num_incoming_edges,
  438. edge_list.end(),
  439. cmp_edge_weight);
  440. }
  441. }
  442. // Compute the local MSF from union of the edges in the MSFs of
  443. // all children.
  444. std::vector<std::size_t> ranks(n);
  445. std::vector<vertex_descriptor> parents(n);
  446. detail::sorted_mutating_kruskal
  447. (g, first_vertex, last_vertex,
  448. edge_list, edge_mapper,
  449. make_iterator_property_map(ranks.begin(), global_index),
  450. make_iterator_property_map(parents.begin(), global_index));
  451. } else {
  452. // The (D-1)p/D processes that are dropping out of further
  453. // computations merely send their MSF edges to their parent
  454. // process in the process tree.
  455. send(pg, id % procs_left, 0, edge_list.size());
  456. if (edge_list.size() > 0) {
  457. std::vector<w_edge> outgoing_edges;
  458. outgoing_edges.reserve(edge_list.size());
  459. for (std::size_t i = 0; i < edge_list.size(); ++i) {
  460. outgoing_edges.push_back(std::make_pair(edge_list[i],
  461. get(weight, edge_list[i])));
  462. }
  463. send(pg, id % procs_left, 1, &outgoing_edges[0],
  464. outgoing_edges.size());
  465. }
  466. synchronize(pg);
  467. }
  468. // Return a process subgroup containing the p/D parent processes
  469. return process_subgroup(pg,
  470. make_counting_iterator(process_id_type(0)),
  471. make_counting_iterator(procs_left));
  472. }
  473. } // end namespace detail
  474. // ---------------------------------------------------------------------
  475. // Dense Boruvka MSF algorithm
  476. // ---------------------------------------------------------------------
  477. template<typename Graph, typename WeightMap, typename OutputIterator,
  478. typename VertexIndexMap, typename RankMap, typename ParentMap,
  479. typename SupervertexMap>
  480. OutputIterator
  481. dense_boruvka_minimum_spanning_tree(const Graph& g, WeightMap weight_map,
  482. OutputIterator out,
  483. VertexIndexMap index_map,
  484. RankMap rank_map, ParentMap parent_map,
  485. SupervertexMap supervertex_map)
  486. {
  487. using boost::graph::parallel::process_group;
  488. typedef typename graph_traits<Graph>::traversal_category traversal_category;
  489. BOOST_STATIC_ASSERT((is_convertible<traversal_category*,
  490. vertex_list_graph_tag*>::value));
  491. typedef typename graph_traits<Graph>::vertices_size_type vertices_size_type;
  492. typedef typename graph_traits<Graph>::vertex_descriptor vertex_descriptor;
  493. typedef typename graph_traits<Graph>::vertex_iterator vertex_iterator;
  494. typedef typename graph_traits<Graph>::edge_descriptor edge_descriptor;
  495. // Don't throw away cached edge weights
  496. weight_map.set_max_ghost_cells(0);
  497. // Initialize the disjoint sets structures
  498. disjoint_sets<RankMap, ParentMap> dset(rank_map, parent_map);
  499. vertex_iterator vi, vi_end;
  500. for (boost::tie(vi, vi_end) = vertices(g); vi != vi_end; ++vi)
  501. dset.make_set(*vi);
  502. std::vector<vertex_descriptor> supervertices;
  503. supervertices.assign(vertices(g).first, vertices(g).second);
  504. // Use Kruskal's algorithm to find the minimum spanning forest
  505. // considering only the local edges. The resulting edges are not
  506. // necessarily going to be in the final minimum spanning
  507. // forest. However, any edge not part of the local MSF cannot be a
  508. // part of the global MSF, so we should have eliminated some edges
  509. // from consideration.
  510. std::vector<edge_descriptor> edge_list;
  511. kruskal_minimum_spanning_tree
  512. (make_vertex_and_edge_range(g, vertices(g).first, vertices(g).second,
  513. edges(g).first, edges(g).second),
  514. std::back_inserter(edge_list),
  515. boost::weight_map(weight_map).
  516. vertex_index_map(index_map));
  517. // While the number of supervertices is decreasing, keep executing
  518. // Boruvka steps to identify additional MSF edges. This loop will
  519. // execute log |V| times.
  520. vertices_size_type old_num_supervertices;
  521. do {
  522. old_num_supervertices = supervertices.size();
  523. out = detail::boruvka_merge_step(process_group(g), g,
  524. weight_map, out,
  525. dset, supervertex_map, supervertices,
  526. edge_list);
  527. } while (supervertices.size() < old_num_supervertices);
  528. return out;
  529. }
  530. template<typename Graph, typename WeightMap, typename OutputIterator,
  531. typename VertexIndex>
  532. OutputIterator
  533. dense_boruvka_minimum_spanning_tree(const Graph& g, WeightMap weight_map,
  534. OutputIterator out, VertexIndex i_map)
  535. {
  536. typedef typename graph_traits<Graph>::vertex_descriptor vertex_descriptor;
  537. std::vector<std::size_t> ranks(num_vertices(g));
  538. std::vector<vertex_descriptor> parents(num_vertices(g));
  539. std::vector<std::size_t> supervertices(num_vertices(g));
  540. return dense_boruvka_minimum_spanning_tree
  541. (g, weight_map, out, i_map,
  542. make_iterator_property_map(ranks.begin(), i_map),
  543. make_iterator_property_map(parents.begin(), i_map),
  544. make_iterator_property_map(supervertices.begin(), i_map));
  545. }
  546. template<typename Graph, typename WeightMap, typename OutputIterator>
  547. OutputIterator
  548. dense_boruvka_minimum_spanning_tree(const Graph& g, WeightMap weight_map,
  549. OutputIterator out)
  550. {
  551. return dense_boruvka_minimum_spanning_tree(g, weight_map, out,
  552. get(vertex_index, g));
  553. }
  554. // ---------------------------------------------------------------------
  555. // Merge local MSFs MSF algorithm
  556. // ---------------------------------------------------------------------
  557. template<typename Graph, typename WeightMap, typename OutputIterator,
  558. typename GlobalIndexMap>
  559. OutputIterator
  560. merge_local_minimum_spanning_trees(const Graph& g, WeightMap weight,
  561. OutputIterator out,
  562. GlobalIndexMap global_index)
  563. {
  564. using boost::graph::parallel::process_group_type;
  565. using boost::graph::parallel::process_group;
  566. typedef typename graph_traits<Graph>::traversal_category traversal_category;
  567. BOOST_STATIC_ASSERT((is_convertible<traversal_category*,
  568. vertex_list_graph_tag*>::value));
  569. typedef typename graph_traits<Graph>::vertex_descriptor vertex_descriptor;
  570. typedef typename graph_traits<Graph>::edge_descriptor edge_descriptor;
  571. // Don't throw away cached edge weights
  572. weight.set_max_ghost_cells(0);
  573. // Compute the initial local minimum spanning forests
  574. std::vector<edge_descriptor> edge_list;
  575. kruskal_minimum_spanning_tree
  576. (make_vertex_and_edge_range(g, vertices(g).first, vertices(g).second,
  577. edges(g).first, edges(g).second),
  578. std::back_inserter(edge_list),
  579. boost::weight_map(weight).vertex_index_map(global_index));
  580. // Merge the local MSFs from p processes into p/D processes,
  581. // reducing the number of processes in each step. Continue looping
  582. // until either (a) the current process drops out or (b) only one
  583. // process remains in the group. This loop will execute log_D p
  584. // times.
  585. typename process_group_type<Graph>::type pg = process_group(g);
  586. while (pg && num_processes(pg) > 1) {
  587. pg = detail::merge_local_minimum_spanning_trees_step
  588. (pg, g, vertices(g).first, vertices(g).second,
  589. edge_list, weight, global_index,
  590. detail::identity_function<edge_descriptor>(), true);
  591. }
  592. // Only process 0 has the entire edge list, so emit it to the output
  593. // iterator.
  594. if (pg && process_id(pg) == 0) {
  595. out = std::copy(edge_list.begin(), edge_list.end(), out);
  596. }
  597. synchronize(process_group(g));
  598. return out;
  599. }
  600. template<typename Graph, typename WeightMap, typename OutputIterator>
  601. inline OutputIterator
  602. merge_local_minimum_spanning_trees(const Graph& g, WeightMap weight,
  603. OutputIterator out)
  604. {
  605. return merge_local_minimum_spanning_trees(g, weight, out,
  606. get(vertex_index, g));
  607. }
  608. // ---------------------------------------------------------------------
  609. // Boruvka-then-merge MSF algorithm
  610. // ---------------------------------------------------------------------
  611. template<typename Graph, typename WeightMap, typename OutputIterator,
  612. typename GlobalIndexMap, typename RankMap, typename ParentMap,
  613. typename SupervertexMap>
  614. OutputIterator
  615. boruvka_then_merge(const Graph& g, WeightMap weight, OutputIterator out,
  616. GlobalIndexMap index, RankMap rank_map,
  617. ParentMap parent_map, SupervertexMap supervertex_map)
  618. {
  619. using std::log;
  620. using boost::graph::parallel::process_group_type;
  621. using boost::graph::parallel::process_group;
  622. typedef typename graph_traits<Graph>::traversal_category traversal_category;
  623. BOOST_STATIC_ASSERT((is_convertible<traversal_category*,
  624. vertex_list_graph_tag*>::value));
  625. typedef typename graph_traits<Graph>::vertices_size_type vertices_size_type;
  626. typedef typename graph_traits<Graph>::vertex_descriptor vertex_descriptor;
  627. typedef typename graph_traits<Graph>::vertex_iterator vertex_iterator;
  628. typedef typename graph_traits<Graph>::edge_descriptor edge_descriptor;
  629. // Don't throw away cached edge weights
  630. weight.set_max_ghost_cells(0);
  631. // Compute the initial local minimum spanning forests
  632. std::vector<edge_descriptor> edge_list;
  633. kruskal_minimum_spanning_tree
  634. (make_vertex_and_edge_range(g, vertices(g).first, vertices(g).second,
  635. edges(g).first, edges(g).second),
  636. std::back_inserter(edge_list),
  637. boost::weight_map(weight).
  638. vertex_index_map(index));
  639. // Initialize the disjoint sets structures for Boruvka steps
  640. disjoint_sets<RankMap, ParentMap> dset(rank_map, parent_map);
  641. vertex_iterator vi, vi_end;
  642. for (boost::tie(vi, vi_end) = vertices(g); vi != vi_end; ++vi)
  643. dset.make_set(*vi);
  644. // Construct the initial set of supervertices (all vertices)
  645. std::vector<vertex_descriptor> supervertices;
  646. supervertices.assign(vertices(g).first, vertices(g).second);
  647. // Continue performing Boruvka merge steps until the number of
  648. // supervertices reaches |V| / (log_D p)^2.
  649. const std::size_t tree_factor = 3; // TBD: same as above! should be param
  650. double log_d_p = log((double)num_processes(process_group(g)))
  651. / log((double)tree_factor);
  652. vertices_size_type target_supervertices =
  653. vertices_size_type(num_vertices(g) / (log_d_p * log_d_p));
  654. vertices_size_type old_num_supervertices;
  655. while (supervertices.size() > target_supervertices) {
  656. old_num_supervertices = supervertices.size();
  657. out = detail::boruvka_merge_step(process_group(g), g,
  658. weight, out, dset,
  659. supervertex_map, supervertices,
  660. edge_list);
  661. if (supervertices.size() == old_num_supervertices)
  662. return out;
  663. }
  664. // Renumber the supervertices
  665. for (std::size_t i = 0; i < supervertices.size(); ++i)
  666. put(supervertex_map, supervertices[i], i);
  667. // Merge local MSFs on the supervertices. (D-1)p/D processors drop
  668. // out each iteration, so this loop executes log_D p times.
  669. typename process_group_type<Graph>::type pg = process_group(g);
  670. bool have_msf = false;
  671. while (pg && num_processes(pg) > 1) {
  672. pg = detail::merge_local_minimum_spanning_trees_step
  673. (pg, g, supervertices.begin(), supervertices.end(),
  674. edge_list, weight, supervertex_map,
  675. detail::make_supervertex_edge_descriptor(g, dset),
  676. have_msf);
  677. have_msf = true;
  678. }
  679. // Only process 0 has the complete list of _supervertex_ MST edges,
  680. // so emit those to the output iterator. This is not the complete
  681. // list of edges in the MSF, however: the Boruvka steps in the
  682. // beginning of the algorithm emitted any edges used to merge
  683. // supervertices.
  684. if (pg && process_id(pg) == 0)
  685. out = std::copy(edge_list.begin(), edge_list.end(), out);
  686. synchronize(process_group(g));
  687. return out;
  688. }
  689. template<typename Graph, typename WeightMap, typename OutputIterator,
  690. typename GlobalIndexMap>
  691. inline OutputIterator
  692. boruvka_then_merge(const Graph& g, WeightMap weight, OutputIterator out,
  693. GlobalIndexMap index)
  694. {
  695. typedef typename graph_traits<Graph>::vertex_descriptor vertex_descriptor;
  696. typedef typename graph_traits<Graph>::vertices_size_type vertices_size_type;
  697. std::vector<vertices_size_type> ranks(num_vertices(g));
  698. std::vector<vertex_descriptor> parents(num_vertices(g));
  699. std::vector<vertices_size_type> supervertex_indices(num_vertices(g));
  700. return boruvka_then_merge
  701. (g, weight, out, index,
  702. make_iterator_property_map(ranks.begin(), index),
  703. make_iterator_property_map(parents.begin(), index),
  704. make_iterator_property_map(supervertex_indices.begin(), index));
  705. }
  706. template<typename Graph, typename WeightMap, typename OutputIterator>
  707. inline OutputIterator
  708. boruvka_then_merge(const Graph& g, WeightMap weight, OutputIterator out)
  709. { return boruvka_then_merge(g, weight, out, get(vertex_index, g)); }
  710. // ---------------------------------------------------------------------
  711. // Boruvka-mixed-merge MSF algorithm
  712. // ---------------------------------------------------------------------
  713. template<typename Graph, typename WeightMap, typename OutputIterator,
  714. typename GlobalIndexMap, typename RankMap, typename ParentMap,
  715. typename SupervertexMap>
  716. OutputIterator
  717. boruvka_mixed_merge(const Graph& g, WeightMap weight, OutputIterator out,
  718. GlobalIndexMap index, RankMap rank_map,
  719. ParentMap parent_map, SupervertexMap supervertex_map)
  720. {
  721. using boost::graph::parallel::process_group_type;
  722. using boost::graph::parallel::process_group;
  723. typedef typename graph_traits<Graph>::traversal_category traversal_category;
  724. BOOST_STATIC_ASSERT((is_convertible<traversal_category*,
  725. vertex_list_graph_tag*>::value));
  726. typedef typename graph_traits<Graph>::vertices_size_type vertices_size_type;
  727. typedef typename graph_traits<Graph>::vertex_descriptor vertex_descriptor;
  728. typedef typename graph_traits<Graph>::vertex_iterator vertex_iterator;
  729. typedef typename graph_traits<Graph>::edge_descriptor edge_descriptor;
  730. // Don't throw away cached edge weights
  731. weight.set_max_ghost_cells(0);
  732. // Initialize the disjoint sets structures for Boruvka steps
  733. disjoint_sets<RankMap, ParentMap> dset(rank_map, parent_map);
  734. vertex_iterator vi, vi_end;
  735. for (boost::tie(vi, vi_end) = vertices(g); vi != vi_end; ++vi)
  736. dset.make_set(*vi);
  737. // Construct the initial set of supervertices (all vertices)
  738. std::vector<vertex_descriptor> supervertices;
  739. supervertices.assign(vertices(g).first, vertices(g).second);
  740. // Compute the initial local minimum spanning forests
  741. std::vector<edge_descriptor> edge_list;
  742. kruskal_minimum_spanning_tree
  743. (make_vertex_and_edge_range(g, vertices(g).first, vertices(g).second,
  744. edges(g).first, edges(g).second),
  745. std::back_inserter(edge_list),
  746. boost::weight_map(weight).
  747. vertex_index_map(index));
  748. if (num_processes(process_group(g)) == 1) {
  749. return std::copy(edge_list.begin(), edge_list.end(), out);
  750. }
  751. // Like the merging local MSFs algorithm and the Boruvka-then-merge
  752. // algorithm, each iteration of this loop reduces the number of
  753. // processes by a constant factor D, and therefore we require log_D
  754. // p iterations. Note also that the number of edges in the edge list
  755. // decreases geometrically, giving us an efficient distributed MSF
  756. // algorithm.
  757. typename process_group_type<Graph>::type pg = process_group(g);
  758. vertices_size_type old_num_supervertices;
  759. while (pg && num_processes(pg) > 1) {
  760. // A single Boruvka step. If this doesn't change anything, we're done
  761. old_num_supervertices = supervertices.size();
  762. out = detail::boruvka_merge_step(pg, g, weight, out, dset,
  763. supervertex_map, supervertices,
  764. edge_list);
  765. if (old_num_supervertices == supervertices.size()) {
  766. edge_list.clear();
  767. break;
  768. }
  769. // Renumber the supervertices
  770. for (std::size_t i = 0; i < supervertices.size(); ++i)
  771. put(supervertex_map, supervertices[i], i);
  772. // A single merging of local MSTs, which reduces the number of
  773. // processes we're using by a constant factor D.
  774. pg = detail::merge_local_minimum_spanning_trees_step
  775. (pg, g, supervertices.begin(), supervertices.end(),
  776. edge_list, weight, supervertex_map,
  777. detail::make_supervertex_edge_descriptor(g, dset),
  778. true);
  779. }
  780. // Only process 0 has the complete edge list, so emit it for the
  781. // user. Note that list edge list only contains the MSF edges in the
  782. // final supervertex graph: all of the other edges were used to
  783. // merge supervertices and have been emitted by the Boruvka steps,
  784. // although only process 0 has received the complete set.
  785. if (pg && process_id(pg) == 0)
  786. out = std::copy(edge_list.begin(), edge_list.end(), out);
  787. synchronize(process_group(g));
  788. return out;
  789. }
  790. template<typename Graph, typename WeightMap, typename OutputIterator,
  791. typename GlobalIndexMap>
  792. inline OutputIterator
  793. boruvka_mixed_merge(const Graph& g, WeightMap weight, OutputIterator out,
  794. GlobalIndexMap index)
  795. {
  796. typedef typename graph_traits<Graph>::vertex_descriptor vertex_descriptor;
  797. typedef typename graph_traits<Graph>::vertices_size_type vertices_size_type;
  798. std::vector<vertices_size_type> ranks(num_vertices(g));
  799. std::vector<vertex_descriptor> parents(num_vertices(g));
  800. std::vector<vertices_size_type> supervertex_indices(num_vertices(g));
  801. return boruvka_mixed_merge
  802. (g, weight, out, index,
  803. make_iterator_property_map(ranks.begin(), index),
  804. make_iterator_property_map(parents.begin(), index),
  805. make_iterator_property_map(supervertex_indices.begin(), index));
  806. }
  807. template<typename Graph, typename WeightMap, typename OutputIterator>
  808. inline OutputIterator
  809. boruvka_mixed_merge(const Graph& g, WeightMap weight, OutputIterator out)
  810. { return boruvka_mixed_merge(g, weight, out, get(vertex_index, g)); }
  811. } // end namespace distributed
  812. using distributed::dense_boruvka_minimum_spanning_tree;
  813. using distributed::merge_local_minimum_spanning_trees;
  814. using distributed::boruvka_then_merge;
  815. using distributed::boruvka_mixed_merge;
  816. } } // end namespace boost::graph
  817. #endif // BOOST_DEHNE_GOTZ_MIN_SPANNING_TREE_HPP