geodesic.hpp 4.7 KB

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