6
votes

How can I copy a graph of type adjacency_list to another one graph of type adjacency_list ?

typedef adjacency_list<setS, setS, undirectedS, NodeDataStruct, EdgeDataStruct> MyGraph;
MyGraph g1, g2;

// processing g1: adding vertices and edges ...
// processing g2: adding some vertices and edges ...

g1.clear();
g1 = g2 // this gives an execution error (exception)
g1 = MyGraph(g2); // this also gives an execution error
g2.clear();
1
I know this is old. I wonder if you needed to define the copy constructor for your vertex and edge properties. I recently needed to copy a graph and the copy constructor worked fine for me.K. Shores

1 Answers

7
votes

Have you tried copy_graph?


Hard to know what the problem is without seeing the errors but if I had to guess, I'd first make sure you're providing a vertex_index map to copy_graph since it's not available by default when you use setS for vertex storage. Based on your earlier question, it looks like you've already got that figured out so we just need to bring it all together.

  typedef adjacency_list<setS, setS, undirectedS, NodeDataStruct, EdgeDataStruct> MyGraph;
  typedef MyGraph::vertex_descriptor NodeID;

  typedef map<NodeID, size_t> IndexMap;
  IndexMap mapIndex;
  associative_property_map<IndexMap> propmapIndex(mapIndex);

  MyGraph g1, g2;

  // processing g1: adding vertices and edges ...
  // processing g2: adding some vertices and edges ...

  int i=0;
  BGL_FORALL_VERTICES(v, g2, MyGraph)
  {
     put(propmapIndex, v, i++);
  }

  g1.clear();
  copy_graph( g2, g1, vertex_index_map( propmapIndex ) );
  g2.clear();