degree_centrality.hpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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_DEGREE_CENTRALITY_HPP
  7. #define BOOST_GRAPH_DEGREE_CENTRALITY_HPP
  8. #include <boost/graph/graph_concepts.hpp>
  9. #include <boost/concept/assert.hpp>
  10. namespace boost {
  11. template <typename Graph>
  12. struct degree_centrality_measure
  13. {
  14. typedef typename graph_traits<Graph>::degree_size_type degree_type;
  15. typedef typename graph_traits<Graph>::vertex_descriptor vertex_type;
  16. };
  17. template <typename Graph>
  18. struct influence_measure
  19. : public degree_centrality_measure<Graph>
  20. {
  21. typedef degree_centrality_measure<Graph> base_type;
  22. typedef typename base_type::degree_type degree_type;
  23. typedef typename base_type::vertex_type vertex_type;
  24. inline degree_type operator ()(vertex_type v, const Graph& g)
  25. {
  26. BOOST_CONCEPT_ASSERT(( IncidenceGraphConcept<Graph> ));
  27. return out_degree(v, g);
  28. }
  29. };
  30. template <typename Graph>
  31. inline influence_measure<Graph>
  32. measure_influence(const Graph&)
  33. { return influence_measure<Graph>(); }
  34. template <typename Graph>
  35. struct prestige_measure
  36. : public degree_centrality_measure<Graph>
  37. {
  38. typedef degree_centrality_measure<Graph> base_type;
  39. typedef typename base_type::degree_type degree_type;
  40. typedef typename base_type::vertex_type vertex_type;
  41. inline degree_type operator ()(vertex_type v, const Graph& g)
  42. {
  43. BOOST_CONCEPT_ASSERT(( BidirectionalGraphConcept<Graph> ));
  44. return in_degree(v, g);
  45. }
  46. };
  47. template <typename Graph>
  48. inline prestige_measure<Graph>
  49. measure_prestige(const Graph&)
  50. { return prestige_measure<Graph>(); }
  51. template <typename Graph, typename Vertex, typename Measure>
  52. inline typename Measure::degree_type
  53. degree_centrality(const Graph& g, Vertex v, Measure measure)
  54. {
  55. BOOST_CONCEPT_ASSERT(( DegreeMeasureConcept<Measure, Graph> ));
  56. return measure(v, g);
  57. }
  58. template <typename Graph, typename Vertex>
  59. inline typename graph_traits<Graph>::degree_size_type
  60. degree_centrality(const Graph& g, Vertex v)
  61. {
  62. return degree_centrality(g, v, measure_influence(g));
  63. }
  64. // These are alias functions, intended to provide a more expressive interface.
  65. template <typename Graph, typename Vertex>
  66. inline typename graph_traits<Graph>::degree_size_type
  67. influence(const Graph& g, Vertex v)
  68. { return degree_centrality(g, v, measure_influence(g)); }
  69. template <typename Graph, typename Vertex>
  70. inline typename graph_traits<Graph>::degree_size_type
  71. prestige(const Graph& g, Vertex v)
  72. { return degree_centrality(g, v, measure_prestige(g)); }
  73. template <typename Graph, typename CentralityMap, typename Measure>
  74. inline void
  75. all_degree_centralities(const Graph& g, CentralityMap cent, Measure measure)
  76. {
  77. BOOST_CONCEPT_ASSERT(( VertexListGraphConcept<Graph> ));
  78. typedef typename graph_traits<Graph>::vertex_descriptor Vertex;
  79. typedef typename graph_traits<Graph>::vertex_iterator VertexIterator;
  80. BOOST_CONCEPT_ASSERT(( WritablePropertyMapConcept<CentralityMap,Vertex> ));
  81. typedef typename property_traits<CentralityMap>::value_type Centrality;
  82. VertexIterator i, end;
  83. for(boost::tie(i, end) = vertices(g); i != end; ++i) {
  84. Centrality c = degree_centrality(g, *i, measure);
  85. put(cent, *i, c);
  86. }
  87. }
  88. template <typename Graph, typename CentralityMap>
  89. inline void all_degree_centralities(const Graph& g, CentralityMap cent)
  90. { all_degree_centralities(g, cent, measure_influence(g)); }
  91. // More helper functions for computing influence and prestige.
  92. // I hate the names of these functions, but influence and prestige
  93. // don't pluralize too well.
  94. template <typename Graph, typename CentralityMap>
  95. inline void all_influence_values(const Graph& g, CentralityMap cent)
  96. { all_degree_centralities(g, cent, measure_influence(g)); }
  97. template <typename Graph, typename CentralityMap>
  98. inline void all_prestige_values(const Graph& g, CentralityMap cent)
  99. { all_degree_centralities(g, cent, measure_prestige(g)); }
  100. } /* namespace boost */
  101. #endif