File indexing completed on 2025-01-18 09:37:22
0001
0002
0003
0004
0005
0006
0007
0008
0009 #ifndef BOOST_GRAPH_BETWEENNESS_CENTRALITY_CLUSTERING_HPP
0010 #define BOOST_GRAPH_BETWEENNESS_CENTRALITY_CLUSTERING_HPP
0011
0012 #include <boost/algorithm/minmax_element.hpp>
0013 #include <boost/graph/betweenness_centrality.hpp>
0014 #include <boost/graph/graph_traits.hpp>
0015 #include <boost/graph/graph_utility.hpp>
0016 #include <boost/pending/indirect_cmp.hpp>
0017 #include <vector>
0018 #include <boost/property_map/property_map.hpp>
0019
0020 namespace boost
0021 {
0022
0023
0024
0025
0026 template < typename T > struct bc_clustering_threshold
0027 {
0028 typedef T centrality_type;
0029
0030
0031
0032 explicit bc_clustering_threshold(T threshold)
0033 : threshold(threshold), dividend(1.0)
0034 {
0035 }
0036
0037
0038
0039
0040
0041
0042
0043
0044
0045
0046
0047
0048
0049 template < typename Graph >
0050 bc_clustering_threshold(T threshold, const Graph& g, bool normalize = true)
0051 : threshold(threshold), dividend(1.0)
0052 {
0053 if (normalize)
0054 {
0055 typename graph_traits< Graph >::vertices_size_type n
0056 = num_vertices(g);
0057 dividend = T((n - 1) * (n - 2)) / T(2);
0058 }
0059 }
0060
0061
0062
0063
0064 template < typename Graph, typename Edge >
0065 bool operator()(T max_centrality, Edge, const Graph&)
0066 {
0067 return (max_centrality / dividend) < threshold;
0068 }
0069
0070 protected:
0071 T threshold;
0072 T dividend;
0073 };
0074
0075
0076
0077
0078
0079
0080
0081
0082
0083
0084
0085
0086
0087
0088
0089
0090
0091
0092
0093
0094
0095
0096
0097
0098
0099
0100
0101
0102
0103
0104
0105
0106
0107
0108
0109
0110 template < typename MutableGraph, typename Done, typename EdgeCentralityMap,
0111 typename VertexIndexMap >
0112 void betweenness_centrality_clustering(MutableGraph& g, Done done,
0113 EdgeCentralityMap edge_centrality, VertexIndexMap vertex_index)
0114 {
0115 typedef typename property_traits< EdgeCentralityMap >::value_type
0116 centrality_type;
0117 typedef typename graph_traits< MutableGraph >::edge_iterator edge_iterator;
0118 typedef
0119 typename graph_traits< MutableGraph >::edge_descriptor edge_descriptor;
0120
0121 if (has_no_edges(g))
0122 return;
0123
0124
0125 indirect_cmp< EdgeCentralityMap, std::less< centrality_type > > cmp(
0126 edge_centrality);
0127
0128 bool is_done;
0129 do
0130 {
0131 brandes_betweenness_centrality(g,
0132 edge_centrality_map(edge_centrality)
0133 .vertex_index_map(vertex_index));
0134 std::pair< edge_iterator, edge_iterator > edges_iters = edges(g);
0135 edge_descriptor e
0136 = *boost::first_max_element(edges_iters.first, edges_iters.second, cmp);
0137 is_done = done(get(edge_centrality, e), e, g);
0138 if (!is_done)
0139 remove_edge(e, g);
0140 } while (!is_done && !has_no_edges(g));
0141 }
0142
0143
0144
0145
0146 template < typename MutableGraph, typename Done, typename EdgeCentralityMap >
0147 void betweenness_centrality_clustering(
0148 MutableGraph& g, Done done, EdgeCentralityMap edge_centrality)
0149 {
0150 betweenness_centrality_clustering(
0151 g, done, edge_centrality, get(vertex_index, g));
0152 }
0153
0154
0155
0156
0157 template < typename MutableGraph, typename Done >
0158 void betweenness_centrality_clustering(MutableGraph& g, Done done)
0159 {
0160 typedef typename Done::centrality_type centrality_type;
0161 std::vector< centrality_type > edge_centrality(num_edges(g));
0162 betweenness_centrality_clustering(g, done,
0163 make_iterator_property_map(edge_centrality.begin(), get(edge_index, g)),
0164 get(vertex_index, g));
0165 }
0166
0167 }
0168
0169 #endif