【问题标题】:adjacency_list with VertexList different from vecS与 vecS 不同的 VertexList 的 adjacency_list
【发布时间】:2011-10-14 13:16:07
【问题描述】:

我有两个包含一些字段的结构:struct MyNodeData 和 struct MyEdgeData。当我使用 VertexList 作为 vecS 创建图形时,访问顶点的描述符等没有问题。例如:

typedef adjacency_list<setS, vecS, undirectedS, MyNodeData, MyEdgeData> Graph;

typedef Graph::vertex_descriptor MyNodeDataID;
typedef Graph::edge_descriptor MyEdgeDataID;
typedef graph_traits < Graph >::vertex_iterator VertexIterator;
typedef graph_traits < Graph >::edge_iterator EdgeIterator;
typedef graph_traits < Graph >::adjacency_iterator AdjacencyIterator;
typedef property_map < Graph, vertex_index_t >::type IndexMap;

Graph g;
const IndexMap index = get(vertex_index, g);

/* Puis après avoir ajouté des vertex et edges, je peux accéder par exemple à la liste des vertex comme suite: */
pair<VertexIterator, VertexIterator> vi;
for(vi = vertices(g); vi.first != vi.second; ++vi.first)
{
   cout << "vertex: " << index[*vi.first] << endl;
   // or: cout << "vertex: " << *vi.first << endl;
}

但我通常需要从图表中添加/删除边和顶点。所以我想使用 setS 或 listS 作为 VertexList,而不是 vecS,因为使用 vecS,当我们删除其中一个索引时,索引会失效! 问题是,如果我将 VertexList 定义为 setS 或 listS,我将无法像以前一样浏览顶点/边列表并访问那里的描述符!

简而言之,我的问题是:由于使用 listS 或 setS 作为顶点容器的 adjacency_list 不会自动提供此 vertex_id 属性,如何将其添加到上面的代码中?

【问题讨论】:

  • 嗨!你能把cmets翻译成英文吗? :) 问候

标签: c++ boost graph boost-graph adjacency-list


【解决方案1】:

目前,您只需要提供关联属性映射。

<...>
typedef Graph::vertex_descriptor NodeID;

typedef map<NodeID, size_t> IndexMap;
IndexMap mapIndex;
associative_property_map<IndexMap> propmapIndex(mapIndex);
<...>

// indexing all vertices
int i=0;
BGL_FORALL_VERTICES(v, g, Graph)
{
   put(propmapIndex, v, i++);
}

【讨论】:

    【解决方案2】:

    但我通常需要在图表中添加/删除边和顶点。

    使用 vecS、setS listS 可以删除顶点和边。只需使用 vertex\edge 描述符调用 remove_vertex\remove_edge 。 在上述所有容器中,删除 \ 添加顶点 \ 边将使迭代器无效。这意味着在您修改了图形之后,您必须再次调用 vertices(g)。在大多数容器中,修改容器会使其迭代器无效。 在 listS 中,添加顶点可能不会使迭代器无效,但这是特定于实现的,不应依赖。

    您可以将 vertex_id 属性添加到图形中,从而使您可以随时访问顶点描述符。

    【讨论】:

      猜你喜欢
      • 2011-03-12
      • 1970-01-01
      • 1970-01-01
      • 2021-05-08
      • 2021-02-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-03-17
      相关资源
      最近更新 更多