2011-11-22 46 views
2

我目前正在尝试定义boost图的外部属性。我使用一些捆绑的属性内部的:在boost图库中绑定std :: vector的外部属性映射

struct VertexProperties 
{ 
    int demand; 
}; 

struct EdgeProperties 
{ 
    uint capacity; 
    int cost; 
}; 

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

然而,该算法时,我需要一些外部的属性,那就是我希望能够到我的图的边/顶点映射到存储在一个std ::元素矢量,以便我可以通过运算符[](Edge e)访问它们。毫无头绪地站在boost文档的前面。似乎我需要一个property_map,但我不知道如何将这些与矢量一起使用。迄今为止我发现的唯一例子涉及从顶点到矢量的映射,但是由于顶点是无符号整数,这很简单。

我真的升压沮丧我到目前为止,我认为这将有救了我一很多的时间来执行,并通过自己的测试图表类,我真不得到这个疯狂的模板元编程的东西...

+0

Boost.Graph是边缘无法使用;它试图过于普遍,文件很差。我建议写你自己的班级。 –

+0

那么,我已经写了很多使用boost图的代码,我不想重写所有这些... – Exp

回答

5

您可以创建外部属性地图,而不管图中的内部和/或捆绑属性如何。在边上创建属性贴图比较困难,因为您需要一个edge_index贴图,而adjacency_list默认情况下没有这些贴图。 compressed_sparse_row_graph确实,但其结构大多是只读后施工。您可以在边上使用associative_property_map,或者创建边缘索引图作为内部属性(如果您不经常更改图形),填充它,然后使用它构建外部属性图(例如使用shared_array_property_map,例如)。

1

我遇到了同样的问题,最近,这里是我是如何结束的附加顶点属性(称为此代码段中):

// 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; 
+0

如果图的顶点被删除,这会正确地工作吗?谢谢。 – Agostino