2011-02-09 20 views
2

最初に説明します。次にコードを貼り付けます。私は実際にこの例のコードをコピーしました http://www.boost.org/doc/libs/1_45_0/libs/graph/example/prim-example.cpp そして、私はBoost Kruskalアルゴリズムと同じように、テキストファイルからの入力で動作させようとしています。私はこのBoost C++コードを使用しようとしていますが、問題があります

デバッガを使用して、私はこの関数の第2引数がエッジの配列を "終了"したいことを知っています。それは私が関数呼び出しで与えていることですが、わかりません。

Graph g(edges, edge_array + num_edges, weights, num_nodes);

私はこのエラー

1>c:\users\edmond\documents\visual studio 2008\projects\boost prim algo\boost prim algo\main.cpp(61) : error C2661: 'boost::adjacency_list<OutEdgeListS,VertexListS,DirectedS,VertexProperty,EdgeProperty>::adjacency_list' : no overloaded function takes 4 arguments 
1>  with 
1>  [ 
1>   OutEdgeListS=boost::vecS, 
1>   VertexListS=boost::vecS, 
1>   DirectedS=boost::undirectedS, 
1>   VertexProperty=boost::property<boost::vertex_distance_t,int>, 
1>   EdgeProperty=boost::property<boost::edge_weight_t,int> 
1>  ] 

はここで完全なコードがあり得ます。私は元のコードをコメントしましたが、あなたが元のコードを私が与えたウェブサイトで見つけることもできます。

//======================================================================= 
// Copyright 2001 Jeremy G. Siek, Andrew Lumsdaine, Lie-Quan Lee, 
// 
// Distributed under the Boost Software License, Version 1.0. (See 
// accompanying file LICENSE_1_0.txt or copy at 
// http://www.boost.org/LICENSE_1_0.txt) 
//======================================================================= 
#include <boost/config.hpp> 
#include <iostream> 
#include <boost/graph/adjacency_list.hpp> 
#include <boost/graph/prim_minimum_spanning_tree.hpp> 

int 
main() 
{ 
    using namespace boost; 
    typedef adjacency_list < vecS, vecS, undirectedS, 
    property<vertex_distance_t, int>, property < edge_weight_t, int > > Graph; 
    typedef std::pair < int, int >E; 

    //const int num_nodes = 5; 
    //E edges[] = { E(0, 2), E(1, 3), E(1, 4), E(2, 1), E(2, 3), 
    // E(3, 4), E(4, 0) 
    //}; 
    //int weights[] = { 1, 1, 2, 7, 3, 1, 1 }; 
    //int num_edges = 7; 

//Lire un fichier contenant les 2 structures de données 
int num_nodes = 0; 
std::size_t num_edges = 0; 
int * weights; 
E * edge_array; 
static char ligne[50]; //Ligne lue 
bool premiereLignefaite = false; 
FILE* fichier = fopen("graph_poids.txt", "r"); 
int i = 0; 

while (fgets(ligne, 50, fichier) != NULL) //retourne 0 quand on a end-of-file 
    { 
     //La premiere ligne est différente 
     if (premiereLignefaite == false) { 
      //Initialiser une matrice d'adjacence NxN 
      sscanf(ligne, "%d %d", &num_nodes, &num_edges); 
      edge_array = new E[num_edges]; 
      weights = new int[num_edges]; 
      premiereLignefaite = true; 
      continue; 
     } 
     //On construit notre liste d'arêtes 
     int sommet1, sommet2, poids; 
     sscanf(ligne, "%d %d %d", &sommet1, &sommet2, &poids); 
     weights[i] = poids; 
     edge_array[i].first = sommet1; 
     edge_array[i].second = sommet2; 
     i++; 
    } 

E* machin = edge_array + num_edges; //aller au dernier élément 

    Graph g(edges, edge_array + num_edges, weights, num_nodes); 
//Graph g(edges, edges + sizeof(edges)/sizeof(E), weights, num_nodes); 
    property_map<Graph, edge_weight_t>::type weightmap = get(edge_weight, g); 

    std::vector < graph_traits <Graph>::vertex_descriptor > 
    p(num_vertices(g)); 

    prim_minimum_spanning_tree(g, &p[0]); 

    for (std::size_t i = 0; i != p.size(); ++i) 
    if (p[i] != i) 
     std::cout << "parent[" << i << "] = " << p[i] << std::endl; 
    else 
     std::cout << "parent[" << i << "] = no parent" << std::endl; 

    return EXIT_SUCCESS; 
} 

試したい場合は、ここにいくつかのテストデータがあります。ファイルの名前は、それが「edge_array」としない「エッジ」(1つは、元のコードの一部であった)であるべきである

14 19 
0 2 4 
0 4 9 
0 1 7 
1 6 2 
2 3 6 
3 5 7 
3 4 4 
4 13 9 
5 7 7 
5 6 6 
6 8 9 
6 10 4 
7 9 4 
7 8 7 
8 11 7 
8 10 7 
9 12 7 
9 11 10 
12 13 5 
+0

"edge_array"であり、 "エッジ"(元のコードの一部)ではないはずです。 Ca devrait marcher par la suite! –

+0

ミカエル、あなたが投稿した場合、私はあなたに受け入れられた答えをあげます。あなたは私のコードを稼働させて、私はこの間違いに気付かず、今は正常に動作します。 – toto

+0

@Mikael Persson上記の発言があなたに向けられていない場合は... – mgiuca

答えて

2

graph_poids.txtあります。

関連する問題