首页 文章

外部属性映射绑定到boost图库中的std :: vector

提问于
浏览
2

我目前正在尝试定义增强图的外部属性 . 我使用一些捆绑属性作为内部属性:

struct VertexProperties
{
  int demand;
};

struct EdgeProperties
{ 
  uint capacity;
  int cost;
};

typedef adjacency_list <vecS, vecS, bidirectionalS, VertexProperties, EdgeProperties> Graph;

但是,在算法中我需要一些外部属性,即我希望能够将我的图形的边/顶点映射到存储在std :: vector中的元素,以便我可以通过operator [](Edge)访问它们E) . 我站在提升文档的前面,没有任何线索 . 看起来我需要一个property_map,但我不知道如何将这些与vector一起使用 . 到目前为止,我发现的唯一例子涉及从顶点到矢量的映射,但由于顶点是无符号整数,因此这是微不足道的 .

到目前为止,我对提升感到非常沮丧,我认为这样可以节省我很多时间来自己实现和测试一个图表类,我真的没有得到这个疯狂的模板元编程的东西......

2 回答

  • 1

    无论图形中的内部属性和/或捆绑属性是什么,都可以创建外部属性映射 . 在边缘上创建属性贴图有点困难,因为您需要 edge_index 贴图,而 adjacency_list 默认情况下没有这些贴图; compressed_sparse_row_graph 但是它的结构在构造之后大多是只读的 . 您可以在边缘上使用 associative_property_map ,或者创建边缘索引图作为内部属性(如果您不经常更改图形),填充它,然后使用它来构建外部属性图(例如,使用 shared_array_property_map ) .

  • 5

    我最近遇到了同样的问题,这是我最终附加一个顶点属性(在此代码片段中称为度数)的方式:

    // Graph has to have _index_ property (vector-based graphs get it implicitly)
    typedef typename property_map<Graph, vertex_index_t>::type IndexMap;
    // Storage type for the _degree_ property
    typedef std::vector<uint> DegreeVector;
    // Type of the _degree_ property map
    typedef iterator_property_map<typename DegreeVector::iterator, IndexMap> DegreeMap;
    
    // Graph in question
    Graph g(5);
    // Actual storage
    DegreeVector degree_storage(num_vertices(g));
    // This is needed to construct _degree_ property map
    IndexMap index_map = get(vertex_index, g);
    // Create _degree_ property map
    DegreeMap degree_map = make_iterator_property_map(degree_storage.begin(), index_map);
    
    // Now degree_map is ready to be used
    degree_map[some_vertex_id] = 10;
    

相关问题