bc_clustering.hpp 5.7 KB

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