dehne_gotz_min_spanning_tree.hpp 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937
  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>::edge_descriptor edge_descriptor;
  570. // Don't throw away cached edge weights
  571. weight.set_max_ghost_cells(0);
  572. // Compute the initial local minimum spanning forests
  573. std::vector<edge_descriptor> edge_list;
  574. kruskal_minimum_spanning_tree
  575. (make_vertex_and_edge_range(g, vertices(g).first, vertices(g).second,
  576. edges(g).first, edges(g).second),
  577. std::back_inserter(edge_list),
  578. boost::weight_map(weight).vertex_index_map(global_index));
  579. // Merge the local MSFs from p processes into p/D processes,
  580. // reducing the number of processes in each step. Continue looping
  581. // until either (a) the current process drops out or (b) only one
  582. // process remains in the group. This loop will execute log_D p
  583. // times.
  584. typename process_group_type<Graph>::type pg = process_group(g);
  585. while (pg && num_processes(pg) > 1) {
  586. pg = detail::merge_local_minimum_spanning_trees_step
  587. (pg, g, vertices(g).first, vertices(g).second,
  588. edge_list, weight, global_index,
  589. detail::identity_function<edge_descriptor>(), true);
  590. }
  591. // Only process 0 has the entire edge list, so emit it to the output
  592. // iterator.
  593. if (pg && process_id(pg) == 0) {
  594. out = std::copy(edge_list.begin(), edge_list.end(), out);
  595. }
  596. synchronize(process_group(g));
  597. return out;
  598. }
  599. template<typename Graph, typename WeightMap, typename OutputIterator>
  600. inline OutputIterator
  601. merge_local_minimum_spanning_trees(const Graph& g, WeightMap weight,
  602. OutputIterator out)
  603. {
  604. return merge_local_minimum_spanning_trees(g, weight, out,
  605. get(vertex_index, g));
  606. }
  607. // ---------------------------------------------------------------------
  608. // Boruvka-then-merge MSF algorithm
  609. // ---------------------------------------------------------------------
  610. template<typename Graph, typename WeightMap, typename OutputIterator,
  611. typename GlobalIndexMap, typename RankMap, typename ParentMap,
  612. typename SupervertexMap>
  613. OutputIterator
  614. boruvka_then_merge(const Graph& g, WeightMap weight, OutputIterator out,
  615. GlobalIndexMap index, RankMap rank_map,
  616. ParentMap parent_map, SupervertexMap supervertex_map)
  617. {
  618. using std::log;
  619. using boost::graph::parallel::process_group_type;
  620. using boost::graph::parallel::process_group;
  621. typedef typename graph_traits<Graph>::traversal_category traversal_category;
  622. BOOST_STATIC_ASSERT((is_convertible<traversal_category*,
  623. vertex_list_graph_tag*>::value));
  624. typedef typename graph_traits<Graph>::vertices_size_type vertices_size_type;
  625. typedef typename graph_traits<Graph>::vertex_descriptor vertex_descriptor;
  626. typedef typename graph_traits<Graph>::vertex_iterator vertex_iterator;
  627. typedef typename graph_traits<Graph>::edge_descriptor edge_descriptor;
  628. // Don't throw away cached edge weights
  629. weight.set_max_ghost_cells(0);
  630. // Compute the initial local minimum spanning forests
  631. std::vector<edge_descriptor> edge_list;
  632. kruskal_minimum_spanning_tree
  633. (make_vertex_and_edge_range(g, vertices(g).first, vertices(g).second,
  634. edges(g).first, edges(g).second),
  635. std::back_inserter(edge_list),
  636. boost::weight_map(weight).
  637. vertex_index_map(index));
  638. // Initialize the disjoint sets structures for Boruvka steps
  639. disjoint_sets<RankMap, ParentMap> dset(rank_map, parent_map);
  640. vertex_iterator vi, vi_end;
  641. for (boost::tie(vi, vi_end) = vertices(g); vi != vi_end; ++vi)
  642. dset.make_set(*vi);
  643. // Construct the initial set of supervertices (all vertices)
  644. std::vector<vertex_descriptor> supervertices;
  645. supervertices.assign(vertices(g).first, vertices(g).second);
  646. // Continue performing Boruvka merge steps until the number of
  647. // supervertices reaches |V| / (log_D p)^2.
  648. const std::size_t tree_factor = 3; // TBD: same as above! should be param
  649. double log_d_p = log((double)num_processes(process_group(g)))
  650. / log((double)tree_factor);
  651. vertices_size_type target_supervertices =
  652. vertices_size_type(num_vertices(g) / (log_d_p * log_d_p));
  653. vertices_size_type old_num_supervertices;
  654. while (supervertices.size() > target_supervertices) {
  655. old_num_supervertices = supervertices.size();
  656. out = detail::boruvka_merge_step(process_group(g), g,
  657. weight, out, dset,
  658. supervertex_map, supervertices,
  659. edge_list);
  660. if (supervertices.size() == old_num_supervertices)
  661. return out;
  662. }
  663. // Renumber the supervertices
  664. for (std::size_t i = 0; i < supervertices.size(); ++i)
  665. put(supervertex_map, supervertices[i], i);
  666. // Merge local MSFs on the supervertices. (D-1)p/D processors drop
  667. // out each iteration, so this loop executes log_D p times.
  668. typename process_group_type<Graph>::type pg = process_group(g);
  669. bool have_msf = false;
  670. while (pg && num_processes(pg) > 1) {
  671. pg = detail::merge_local_minimum_spanning_trees_step
  672. (pg, g, supervertices.begin(), supervertices.end(),
  673. edge_list, weight, supervertex_map,
  674. detail::make_supervertex_edge_descriptor(g, dset),
  675. have_msf);
  676. have_msf = true;
  677. }
  678. // Only process 0 has the complete list of _supervertex_ MST edges,
  679. // so emit those to the output iterator. This is not the complete
  680. // list of edges in the MSF, however: the Boruvka steps in the
  681. // beginning of the algorithm emitted any edges used to merge
  682. // supervertices.
  683. if (pg && process_id(pg) == 0)
  684. out = std::copy(edge_list.begin(), edge_list.end(), out);
  685. synchronize(process_group(g));
  686. return out;
  687. }
  688. template<typename Graph, typename WeightMap, typename OutputIterator,
  689. typename GlobalIndexMap>
  690. inline OutputIterator
  691. boruvka_then_merge(const Graph& g, WeightMap weight, OutputIterator out,
  692. GlobalIndexMap index)
  693. {
  694. typedef typename graph_traits<Graph>::vertex_descriptor vertex_descriptor;
  695. typedef typename graph_traits<Graph>::vertices_size_type vertices_size_type;
  696. std::vector<vertices_size_type> ranks(num_vertices(g));
  697. std::vector<vertex_descriptor> parents(num_vertices(g));
  698. std::vector<vertices_size_type> supervertex_indices(num_vertices(g));
  699. return boruvka_then_merge
  700. (g, weight, out, index,
  701. make_iterator_property_map(ranks.begin(), index),
  702. make_iterator_property_map(parents.begin(), index),
  703. make_iterator_property_map(supervertex_indices.begin(), index));
  704. }
  705. template<typename Graph, typename WeightMap, typename OutputIterator>
  706. inline OutputIterator
  707. boruvka_then_merge(const Graph& g, WeightMap weight, OutputIterator out)
  708. { return boruvka_then_merge(g, weight, out, get(vertex_index, g)); }
  709. // ---------------------------------------------------------------------
  710. // Boruvka-mixed-merge MSF algorithm
  711. // ---------------------------------------------------------------------
  712. template<typename Graph, typename WeightMap, typename OutputIterator,
  713. typename GlobalIndexMap, typename RankMap, typename ParentMap,
  714. typename SupervertexMap>
  715. OutputIterator
  716. boruvka_mixed_merge(const Graph& g, WeightMap weight, OutputIterator out,
  717. GlobalIndexMap index, RankMap rank_map,
  718. ParentMap parent_map, SupervertexMap supervertex_map)
  719. {
  720. using boost::graph::parallel::process_group_type;
  721. using boost::graph::parallel::process_group;
  722. typedef typename graph_traits<Graph>::traversal_category traversal_category;
  723. BOOST_STATIC_ASSERT((is_convertible<traversal_category*,
  724. vertex_list_graph_tag*>::value));
  725. typedef typename graph_traits<Graph>::vertices_size_type vertices_size_type;
  726. typedef typename graph_traits<Graph>::vertex_descriptor vertex_descriptor;
  727. typedef typename graph_traits<Graph>::vertex_iterator vertex_iterator;
  728. typedef typename graph_traits<Graph>::edge_descriptor edge_descriptor;
  729. // Don't throw away cached edge weights
  730. weight.set_max_ghost_cells(0);
  731. // Initialize the disjoint sets structures for Boruvka steps
  732. disjoint_sets<RankMap, ParentMap> dset(rank_map, parent_map);
  733. vertex_iterator vi, vi_end;
  734. for (boost::tie(vi, vi_end) = vertices(g); vi != vi_end; ++vi)
  735. dset.make_set(*vi);
  736. // Construct the initial set of supervertices (all vertices)
  737. std::vector<vertex_descriptor> supervertices;
  738. supervertices.assign(vertices(g).first, vertices(g).second);
  739. // Compute the initial local minimum spanning forests
  740. std::vector<edge_descriptor> edge_list;
  741. kruskal_minimum_spanning_tree
  742. (make_vertex_and_edge_range(g, vertices(g).first, vertices(g).second,
  743. edges(g).first, edges(g).second),
  744. std::back_inserter(edge_list),
  745. boost::weight_map(weight).
  746. vertex_index_map(index));
  747. if (num_processes(process_group(g)) == 1) {
  748. return std::copy(edge_list.begin(), edge_list.end(), out);
  749. }
  750. // Like the merging local MSFs algorithm and the Boruvka-then-merge
  751. // algorithm, each iteration of this loop reduces the number of
  752. // processes by a constant factor D, and therefore we require log_D
  753. // p iterations. Note also that the number of edges in the edge list
  754. // decreases geometrically, giving us an efficient distributed MSF
  755. // algorithm.
  756. typename process_group_type<Graph>::type pg = process_group(g);
  757. vertices_size_type old_num_supervertices;
  758. while (pg && num_processes(pg) > 1) {
  759. // A single Boruvka step. If this doesn't change anything, we're done
  760. old_num_supervertices = supervertices.size();
  761. out = detail::boruvka_merge_step(pg, g, weight, out, dset,
  762. supervertex_map, supervertices,
  763. edge_list);
  764. if (old_num_supervertices == supervertices.size()) {
  765. edge_list.clear();
  766. break;
  767. }
  768. // Renumber the supervertices
  769. for (std::size_t i = 0; i < supervertices.size(); ++i)
  770. put(supervertex_map, supervertices[i], i);
  771. // A single merging of local MSTs, which reduces the number of
  772. // processes we're using by a constant factor D.
  773. pg = detail::merge_local_minimum_spanning_trees_step
  774. (pg, g, supervertices.begin(), supervertices.end(),
  775. edge_list, weight, supervertex_map,
  776. detail::make_supervertex_edge_descriptor(g, dset),
  777. true);
  778. }
  779. // Only process 0 has the complete edge list, so emit it for the
  780. // user. Note that list edge list only contains the MSF edges in the
  781. // final supervertex graph: all of the other edges were used to
  782. // merge supervertices and have been emitted by the Boruvka steps,
  783. // although only process 0 has received the complete set.
  784. if (pg && process_id(pg) == 0)
  785. out = std::copy(edge_list.begin(), edge_list.end(), out);
  786. synchronize(process_group(g));
  787. return out;
  788. }
  789. template<typename Graph, typename WeightMap, typename OutputIterator,
  790. typename GlobalIndexMap>
  791. inline OutputIterator
  792. boruvka_mixed_merge(const Graph& g, WeightMap weight, OutputIterator out,
  793. GlobalIndexMap index)
  794. {
  795. typedef typename graph_traits<Graph>::vertex_descriptor vertex_descriptor;
  796. typedef typename graph_traits<Graph>::vertices_size_type vertices_size_type;
  797. std::vector<vertices_size_type> ranks(num_vertices(g));
  798. std::vector<vertex_descriptor> parents(num_vertices(g));
  799. std::vector<vertices_size_type> supervertex_indices(num_vertices(g));
  800. return boruvka_mixed_merge
  801. (g, weight, out, index,
  802. make_iterator_property_map(ranks.begin(), index),
  803. make_iterator_property_map(parents.begin(), index),
  804. make_iterator_property_map(supervertex_indices.begin(), index));
  805. }
  806. template<typename Graph, typename WeightMap, typename OutputIterator>
  807. inline OutputIterator
  808. boruvka_mixed_merge(const Graph& g, WeightMap weight, OutputIterator out)
  809. { return boruvka_mixed_merge(g, weight, out, get(vertex_index, g)); }
  810. } // end namespace distributed
  811. using distributed::dense_boruvka_minimum_spanning_tree;
  812. using distributed::merge_local_minimum_spanning_trees;
  813. using distributed::boruvka_then_merge;
  814. using distributed::boruvka_mixed_merge;
  815. } } // end namespace boost::graph
  816. #endif // BOOST_DEHNE_GOTZ_MIN_SPANNING_TREE_HPP