bc_clustering.hpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. // Copyright 2004 The Trustees of Indiana University.
  2. // Distributed under the Boost Software License, Version 1.0.
  3. // (See accompanying file LICENSE_1_0.txt or copy at
  4. // http://www.boost.org/LICENSE_1_0.txt)
  5. // Authors: Douglas Gregor
  6. // Andrew Lumsdaine
  7. #ifndef BOOST_GRAPH_BETWEENNESS_CENTRALITY_CLUSTERING_HPP
  8. #define BOOST_GRAPH_BETWEENNESS_CENTRALITY_CLUSTERING_HPP
  9. #include <boost/graph/betweenness_centrality.hpp>
  10. #include <boost/graph/graph_traits.hpp>
  11. #include <boost/graph/graph_utility.hpp>
  12. #include <boost/pending/indirect_cmp.hpp>
  13. #include <algorithm>
  14. #include <vector>
  15. #include <boost/property_map/property_map.hpp>
  16. namespace boost
  17. {
  18. /** Threshold termination function for the betweenness centrality
  19. * clustering algorithm.
  20. */
  21. template < typename T > struct bc_clustering_threshold
  22. {
  23. typedef T centrality_type;
  24. /// Terminate clustering when maximum absolute edge centrality is
  25. /// below the given threshold.
  26. explicit bc_clustering_threshold(T threshold)
  27. : threshold(threshold), dividend(1.0)
  28. {
  29. }
  30. /**
  31. * Terminate clustering when the maximum edge centrality is below
  32. * the given threshold.
  33. *
  34. * @param threshold the threshold value
  35. *
  36. * @param g the graph on which the threshold will be calculated
  37. *
  38. * @param normalize when true, the threshold is compared against the
  39. * normalized edge centrality based on the input graph; otherwise,
  40. * the threshold is compared against the absolute edge centrality.
  41. */
  42. template < typename Graph >
  43. bc_clustering_threshold(T threshold, const Graph& g, bool normalize = true)
  44. : threshold(threshold), dividend(1.0)
  45. {
  46. if (normalize)
  47. {
  48. typename graph_traits< Graph >::vertices_size_type n
  49. = num_vertices(g);
  50. dividend = T((n - 1) * (n - 2)) / T(2);
  51. }
  52. }
  53. /** Returns true when the given maximum edge centrality (potentially
  54. * normalized) falls below the threshold.
  55. */
  56. template < typename Graph, typename Edge >
  57. bool operator()(T max_centrality, Edge, const Graph&)
  58. {
  59. return (max_centrality / dividend) < threshold;
  60. }
  61. protected:
  62. T threshold;
  63. T dividend;
  64. };
  65. /** Graph clustering based on edge betweenness centrality.
  66. *
  67. * This algorithm implements graph clustering based on edge
  68. * betweenness centrality. It is an iterative algorithm, where in each
  69. * step it compute the edge betweenness centrality (via @ref
  70. * brandes_betweenness_centrality) and removes the edge with the
  71. * maximum betweenness centrality. The @p done function object
  72. * determines when the algorithm terminates (the edge found when the
  73. * algorithm terminates will not be removed).
  74. *
  75. * @param g The graph on which clustering will be performed. The type
  76. * of this parameter (@c MutableGraph) must be a model of the
  77. * VertexListGraph, IncidenceGraph, EdgeListGraph, and Mutable Graph
  78. * concepts.
  79. *
  80. * @param done The function object that indicates termination of the
  81. * algorithm. It must be a ternary function object thats accepts the
  82. * maximum centrality, the descriptor of the edge that will be
  83. * removed, and the graph @p g.
  84. *
  85. * @param edge_centrality (UTIL/OUT) The property map that will store
  86. * the betweenness centrality for each edge. When the algorithm
  87. * terminates, it will contain the edge centralities for the
  88. * graph. The type of this property map must model the
  89. * ReadWritePropertyMap concept. Defaults to an @c
  90. * iterator_property_map whose value type is
  91. * @c Done::centrality_type and using @c get(edge_index, g) for the
  92. * index map.
  93. *
  94. * @param vertex_index (IN) The property map that maps vertices to
  95. * indices in the range @c [0, num_vertices(g)). This type of this
  96. * property map must model the ReadablePropertyMap concept and its
  97. * value type must be an integral type. Defaults to
  98. * @c get(vertex_index, g).
  99. */
  100. template < typename MutableGraph, typename Done, typename EdgeCentralityMap,
  101. typename VertexIndexMap >
  102. void betweenness_centrality_clustering(MutableGraph& g, Done done,
  103. EdgeCentralityMap edge_centrality, VertexIndexMap vertex_index)
  104. {
  105. typedef typename property_traits< EdgeCentralityMap >::value_type
  106. centrality_type;
  107. typedef typename graph_traits< MutableGraph >::edge_iterator edge_iterator;
  108. typedef
  109. typename graph_traits< MutableGraph >::edge_descriptor edge_descriptor;
  110. if (has_no_edges(g))
  111. return;
  112. // Function object that compares the centrality of edges
  113. indirect_cmp< EdgeCentralityMap, std::less< centrality_type > > cmp(
  114. edge_centrality);
  115. bool is_done;
  116. do
  117. {
  118. brandes_betweenness_centrality(g,
  119. edge_centrality_map(edge_centrality)
  120. .vertex_index_map(vertex_index));
  121. std::pair< edge_iterator, edge_iterator > edges_iters = edges(g);
  122. edge_descriptor e
  123. = *max_element(edges_iters.first, edges_iters.second, cmp);
  124. is_done = done(get(edge_centrality, e), e, g);
  125. if (!is_done)
  126. remove_edge(e, g);
  127. } while (!is_done && !has_no_edges(g));
  128. }
  129. /**
  130. * \overload
  131. */
  132. template < typename MutableGraph, typename Done, typename EdgeCentralityMap >
  133. void betweenness_centrality_clustering(
  134. MutableGraph& g, Done done, EdgeCentralityMap edge_centrality)
  135. {
  136. betweenness_centrality_clustering(
  137. g, done, edge_centrality, get(vertex_index, g));
  138. }
  139. /**
  140. * \overload
  141. */
  142. template < typename MutableGraph, typename Done >
  143. void betweenness_centrality_clustering(MutableGraph& g, Done done)
  144. {
  145. typedef typename Done::centrality_type centrality_type;
  146. std::vector< centrality_type > edge_centrality(num_edges(g));
  147. betweenness_centrality_clustering(g, done,
  148. make_iterator_property_map(edge_centrality.begin(), get(edge_index, g)),
  149. get(vertex_index, g));
  150. }
  151. } // end namespace boost
  152. #endif // BOOST_GRAPH_BETWEENNESS_CENTRALITY_CLUSTERING_HPP