与 boost 图形库中的 std::vector 相关的外部属性映射
我目前正在尝试定义增强图的外部属性。我使用一些捆绑属性作为内部属性:
struct VertexProperties
{
int demand;
};
struct EdgeProperties
{
uint capacity;
int cost;
};
typedef adjacency_list <vecS, vecS, bidirectionalS, VertexProperties, EdgeProperties> Graph;
但是,在算法过程中,我需要一些外部属性,也就是说,我希望能够将图形的边/顶点映射到存储在 std::vector 中的元素,这样我就可以可以通过操作符[](Edge e)访问它们。我站在 boost 文档前面,毫无头绪。看来我需要一个 property_map,但我不知道如何将它们与向量一起使用。到目前为止,我发现的唯一示例涉及从顶点到向量的映射,但由于顶点是无符号整数,因此这是微不足道的。
到目前为止,我对 boost 感到非常沮丧,我认为它会节省我很多时间来自己实现和测试图形类,我真的不明白这种疯狂的模板元编程的东西......
I am currently trying to define external properties of a boost graph. I use some bundled properties as internal ones:
struct VertexProperties
{
int demand;
};
struct EdgeProperties
{
uint capacity;
int cost;
};
typedef adjacency_list <vecS, vecS, bidirectionalS, VertexProperties, EdgeProperties> Graph;
However, during the algorithm I need some external properties, that is I want to be able to map edges/vertices of my graph to elements stored in a std::vector in such a way that I can access them via operator[] (Edge e). I am standing in front of the boost documentation without a clue. It seems like I need a property_map, but I don't know how to use these together with vectors. The only examples that I found so far concern maps from vertices to a vector, but as the vertices are unsigned ints this is trivial.
I am really frustrated by boost so far, I think it would have saved me a lot of time to implement and test a graph class by myself, I really don get this crazy template metaprogramming stuff...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
无论图表中有哪些内部和/或捆绑属性,您都可以创建外部属性映射。在边缘上创建属性映射有些困难,因为您需要一个
edge_index
映射,而adjacency_list
默认情况下没有这些映射;compressed_sparse_row_graph
确实如此,但其结构在构造后大多是只读的。您可以在边缘上使用associative_property_map,或者创建边缘索引图作为内部属性(如果您不经常更改图形),填充它,然后使用它来构建外部属性映射(例如,使用shared_array_property_map
)。You can create external property maps regardless of what internal and/or bundled properties are in your graph. Creating property maps on edges is somewhat more difficult because you need an
edge_index
map, andadjacency_list
doesn't have those by default;compressed_sparse_row_graph
does but its structure is mostly read-only after construction. You can either useassociative_property_map
on the edges, or create an edge index map as an internal property (if you don't change your graph too often), fill it, then use it to build the external property map (usingshared_array_property_map
, for example).我最近遇到了同样的问题,以下是我最终附加顶点属性的方法(在此代码片段中称为度):
I ran into the same problem recently, and here is how I ended up attaching a vertex property (called degree in this code snippet):