closeness_centrality.hpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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_CLOSENESS_CENTRALITY_HPP
  7. #define BOOST_GRAPH_CLOSENESS_CENTRALITY_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 Reciprocal = detail::reciprocal< ResultType > >
  15. struct closeness_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&)
  22. {
  23. BOOST_CONCEPT_ASSERT((NumericValueConcept< DistanceType >));
  24. BOOST_CONCEPT_ASSERT((NumericValueConcept< ResultType >));
  25. BOOST_CONCEPT_ASSERT((AdaptableUnaryFunctionConcept< Reciprocal,
  26. ResultType, ResultType >));
  27. return (d == base_type::infinite_distance()) ? base_type::zero_result()
  28. : rec(result_type(d));
  29. }
  30. Reciprocal rec;
  31. };
  32. template < typename Graph, typename DistanceMap >
  33. inline closeness_measure< Graph,
  34. typename property_traits< DistanceMap >::value_type, double,
  35. detail::reciprocal< double > >
  36. measure_closeness(const Graph&, DistanceMap)
  37. {
  38. typedef typename property_traits< DistanceMap >::value_type Distance;
  39. return closeness_measure< Graph, Distance, double,
  40. detail::reciprocal< double > >();
  41. }
  42. template < typename T, typename Graph, typename DistanceMap >
  43. inline closeness_measure< Graph,
  44. typename property_traits< DistanceMap >::value_type, T,
  45. detail::reciprocal< T > >
  46. measure_closeness(const Graph&, DistanceMap)
  47. {
  48. typedef typename property_traits< DistanceMap >::value_type Distance;
  49. return closeness_measure< Graph, Distance, T, detail::reciprocal< T > >();
  50. }
  51. template < typename T, typename Graph, typename DistanceMap,
  52. typename Reciprocal >
  53. inline closeness_measure< Graph,
  54. typename property_traits< DistanceMap >::value_type, T, Reciprocal >
  55. measure_closeness(const Graph&, DistanceMap)
  56. {
  57. typedef typename property_traits< DistanceMap >::value_type Distance;
  58. return closeness_measure< Graph, Distance, T, Reciprocal >();
  59. }
  60. template < typename Graph, typename DistanceMap, typename Measure,
  61. typename Combinator >
  62. inline typename Measure::result_type closeness_centrality(
  63. const Graph& g, DistanceMap dist, Measure measure, Combinator combine)
  64. {
  65. BOOST_CONCEPT_ASSERT((VertexListGraphConcept< Graph >));
  66. typedef typename graph_traits< Graph >::vertex_descriptor Vertex;
  67. BOOST_CONCEPT_ASSERT((ReadablePropertyMapConcept< DistanceMap, Vertex >));
  68. typedef typename property_traits< DistanceMap >::value_type Distance;
  69. BOOST_CONCEPT_ASSERT((NumericValueConcept< Distance >));
  70. BOOST_CONCEPT_ASSERT((DistanceMeasureConcept< Measure, Graph >));
  71. Distance n = detail::combine_distances(g, dist, combine, Distance(0));
  72. return measure(n, g);
  73. }
  74. template < typename Graph, typename DistanceMap, typename Measure >
  75. inline typename Measure::result_type closeness_centrality(
  76. const Graph& g, DistanceMap dist, Measure measure)
  77. {
  78. BOOST_CONCEPT_ASSERT((GraphConcept< Graph >));
  79. typedef typename graph_traits< Graph >::vertex_descriptor Vertex;
  80. BOOST_CONCEPT_ASSERT((ReadablePropertyMapConcept< DistanceMap, Vertex >));
  81. typedef typename property_traits< DistanceMap >::value_type Distance;
  82. return closeness_centrality(g, dist, measure, std::plus< Distance >());
  83. }
  84. template < typename Graph, typename DistanceMap >
  85. inline double closeness_centrality(const Graph& g, DistanceMap dist)
  86. {
  87. return closeness_centrality(g, dist, measure_closeness(g, dist));
  88. }
  89. template < typename T, typename Graph, typename DistanceMap >
  90. inline T closeness_centrality(const Graph& g, DistanceMap dist)
  91. {
  92. return closeness_centrality(g, dist, measure_closeness< T >(g, dist));
  93. }
  94. template < typename Graph, typename DistanceMatrixMap, typename CentralityMap,
  95. typename Measure >
  96. inline void all_closeness_centralities(
  97. const Graph& g, DistanceMatrixMap dist, CentralityMap cent, Measure measure)
  98. {
  99. BOOST_CONCEPT_ASSERT((VertexListGraphConcept< Graph >));
  100. typedef typename graph_traits< Graph >::vertex_descriptor Vertex;
  101. BOOST_CONCEPT_ASSERT(
  102. (ReadablePropertyMapConcept< DistanceMatrixMap, Vertex >));
  103. typedef
  104. typename property_traits< DistanceMatrixMap >::value_type DistanceMap;
  105. BOOST_CONCEPT_ASSERT((ReadablePropertyMapConcept< DistanceMap, Vertex >));
  106. BOOST_CONCEPT_ASSERT((WritablePropertyMapConcept< CentralityMap, Vertex >));
  107. typedef typename property_traits< CentralityMap >::value_type Centrality;
  108. typename graph_traits< Graph >::vertex_iterator i, end;
  109. for (boost::tie(i, end) = vertices(g); i != end; ++i)
  110. {
  111. DistanceMap dm = get(dist, *i);
  112. Centrality c = closeness_centrality(g, dm, measure);
  113. put(cent, *i, c);
  114. }
  115. }
  116. template < typename Graph, typename DistanceMatrixMap, typename CentralityMap >
  117. inline void all_closeness_centralities(
  118. const Graph& g, DistanceMatrixMap dist, CentralityMap cent)
  119. {
  120. BOOST_CONCEPT_ASSERT((GraphConcept< Graph >));
  121. typedef typename graph_traits< Graph >::vertex_descriptor Vertex;
  122. BOOST_CONCEPT_ASSERT(
  123. (ReadablePropertyMapConcept< DistanceMatrixMap, Vertex >));
  124. typedef
  125. typename property_traits< DistanceMatrixMap >::value_type DistanceMap;
  126. BOOST_CONCEPT_ASSERT((ReadablePropertyMapConcept< DistanceMap, Vertex >));
  127. typedef typename property_traits< CentralityMap >::value_type Result;
  128. all_closeness_centralities(
  129. g, dist, cent, measure_closeness< Result >(g, DistanceMap()));
  130. }
  131. } /* namespace boost */
  132. #endif