my first boost graph library program failed
Apr 19, 2015 at 12:38pm UTC
I tried to create a program loading a graph from a file with boost graph library. The execution of the following codes causes an segment fault. Could any veteran of BGL help me out? Thx!
header file
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
#ifndef COOPERATIVEGRAPH_H
#define COOPERATIVEGRAPH_H
#include <map>
#include <fstream>
#include <string>
#include <utility>
#include <boost/graph/graph_traits.hpp>
#include <boost/graph/adjacency_list.hpp>
#include <boost/numeric/ublas/matrix.hpp>
#include <boost/numeric/ublas/matrix_proxy.hpp>
#include <boost/numeric/ublas/io.hpp>
using namespace std;
using namespace boost;
namespace ublas = boost::numeric::ublas;
enum edge_q_function_t {edge_q_function};
namespace boost {
BOOST_INSTALL_PROPERTY(edge,q_function);
}
class CooperativeGraph {
public :
typedef property<edge_q_function_t,ublas::matrix<double > > QFunc;
typedef adjacency_list<
vecS,setS,directedS,
no_property,QFunc
> Graph;
CooperativeGraph(string path);
virtual ~CooperativeGraph();
private :
Graph g;
};
namespace std {
template <typename T> istream & operator >>(istream & in,pair<T,T>& p) {
in >> p.first >> p.second;
return in;
}
}
#endif
cpp files
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
#include "CooperativeGraph.h"
CooperativeGraph::CooperativeGraph(string path)
{
//打开配置文件
ifstream in(path.c_str());
typedef graph_traits<Graph>::vertices_size_type size_type;
size_type n_vertices;
in >> n_vertices;
istream_iterator<pair<size_type,size_type> > input_begin(in), input_end;
//读取配置文件
g = Graph(input_begin,input_end,n_vertices);
}
CooperativeGraph::~CooperativeGraph()
{
}
1 2 3 4 5 6 7 8 9 10
#include <cstdlib>
#include <iostream>
#include "CooperativeGraph.h"
using namespace std;
int main()
{
CooperativeGraph g("graph.txt" );
}
data file
1 2 3 4
20 1 2 1 5 1 6 2 1 2 3 2 8 3 2 3 4 3 10 4 3 4 5 4 12 5 1 5 4 5 14 6 1 6 7 6 15 7 6
7 8 7 17 8 2 8 7 8 9 9 8 9 10 9 18 10 3 10 9 10 11 11 10 11 12 11 19 12 4 12 11
12 13 13 12 13 14 13 20 14 5 14 13 14 15 15 6 15 14 15 16 16 15 16 17 16 20
17 7 17 16 17 18 18 9 18 17 18 19 19 11 19 18 19 20 20 13 20 19 20 16
Last edited on Apr 19, 2015 at 12:39pm UTC
Apr 19, 2015 at 2:52pm UTC
The range for the vertices is [0, n)
Apr 20, 2015 at 12:08am UTC
oh i see the problem. it is the difference between matlab and BGL. Thanks for you prompt reply! :-)
Topic archived. No new replies allowed.