kruskal_min_spanning_tree.hpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. //
  2. //=======================================================================
  3. // Copyright 1997, 1998, 1999, 2000 University of Notre Dame.
  4. // Authors: Andrew Lumsdaine, Lie-Quan Lee, Jeremy G. Siek
  5. //
  6. // Distributed under the Boost Software License, Version 1.0. (See
  7. // accompanying file LICENSE_1_0.txt or copy at
  8. // http://www.boost.org/LICENSE_1_0.txt)
  9. //=======================================================================
  10. //
  11. #ifndef BOOST_GRAPH_MST_KRUSKAL_HPP
  12. #define BOOST_GRAPH_MST_KRUSKAL_HPP
  13. /*
  14. *Minimum Spanning Tree
  15. * Kruskal Algorithm
  16. *
  17. *Requirement:
  18. * undirected graph
  19. */
  20. #include <vector>
  21. #include <queue>
  22. #include <functional>
  23. #include <boost/property_map/property_map.hpp>
  24. #include <boost/graph/graph_concepts.hpp>
  25. #include <boost/graph/named_function_params.hpp>
  26. #include <boost/pending/disjoint_sets.hpp>
  27. #include <boost/pending/indirect_cmp.hpp>
  28. #include <boost/concept/assert.hpp>
  29. namespace boost
  30. {
  31. // Kruskal's algorithm for Minimum Spanning Tree
  32. //
  33. // This is a greedy algorithm to calculate the Minimum Spanning Tree
  34. // for an undirected graph with weighted edges. The output will be a
  35. // set of edges.
  36. //
  37. namespace detail
  38. {
  39. template < class Graph, class OutputIterator, class Rank, class Parent,
  40. class Weight >
  41. void kruskal_mst_impl(const Graph& G, OutputIterator spanning_tree_edges,
  42. Rank rank, Parent parent, Weight weight)
  43. {
  44. if (num_vertices(G) == 0)
  45. return; // Nothing to do in this case
  46. typedef typename graph_traits< Graph >::vertex_descriptor Vertex;
  47. typedef typename graph_traits< Graph >::edge_descriptor Edge;
  48. BOOST_CONCEPT_ASSERT((VertexListGraphConcept< Graph >));
  49. BOOST_CONCEPT_ASSERT((EdgeListGraphConcept< Graph >));
  50. BOOST_CONCEPT_ASSERT((OutputIteratorConcept< OutputIterator, Edge >));
  51. BOOST_CONCEPT_ASSERT((ReadWritePropertyMapConcept< Rank, Vertex >));
  52. BOOST_CONCEPT_ASSERT((ReadWritePropertyMapConcept< Parent, Vertex >));
  53. BOOST_CONCEPT_ASSERT((ReadablePropertyMapConcept< Weight, Edge >));
  54. typedef typename property_traits< Weight >::value_type W_value;
  55. typedef typename property_traits< Rank >::value_type R_value;
  56. typedef typename property_traits< Parent >::value_type P_value;
  57. BOOST_CONCEPT_ASSERT((ComparableConcept< W_value >));
  58. BOOST_CONCEPT_ASSERT((ConvertibleConcept< P_value, Vertex >));
  59. BOOST_CONCEPT_ASSERT((IntegerConcept< R_value >));
  60. disjoint_sets< Rank, Parent > dset(rank, parent);
  61. typename graph_traits< Graph >::vertex_iterator ui, uiend;
  62. for (boost::tie(ui, uiend) = vertices(G); ui != uiend; ++ui)
  63. dset.make_set(*ui);
  64. typedef indirect_cmp< Weight, std::greater< W_value > > weight_greater;
  65. weight_greater wl(weight);
  66. std::priority_queue< Edge, std::vector< Edge >, weight_greater > Q(wl);
  67. /*push all edge into Q*/
  68. typename graph_traits< Graph >::edge_iterator ei, eiend;
  69. for (boost::tie(ei, eiend) = edges(G); ei != eiend; ++ei)
  70. Q.push(*ei);
  71. while (!Q.empty())
  72. {
  73. Edge e = Q.top();
  74. Q.pop();
  75. Vertex u = dset.find_set(source(e, G));
  76. Vertex v = dset.find_set(target(e, G));
  77. if (u != v)
  78. {
  79. *spanning_tree_edges++ = e;
  80. dset.link(u, v);
  81. }
  82. }
  83. }
  84. } // namespace detail
  85. // Named Parameters Variants
  86. template < class Graph, class OutputIterator >
  87. inline void kruskal_minimum_spanning_tree(
  88. const Graph& g, OutputIterator spanning_tree_edges)
  89. {
  90. typedef typename graph_traits< Graph >::vertices_size_type size_type;
  91. typedef typename graph_traits< Graph >::vertex_descriptor vertex_t;
  92. if (num_vertices(g) == 0)
  93. return; // Nothing to do in this case
  94. typename graph_traits< Graph >::vertices_size_type n = num_vertices(g);
  95. std::vector< size_type > rank_map(n);
  96. std::vector< vertex_t > pred_map(n);
  97. detail::kruskal_mst_impl(g, spanning_tree_edges,
  98. make_iterator_property_map(
  99. rank_map.begin(), get(vertex_index, g), rank_map[0]),
  100. make_iterator_property_map(
  101. pred_map.begin(), get(vertex_index, g), pred_map[0]),
  102. get(edge_weight, g));
  103. }
  104. template < class Graph, class OutputIterator, class P, class T, class R >
  105. inline void kruskal_minimum_spanning_tree(const Graph& g,
  106. OutputIterator spanning_tree_edges,
  107. const bgl_named_params< P, T, R >& params)
  108. {
  109. typedef typename graph_traits< Graph >::vertices_size_type size_type;
  110. typedef typename graph_traits< Graph >::vertex_descriptor vertex_t;
  111. if (num_vertices(g) == 0)
  112. return; // Nothing to do in this case
  113. typename graph_traits< Graph >::vertices_size_type n;
  114. n = is_default_param(get_param(params, vertex_rank)) ? num_vertices(g) : 1;
  115. std::vector< size_type > rank_map(n);
  116. n = is_default_param(get_param(params, vertex_predecessor))
  117. ? num_vertices(g)
  118. : 1;
  119. std::vector< vertex_t > pred_map(n);
  120. detail::kruskal_mst_impl(g, spanning_tree_edges,
  121. choose_param(get_param(params, vertex_rank),
  122. make_iterator_property_map(rank_map.begin(),
  123. choose_pmap(get_param(params, vertex_index), g, vertex_index),
  124. rank_map[0])),
  125. choose_param(get_param(params, vertex_predecessor),
  126. make_iterator_property_map(pred_map.begin(),
  127. choose_const_pmap(
  128. get_param(params, vertex_index), g, vertex_index),
  129. pred_map[0])),
  130. choose_const_pmap(get_param(params, edge_weight), g, edge_weight));
  131. }
  132. } // namespace boost
  133. #endif // BOOST_GRAPH_MST_KRUSKAL_HPP