将一个图(邻接表)复制到另一个图中。

6
我该如何将一种邻接表类型的图复制到另一种邻接表类型的图中?
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();

我知道这已经过时了。不知道你是否需要为你的顶点和边属性定义复制构造函数。最近我需要复制一个图,复制构造函数对我来说很好用。 - K. Shores
1个回答

7
你试过copy_graph吗?
没有看到错误无法知道问题所在,但如果我必须猜测,首先我会确保你在使用setS作为顶点存储方式时向copy_graph提供了一个vertex_index映射。根据你之前的问题,看起来你已经弄清楚了,现在我们只需要将它们整合在一起。
  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();

对于copy_graph函数,要求图类型必须是VertexListGraph的模型。在我的情况下,我已经指定它是一个adjacency_list。 - shn
2
@user995434 但 adjacency_list 是一个 VertexAndEdgeListGraph 的模型,它是 VertexListGraph 的细化。因此 adjacency_list 是 VertexListGraph 的一个模型。 - Andrew Durward
请问您能否给出一个使用copy_graph()的小例子,以便我可以了解如何使用它?因为我总是遇到编译错误。先谢谢您了。 - shn

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接