BGL: EdgeIterator construction of graphs

Hi everybody! I'm trying to initialize my adjacency_list graph via an EdgeIterator. Since I do not want parallel edges to be present in my graph, but still use vecS as EdgeList-storage because of memory considerations, my EdgeIterator needs somehow a reference to the graph under construction. Some code might explain it better: typedef boost::adjacency_list<boost::vecS, boost::vecS, boost::undirectedS > graph_t; typedef boost::my_edge_iterator<base_rand_gen_t, graph_t> SFGen; graph_t bg = graph_t(SFGen(bg, base_rand, 100), SFGen(), 100); This code compiles fine, but seg-faults immediatly since bg does not seem to be defined when my iterator gets constructed. A solution would be to provide a add_edge_range-function which gets as input the EdgeIterator and the corresponding empty boost graph. This function would then build up the graph or is there a better way to do this? Thanks a lot in advance for your help. Greetings, Sebastian

Hi Sebastian, On Jul 22, 2005, at 7:57 AM, Sebastian Weber wrote:
I'm trying to initialize my adjacency_list graph via an EdgeIterator. Since I do not want parallel edges to be present in my graph, but still use vecS as EdgeList-storage because of memory considerations, my EdgeIterator needs somehow a reference to the graph under construction. Some code might explain it better:
typedef boost::adjacency_list<boost::vecS, boost::vecS, boost::undirectedS > graph_t; typedef boost::my_edge_iterator<base_rand_gen_t, graph_t> SFGen;
graph_t bg = graph_t(SFGen(bg, base_rand, 100), SFGen(), 100);
This code compiles fine, but seg-faults immediatly since bg does not seem to be defined when my iterator gets constructed.
My gut feeling is that this should work.. you're not trying to make a copy of "bg" when you builf SFGen, right? SFGen needs to store a pointer to a graph_t, not a reference or copy.
A solution would be to provide a add_edge_range-function which gets as input the EdgeIterator and the corresponding empty boost graph.
Good idea.
This function would then build up the graph or is there a better way to do this?
Nothing better than making lots of add_edge calls, which is exactly what the EdgeIterator constructor does. Doug

Hi Doug!
typedef boost::adjacency_list<boost::vecS, boost::vecS, boost::undirectedS > graph_t; typedef boost::my_edge_iterator<base_rand_gen_t, graph_t> SFGen;
graph_t bg = graph_t(SFGen(bg, base_rand, 100), SFGen(), 100);
This code compiles fine, but seg-faults immediatly since bg does not seem to be defined when my iterator gets constructed.
My gut feeling is that this should work.. you're not trying to make a copy of "bg" when you builf SFGen, right? SFGen needs to store a pointer to a graph_t, not a reference or copy.
Yes thats right, I only need a pointer/reference to the graph object which is going to be constructed. I guess the trouble with the statement above is that I first construct the SFGen-object and give this as an argument to the constructor of my graph-object which I want to build. I dont know if just my compiler fools me or if there is actually a way to do this. I have a debian sarge system with gcc-3.3 and latest cvs boost.
A solution would be to provide a add_edge_range-function which gets as input the EdgeIterator and the corresponding empty boost graph.
Good idea.
Actually I copied the code from the adjacency_list.hpp and renamed it to build_graph: template <class Graph, class EdgeIterator> void build_graph(Graph& g, EdgeIterator first, EdgeIterator last) { typedef graph_traits<Graph> gtraits; typename gtraits::vertex_descriptor* v = new typename gtraits::vertex_descriptor[num_vertices(g)]; typename gtraits::vertex_iterator vi, vend; std::size_t i = 0; cout << "Initializing vertex list ..." << endl; for (tie(vi, vend) = vertices(g); vi != vend; ++vi) v[i++] = *vi; cout << "Populating graph ..." << endl; while (first != last) { add_edge(v[(*first).first], v[(*first).second], g); ++first; } cout << "done building graph" << endl; delete [] v; } It does what I want, but using the constructor would be nicer. Greetings, Sebastian
participants (2)
-
Doug Gregor
-
Sebastian Weber