geodesic.hpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. // (C) Copyright 2007 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_DETAIL_GEODESIC_HPP
  7. #define BOOST_GRAPH_DETAIL_GEODESIC_HPP
  8. #include <functional>
  9. #include <boost/config.hpp>
  10. #include <boost/graph/graph_concepts.hpp>
  11. #include <boost/graph/numeric_values.hpp>
  12. #include <boost/concept/assert.hpp>
  13. // TODO: Should this really be in detail?
  14. namespace boost
  15. {
  16. // This is a very good discussion on centrality measures. While I can't
  17. // say that this has been the motivating factor for the design and
  18. // implementation of ths centrality framework, it does provide a single
  19. // point of reference for defining things like degree and closeness
  20. // centrality. Plus, the bibliography seems fairly complete.
  21. //
  22. // @article{citeulike:1144245,
  23. // author = {Borgatti, Stephen P. and Everett, Martin G.},
  24. // citeulike-article-id = {1144245},
  25. // doi = {10.1016/j.socnet.2005.11.005},
  26. // journal = {Social Networks},
  27. // month = {October},
  28. // number = {4},
  29. // pages = {466--484},
  30. // priority = {0},
  31. // title = {A Graph-theoretic perspective on centrality},
  32. // url = {https://doi.org/10.1016/j.socnet.2005.11.005},
  33. // volume = {28},
  34. // year = {2006}
  35. // }
  36. // }
  37. namespace detail
  38. {
  39. // Note that this assumes T == property_traits<DistanceMap>::value_type
  40. // and that the args and return of combine are also T.
  41. template < typename Graph, typename DistanceMap, typename Combinator,
  42. typename Distance >
  43. inline Distance combine_distances(
  44. const Graph& g, DistanceMap dist, Combinator combine, Distance init)
  45. {
  46. BOOST_CONCEPT_ASSERT((VertexListGraphConcept< Graph >));
  47. typedef typename graph_traits< Graph >::vertex_descriptor Vertex;
  48. typedef typename graph_traits< Graph >::vertex_iterator VertexIterator;
  49. BOOST_CONCEPT_ASSERT(
  50. (ReadablePropertyMapConcept< DistanceMap, Vertex >));
  51. BOOST_CONCEPT_ASSERT((NumericValueConcept< Distance >));
  52. typedef numeric_values< Distance > DistanceNumbers;
  53. BOOST_CONCEPT_ASSERT((AdaptableBinaryFunction< Combinator, Distance,
  54. Distance, Distance >));
  55. // If there's ever an infinite distance, then we simply return
  56. // infinity. Note that this /will/ include the a non-zero
  57. // distance-to-self in the combined values. However, this is usually
  58. // zero, so it shouldn't be too problematic.
  59. Distance ret = init;
  60. VertexIterator i, end;
  61. for (boost::tie(i, end) = vertices(g); i != end; ++i)
  62. {
  63. Vertex v = *i;
  64. if (get(dist, v) != DistanceNumbers::infinity())
  65. {
  66. ret = combine(ret, get(dist, v));
  67. }
  68. else
  69. {
  70. ret = DistanceNumbers::infinity();
  71. break;
  72. }
  73. }
  74. return ret;
  75. }
  76. // Similar to std::plus<T>, but maximizes parameters
  77. // rather than adding them.
  78. template < typename T > struct maximize
  79. {
  80. typedef T result_type;
  81. typedef T first_argument_type;
  82. typedef T second_argument_type;
  83. T operator()(T x, T y) const
  84. {
  85. BOOST_USING_STD_MAX();
  86. return max BOOST_PREVENT_MACRO_SUBSTITUTION(x, y);
  87. }
  88. };
  89. // Another helper, like maximize() to help abstract functional
  90. // concepts. This is trivially instantiated for builtin numeric
  91. // types, but should be specialized for those types that have
  92. // discrete notions of reciprocals.
  93. template < typename T > struct reciprocal
  94. {
  95. typedef T result_type;
  96. typedef T argument_type;
  97. T operator()(T t) { return T(1) / t; }
  98. };
  99. } /* namespace detail */
  100. // This type defines the basic facilities used for computing values
  101. // based on the geodesic distances between vertices. Examples include
  102. // closeness centrality and mean geodesic distance.
  103. template < typename Graph, typename DistanceType, typename ResultType >
  104. struct geodesic_measure
  105. {
  106. typedef DistanceType distance_type;
  107. typedef ResultType result_type;
  108. typedef typename graph_traits< Graph >::vertices_size_type size_type;
  109. typedef numeric_values< distance_type > distance_values;
  110. typedef numeric_values< result_type > result_values;
  111. static inline distance_type infinite_distance()
  112. {
  113. return distance_values::infinity();
  114. }
  115. static inline result_type infinite_result()
  116. {
  117. return result_values::infinity();
  118. }
  119. static inline result_type zero_result() { return result_values::zero(); }
  120. };
  121. } /* namespace boost */
  122. #endif