【问题标题】:Random order iteration over BGL verticesBGL 顶点上的随机顺序迭代
【发布时间】:2017-03-26 02:29:50
【问题描述】:

这里有一些示例代码,用于使用 bgl 创建图形并迭代顶点。我想以随机顺序进行此迭代 - 换句话说:循环应该操纵每个顶点,但是对于主函数的每次调用,顶点的顺序应该是随机的。我怎样才能做到这一点?

我尝试使用std::random_shuffle 失败。我认为有不同种类的迭代器概念,但我还不明白其中的区别。

  #include <iostream>                  
  #include <boost/graph/graph_traits.hpp>
  #include <boost/graph/adjacency_list.hpp>

  using namespace boost;

  // vertex struct to store some properties in vertices
  struct Vertex {
    std::string name;
  };

  int main(int,char*[]) {
    // create a typedef for the graph type
    typedef adjacency_list<vecS, vecS, undirectedS, Vertex> Graph;

    // declare a graph object
    Graph g(3);

    // prepare iteration 
    typedef graph_traits<Graph>::vertex_iterator vertex_iter;
    std::pair<vertex_iter, vertex_iter> vp;

    // add some property data to the vertices
    vp = vertices(g);
    g[*vp.first].name = "A";
    g[*(++vp.first)].name = "B";
    g[*(++vp.first)].name = "C";

    // iterate over the vertices
    for (vp = vertices(g); vp.first != vp.second; ++vp.first)     
      std::cout << g[*vp.first].name <<  " ";
    std::cout << std::endl;

    return 0;
  }

编辑:感谢@Jay 的回答,这是我想出的解决方案。

  #include <iostream>                  
  #include <boost/graph/graph_traits.hpp>
  #include <boost/graph/adjacency_list.hpp>
  #include <algorithm>    // std::random_shuffle
  #include <vector>       // std::vector
  #include <ctime>        // std::time
  #include <cstdlib>      // std::rand, std::srand

  using namespace boost;

  // vertex struct to store some properties in vertices
  struct Vertex {
    std::string name;
  };

  // random number generator function
  int myrandom (int i) { 
    return std::rand()%i;
  }

  int main(int,char*[]) {
    // create a typedef for the graph type
    typedef adjacency_list<vecS, vecS, undirectedS, Vertex> Graph;

    // declare a graph object
    Graph g(3);

    // prepare iteration 
    typedef graph_traits<Graph>::vertex_iterator vertex_iter;
    std::pair<vertex_iter, vertex_iter> vp;

    // add some property data to the vertices
    vp = vertices(g);
    g[*vp.first].name = "A";
    g[*(++vp.first)].name = "B";
    g[*(++vp.first)].name = "C";

    // initialize pseudo random number generator
    std::srand(unsigned (std::time(0)));

    // create offset vector
    std::vector<int> myvector;
    for (int i=0; i<3; ++i) {
      myvector.push_back(i);
    }

    // using myrandom to shuffle offset vector
    std::random_shuffle(myvector.begin(), myvector.end(), myrandom);

    // keep vp.first at the start 
    vp = vertices(g);

    // iterate over the vertices effectively shuffled by the offset
    vertex_iter dummy_iter;
    for (std::vector<int>::iterator it=myvector.begin(); it!=myvector.end(); ++it) {
      dummy_iter = vp.first + *it;
      std::cout << g[*dummy_iter].name <<  " ";
    }
    std::cout << std::endl;

    return 0;
  }

【问题讨论】:

  • 使用 std::random_shuffle 失败的原因是什么?
  • @Jay 我尝试添加std::random_shuffle(vp.first, vp.second);,但随后编译失败。该错误非常冗长,我不确定重要的部分是什么,但我猜由 random_shuffle 内部调用的交换函数需要另一种输入。

标签: c++ boost boost-graph


【解决方案1】:

我认为最简单的做法是设置一个随机的索引向量,as outlined here。然后,您可以迭代 shuffled 列表并将其用作顶点迭代器的偏移量。

例如

vp = vertices(g); // Keep vp.first at the start 
vertex_iter dummy_iter;
// Looping on a shuffled vector, values should be 0..N-1
for (std::vector<int>::iterator it=myvector.begin(); it!=myvector.end(); ++it)
{
    dummy_iter = vp.first + *it;
    Vertex* v = *dummy_iter;
    ...

【讨论】:

    【解决方案2】:

    要在给定范围内创建一个随机数,请使用以下代码。 #include ctime 和 #include stdlib.h

        int getNumberRange(int min, int max)
        {
            srand(static_cast<unsigned int>(time(0)));
    
            // always call rand(); after srand() on visual vasic;
            rand();
    
            static const double fraction = 1.0 / (static_cast<double>(RAND_MAX) + 1.0);
            return static_cast<int>(rand() * fraction * (max - min + 1) + min);
        }
    
    
    
        getNumberRange(1, 100); //picks number between 1 and 100
    

    每次您需要一个新数字时,修改范围值 (1, 100) 并再次调用该函数。

    【讨论】:

    • 我的问题不在于创建一个随机数生成器。我不明白如何将它与迭代器概念结合起来。您能否详细说明在我的示例代码中调用函数以实现随机顶点顺序的位置?也许我只是没有看到明显的东西。
    猜你喜欢
    • 1970-01-01
    • 2012-11-07
    • 2021-07-08
    • 2020-09-18
    • 2013-09-30
    • 2012-09-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多