geodesic_distance.hpp 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. // (C) Copyright Andrew Sutton 2007
  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_GEODESIC_DISTANCE_HPP
  7. #define BOOST_GRAPH_GEODESIC_DISTANCE_HPP
  8. #include <boost/graph/detail/geodesic.hpp>
  9. #include <boost/graph/exterior_property.hpp>
  10. #include <boost/concept/assert.hpp>
  11. namespace boost
  12. {
  13. template < typename Graph, typename DistanceType, typename ResultType,
  14. typename Divides = std::divides< ResultType > >
  15. struct mean_geodesic_measure
  16. : public geodesic_measure< Graph, DistanceType, ResultType >
  17. {
  18. typedef geodesic_measure< Graph, DistanceType, ResultType > base_type;
  19. typedef typename base_type::distance_type distance_type;
  20. typedef typename base_type::result_type result_type;
  21. result_type operator()(distance_type d, const Graph& g)
  22. {
  23. BOOST_CONCEPT_ASSERT((VertexListGraphConcept< Graph >));
  24. BOOST_CONCEPT_ASSERT((NumericValueConcept< DistanceType >));
  25. BOOST_CONCEPT_ASSERT((NumericValueConcept< ResultType >));
  26. BOOST_CONCEPT_ASSERT((AdaptableBinaryFunctionConcept< Divides,
  27. ResultType, ResultType, ResultType >));
  28. return (d == base_type::infinite_distance())
  29. ? base_type::infinite_result()
  30. : div(result_type(d), result_type(num_vertices(g) - 1));
  31. }
  32. Divides div;
  33. };
  34. template < typename Graph, typename DistanceMap >
  35. inline mean_geodesic_measure< Graph,
  36. typename property_traits< DistanceMap >::value_type, double >
  37. measure_mean_geodesic(const Graph&, DistanceMap)
  38. {
  39. return mean_geodesic_measure< Graph,
  40. typename property_traits< DistanceMap >::value_type, double >();
  41. }
  42. template < typename T, typename Graph, typename DistanceMap >
  43. inline mean_geodesic_measure< Graph,
  44. typename property_traits< DistanceMap >::value_type, T >
  45. measure_mean_geodesic(const Graph&, DistanceMap)
  46. {
  47. return mean_geodesic_measure< Graph,
  48. typename property_traits< DistanceMap >::value_type, T >();
  49. }
  50. // This is a little different because it's expected that the result type
  51. // should (must?) be the same as the distance type. There's a type of
  52. // transitivity in this thinking... If the average of distances has type
  53. // X then the average of x's should also be type X. Is there a case where this
  54. // is not true?
  55. //
  56. // This type is a little under-genericized... It needs generic parameters
  57. // for addition and division.
  58. template < typename Graph, typename DistanceType >
  59. struct mean_graph_distance_measure
  60. : public geodesic_measure< Graph, DistanceType, DistanceType >
  61. {
  62. typedef geodesic_measure< Graph, DistanceType, DistanceType > base_type;
  63. typedef typename base_type::distance_type distance_type;
  64. typedef typename base_type::result_type result_type;
  65. inline result_type operator()(distance_type d, const Graph& g)
  66. {
  67. BOOST_CONCEPT_ASSERT((VertexListGraphConcept< Graph >));
  68. BOOST_CONCEPT_ASSERT((NumericValueConcept< DistanceType >));
  69. if (d == base_type::infinite_distance())
  70. {
  71. return base_type::infinite_result();
  72. }
  73. else
  74. {
  75. return d / result_type(num_vertices(g));
  76. }
  77. }
  78. };
  79. template < typename Graph, typename DistanceMap >
  80. inline mean_graph_distance_measure< Graph,
  81. typename property_traits< DistanceMap >::value_type >
  82. measure_graph_mean_geodesic(const Graph&, DistanceMap)
  83. {
  84. typedef typename property_traits< DistanceMap >::value_type T;
  85. return mean_graph_distance_measure< Graph, T >();
  86. }
  87. template < typename Graph, typename DistanceMap, typename Measure,
  88. typename Combinator >
  89. inline typename Measure::result_type mean_geodesic(
  90. const Graph& g, DistanceMap dist, Measure measure, Combinator combine)
  91. {
  92. BOOST_CONCEPT_ASSERT((DistanceMeasureConcept< Measure, Graph >));
  93. typedef typename Measure::distance_type Distance;
  94. Distance n = detail::combine_distances(g, dist, combine, Distance(0));
  95. return measure(n, g);
  96. }
  97. template < typename Graph, typename DistanceMap, typename Measure >
  98. inline typename Measure::result_type mean_geodesic(
  99. const Graph& g, DistanceMap dist, Measure measure)
  100. {
  101. BOOST_CONCEPT_ASSERT((DistanceMeasureConcept< Measure, Graph >));
  102. typedef typename Measure::distance_type Distance;
  103. return mean_geodesic(g, dist, measure, std::plus< Distance >());
  104. }
  105. template < typename Graph, typename DistanceMap >
  106. inline double mean_geodesic(const Graph& g, DistanceMap dist)
  107. {
  108. return mean_geodesic(g, dist, measure_mean_geodesic(g, dist));
  109. }
  110. template < typename T, typename Graph, typename DistanceMap >
  111. inline T mean_geodesic(const Graph& g, DistanceMap dist)
  112. {
  113. return mean_geodesic(g, dist, measure_mean_geodesic< T >(g, dist));
  114. }
  115. template < typename Graph, typename DistanceMatrixMap, typename GeodesicMap,
  116. typename Measure >
  117. inline typename property_traits< GeodesicMap >::value_type all_mean_geodesics(
  118. const Graph& g, DistanceMatrixMap dist, GeodesicMap geo, Measure measure)
  119. {
  120. BOOST_CONCEPT_ASSERT((VertexListGraphConcept< Graph >));
  121. typedef typename graph_traits< Graph >::vertex_descriptor Vertex;
  122. typedef typename graph_traits< Graph >::vertex_iterator VertexIterator;
  123. BOOST_CONCEPT_ASSERT(
  124. (ReadablePropertyMapConcept< DistanceMatrixMap, Vertex >));
  125. typedef
  126. typename property_traits< DistanceMatrixMap >::value_type DistanceMap;
  127. BOOST_CONCEPT_ASSERT((DistanceMeasureConcept< Measure, Graph >));
  128. typedef typename Measure::result_type Result;
  129. BOOST_CONCEPT_ASSERT((WritablePropertyMapConcept< GeodesicMap, Vertex >));
  130. BOOST_CONCEPT_ASSERT((NumericValueConcept< Result >));
  131. // NOTE: We could compute the mean geodesic here by performing additional
  132. // computations (i.e., adding and dividing). However, I don't really feel
  133. // like fully genericizing the entire operation yet so I'm not going to.
  134. Result inf = numeric_values< Result >::infinity();
  135. Result sum = numeric_values< Result >::zero();
  136. VertexIterator i, end;
  137. for (boost::tie(i, end) = vertices(g); i != end; ++i)
  138. {
  139. DistanceMap dm = get(dist, *i);
  140. Result r = mean_geodesic(g, dm, measure);
  141. put(geo, *i, r);
  142. // compute the sum along with geodesics
  143. if (r == inf)
  144. {
  145. sum = inf;
  146. }
  147. else if (sum != inf)
  148. {
  149. sum += r;
  150. }
  151. }
  152. // return the average of averages.
  153. return sum / Result(num_vertices(g));
  154. }
  155. template < typename Graph, typename DistanceMatrixMap, typename GeodesicMap >
  156. inline typename property_traits< GeodesicMap >::value_type all_mean_geodesics(
  157. const Graph& g, DistanceMatrixMap dist, GeodesicMap geo)
  158. {
  159. BOOST_CONCEPT_ASSERT((GraphConcept< Graph >));
  160. typedef typename graph_traits< Graph >::vertex_descriptor Vertex;
  161. BOOST_CONCEPT_ASSERT(
  162. (ReadablePropertyMapConcept< DistanceMatrixMap, Vertex >));
  163. typedef
  164. typename property_traits< DistanceMatrixMap >::value_type DistanceMap;
  165. BOOST_CONCEPT_ASSERT((WritablePropertyMapConcept< GeodesicMap, Vertex >));
  166. typedef typename property_traits< GeodesicMap >::value_type Result;
  167. return all_mean_geodesics(
  168. g, dist, geo, measure_mean_geodesic< Result >(g, DistanceMap()));
  169. }
  170. template < typename Graph, typename GeodesicMap, typename Measure >
  171. inline typename Measure::result_type small_world_distance(
  172. const Graph& g, GeodesicMap geo, Measure measure)
  173. {
  174. BOOST_CONCEPT_ASSERT((DistanceMeasureConcept< Measure, Graph >));
  175. typedef typename Measure::result_type Result;
  176. Result sum
  177. = detail::combine_distances(g, geo, std::plus< Result >(), Result(0));
  178. return measure(sum, g);
  179. }
  180. template < typename Graph, typename GeodesicMap >
  181. inline typename property_traits< GeodesicMap >::value_type small_world_distance(
  182. const Graph& g, GeodesicMap geo)
  183. {
  184. return small_world_distance(g, geo, measure_graph_mean_geodesic(g, geo));
  185. }
  186. }
  187. #endif