gpt4 book ai didi

c++ - BGL 顶点的随机顺序迭代

转载 作者:行者123 更新时间:2023-11-28 05:13:58 24 4
gpt4 key购买 nike

下面是一些使用 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;
}

最佳答案

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

例如

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;
...

关于c++ - BGL 顶点的随机顺序迭代,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43024314/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com