degree_centrality.hpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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. {
  12. template < typename Graph > 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 : public degree_centrality_measure< Graph >
  19. {
  20. typedef degree_centrality_measure< Graph > base_type;
  21. typedef typename base_type::degree_type degree_type;
  22. typedef typename base_type::vertex_type vertex_type;
  23. inline degree_type operator()(vertex_type v, const Graph& g)
  24. {
  25. BOOST_CONCEPT_ASSERT((IncidenceGraphConcept< Graph >));
  26. return out_degree(v, g);
  27. }
  28. };
  29. template < typename Graph >
  30. inline influence_measure< Graph > measure_influence(const Graph&)
  31. {
  32. return influence_measure< Graph >();
  33. }
  34. template < typename Graph >
  35. struct prestige_measure : public degree_centrality_measure< Graph >
  36. {
  37. typedef degree_centrality_measure< Graph > base_type;
  38. typedef typename base_type::degree_type degree_type;
  39. typedef typename base_type::vertex_type vertex_type;
  40. inline degree_type operator()(vertex_type v, const Graph& g)
  41. {
  42. BOOST_CONCEPT_ASSERT((BidirectionalGraphConcept< Graph >));
  43. return in_degree(v, g);
  44. }
  45. };
  46. template < typename Graph >
  47. inline prestige_measure< Graph > measure_prestige(const Graph&)
  48. {
  49. return prestige_measure< Graph >();
  50. }
  51. template < typename Graph, typename Vertex, typename Measure >
  52. inline typename Measure::degree_type degree_centrality(
  53. 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 degree_centrality(
  60. 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 influence(
  67. const Graph& g, Vertex v)
  68. {
  69. return degree_centrality(g, v, measure_influence(g));
  70. }
  71. template < typename Graph, typename Vertex >
  72. inline typename graph_traits< Graph >::degree_size_type prestige(
  73. const Graph& g, Vertex v)
  74. {
  75. return degree_centrality(g, v, measure_prestige(g));
  76. }
  77. template < typename Graph, typename CentralityMap, typename Measure >
  78. inline void all_degree_centralities(
  79. const Graph& g, CentralityMap cent, Measure measure)
  80. {
  81. BOOST_CONCEPT_ASSERT((VertexListGraphConcept< Graph >));
  82. typedef typename graph_traits< Graph >::vertex_descriptor Vertex;
  83. typedef typename graph_traits< Graph >::vertex_iterator VertexIterator;
  84. BOOST_CONCEPT_ASSERT((WritablePropertyMapConcept< CentralityMap, Vertex >));
  85. typedef typename property_traits< CentralityMap >::value_type Centrality;
  86. VertexIterator i, end;
  87. for (boost::tie(i, end) = vertices(g); i != end; ++i)
  88. {
  89. Centrality c = degree_centrality(g, *i, measure);
  90. put(cent, *i, c);
  91. }
  92. }
  93. template < typename Graph, typename CentralityMap >
  94. inline void all_degree_centralities(const Graph& g, CentralityMap cent)
  95. {
  96. all_degree_centralities(g, cent, measure_influence(g));
  97. }
  98. // More helper functions for computing influence and prestige.
  99. // I hate the names of these functions, but influence and prestige
  100. // don't pluralize too well.
  101. template < typename Graph, typename CentralityMap >
  102. inline void all_influence_values(const Graph& g, CentralityMap cent)
  103. {
  104. all_degree_centralities(g, cent, measure_influence(g));
  105. }
  106. template < typename Graph, typename CentralityMap >
  107. inline void all_prestige_values(const Graph& g, CentralityMap cent)
  108. {
  109. all_degree_centralities(g, cent, measure_prestige(g));
  110. }
  111. } /* namespace boost */
  112. #endif