geodesic.hpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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. // NOTE: Disabled until this concept assert is fixed in Boost.ConceptCheck.
  54. // BOOST_CONCEPT_ASSERT((AdaptableBinaryFunction< Combinator, Distance,
  55. // Distance, Distance >));
  56. // If there's ever an infinite distance, then we simply return
  57. // infinity. Note that this /will/ include the a non-zero
  58. // distance-to-self in the combined values. However, this is usually
  59. // zero, so it shouldn't be too problematic.
  60. Distance ret = init;
  61. VertexIterator i, end;
  62. for (boost::tie(i, end) = vertices(g); i != end; ++i)
  63. {
  64. Vertex v = *i;
  65. if (get(dist, v) != DistanceNumbers::infinity())
  66. {
  67. ret = combine(ret, get(dist, v));
  68. }
  69. else
  70. {
  71. ret = DistanceNumbers::infinity();
  72. break;
  73. }
  74. }
  75. return ret;
  76. }
  77. // Similar to std::plus<T>, but maximizes parameters
  78. // rather than adding them.
  79. template < typename T > struct maximize
  80. {
  81. typedef T result_type;
  82. typedef T first_argument_type;
  83. typedef T second_argument_type;
  84. T operator()(T x, T y) const
  85. {
  86. BOOST_USING_STD_MAX();
  87. return max BOOST_PREVENT_MACRO_SUBSTITUTION(x, y);
  88. }
  89. };
  90. // Another helper, like maximize() to help abstract functional
  91. // concepts. This is trivially instantiated for builtin numeric
  92. // types, but should be specialized for those types that have
  93. // discrete notions of reciprocals.
  94. template < typename T > struct reciprocal
  95. {
  96. typedef T result_type;
  97. typedef T argument_type;
  98. T operator()(T t) { return T(1) / t; }
  99. };
  100. } /* namespace detail */
  101. // This type defines the basic facilities used for computing values
  102. // based on the geodesic distances between vertices. Examples include
  103. // closeness centrality and mean geodesic distance.
  104. template < typename Graph, typename DistanceType, typename ResultType >
  105. struct geodesic_measure
  106. {
  107. typedef DistanceType distance_type;
  108. typedef ResultType result_type;
  109. typedef typename graph_traits< Graph >::vertices_size_type size_type;
  110. typedef numeric_values< distance_type > distance_values;
  111. typedef numeric_values< result_type > result_values;
  112. static inline distance_type infinite_distance()
  113. {
  114. return distance_values::infinity();
  115. }
  116. static inline result_type infinite_result()
  117. {
  118. return result_values::infinity();
  119. }
  120. static inline result_type zero_result() { return result_values::zero(); }
  121. };
  122. } /* namespace boost */
  123. #endif