将图形(adjacency_list)复制到另一个图形

时间:2012-02-13 13:45:29

标签: c++ boost copy boost-graph

如何将adjacency_list类型的图形复制到另一个类型为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 个答案:

答案 0 :(得分:6)

您是否尝试过copy_graph


很难在没有看到错误的情况下知道问题是什么,但如果我不得不猜测,我首先要确保您向vertex_index提供copy_graph地图,因为它默认不可用当您使用setS进行顶点存储时。根据您的earlier question,看起来您已经弄明白了,所以我们只需要将它们整合在一起。

  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();