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

时间:2011-11-22 22:38:45

标签: c++ boost graph boost-graph boost-property-map

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

struct VertexProperties
{
  int demand;
};

struct EdgeProperties
{ 
  uint capacity;
  int cost;
};

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

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

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

2 个答案:

答案 0 :(得分:5)

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

答案 1 :(得分:1)

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

// 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;
相关问题