6

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();
shn
  • 5,116
  • 9
  • 34
  • 62
  • 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 Jul 06 '18 at 19:41

1 Answers1

7

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();
Community
  • 1
  • 1
Andrew Durward
  • 3,771
  • 1
  • 19
  • 30
  • For copy_graph it is said that the graph type must be a model of VertexListGraph. In my case I've said that it is an adjacency_list. – shn Feb 13 '12 at 14:56
  • 2
    @user995434 But adjacency_list is a model of VertexAndEdgeListGraph which is a refinement of VertexListGraph. Hence adjacency_list is a model of VertexListGraph. – Andrew Durward Feb 13 '12 at 15:25
  • Can you please give a small illustrative example of using copy_graph() for what I would like to do ? It always give me compilation errors. Thanks in advance. – shn Feb 13 '12 at 21:20