【问题标题】:How to calculate the betweenness centrality with weights如何用权重计算中介中心性
【发布时间】:2021-02-18 20:40:54
【问题描述】:

我正在尝试使用 Edge length 属性作为权重来计算城市街道网络的中介中心性,但它似乎不起作用。

当我在没有重量的情况下跑步时,它会给出以下结果:

min: 0
mean: -nan(ind)
max: inf
stddev: -nan(ind)

还有权重:

min: 0
mean: 0
max: 0
stddev: 0

任何帮助将不胜感激

我的实现(在较小的图表中有效):

struct TravelEdge {
    float length = 0.0f;
};

struct TravelNode {
    float xcoord = 0.0f;
    float ycoord = 0.0f;
    std::string osmid;
};

typedef boost::adjacency_list<
    boost::vecS, boost::vecS, boost::directedS,
    TravelNode, TravelEdge
> GraphMap;
typedef boost::property_map<GraphMap, boost::vertex_index_t>::type VertexIndexMap;

int main(){
  GraphMap graph;

  // ... loads from graphml file

  std::vector<double> centrality_vector(boost::num_vertices(graph), 0.0);
  VertexIndexMap v_index = get(boost::vertex_index, graph);
  boost::iterator_property_map<std::vector<double>::iterator, VertexIndexMap>
      vertex_property_map = make_iterator_property_map(centrality_vector.begin(), v_index);

  auto weight_map = make_transform_value_property_map([](TravelEdge& e) { return e.length; }, get(boost::edge_bundle, graph));

  // without weights
  //boost::brandes_betweenness_centrality(graph, vertex_property_map);
  // with weights
  boost::brandes_betweenness_centrality(graph, boost::predecessor_map(vertex_property_map).weight_map(weight_map));

  boost::accumulators::accumulator_set<
      double,
      boost::accumulators::features<
      boost::accumulators::tag::min,
      boost::accumulators::tag::max,
      boost::accumulators::tag::mean,
      boost::accumulators::tag::variance
      >
  > acc;

  std::for_each(centrality_vector.begin(), centrality_vector.end(), std::ref(acc));

  return 0;
}

【问题讨论】:

    标签: c++ boost boost-graph


    【解决方案1】:

    嗯,这很尴尬,结果证明代码是正确的,我的图表有些错误,我重新生成它并且它已经工作了。

    编辑 1

    在看到加载图表所需的时间和内存量后,我 能够找到问题,在我对节点进行了一些操作之后,我是 将它们插入同一个图表,而不是新的图表。

    编辑 2

    使用权重函数的介数调用方式是错误的,正确的方式是:

    boost::brandes_betweenness_centrality(
        graph,
        centrality_map(vertex_property_map)
        .vertex_index_map(v_index)
        .weight_map(get(&TravelEdge::travel_time, graph))
    );
    

    我希望有人觉得这很有用?

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-04-08
    • 1970-01-01
    • 1970-01-01
    • 2021-09-10
    • 1970-01-01
    • 1970-01-01
    • 2021-05-25
    • 1970-01-01
    相关资源
    最近更新 更多