eccentricity.hpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. // (C) Copyright 2007-2009 Andrew Sutton
  2. //
  3. // Use, modification and distribution are subject to the
  4. // Boost Software License, Version 1.0 (See accompanying file
  5. // LICENSE_1_0.txt or http://www.boost.org/LICENSE_1_0.txt)
  6. #ifndef BOOST_GRAPH_ECCENTRICITY_HPP
  7. #define BOOST_GRAPH_ECCENTRICITY_HPP
  8. #include <boost/next_prior.hpp>
  9. #include <boost/config.hpp>
  10. #include <boost/graph/detail/geodesic.hpp>
  11. #include <boost/concept/assert.hpp>
  12. namespace boost
  13. {
  14. template <typename Graph,
  15. typename DistanceMap,
  16. typename Combinator>
  17. inline typename property_traits<DistanceMap>::value_type
  18. eccentricity(const Graph& g, DistanceMap dist, Combinator combine)
  19. {
  20. BOOST_CONCEPT_ASSERT(( GraphConcept<Graph> ));
  21. typedef typename graph_traits<Graph>::vertex_descriptor Vertex;
  22. BOOST_CONCEPT_ASSERT(( ReadablePropertyMapConcept<DistanceMap,Vertex> ));
  23. typedef typename property_traits<DistanceMap>::value_type Distance;
  24. return detail::combine_distances(g, dist, combine, Distance(0));
  25. }
  26. template <typename Graph, typename DistanceMap>
  27. inline typename property_traits<DistanceMap>::value_type
  28. eccentricity(const Graph& g, DistanceMap dist)
  29. {
  30. BOOST_CONCEPT_ASSERT(( GraphConcept<Graph> ));
  31. typedef typename graph_traits<Graph>::vertex_descriptor Vertex;
  32. BOOST_CONCEPT_ASSERT(( ReadablePropertyMapConcept<DistanceMap,Vertex> ));
  33. typedef typename property_traits<DistanceMap>::value_type Distance;
  34. return eccentricity(g, dist, detail::maximize<Distance>());
  35. }
  36. template <typename Graph, typename DistanceMatrix, typename EccentricityMap>
  37. inline std::pair<typename property_traits<EccentricityMap>::value_type,
  38. typename property_traits<EccentricityMap>::value_type>
  39. all_eccentricities(const Graph& g, const DistanceMatrix& dist, EccentricityMap ecc)
  40. {
  41. BOOST_CONCEPT_ASSERT(( VertexListGraphConcept<Graph> ));
  42. typedef typename graph_traits<Graph>::vertex_descriptor Vertex;
  43. typedef typename graph_traits<Graph>::vertex_iterator VertexIterator;
  44. BOOST_CONCEPT_ASSERT(( ReadablePropertyMapConcept<DistanceMatrix,Vertex> ));
  45. typedef typename property_traits<DistanceMatrix>::value_type DistanceMap;
  46. BOOST_CONCEPT_ASSERT(( WritablePropertyMapConcept<EccentricityMap,Vertex> ));
  47. typedef typename property_traits<EccentricityMap>::value_type Eccentricity;
  48. BOOST_USING_STD_MIN();
  49. BOOST_USING_STD_MAX();
  50. Eccentricity
  51. r = numeric_values<Eccentricity>::infinity(),
  52. d = numeric_values<Eccentricity>::zero();
  53. VertexIterator i, end;
  54. boost::tie(i, end) = vertices(g);
  55. for(boost::tie(i, end) = vertices(g); i != end; ++i) {
  56. DistanceMap dm = get(dist, *i);
  57. Eccentricity e = eccentricity(g, dm);
  58. put(ecc, *i, e);
  59. // track the radius and diameter at the same time
  60. r = min BOOST_PREVENT_MACRO_SUBSTITUTION (r, e);
  61. d = max BOOST_PREVENT_MACRO_SUBSTITUTION (d, e);
  62. }
  63. return std::make_pair(r, d);
  64. }
  65. template <typename Graph, typename EccentricityMap>
  66. inline std::pair<typename property_traits<EccentricityMap>::value_type,
  67. typename property_traits<EccentricityMap>::value_type>
  68. radius_and_diameter(const Graph& g, EccentricityMap ecc)
  69. {
  70. BOOST_CONCEPT_ASSERT(( VertexListGraphConcept<Graph> ));
  71. typedef typename graph_traits<Graph>::vertex_descriptor Vertex;
  72. typedef typename graph_traits<Graph>::vertex_iterator VertexIterator;
  73. BOOST_CONCEPT_ASSERT(( ReadablePropertyMapConcept<EccentricityMap, Vertex> ));
  74. typedef typename property_traits<EccentricityMap>::value_type Eccentricity;
  75. BOOST_USING_STD_MIN();
  76. BOOST_USING_STD_MAX();
  77. VertexIterator i, end;
  78. boost::tie(i, end) = vertices(g);
  79. Eccentricity radius = get(ecc, *i);
  80. Eccentricity diameter = get(ecc, *i);
  81. for(i = boost::next(i); i != end; ++i) {
  82. Eccentricity cur = get(ecc, *i);
  83. radius = min BOOST_PREVENT_MACRO_SUBSTITUTION (radius, cur);
  84. diameter = max BOOST_PREVENT_MACRO_SUBSTITUTION (diameter, cur);
  85. }
  86. return std::make_pair(radius, diameter);
  87. }
  88. template <typename Graph, typename EccentricityMap>
  89. inline typename property_traits<EccentricityMap>::value_type
  90. radius(const Graph& g, EccentricityMap ecc)
  91. { return radius_and_diameter(g, ecc).first; }
  92. template <typename Graph, typename EccentricityMap>
  93. inline typename property_traits<EccentricityMap>::value_type
  94. diameter(const Graph& g, EccentricityMap ecc)
  95. { return radius_and_diameter(g, ecc).second; }
  96. } /* namespace boost */
  97. #endif