我正在根据以下建议构建图形类:Modifying vertex properties in a Boost::Graph
不幸的是,我意识到了一个意想不到的行为。当使用我自己的顶点属性时(为简单起见请忽略边缘属性),内置属性似乎没有被使用。 例如,当我有:
typedef adjacency_list<
setS, // disallow parallel edges
listS, // vertex container
undirectedS, // undirected graph
property<vertex_properties_t, VERTEXPROPERTIES>,
property<edge_properties_t, EDGEPROPERTIES>
> GraphContainer;
我可以毫无问题地检索我的自定义属性,但是当我想检索 vertex_index-property 时,我总是为每个顶点获得相同的值,这意味着值为 0。节点是不同的,这由 num_vertices(我的图表)。 然后我认为这可能是由于缺少内置属性,所以我尝试了:
typedef adjacency_list<
setS, // disallow parallel edges
listS, // vertex container
undirectedS, // undirected graph
property<vertex_index_t, unsigned int , property< vertex_properties_t, VERTEXPROPERTIES> >,
property<edge_properties_t, EDGEPROPERTIES>
> GraphContainer;
同样,当想要检索任何顶点的索引时,我得到的值为 0。 这种行为正常吗?使用自定义属性时是否也必须设置内置属性?
最佳答案
自从我使用 Boost.Graph 以来已经有很长时间了,但是谷歌搜索“vertex_index_t”,in hit #5 Andrew Sutton says :
Just declaring a vertex index as a property (either bundled or interior, as here) won't buy you any new functionality. It just provides a place where you can assign an index for each vertex or edge. The problem that this half-solves is that nearly every algorithm in the distro requires a vertex index map (or edge index map, more rarely), and providing this will allow the default arguments to automatically extract a property map for vertices/edges.
It won't - or shouldn't??? automatically assign indices. Also, if you remove a vertex or edge, you may have to renumber vertices.
看来这个想法是要在想要读取数字的算法之间标准化概念,但您仍然必须自己写入。
关于c++ - 在 Boost::Graph 中使用自定义和内置属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2411017/