123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169 |
- #ifndef BOOST_GRAPH_BETWEENNESS_CENTRALITY_CLUSTERING_HPP
- #define BOOST_GRAPH_BETWEENNESS_CENTRALITY_CLUSTERING_HPP
- #include <boost/algorithm/minmax_element.hpp>
- #include <boost/graph/betweenness_centrality.hpp>
- #include <boost/graph/graph_traits.hpp>
- #include <boost/graph/graph_utility.hpp>
- #include <boost/pending/indirect_cmp.hpp>
- #include <vector>
- #include <boost/property_map/property_map.hpp>
- namespace boost
- {
- template < typename T > struct bc_clustering_threshold
- {
- typedef T centrality_type;
-
-
- explicit bc_clustering_threshold(T threshold)
- : threshold(threshold), dividend(1.0)
- {
- }
-
- template < typename Graph >
- bc_clustering_threshold(T threshold, const Graph& g, bool normalize = true)
- : threshold(threshold), dividend(1.0)
- {
- if (normalize)
- {
- typename graph_traits< Graph >::vertices_size_type n
- = num_vertices(g);
- dividend = T((n - 1) * (n - 2)) / T(2);
- }
- }
-
- template < typename Graph, typename Edge >
- bool operator()(T max_centrality, Edge, const Graph&)
- {
- return (max_centrality / dividend) < threshold;
- }
- protected:
- T threshold;
- T dividend;
- };
- template < typename MutableGraph, typename Done, typename EdgeCentralityMap,
- typename VertexIndexMap >
- void betweenness_centrality_clustering(MutableGraph& g, Done done,
- EdgeCentralityMap edge_centrality, VertexIndexMap vertex_index)
- {
- typedef typename property_traits< EdgeCentralityMap >::value_type
- centrality_type;
- typedef typename graph_traits< MutableGraph >::edge_iterator edge_iterator;
- typedef
- typename graph_traits< MutableGraph >::edge_descriptor edge_descriptor;
- if (has_no_edges(g))
- return;
-
- indirect_cmp< EdgeCentralityMap, std::less< centrality_type > > cmp(
- edge_centrality);
- bool is_done;
- do
- {
- brandes_betweenness_centrality(g,
- edge_centrality_map(edge_centrality)
- .vertex_index_map(vertex_index));
- std::pair< edge_iterator, edge_iterator > edges_iters = edges(g);
- edge_descriptor e
- = *boost::first_max_element(edges_iters.first, edges_iters.second, cmp);
- is_done = done(get(edge_centrality, e), e, g);
- if (!is_done)
- remove_edge(e, g);
- } while (!is_done && !has_no_edges(g));
- }
- template < typename MutableGraph, typename Done, typename EdgeCentralityMap >
- void betweenness_centrality_clustering(
- MutableGraph& g, Done done, EdgeCentralityMap edge_centrality)
- {
- betweenness_centrality_clustering(
- g, done, edge_centrality, get(vertex_index, g));
- }
- template < typename MutableGraph, typename Done >
- void betweenness_centrality_clustering(MutableGraph& g, Done done)
- {
- typedef typename Done::centrality_type centrality_type;
- std::vector< centrality_type > edge_centrality(num_edges(g));
- betweenness_centrality_clustering(g, done,
- make_iterator_property_map(edge_centrality.begin(), get(edge_index, g)),
- get(vertex_index, g));
- }
- }
- #endif
|