prim_minimum_spanning_tree.hpp 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. //=======================================================================
  2. // Copyright 1997, 1998, 1999, 2000 University of Notre Dame.
  3. // Authors: Andrew Lumsdaine, Lie-Quan Lee, Jeremy G. Siek
  4. //
  5. // Distributed under the Boost Software License, Version 1.0. (See
  6. // accompanying file LICENSE_1_0.txt or copy at
  7. // http://www.boost.org/LICENSE_1_0.txt)
  8. //=======================================================================
  9. //
  10. #ifndef BOOST_GRAPH_MST_PRIM_HPP
  11. #define BOOST_GRAPH_MST_PRIM_HPP
  12. #include <functional>
  13. #include <boost/graph/graph_traits.hpp>
  14. #include <boost/graph/dijkstra_shortest_paths.hpp>
  15. namespace boost {
  16. namespace detail {
  17. // this should be somewhere else in boost...
  18. template <class U, class V> struct _project2nd {
  19. V operator()(U, V v) const { return v; }
  20. };
  21. }
  22. namespace detail {
  23. // This is Prim's algorithm to calculate the Minimum Spanning Tree
  24. // for an undirected graph with weighted edges.
  25. template <class Graph, class P, class T, class R, class Weight>
  26. inline void
  27. prim_mst_impl(const Graph& G,
  28. typename graph_traits<Graph>::vertex_descriptor s,
  29. const bgl_named_params<P,T,R>& params,
  30. Weight)
  31. {
  32. typedef typename property_traits<Weight>::value_type W;
  33. std::less<W> compare;
  34. detail::_project2nd<W,W> combine;
  35. dijkstra_shortest_paths(G, s, params.distance_compare(compare).
  36. distance_combine(combine));
  37. }
  38. } // namespace detail
  39. template <class VertexListGraph, class DijkstraVisitor,
  40. class PredecessorMap, class DistanceMap,
  41. class WeightMap, class IndexMap>
  42. inline void
  43. prim_minimum_spanning_tree
  44. (const VertexListGraph& g,
  45. typename graph_traits<VertexListGraph>::vertex_descriptor s,
  46. PredecessorMap predecessor, DistanceMap distance, WeightMap weight,
  47. IndexMap index_map,
  48. DijkstraVisitor vis)
  49. {
  50. typedef typename property_traits<WeightMap>::value_type W;
  51. std::less<W> compare;
  52. detail::_project2nd<W,W> combine;
  53. dijkstra_shortest_paths(g, s, predecessor, distance, weight, index_map,
  54. compare, combine, (std::numeric_limits<W>::max)(), 0,
  55. vis);
  56. }
  57. template <class VertexListGraph, class PredecessorMap,
  58. class P, class T, class R>
  59. inline void prim_minimum_spanning_tree
  60. (const VertexListGraph& g,
  61. PredecessorMap p_map,
  62. const bgl_named_params<P,T,R>& params)
  63. {
  64. detail::prim_mst_impl
  65. (g,
  66. choose_param(get_param(params, root_vertex_t()), *vertices(g).first),
  67. params.predecessor_map(p_map),
  68. choose_const_pmap(get_param(params, edge_weight), g, edge_weight));
  69. }
  70. template <class VertexListGraph, class PredecessorMap>
  71. inline void prim_minimum_spanning_tree
  72. (const VertexListGraph& g, PredecessorMap p_map)
  73. {
  74. detail::prim_mst_impl
  75. (g, *vertices(g).first, predecessor_map(p_map).
  76. weight_map(get(edge_weight, g)),
  77. get(edge_weight, g));
  78. }
  79. } // namespace boost
  80. #endif // BOOST_GRAPH_MST_PRIM_HPP