BGL通过键索引顶点

时间:2015-08-30 11:45:36

标签: c++ boost boost-graph

我的要求是有一个图形结构,其中每个顶点由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;
}

对于列表来自顶点的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. 查找顶点
  3. 遍历boost::property_map
  4. 将外部std::mapbimapindex属性同步

1 个答案:

答案 0 :(得分:2)

我记得这个库有一个labeled_graph实用程序,大致支持这个。它具有很高的便利性,我似乎记得它在效率方面不那么有趣¹。应该有一个使用它的样本:

无论如何(并回顾您之前的问题),您当然可以使用外部属性地图。这有好处:

  • 您可以随时保留不在图表中的条目
  • 您可以使用所需的反向索引,例如参见

回答子弹:

  1.   

    通过boost::vertex_index_t值找到顶点

    是的,但是如果你想提高效率,确实你需要有一个外部映射用于反向查找 OR 使用你自己的数据结构并使其适应model the Graph Concepts you require(更多工作,显然)

  2.   

    遍历boost::property_map

    你可以。使用boost::get(tag, graph)获取属性映射,遍历要访问的所有实体并调用每个属性的属性映射。 E.g。

    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.   

    将外部std::mapbimapindex属性同步

    以上链接应该为您提供想法

  4. ¹可以解释我自己没有用过它。

相关问题