[graph] adjacency_list constructor fails, wrong usage?
Hi, I originally wanted to construct an undirected graph from a directed graph and vice versa. For this purpose i tried to use template <class EdgeIterator> adjacency_list(EdgeIterator first, EdgeIterator last, vertices_size_type n, edges_size_type m = 0, const GraphProperty& p = GraphProperty()) as shown in http://www.boost.org/doc/libs/1_47_0/libs/graph/doc/adjacency_list.html but i do not even manage to construct a graph from the same type with this constructor. What am i missing? Please have a look at the example below. If you uncomment the last two lines, the code will not compile. // begin code #include <iostream> #include <boost/graph/adjacency_list.hpp> #include <boost/graph/graph_utility.hpp> int main(int argc, char** argv) { typedef boost::adjacency_list < boost::listS, boost::vecS, boost::directedS> Dgraph; typedef boost::adjacency_list < boost::listS, boost::vecS, boost::undirectedS> Graph; Dgraph g(5); add_edge (0,1,g);add_edge (2,3,g);add_edge (4,5,g); print_graph (g, boost::get(boost::vertex_index, g)); boost::graph_traits<Dgraph>::edge_iterator i1 = (edges(g)).first; boost::graph_traits<Dgraph>::edge_iterator i2 = (edges(g)).second; // Dgraph g2 = Dgraph (i1, i2, num_vertices (g), num_edges (g)); // print_graph (g2, boost::get(boost::vertex_index, g2)); return 0; } // end code best regards Christoph
any suggestions?
If I construct a vector of std::pair<Vertex,Vertex> explicitly I can get this to compile. But it's hardly an elegant way to copy a graph... // begin code #include <iostream> #include <sstream> #include <boost/graph/adjacency_list.hpp> #include <boost/graph/graph_utility.hpp> int main(int argc, char** argv) { typedef boost::adjacency_list < boost::listS, // outedgelist boost::vecS, // vertexlist boost::directedS> // directed Dgraph; typedef boost::graph_traits<Dgraph>::edge_iterator EdgeItr; typedef boost::graph_traits<Dgraph>::vertex_descriptor Vertex; Dgraph g(5); add_edge (0,1,g); add_edge (2,3,g); add_edge (4,5,g); print_graph (g, boost::get(boost::vertex_index, g) ); EdgeItr i1 = (boost::edges(g)).first; EdgeItr i2 = (boost::edges(g)).second; std::vector< std::pair<Vertex, Vertex> > edgevector; for (EdgeItr e = i1; e!=i2 ; e++ ) { std::pair<Vertex, Vertex> p = std::make_pair( source(*e,g), target(*e,g) ); edgevector.push_back(p); } Dgraph g2 = Dgraph( edgevector.begin(), edgevector.end(), num_vertices(g), num_edges(g)); print_graph (g2, boost::get(boost::vertex_index, g2)); return 0; } // end code
On Mon, 14 Nov 2011, Anders Wallin wrote:
any suggestions?
If I construct a vector of std::pair<Vertex,Vertex> explicitly I can get this to compile. But it's hardly an elegant way to copy a graph...
Have you tried copy_graph() (http://www.boost.org/doc/libs/1_47_0/libs/graph/doc/copy_graph.html)? Is there some reason that does not work? -- Jeremiah Willcock
participants (3)
-
Anders Wallin
-
Christoph
-
Jeremiah Willcock