【问题标题】:External property map tied to std::vector in boost graph library与 boost 图形库中的 std::vector 关联的外部属性映射
【发布时间】:2012-01-04 07:34:09
【问题描述】:

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

struct VertexProperties
{
  int demand;
};

struct EdgeProperties
{ 
  uint capacity;
  int cost;
};

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

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

到目前为止,我对 boost 感到非常沮丧,我认为它会为我节省 很多 时间来自己实现和测试一个图形类,我真的不明白这种疯狂的模板元编程的东西。 ..

【问题讨论】:

  • Boost.Graph 边缘无法使用;它试图过于通用并且文档很差。我建议编写自己的课程。
  • 嗯,我已经用 boost graph 写了很多代码了,我不想全部重写...

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


【解决方案1】:

我最近遇到了同样的问题,这就是我最终附加顶点属性的方式(在此代码 sn-p 中称为 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;

【讨论】:

  • 如果图的一个顶点被删除,这会正常工作吗?谢谢。
【解决方案2】:

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

【讨论】:

    猜你喜欢
    • 2014-02-27
    • 1970-01-01
    • 2012-01-26
    • 1970-01-01
    • 1970-01-01
    • 2018-06-17
    • 1970-01-01
    • 2011-09-17
    • 1970-01-01
    相关资源
    最近更新 更多