BGL按键索引顶点

BGL indexing a vertex by keys

本文关键字:顶点 索引 BGL      更新时间:2023-10-16

我的要求是有一个图结构,其中每个顶点都是由boost::uuids::uuid唯一标识的。所有顶点都有一个颜色属性,相似类别的顶点将根据颜色属性分组。我不是在静态地图上工作,顶点和边缘将动态创建和删除。

typedef boost::adjacency_list<
      boost::listS,
      boost::listS,
      boost::bidirectionalS,
      boost::property<boost::vertex_index_t, boost::uuids::uuid, 
        boost::property<boost::vertex_color_t, resource_color,
          boost::property<boost::vertex_underlying_t,   boost::shared_ptr<actual_object*> > > >,
      detail::edge_property
      > graph_type;
graph_type _graph;
boost::property_map<graph_type, boost::vertex_index_t>::type    _index_map;
boost::property_map<graph_type, boost::vertex_color_t>::type    _color_map;
boost::property_map<graph_type, boost::vertex_underlying_t>::type   _underlying_map;

构造函数我创建了所有3个地图

_index_map = boost::get(boost::vertex_index_t(), _graph);
_color_map = boost::get(boost::vertex_color_t(), _graph);
_underlying_map = boost::get(boost::vertex_underlying_t(), _graph);

添加一个顶点

add_resource(resource_color c, actual_object* o){
  graph_type::vertex_descriptor v = boost::add_vertex(o->uuid(), _graph);
  _color_map[v] = c;
  _underlying_map[v] = o;
}

For list一个顶点的UUID

uuid_list list;
boost::graph_traits<graph_type>::vertex_iterator vi, vi_end;
for(boost::tie(vi, vi_end) = boost::vertices(_graph); vi != vi_end; ++vi){
  list.push_back(_index_map[*vi]);
}
return list; 

这样我总是迭代图的顶点并获得它的属性。但是我也想要另一种方式。从UUID到顶点,就像一个并行的std::map,它将通过添加/删除操作或类似的操作自动更新。

我也不能保持外部std::map和手动同步,因为boost::adjacency_list<boost::listS, boost::listS>::vertex_descriptor计算为void*,我需要序列化支持。

那么下面的事情是可行的吗

  1. 通过boost::vertex_index_t值查找顶点
  2. 遍历boost::property_map
  3. std::mapbimapindex属性同步

我记得库中有一个labeled_graph实用程序大致支持这一点。它有一个很高的便利系数,我记得它在效率方面不那么有趣。应该有一个使用它的示例:

  • http://www.boost.org/doc/libs/1_46_1/libs/graph/example/labeled_graph.cpp

无论如何(并参考您前面的问题),您当然可以使用外部属性映射。这有以下好处:

  • 你可以一直保留不在图表中的条目
  • 你可以有你需要的反向索引,例如

    • 图形的切割集,增强图形库(用于获得颜色地图的反向查找)。

      它也包含一个等效的方法,但使用Boost多索引容器,甚至更灵活

回答子弹:

  1. find vertex through boost::vertex_index_t value

    是的,但是如果你想要高效,你确实需要有一个外部映射的反向查找使用你自己的数据结构,并适应它来建模你需要的图形概念(更多的工作,显然)

  2. 遍历boost::property_map

    。使用boost::get(tag, graph)获取属性映射,遍历要访问的所有实体,并为每个属性调用属性映射。例如

    boost::property_map<Graph, boost::vertex_index_t>::type pmap = boost::get(boost::vertex_index, graph);
    boost::graph_traits<Graph>::vertex_iterator b, e;
    for (boost::tie(b,e) = boost::vertices(graph); b!=e; ++b)
        std::cout << "The vertex ID is: " << boost::get(pmap, *b) << "n";
    
  3. 使用index属性同步外部std::mapbimap

    上面的链接应该给你一些想法