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

发布于 2025-01-05 09:26:31 字数 429 浏览 7 评论 0原文

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

土豪 2025-01-12 09:26:31

您尝试过 copy_graph 吗?


在没有看到错误的情况下很难知道问题是什么,但如果我不得不猜测,我首先确保您提供了到 copy_graphvertex_index 映射,因为它不是当您使用 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();

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();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文