簡體   English   中英

更改 Boost Graph Library 中頂點的迭代順序

[英]Change the order of iteration over vertex in Boost Graph Library

我需要迭代圖形頂點 N 次,在每次迭代中,我希望每個頂點的外觀順序不同。 我知道如何通過創建頂點描述符向量來做到這一點:

...
VertexIter itG1=boost::vertices(g).first;
VertexIter itG1End=boost::vertices(g).second;

while(itG1!=itG1End)
    vID.push_back(*itG1)
    itG1++

for (i=0; i< N; i++){
    shuffle(vID.begin(),vID.end(),RNG)
    vector<vertexDesc>::iterator idIterator;
    idIterator idIT = vID.begin();
    idIterator idEND = vID.end();

    while(idIT!=idEND){

        do_something_with(*idIT);
        idIT++;

   }
}

我想改為實現,像這樣:

#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/erdos_renyi_generator.hpp>
#include <boost/random/linear_congruential.hpp>
#include <boost/graph/random.hpp>
#include <boost/graph/graph_utility.hpp>
#include <random>

int main(int argc, char *argv[])
{
  boost::minstd_rand gen;
  struct Vertex{ int foo;};
  struct Edge{std::string blah;};

  typedef boost::adjacency_list<boost::setS, boost::vecS, boost::directedS, Vertex, Edge > Graph;
  typedef boost::graph_traits<Graph>::vertex_descriptor vertex_t;
  typedef boost::graph_traits<Graph>::edge_descriptor edge_t;
  typedef boost::graph_traits<Graph>::vertex_iterator vertexIter;
  typedef boost::graph_traits<Graph>::edge_iterator edgeIter;

  Graph g;

  boost::generate_random_graph<Graph,boost::minstd_rand>(g, 10,20, gen);
std::shuffle(boost::vertices(g).first,boost::vertices(g).second,std::default_random_engine(time(NULL)));

  return 0;

}

但是編譯器抱怨 std::swap 的問題。

你為什么要改變擁有向量的描述符? 這絕對是更有效的方式。

即使您實際上能夠交換頂點(您已經發現,不能用於鄰接列表),因為在這種情況下描述符頂點 id,您將有效地僅對邊進行洗牌(保持圖同構)。

所以,如果那是你真正喜歡的,請看

如果真的,您想控制內部庫工具的遍歷順序,例如使用boost::vertices那么您需要通過定義自己的容器生成器來自定義頂點容器 可以在此處找到一個示例,但是您可以對圖形模型進行大量修改以獲得您想要的行為,我懷疑您會獲得什么顯着的好處(因此是我的開場白)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM