eccentricity.hpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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, typename DistanceMap, typename Combinator >
  15. inline typename property_traits< DistanceMap >::value_type eccentricity(
  16. const Graph& g, DistanceMap dist, Combinator combine)
  17. {
  18. BOOST_CONCEPT_ASSERT((GraphConcept< Graph >));
  19. typedef typename graph_traits< Graph >::vertex_descriptor Vertex;
  20. BOOST_CONCEPT_ASSERT((ReadablePropertyMapConcept< DistanceMap, Vertex >));
  21. typedef typename property_traits< DistanceMap >::value_type Distance;
  22. return detail::combine_distances(g, dist, combine, Distance(0));
  23. }
  24. template < typename Graph, typename DistanceMap >
  25. inline typename property_traits< DistanceMap >::value_type eccentricity(
  26. const Graph& g, DistanceMap dist)
  27. {
  28. BOOST_CONCEPT_ASSERT((GraphConcept< Graph >));
  29. typedef typename graph_traits< Graph >::vertex_descriptor Vertex;
  30. BOOST_CONCEPT_ASSERT((ReadablePropertyMapConcept< DistanceMap, Vertex >));
  31. typedef typename property_traits< DistanceMap >::value_type Distance;
  32. return eccentricity(g, dist, detail::maximize< Distance >());
  33. }
  34. template < typename Graph, typename DistanceMatrix, typename EccentricityMap >
  35. inline std::pair< typename property_traits< EccentricityMap >::value_type,
  36. typename property_traits< EccentricityMap >::value_type >
  37. all_eccentricities(
  38. const Graph& g, const DistanceMatrix& dist, EccentricityMap ecc)
  39. {
  40. BOOST_CONCEPT_ASSERT((VertexListGraphConcept< Graph >));
  41. typedef typename graph_traits< Graph >::vertex_descriptor Vertex;
  42. typedef typename graph_traits< Graph >::vertex_iterator VertexIterator;
  43. BOOST_CONCEPT_ASSERT(
  44. (ReadablePropertyMapConcept< DistanceMatrix, Vertex >));
  45. typedef typename property_traits< DistanceMatrix >::value_type DistanceMap;
  46. BOOST_CONCEPT_ASSERT(
  47. (WritablePropertyMapConcept< EccentricityMap, Vertex >));
  48. typedef
  49. typename property_traits< EccentricityMap >::value_type Eccentricity;
  50. BOOST_USING_STD_MIN();
  51. BOOST_USING_STD_MAX();
  52. Eccentricity r = numeric_values< Eccentricity >::infinity(),
  53. d = numeric_values< Eccentricity >::zero();
  54. VertexIterator i, end;
  55. boost::tie(i, end) = vertices(g);
  56. for (boost::tie(i, end) = vertices(g); i != end; ++i)
  57. {
  58. DistanceMap dm = get(dist, *i);
  59. Eccentricity e = eccentricity(g, dm);
  60. put(ecc, *i, e);
  61. // track the radius and diameter at the same time
  62. r = min BOOST_PREVENT_MACRO_SUBSTITUTION(r, e);
  63. d = max BOOST_PREVENT_MACRO_SUBSTITUTION(d, e);
  64. }
  65. return std::make_pair(r, d);
  66. }
  67. template < typename Graph, typename EccentricityMap >
  68. inline std::pair< typename property_traits< EccentricityMap >::value_type,
  69. typename property_traits< EccentricityMap >::value_type >
  70. radius_and_diameter(const Graph& g, EccentricityMap ecc)
  71. {
  72. BOOST_CONCEPT_ASSERT((VertexListGraphConcept< Graph >));
  73. typedef typename graph_traits< Graph >::vertex_descriptor Vertex;
  74. typedef typename graph_traits< Graph >::vertex_iterator VertexIterator;
  75. BOOST_CONCEPT_ASSERT(
  76. (ReadablePropertyMapConcept< EccentricityMap, Vertex >));
  77. typedef
  78. typename property_traits< EccentricityMap >::value_type Eccentricity;
  79. BOOST_USING_STD_MIN();
  80. BOOST_USING_STD_MAX();
  81. VertexIterator i, end;
  82. boost::tie(i, end) = vertices(g);
  83. Eccentricity radius = get(ecc, *i);
  84. Eccentricity diameter = get(ecc, *i);
  85. for (i = boost::next(i); i != end; ++i)
  86. {
  87. Eccentricity cur = get(ecc, *i);
  88. radius = min BOOST_PREVENT_MACRO_SUBSTITUTION(radius, cur);
  89. diameter = max BOOST_PREVENT_MACRO_SUBSTITUTION(diameter, cur);
  90. }
  91. return std::make_pair(radius, diameter);
  92. }
  93. template < typename Graph, typename EccentricityMap >
  94. inline typename property_traits< EccentricityMap >::value_type radius(
  95. const Graph& g, EccentricityMap ecc)
  96. {
  97. return radius_and_diameter(g, ecc).first;
  98. }
  99. template < typename Graph, typename EccentricityMap >
  100. inline typename property_traits< EccentricityMap >::value_type diameter(
  101. const Graph& g, EccentricityMap ecc)
  102. {
  103. return radius_and_diameter(g, ecc).second;
  104. }
  105. } /* namespace boost */
  106. #endif