如何使用boostlib为邻接表计算中间性? [英] How to calculate betweenness using boostlib for adjacency list?
问题描述
我正在尝试编写一个简单的程序,使用boostlib中的brandes_betweenness_centrality计算之间的距离.我陷入了获取输出(CentralityMap)的困境.我一直在阅读文档,但无法弄清楚如何将它们放在一起.
I'm trying to write a simple program to calculate betweeness using brandes_betweenness_centrality from boostlib. I got stuck at getting an output (CentralityMap). I've been reading the documentation but I can't figure out how to put it all together.
这是我的简单代码:
#include <iostream> // std::cout
#include <utility> // std::pair
#include <boost/graph/graph_traits.hpp>
#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/betweenness_centrality.hpp>
using namespace boost;
int main()
{
int nVertices = 100;
srand ( time(NULL) );
typedef std::pair<int, int> Edge;
std::vector<Edge> edges;
for(int i=0; i<nVertices; i++){
std::cout << i << " : ";
for(int j=0; j<nVertices; j++){
if(rand() % 100 < 9){ /// chances of making a connection is 9 out of 100. may not be accurate
std::cout << j << " ";
edges.push_back(std::make_pair(i,j));
}
}
std::cout << std::endl;
}
typedef adjacency_list<vecS, vecS, bidirectionalS,
property<vertex_color_t, default_color_type>
> Graph;
Graph g(edges.begin(), edges.end(), edges.size());
brandes_betweenness_centrality(g,?????? );
return 0;
}
根据我的理解,我需要定义将要写入结果的中心位置图.它与读/写属性映射有关,但是我不知道如何定义它.
From my understanding, I need define centrality map where the result will be written. It's related to read/write property map but I can't figure out how to define one.
最终我需要输出介于两者之间.
Eventually I need to output betweenness.
推荐答案
填写缺失部分的最简单方法是:
The simplest way to fill in the missing parts is:
boost::shared_array_property_map<double, boost::property_map<Graph, vertex_index_t>::const_type>
centrality_map(num_vertices(g), get(boost::vertex_index, g));
然后将centrality_map
作为中心图传递给brandes_betweenness_centrality
.
then pass centrality_map
as the centrality map to brandes_betweenness_centrality
.
这篇关于如何使用boostlib为邻接表计算中间性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!