6

如何将 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();
4

1 回答 1

7

你试过copy_graph吗?


很难在没有看到错误的情况下知道问题是什么,但如果我不得不猜测,我首先要确保你提供了一个vertex_index地图,因为当你用于顶点存储copy_graph时它默认不可用。setS根据您之前的问题,您似乎已经弄清楚了,所以我们只需要将它们放在一起。

  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();
于 2012-02-13T13:49:59.367 回答