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_BRANDES_BETWEENNESS_CENTRALITY_HPP sl@0: #define BOOST_GRAPH_BRANDES_BETWEENNESS_CENTRALITY_HPP sl@0: sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: #include 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: namespace detail { namespace graph { sl@0: sl@0: /** sl@0: * Customized visitor passed to Dijkstra's algorithm by Brandes' sl@0: * betweenness centrality algorithm. This visitor is responsible for sl@0: * keeping track of the order in which vertices are discovered, the sl@0: * predecessors on the shortest path(s) to a vertex, and the number sl@0: * of shortest paths. sl@0: */ sl@0: template sl@0: struct brandes_dijkstra_visitor : public bfs_visitor<> sl@0: { sl@0: typedef typename graph_traits::vertex_descriptor vertex_descriptor; sl@0: typedef typename graph_traits::edge_descriptor edge_descriptor; sl@0: sl@0: brandes_dijkstra_visitor(std::stack& ordered_vertices, sl@0: WeightMap weight, sl@0: IncomingMap incoming, sl@0: DistanceMap distance, sl@0: PathCountMap path_count) sl@0: : ordered_vertices(ordered_vertices), weight(weight), sl@0: incoming(incoming), distance(distance), sl@0: path_count(path_count) sl@0: { } sl@0: sl@0: /** sl@0: * Whenever an edge e = (v, w) is relaxed, the incoming edge list sl@0: * for w is set to {(v, w)} and the shortest path count of w is set to sl@0: * the number of paths that reach {v}. sl@0: */ sl@0: void edge_relaxed(edge_descriptor e, const Graph& g) sl@0: { sl@0: vertex_descriptor v = source(e, g), w = target(e, g); sl@0: incoming[w].clear(); sl@0: incoming[w].push_back(e); sl@0: put(path_count, w, get(path_count, v)); sl@0: } sl@0: sl@0: /** sl@0: * If an edge e = (v, w) was not relaxed, it may still be the case sl@0: * that we've found more equally-short paths, so include {(v, w)} in the sl@0: * incoming edges of w and add all of the shortest paths to v to the sl@0: * shortest path count of w. sl@0: */ sl@0: void edge_not_relaxed(edge_descriptor e, const Graph& g) sl@0: { sl@0: typedef typename property_traits::value_type weight_type; sl@0: typedef typename property_traits::value_type distance_type; sl@0: vertex_descriptor v = source(e, g), w = target(e, g); sl@0: distance_type d_v = get(distance, v), d_w = get(distance, w); sl@0: weight_type w_e = get(weight, e); sl@0: sl@0: closed_plus combine; sl@0: if (d_w == combine(d_v, w_e)) { sl@0: put(path_count, w, get(path_count, w) + get(path_count, v)); sl@0: incoming[w].push_back(e); sl@0: } sl@0: } sl@0: sl@0: /// Keep track of vertices as they are reached sl@0: void examine_vertex(vertex_descriptor w, const Graph&) sl@0: { sl@0: ordered_vertices.push(w); sl@0: } sl@0: sl@0: private: sl@0: std::stack& ordered_vertices; sl@0: WeightMap weight; sl@0: IncomingMap incoming; sl@0: DistanceMap distance; sl@0: PathCountMap path_count; sl@0: }; sl@0: sl@0: /** sl@0: * Function object that calls Dijkstra's shortest paths algorithm sl@0: * using the Dijkstra visitor for the Brandes betweenness centrality sl@0: * algorithm. sl@0: */ sl@0: template sl@0: struct brandes_dijkstra_shortest_paths sl@0: { sl@0: brandes_dijkstra_shortest_paths(WeightMap weight_map) sl@0: : weight_map(weight_map) { } sl@0: sl@0: template sl@0: void sl@0: operator()(Graph& g, sl@0: typename graph_traits::vertex_descriptor s, sl@0: std::stack::vertex_descriptor>& ov, sl@0: IncomingMap incoming, sl@0: DistanceMap distance, sl@0: PathCountMap path_count, sl@0: VertexIndexMap vertex_index) sl@0: { sl@0: typedef brandes_dijkstra_visitor visitor_type; sl@0: visitor_type visitor(ov, weight_map, incoming, distance, path_count); sl@0: sl@0: dijkstra_shortest_paths(g, s, sl@0: boost::weight_map(weight_map) sl@0: .vertex_index_map(vertex_index) sl@0: .distance_map(distance) sl@0: .visitor(visitor)); sl@0: } sl@0: sl@0: private: sl@0: WeightMap weight_map; sl@0: }; sl@0: sl@0: /** sl@0: * Function object that invokes breadth-first search for the sl@0: * unweighted form of the Brandes betweenness centrality algorithm. sl@0: */ sl@0: struct brandes_unweighted_shortest_paths sl@0: { sl@0: /** sl@0: * Customized visitor passed to breadth-first search, which sl@0: * records predecessor and the number of shortest paths to each sl@0: * vertex. sl@0: */ sl@0: template sl@0: struct visitor_type : public bfs_visitor<> sl@0: { sl@0: typedef typename graph_traits::edge_descriptor edge_descriptor; sl@0: typedef typename graph_traits::vertex_descriptor sl@0: vertex_descriptor; sl@0: sl@0: visitor_type(IncomingMap incoming, DistanceMap distance, sl@0: PathCountMap path_count, sl@0: std::stack& ordered_vertices) sl@0: : incoming(incoming), distance(distance), sl@0: path_count(path_count), ordered_vertices(ordered_vertices) { } sl@0: sl@0: /// Keep track of vertices as they are reached sl@0: void examine_vertex(vertex_descriptor v, Graph&) sl@0: { sl@0: ordered_vertices.push(v); sl@0: } sl@0: sl@0: /** sl@0: * Whenever an edge e = (v, w) is labelled a tree edge, the sl@0: * incoming edge list for w is set to {(v, w)} and the shortest sl@0: * path count of w is set to the number of paths that reach {v}. sl@0: */ sl@0: void tree_edge(edge_descriptor e, Graph& g) sl@0: { sl@0: vertex_descriptor v = source(e, g); sl@0: vertex_descriptor w = target(e, g); sl@0: put(distance, w, get(distance, v) + 1); sl@0: sl@0: put(path_count, w, get(path_count, v)); sl@0: incoming[w].push_back(e); sl@0: } sl@0: sl@0: /** sl@0: * If an edge e = (v, w) is not a tree edge, it may still be the sl@0: * case that we've found more equally-short paths, so include (v, w) sl@0: * in the incoming edge list of w and add all of the shortest sl@0: * paths to v to the shortest path count of w. sl@0: */ sl@0: void non_tree_edge(edge_descriptor e, Graph& g) sl@0: { sl@0: vertex_descriptor v = source(e, g); sl@0: vertex_descriptor w = target(e, g); sl@0: if (get(distance, w) == get(distance, v) + 1) { sl@0: put(path_count, w, get(path_count, w) + get(path_count, v)); sl@0: incoming[w].push_back(e); sl@0: } sl@0: } sl@0: sl@0: private: sl@0: IncomingMap incoming; sl@0: DistanceMap distance; sl@0: PathCountMap path_count; sl@0: std::stack& ordered_vertices; sl@0: }; sl@0: sl@0: template sl@0: void sl@0: operator()(Graph& g, sl@0: typename graph_traits::vertex_descriptor s, sl@0: std::stack::vertex_descriptor>& ov, sl@0: IncomingMap incoming, sl@0: DistanceMap distance, sl@0: PathCountMap path_count, sl@0: VertexIndexMap vertex_index) sl@0: { sl@0: typedef typename graph_traits::vertex_descriptor sl@0: vertex_descriptor; sl@0: sl@0: visitor_type sl@0: visitor(incoming, distance, path_count, ov); sl@0: sl@0: std::vector sl@0: colors(num_vertices(g), color_traits::white()); sl@0: boost::queue Q; sl@0: breadth_first_visit(g, s, Q, visitor, sl@0: make_iterator_property_map(colors.begin(), sl@0: vertex_index)); sl@0: } sl@0: }; sl@0: sl@0: // When the edge centrality map is a dummy property map, no sl@0: // initialization is needed. sl@0: template sl@0: inline void sl@0: init_centrality_map(std::pair, dummy_property_map) { } sl@0: sl@0: // When we have a real edge centrality map, initialize all of the sl@0: // centralities to zero. sl@0: template sl@0: void sl@0: init_centrality_map(std::pair keys, Centrality centrality_map) sl@0: { sl@0: typedef typename property_traits::value_type sl@0: centrality_type; sl@0: while (keys.first != keys.second) { sl@0: put(centrality_map, *keys.first, centrality_type(0)); sl@0: ++keys.first; sl@0: } sl@0: } sl@0: sl@0: // When the edge centrality map is a dummy property map, no update sl@0: // is performed. sl@0: template sl@0: inline void sl@0: update_centrality(dummy_property_map, const Key&, const T&) { } sl@0: sl@0: // When we have a real edge centrality map, add the value to the map sl@0: template sl@0: inline void sl@0: update_centrality(CentralityMap centrality_map, Key k, const T& x) sl@0: { put(centrality_map, k, get(centrality_map, k) + x); } sl@0: sl@0: template sl@0: inline void sl@0: divide_centrality_by_two(std::pair, dummy_property_map) {} sl@0: sl@0: template sl@0: inline void sl@0: divide_centrality_by_two(std::pair keys, sl@0: CentralityMap centrality_map) sl@0: { sl@0: typename property_traits::value_type two(2); sl@0: while (keys.first != keys.second) { sl@0: put(centrality_map, *keys.first, get(centrality_map, *keys.first) / two); sl@0: ++keys.first; sl@0: } sl@0: } sl@0: sl@0: template sl@0: void sl@0: brandes_betweenness_centrality_impl(const Graph& g, sl@0: CentralityMap centrality, // C_B sl@0: EdgeCentralityMap edge_centrality_map, sl@0: IncomingMap incoming, // P sl@0: DistanceMap distance, // d sl@0: DependencyMap dependency, // delta sl@0: PathCountMap path_count, // sigma sl@0: VertexIndexMap vertex_index, sl@0: ShortestPaths shortest_paths) sl@0: { sl@0: typedef typename graph_traits::vertex_iterator vertex_iterator; sl@0: typedef typename graph_traits::edge_iterator edge_iterator; sl@0: typedef typename graph_traits::vertex_descriptor vertex_descriptor; sl@0: sl@0: // Initialize centrality sl@0: init_centrality_map(vertices(g), centrality); sl@0: init_centrality_map(edges(g), edge_centrality_map); sl@0: sl@0: std::stack ordered_vertices; sl@0: vertex_iterator s, s_end; sl@0: for (tie(s, s_end) = vertices(g); s != s_end; ++s) { sl@0: // Initialize for this iteration sl@0: vertex_iterator w, w_end; sl@0: for (tie(w, w_end) = vertices(g); w != w_end; ++w) { sl@0: incoming[*w].clear(); sl@0: put(path_count, *w, 0); sl@0: put(dependency, *w, 0); sl@0: } sl@0: put(path_count, *s, 1); sl@0: sl@0: // Execute the shortest paths algorithm. This will be either sl@0: // Dijkstra's algorithm or a customized breadth-first search, sl@0: // depending on whether the graph is weighted or unweighted. sl@0: shortest_paths(g, *s, ordered_vertices, incoming, distance, sl@0: path_count, vertex_index); sl@0: sl@0: while (!ordered_vertices.empty()) { sl@0: vertex_descriptor w = ordered_vertices.top(); sl@0: ordered_vertices.pop(); sl@0: sl@0: typedef typename property_traits::value_type sl@0: incoming_type; sl@0: typedef typename incoming_type::iterator incoming_iterator; sl@0: typedef typename property_traits::value_type sl@0: dependency_type; sl@0: sl@0: for (incoming_iterator vw = incoming[w].begin(); sl@0: vw != incoming[w].end(); ++vw) { sl@0: vertex_descriptor v = source(*vw, g); sl@0: dependency_type factor = dependency_type(get(path_count, v)) sl@0: / dependency_type(get(path_count, w)); sl@0: factor *= (dependency_type(1) + get(dependency, w)); sl@0: put(dependency, v, get(dependency, v) + factor); sl@0: update_centrality(edge_centrality_map, *vw, factor); sl@0: } sl@0: sl@0: if (w != *s) { sl@0: update_centrality(centrality, w, get(dependency, w)); sl@0: } sl@0: } sl@0: } sl@0: sl@0: typedef typename graph_traits::directed_category directed_category; sl@0: const bool is_undirected = sl@0: is_convertible::value; sl@0: if (is_undirected) { sl@0: divide_centrality_by_two(vertices(g), centrality); sl@0: divide_centrality_by_two(edges(g), edge_centrality_map); sl@0: } sl@0: } sl@0: sl@0: } } // end namespace detail::graph sl@0: sl@0: template sl@0: void sl@0: brandes_betweenness_centrality(const Graph& g, sl@0: CentralityMap centrality, // C_B sl@0: EdgeCentralityMap edge_centrality_map, sl@0: IncomingMap incoming, // P sl@0: DistanceMap distance, // d sl@0: DependencyMap dependency, // delta sl@0: PathCountMap path_count, // sigma sl@0: VertexIndexMap vertex_index) sl@0: { sl@0: detail::graph::brandes_unweighted_shortest_paths shortest_paths; sl@0: sl@0: detail::graph::brandes_betweenness_centrality_impl(g, centrality, sl@0: edge_centrality_map, sl@0: incoming, distance, sl@0: dependency, path_count, sl@0: vertex_index, sl@0: shortest_paths); sl@0: } sl@0: sl@0: template sl@0: void sl@0: brandes_betweenness_centrality(const Graph& g, sl@0: CentralityMap centrality, // C_B sl@0: EdgeCentralityMap edge_centrality_map, sl@0: IncomingMap incoming, // P sl@0: DistanceMap distance, // d sl@0: DependencyMap dependency, // delta sl@0: PathCountMap path_count, // sigma sl@0: VertexIndexMap vertex_index, sl@0: WeightMap weight_map) sl@0: { sl@0: detail::graph::brandes_dijkstra_shortest_paths sl@0: shortest_paths(weight_map); sl@0: sl@0: detail::graph::brandes_betweenness_centrality_impl(g, centrality, sl@0: edge_centrality_map, sl@0: incoming, distance, sl@0: dependency, path_count, sl@0: vertex_index, sl@0: shortest_paths); sl@0: } sl@0: sl@0: namespace detail { namespace graph { sl@0: template sl@0: void sl@0: brandes_betweenness_centrality_dispatch2(const Graph& g, sl@0: CentralityMap centrality, sl@0: EdgeCentralityMap edge_centrality_map, sl@0: WeightMap weight_map, sl@0: VertexIndexMap vertex_index) sl@0: { sl@0: typedef typename graph_traits::degree_size_type degree_size_type; sl@0: typedef typename graph_traits::vertex_descriptor vertex_descriptor; sl@0: typedef typename graph_traits::edge_descriptor edge_descriptor; sl@0: typedef typename mpl::if_c<(is_same::value), sl@0: EdgeCentralityMap, sl@0: CentralityMap>::type a_centrality_map; sl@0: typedef typename property_traits::value_type sl@0: centrality_type; sl@0: sl@0: typename graph_traits::vertices_size_type V = num_vertices(g); sl@0: sl@0: std::vector > incoming(V); sl@0: std::vector distance(V); sl@0: std::vector dependency(V); sl@0: std::vector path_count(V); sl@0: sl@0: brandes_betweenness_centrality( sl@0: g, centrality, edge_centrality_map, sl@0: make_iterator_property_map(incoming.begin(), vertex_index), sl@0: make_iterator_property_map(distance.begin(), vertex_index), sl@0: make_iterator_property_map(dependency.begin(), vertex_index), sl@0: make_iterator_property_map(path_count.begin(), vertex_index), sl@0: vertex_index, sl@0: weight_map); sl@0: } sl@0: sl@0: sl@0: template sl@0: void sl@0: brandes_betweenness_centrality_dispatch2(const Graph& g, sl@0: CentralityMap centrality, sl@0: EdgeCentralityMap edge_centrality_map, sl@0: VertexIndexMap vertex_index) sl@0: { sl@0: typedef typename graph_traits::degree_size_type degree_size_type; sl@0: typedef typename graph_traits::vertex_descriptor vertex_descriptor; sl@0: typedef typename graph_traits::edge_descriptor edge_descriptor; sl@0: typedef typename mpl::if_c<(is_same::value), sl@0: EdgeCentralityMap, sl@0: CentralityMap>::type a_centrality_map; sl@0: typedef typename property_traits::value_type sl@0: centrality_type; sl@0: sl@0: typename graph_traits::vertices_size_type V = num_vertices(g); sl@0: sl@0: std::vector > incoming(V); sl@0: std::vector distance(V); sl@0: std::vector dependency(V); sl@0: std::vector path_count(V); sl@0: sl@0: brandes_betweenness_centrality( sl@0: g, centrality, edge_centrality_map, sl@0: make_iterator_property_map(incoming.begin(), vertex_index), sl@0: make_iterator_property_map(distance.begin(), vertex_index), sl@0: make_iterator_property_map(dependency.begin(), vertex_index), sl@0: make_iterator_property_map(path_count.begin(), vertex_index), sl@0: vertex_index); sl@0: } sl@0: sl@0: template sl@0: struct brandes_betweenness_centrality_dispatch1 sl@0: { sl@0: template sl@0: static void sl@0: run(const Graph& g, CentralityMap centrality, sl@0: EdgeCentralityMap edge_centrality_map, VertexIndexMap vertex_index, sl@0: WeightMap weight_map) sl@0: { sl@0: brandes_betweenness_centrality_dispatch2(g, centrality, edge_centrality_map, sl@0: weight_map, vertex_index); sl@0: } sl@0: }; sl@0: sl@0: template<> sl@0: struct brandes_betweenness_centrality_dispatch1 sl@0: { sl@0: template sl@0: static void sl@0: run(const Graph& g, CentralityMap centrality, sl@0: EdgeCentralityMap edge_centrality_map, VertexIndexMap vertex_index, sl@0: error_property_not_found) sl@0: { sl@0: brandes_betweenness_centrality_dispatch2(g, centrality, edge_centrality_map, sl@0: vertex_index); sl@0: } sl@0: }; sl@0: sl@0: } } // end namespace detail::graph sl@0: sl@0: template sl@0: void sl@0: brandes_betweenness_centrality(const Graph& g, sl@0: const bgl_named_params& params) sl@0: { sl@0: typedef bgl_named_params named_params; sl@0: sl@0: typedef typename property_value::type ew; sl@0: detail::graph::brandes_betweenness_centrality_dispatch1::run( sl@0: g, sl@0: choose_param(get_param(params, vertex_centrality), sl@0: dummy_property_map()), sl@0: choose_param(get_param(params, edge_centrality), sl@0: dummy_property_map()), sl@0: choose_const_pmap(get_param(params, vertex_index), g, vertex_index), sl@0: get_param(params, edge_weight)); sl@0: } sl@0: sl@0: template sl@0: void sl@0: brandes_betweenness_centrality(const Graph& g, CentralityMap centrality) sl@0: { sl@0: detail::graph::brandes_betweenness_centrality_dispatch2( sl@0: g, centrality, dummy_property_map(), get(vertex_index, g)); sl@0: } sl@0: sl@0: template sl@0: void sl@0: brandes_betweenness_centrality(const Graph& g, CentralityMap centrality, sl@0: EdgeCentralityMap edge_centrality_map) sl@0: { sl@0: detail::graph::brandes_betweenness_centrality_dispatch2( sl@0: g, centrality, edge_centrality_map, get(vertex_index, g)); sl@0: } sl@0: sl@0: /** sl@0: * Converts "absolute" betweenness centrality (as computed by the sl@0: * brandes_betweenness_centrality algorithm) in the centrality map sl@0: * into "relative" centrality. The result is placed back into the sl@0: * given centrality map. sl@0: */ sl@0: template sl@0: void sl@0: relative_betweenness_centrality(const Graph& g, CentralityMap centrality) sl@0: { sl@0: typedef typename graph_traits::vertex_iterator vertex_iterator; sl@0: typedef typename property_traits::value_type centrality_type; sl@0: sl@0: typename graph_traits::vertices_size_type n = num_vertices(g); sl@0: centrality_type factor = centrality_type(2)/centrality_type(n*n - 3*n + 2); sl@0: vertex_iterator v, v_end; sl@0: for (tie(v, v_end) = vertices(g); v != v_end; ++v) { sl@0: put(centrality, *v, factor * get(centrality, *v)); sl@0: } sl@0: } sl@0: sl@0: // Compute the central point dominance of a graph. sl@0: template sl@0: typename property_traits::value_type sl@0: central_point_dominance(const Graph& g, CentralityMap centrality) sl@0: { sl@0: using std::max; sl@0: sl@0: typedef typename graph_traits::vertex_iterator vertex_iterator; sl@0: typedef typename property_traits::value_type centrality_type; sl@0: sl@0: typename graph_traits::vertices_size_type n = num_vertices(g); sl@0: sl@0: // Find max centrality sl@0: centrality_type max_centrality(0); sl@0: vertex_iterator v, v_end; sl@0: for (tie(v, v_end) = vertices(g); v != v_end; ++v) { sl@0: max_centrality = (max)(max_centrality, get(centrality, *v)); sl@0: } sl@0: sl@0: // Compute central point dominance sl@0: centrality_type sum(0); sl@0: for (tie(v, v_end) = vertices(g); v != v_end; ++v) { sl@0: sum += (max_centrality - get(centrality, *v)); sl@0: } sl@0: return sum/(n-1); sl@0: } sl@0: sl@0: } // end namespace boost sl@0: sl@0: #endif // BOOST_GRAPH_BRANDES_BETWEENNESS_CENTRALITY_HPP