Chris@16: // Copyright 2004 The Trustees of Indiana University. Chris@16: Chris@16: // Distributed under the Boost Software License, Version 1.0. Chris@16: // (See accompanying file LICENSE_1_0.txt or copy at Chris@16: // http://www.boost.org/LICENSE_1_0.txt) Chris@16: Chris@16: // Authors: Douglas Gregor Chris@16: // Andrew Lumsdaine Chris@16: #ifndef BOOST_GRAPH_BETWEENNESS_CENTRALITY_CLUSTERING_HPP Chris@16: #define BOOST_GRAPH_BETWEENNESS_CENTRALITY_CLUSTERING_HPP Chris@16: Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: Chris@16: namespace boost { Chris@16: Chris@16: /** Threshold termination function for the betweenness centrality Chris@16: * clustering algorithm. Chris@16: */ Chris@16: template Chris@16: struct bc_clustering_threshold Chris@16: { Chris@16: typedef T centrality_type; Chris@16: Chris@16: /// Terminate clustering when maximum absolute edge centrality is Chris@16: /// below the given threshold. Chris@16: explicit bc_clustering_threshold(T threshold) Chris@16: : threshold(threshold), dividend(1.0) {} Chris@16: Chris@16: /** Chris@16: * Terminate clustering when the maximum edge centrality is below Chris@16: * the given threshold. Chris@16: * Chris@16: * @param threshold the threshold value Chris@16: * Chris@16: * @param g the graph on which the threshold will be calculated Chris@16: * Chris@16: * @param normalize when true, the threshold is compared against the Chris@16: * normalized edge centrality based on the input graph; otherwise, Chris@16: * the threshold is compared against the absolute edge centrality. Chris@16: */ Chris@16: template Chris@16: bc_clustering_threshold(T threshold, const Graph& g, bool normalize = true) Chris@16: : threshold(threshold), dividend(1.0) Chris@16: { Chris@16: if (normalize) { Chris@16: typename graph_traits::vertices_size_type n = num_vertices(g); Chris@16: dividend = T((n - 1) * (n - 2)) / T(2); Chris@16: } Chris@16: } Chris@16: Chris@16: /** Returns true when the given maximum edge centrality (potentially Chris@16: * normalized) falls below the threshold. Chris@16: */ Chris@16: template Chris@16: bool operator()(T max_centrality, Edge, const Graph&) Chris@16: { Chris@16: return (max_centrality / dividend) < threshold; Chris@16: } Chris@16: Chris@16: protected: Chris@16: T threshold; Chris@16: T dividend; Chris@16: }; Chris@16: Chris@16: /** Graph clustering based on edge betweenness centrality. Chris@16: * Chris@16: * This algorithm implements graph clustering based on edge Chris@16: * betweenness centrality. It is an iterative algorithm, where in each Chris@16: * step it compute the edge betweenness centrality (via @ref Chris@16: * brandes_betweenness_centrality) and removes the edge with the Chris@16: * maximum betweenness centrality. The @p done function object Chris@16: * determines when the algorithm terminates (the edge found when the Chris@16: * algorithm terminates will not be removed). Chris@16: * Chris@16: * @param g The graph on which clustering will be performed. The type Chris@16: * of this parameter (@c MutableGraph) must be a model of the Chris@16: * VertexListGraph, IncidenceGraph, EdgeListGraph, and Mutable Graph Chris@16: * concepts. Chris@16: * Chris@16: * @param done The function object that indicates termination of the Chris@16: * algorithm. It must be a ternary function object thats accepts the Chris@16: * maximum centrality, the descriptor of the edge that will be Chris@16: * removed, and the graph @p g. Chris@16: * Chris@16: * @param edge_centrality (UTIL/OUT) The property map that will store Chris@16: * the betweenness centrality for each edge. When the algorithm Chris@16: * terminates, it will contain the edge centralities for the Chris@16: * graph. The type of this property map must model the Chris@16: * ReadWritePropertyMap concept. Defaults to an @c Chris@16: * iterator_property_map whose value type is Chris@16: * @c Done::centrality_type and using @c get(edge_index, g) for the Chris@16: * index map. Chris@16: * Chris@16: * @param vertex_index (IN) The property map that maps vertices to Chris@16: * indices in the range @c [0, num_vertices(g)). This type of this Chris@16: * property map must model the ReadablePropertyMap concept and its Chris@16: * value type must be an integral type. Defaults to Chris@16: * @c get(vertex_index, g). Chris@16: */ Chris@16: template Chris@16: void Chris@16: betweenness_centrality_clustering(MutableGraph& g, Done done, Chris@16: EdgeCentralityMap edge_centrality, Chris@16: VertexIndexMap vertex_index) Chris@16: { Chris@16: typedef typename property_traits::value_type Chris@16: centrality_type; Chris@16: typedef typename graph_traits::edge_iterator edge_iterator; Chris@16: typedef typename graph_traits::edge_descriptor edge_descriptor; Chris@16: typedef typename graph_traits::vertices_size_type Chris@16: vertices_size_type; Chris@16: Chris@16: if (has_no_edges(g)) return; Chris@16: Chris@16: // Function object that compares the centrality of edges Chris@16: indirect_cmp > Chris@16: cmp(edge_centrality); Chris@16: Chris@16: bool is_done; Chris@16: do { Chris@16: brandes_betweenness_centrality(g, Chris@16: edge_centrality_map(edge_centrality) Chris@16: .vertex_index_map(vertex_index)); Chris@16: std::pair edges_iters = edges(g); Chris@16: edge_descriptor e = *max_element(edges_iters.first, edges_iters.second, cmp); Chris@16: is_done = done(get(edge_centrality, e), e, g); Chris@16: if (!is_done) remove_edge(e, g); Chris@16: } while (!is_done && !has_no_edges(g)); Chris@16: } Chris@16: Chris@16: /** Chris@16: * \overload Chris@16: */ Chris@16: template Chris@16: void Chris@16: betweenness_centrality_clustering(MutableGraph& g, Done done, Chris@16: EdgeCentralityMap edge_centrality) Chris@16: { Chris@16: betweenness_centrality_clustering(g, done, edge_centrality, Chris@16: get(vertex_index, g)); Chris@16: } Chris@16: Chris@16: /** Chris@16: * \overload Chris@16: */ Chris@16: template Chris@16: void Chris@16: betweenness_centrality_clustering(MutableGraph& g, Done done) Chris@16: { Chris@16: typedef typename Done::centrality_type centrality_type; Chris@16: std::vector edge_centrality(num_edges(g)); Chris@16: betweenness_centrality_clustering(g, done, Chris@16: make_iterator_property_map(edge_centrality.begin(), get(edge_index, g)), Chris@16: get(vertex_index, g)); Chris@16: } Chris@16: Chris@16: } // end namespace boost Chris@16: Chris@16: #endif // BOOST_GRAPH_BETWEENNESS_CENTRALITY_CLUSTERING_HPP