⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 betweenness_centrality.hpp

📁 CGAL is a collaborative effort of several sites in Europe and Israel. The goal is to make the most i
💻 HPP
📖 第 1 页 / 共 2 页
字号:
// Copyright 2004 The Trustees of Indiana University.// Use, modification and distribution is subject to the Boost Software// License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at// http://www.boost.org/LICENSE_1_0.txt)//  Authors: Douglas Gregor//           Andrew Lumsdaine#ifndef BOOST_GRAPH_BRANDES_BETWEENNESS_CENTRALITY_HPP#define BOOST_GRAPH_BRANDES_BETWEENNESS_CENTRALITY_HPP#include <stack>#include <vector>#include <boost/graph/dijkstra_shortest_paths.hpp>#include <boost/graph/breadth_first_search.hpp>#include <boost/graph/relax.hpp>#include <boost/graph/graph_traits.hpp>#include <boost/tuple/tuple.hpp>#include <boost/type_traits/is_convertible.hpp>#include <boost/type_traits/is_same.hpp>#include <boost/mpl/if.hpp>#include <boost/property_map.hpp>#include <boost/graph/named_function_params.hpp>#include <algorithm>namespace boost {namespace detail { namespace graph {  /**   * Customized visitor passed to Dijkstra's algorithm by Brandes'   * betweenness centrality algorithm. This visitor is responsible for   * keeping track of the order in which vertices are discovered, the   * predecessors on the shortest path(s) to a vertex, and the number   * of shortest paths.   */  template<typename Graph, typename WeightMap, typename IncomingMap,           typename DistanceMap, typename PathCountMap>  struct brandes_dijkstra_visitor : public bfs_visitor<>  {    typedef typename graph_traits<Graph>::vertex_descriptor vertex_descriptor;    typedef typename graph_traits<Graph>::edge_descriptor edge_descriptor;    brandes_dijkstra_visitor(std::stack<vertex_descriptor>& ordered_vertices,                             WeightMap weight,                             IncomingMap incoming,                             DistanceMap distance,                             PathCountMap path_count)      : ordered_vertices(ordered_vertices), weight(weight),         incoming(incoming), distance(distance),        path_count(path_count)    { }    /**     * Whenever an edge e = (v, w) is relaxed, the incoming edge list     * for w is set to {(v, w)} and the shortest path count of w is set to     * the number of paths that reach {v}.     */    void edge_relaxed(edge_descriptor e, const Graph& g)     {       vertex_descriptor v = source(e, g), w = target(e, g);      incoming[w].clear();      incoming[w].push_back(e);      put(path_count, w, get(path_count, v));    }    /**     * If an edge e = (v, w) was not relaxed, it may still be the case     * that we've found more equally-short paths, so include {(v, w)} in the     * incoming edges of w and add all of the shortest paths to v to the     * shortest path count of w.     */    void edge_not_relaxed(edge_descriptor e, const Graph& g)     {      typedef typename property_traits<WeightMap>::value_type weight_type;      typedef typename property_traits<DistanceMap>::value_type distance_type;      vertex_descriptor v = source(e, g), w = target(e, g);      distance_type d_v = get(distance, v), d_w = get(distance, w);      weight_type w_e = get(weight, e);      closed_plus<distance_type> combine;      if (d_w == combine(d_v, w_e)) {        put(path_count, w, get(path_count, w) + get(path_count, v));        incoming[w].push_back(e);      }    }    /// Keep track of vertices as they are reached    void examine_vertex(vertex_descriptor w, const Graph&)     {       ordered_vertices.push(w);    }  private:    std::stack<vertex_descriptor>& ordered_vertices;    WeightMap weight;    IncomingMap incoming;    DistanceMap distance;    PathCountMap path_count;  };  /**   * Function object that calls Dijkstra's shortest paths algorithm   * using the Dijkstra visitor for the Brandes betweenness centrality   * algorithm.   */  template<typename WeightMap>  struct brandes_dijkstra_shortest_paths  {    brandes_dijkstra_shortest_paths(WeightMap weight_map)       : weight_map(weight_map) { }    template<typename Graph, typename IncomingMap, typename DistanceMap,              typename PathCountMap, typename VertexIndexMap>    void     operator()(Graph& g,                typename graph_traits<Graph>::vertex_descriptor s,               std::stack<typename graph_traits<Graph>::vertex_descriptor>& ov,               IncomingMap incoming,               DistanceMap distance,               PathCountMap path_count,               VertexIndexMap vertex_index)    {      typedef brandes_dijkstra_visitor<Graph, WeightMap, IncomingMap,                                        DistanceMap, PathCountMap> visitor_type;      visitor_type visitor(ov, weight_map, incoming, distance, path_count);      dijkstra_shortest_paths(g, s,                               boost::weight_map(weight_map)                              .vertex_index_map(vertex_index)                              .distance_map(distance)                              .visitor(visitor));    }  private:    WeightMap weight_map;  };  /**   * Function object that invokes breadth-first search for the   * unweighted form of the Brandes betweenness centrality algorithm.   */  struct brandes_unweighted_shortest_paths  {    /**     * Customized visitor passed to breadth-first search, which     * records predecessor and the number of shortest paths to each     * vertex.     */    template<typename Graph, typename IncomingMap, typename DistanceMap,              typename PathCountMap>    struct visitor_type : public bfs_visitor<>    {      typedef typename graph_traits<Graph>::edge_descriptor edge_descriptor;      typedef typename graph_traits<Graph>::vertex_descriptor         vertex_descriptor;            visitor_type(IncomingMap incoming, DistanceMap distance,                    PathCountMap path_count,                    std::stack<vertex_descriptor>& ordered_vertices)        : incoming(incoming), distance(distance),           path_count(path_count), ordered_vertices(ordered_vertices) { }      /// Keep track of vertices as they are reached      void examine_vertex(vertex_descriptor v, Graph&)      {        ordered_vertices.push(v);      }      /**       * Whenever an edge e = (v, w) is labelled a tree edge, the       * incoming edge list for w is set to {(v, w)} and the shortest       * path count of w is set to the number of paths that reach {v}.       */      void tree_edge(edge_descriptor e, Graph& g)      {        vertex_descriptor v = source(e, g);        vertex_descriptor w = target(e, g);        put(distance, w, get(distance, v) + 1);                put(path_count, w, get(path_count, v));        incoming[w].push_back(e);      }      /**       * If an edge e = (v, w) is not a tree edge, it may still be the       * case that we've found more equally-short paths, so include (v, w)       * in the incoming edge list of w and add all of the shortest       * paths to v to the shortest path count of w.       */      void non_tree_edge(edge_descriptor e, Graph& g)      {        vertex_descriptor v = source(e, g);        vertex_descriptor w = target(e, g);        if (get(distance, w) == get(distance, v) + 1) {          put(path_count, w, get(path_count, w) + get(path_count, v));          incoming[w].push_back(e);        }      }    private:      IncomingMap incoming;      DistanceMap distance;      PathCountMap path_count;      std::stack<vertex_descriptor>& ordered_vertices;    };    template<typename Graph, typename IncomingMap, typename DistanceMap,              typename PathCountMap, typename VertexIndexMap>    void     operator()(Graph& g,                typename graph_traits<Graph>::vertex_descriptor s,               std::stack<typename graph_traits<Graph>::vertex_descriptor>& ov,               IncomingMap incoming,               DistanceMap distance,               PathCountMap path_count,               VertexIndexMap vertex_index)    {      typedef typename graph_traits<Graph>::vertex_descriptor        vertex_descriptor;      visitor_type<Graph, IncomingMap, DistanceMap, PathCountMap>        visitor(incoming, distance, path_count, ov);            std::vector<default_color_type>         colors(num_vertices(g), color_traits<default_color_type>::white());      boost::queue<vertex_descriptor> Q;      breadth_first_visit(g, s, Q, visitor,                           make_iterator_property_map(colors.begin(),                                                      vertex_index));    }  };  // When the edge centrality map is a dummy property map, no  // initialization is needed.  template<typename Iter>  inline void   init_centrality_map(std::pair<Iter, Iter>, dummy_property_map) { }  // When we have a real edge centrality map, initialize all of the  // centralities to zero.  template<typename Iter, typename Centrality>  void   init_centrality_map(std::pair<Iter, Iter> keys, Centrality centrality_map)  {    typedef typename property_traits<Centrality>::value_type       centrality_type;    while (keys.first != keys.second) {      put(centrality_map, *keys.first, centrality_type(0));      ++keys.first;    }  }  // When the edge centrality map is a dummy property map, no update  // is performed.  template<typename Key, typename T>  inline void   update_centrality(dummy_property_map, const Key&, const T&) { }  // When we have a real edge centrality map, add the value to the map  template<typename CentralityMap, typename Key, typename T>  inline void   update_centrality(CentralityMap centrality_map, Key k, const T& x)  { put(centrality_map, k, get(centrality_map, k) + x); }  template<typename Iter>  inline void   divide_centrality_by_two(std::pair<Iter, Iter>, dummy_property_map) {}  template<typename Iter, typename CentralityMap>  inline void  divide_centrality_by_two(std::pair<Iter, Iter> keys,                            CentralityMap centrality_map)  {    typename property_traits<CentralityMap>::value_type two(2);    while (keys.first != keys.second) {      put(centrality_map, *keys.first, get(centrality_map, *keys.first) / two);      ++keys.first;    }  }  template<typename Graph, typename CentralityMap, typename EdgeCentralityMap,           typename IncomingMap, typename DistanceMap,            typename DependencyMap, typename PathCountMap,           typename VertexIndexMap, typename ShortestPaths>  void   brandes_betweenness_centrality_impl(const Graph& g,                                       CentralityMap centrality,     // C_B                                      EdgeCentralityMap edge_centrality_map,                                      IncomingMap incoming, // P                                      DistanceMap distance,         // d                                      DependencyMap dependency,     // delta                                      PathCountMap path_count,      // sigma                                      VertexIndexMap vertex_index,                                      ShortestPaths shortest_paths)  {    typedef typename graph_traits<Graph>::vertex_iterator vertex_iterator;    typedef typename graph_traits<Graph>::edge_iterator edge_iterator;    typedef typename graph_traits<Graph>::vertex_descriptor vertex_descriptor;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -