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