prim_minimum_spanning_tree.hpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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. {
  17. namespace detail
  18. {
  19. // this should be somewhere else in boost...
  20. template < class U, class V > struct _project2nd
  21. {
  22. V operator()(U, V v) const { return v; }
  23. };
  24. }
  25. namespace detail
  26. {
  27. // This is Prim's algorithm to calculate the Minimum Spanning Tree
  28. // for an undirected graph with weighted edges.
  29. template < class Graph, class P, class T, class R, class Weight >
  30. inline void prim_mst_impl(const Graph& G,
  31. typename graph_traits< Graph >::vertex_descriptor s,
  32. const bgl_named_params< P, T, R >& params, Weight)
  33. {
  34. typedef typename property_traits< Weight >::value_type W;
  35. std::less< W > compare;
  36. detail::_project2nd< W, W > combine;
  37. dijkstra_shortest_paths(
  38. G, s, params.distance_compare(compare).distance_combine(combine));
  39. }
  40. } // namespace detail
  41. template < class VertexListGraph, class DijkstraVisitor, class PredecessorMap,
  42. class DistanceMap, class WeightMap, class IndexMap >
  43. inline void prim_minimum_spanning_tree(const VertexListGraph& g,
  44. typename graph_traits< VertexListGraph >::vertex_descriptor s,
  45. PredecessorMap predecessor, DistanceMap distance, WeightMap weight,
  46. IndexMap index_map, DijkstraVisitor vis)
  47. {
  48. typedef typename property_traits< WeightMap >::value_type W;
  49. std::less< W > compare;
  50. detail::_project2nd< W, W > combine;
  51. dijkstra_shortest_paths(g, s, predecessor, distance, weight, index_map,
  52. compare, combine, (std::numeric_limits< W >::max)(), 0, vis);
  53. }
  54. template < class VertexListGraph, class PredecessorMap, class P, class T,
  55. class R >
  56. inline void prim_minimum_spanning_tree(const VertexListGraph& g,
  57. PredecessorMap p_map, const bgl_named_params< P, T, R >& params)
  58. {
  59. detail::prim_mst_impl(g,
  60. choose_param(get_param(params, root_vertex_t()), *vertices(g).first),
  61. params.predecessor_map(p_map),
  62. choose_const_pmap(get_param(params, edge_weight), g, edge_weight));
  63. }
  64. template < class VertexListGraph, class PredecessorMap >
  65. inline void prim_minimum_spanning_tree(
  66. const VertexListGraph& g, PredecessorMap p_map)
  67. {
  68. detail::prim_mst_impl(g, *vertices(g).first,
  69. predecessor_map(p_map).weight_map(get(edge_weight, g)),
  70. get(edge_weight, g));
  71. }
  72. } // namespace boost
  73. #endif // BOOST_GRAPH_MST_PRIM_HPP