2017-03-26 17 views
0

bglでグラフを作成し、頂点を反復処理するコードの例を以下に示します。つまり、ループはすべての頂点を操作する必要がありますが、頂点の順序はメイン関数の呼び出しごとにランダムでなければなりません。どうすればこれを達成できますか?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; 
    } 
+0

例えば

? – Jay

+0

@Jay 'std :: random_shuffle(vp.first、vp.second);'を追加しようとしましたが、コンパイルに失敗します。エラーは非常に冗長で、私は何が重要なのか分からないが、random_shuffleによって内部的に呼び出されるスワップ関数は別の種類の入力を期待している。 – nevrome

答えて

1

私が考える最も簡単なことは、インデックスのランダムベクトルas outlined hereを設定すると思います。シャッフルされたリストを反復し、それを頂点イテレータのオフセットとして使用することができます。 STDを使用して:: random_shuffle約失敗したものを

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

指定した範囲内の乱数を作成するには、次のコードを使用します。 の#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)を変更して、もう一度関数を呼び出す必要があります。

+0

私の問題はあまり乱数ジェネレータを作成することではありません。これをイテレーターのコンセプトと組み合わせる方法はわかりません。私のサンプルコードでランダムな頂点順序を得るためにあなたの関数をどこで呼び出すべきかを詳しく教えてください。たぶん私は明らかに見ていないだけかもしれない。 – nevrome

関連する問題