【发布时间】:2016-05-03 22:17:43
【问题描述】:
编辑:正如@sehe 所指出的,错误位于介数中心性计算之前的某处。继续前进!
我在Python 和C++ 中实现了一个最小程序来计算无向图的介数中心性。令人惊讶的是,networkx (Python) 版本 far 优于 boost::graph (C++) 实现,即使考虑到加载开销等。
我在做一些完全没有效率的事情吗?
Python 代码的要点很简单
# load graph and start chrono
clist = nx.betweenness_centrality(g)
# output
对于 C++,我们有
typedef boost::adjacency_list<boost::vecS,
boost::vecS,
boost::undirectedS> Graph;
typedef boost::property_map< Graph, boost::vertex_index_t>::type VertexIndexMap;
int main() {
Graph g;
// ...
// load graph
// ...
VertexIndexMap v_index = get(boost::vertex_index, g);
std::vector< double > vertex_property_vec(boost::num_vertices(g), 0.0);
boost::iterator_property_map< std::vector< double >::iterator, VertexIndexMap >
vertex_property_map(vertex_property_vec.begin(), v_index);
boost::brandes_betweenness_centrality(g, vertex_property_map);
// Output ...
return 0;
}
请注意,这两个库似乎都实现了完全相同的算法(Brandes 2001)。
【问题讨论】:
-
如何编译?你如何测量? 远表现如何?
-
我不确定,但有可能两者都在做不同的事情。根据this,python 版本有一个可选参数 k 表示“如果 k 不是 None,则使用 k 节点样本来估计介数。k clist = nx.betweenness_centrality(g,num_vertices) 看看它们是否具有可比性? (您还应该检查结果)。
-
@Drop 我在 gcc-5.3 上使用简单的
g++ -std=c++0x -o3 -o bc编译,并使用 boost 1.60.2。当我说“远远优于”时,我的意思是用 networkx 计算 90 个小型(n错误。 -
@cv_and_he 感谢您的指点。问题不在于 nx,它正确实现了该方法(对源代码的检查显示
k=None默认为精确计算)。但是,我检查了 C++ 代码的输出,似乎brandes_between_centrality 的输出甚至没有存储在vertex_property_vec...
标签: c++ boost networkx boost-graph