【发布时间】:2017-02-25 14:16:48
【问题描述】:
我需要使用 Boost Graph Library 从我的图中的给定顶点中选择一个随机的外邻或内邻。我从 RNG 收到索引 i 并且需要选择第 i 个出边(它们可以按任何顺序排列,只需要在调用之间保持一致)。为此,我像这样使用std::advance:
typedef adjacency_list<vecS, vecS, bidirectionalS> Graph;
Graph g; // Given graph
int i;
int v; // Given vertex
//
// Code to generate random index (guaranteed to be valid) and store it in i
//
typename graph_traits < graph_t >::in_edge_iterator ei, ei_end;
tie(ei,ei_end) = in_edges(v,g);
std::advance(ei,i);
// Now ei points to the ith out edge, and is ready to use
// Return the corresponding in-neighbor
return source(*ei,g);
现在这工作正常,我得到了正确的输出。但是我需要知道这是否有效,即std::advance 会在恒定时间内工作,而不管i 的值如何,因为我使用vecS 来存储邻接列表?如果没有,有没有有效的方法?
我应该提一下,我只需要选择一个随机的内邻居或外邻居,所以如果你有办法在不处理边缘的情况下做到这一点,那也很好。
【问题讨论】:
标签: c++ boost boost-graph