如何为我的图形提供 vertex_index 属性

2021-12-21 00:00:00 indexing c++ boost boost-graph vertex

由于我的图使用 setS 作为顶点,我必须为我的图提供一个 vertex_index 属性映射,或者为 write_graphviz 提供一个显式的 vertex_id 参数,以便能够使用 write_graphviz.我的图定义为:typedef adjacency_list图;其中 NodeData 和 EdgeData 是结构.你能给我一个非常简单的例子来说明如何为我的图形提供一个 vertex_index 属性映射吗?或者如何给 write_graphviz 一个明确的 vertex_id 参数?

Since my graph use setS for vertex, I have to either provide a vertex_index property map for my graph, or give an explicit vertex_id argument to write_graphviz, to be able to use write_graphviz. My graph is defined as: typedef adjacency_list<setS, setS, undirectedS, NodeData, EdgeData> Graph; Where NodeData and EdgeData are structures. Can you please give me a very simple example of how to provide a vertex_index property map for my graph ? or how to give an explicit vertex_id argument to write_graphviz ?

谢谢

推荐答案

解决方案就是:1) 假设顶点描述符被定义为 typedef Graph::vertex_descriptor NodeID; 那么你需要定义一个关联属性映射如下:

The solution is just to: 1) Say the vertex descriptor is defined as typedef Graph::vertex_descriptor NodeID; then you need to define an associative property map as following:

typedef map<NodeID, size_t> IndexMap;
IndexMap mapIndex;
associative_property_map<IndexMap> propmapIndex(mapIndex);

2) 在代码中,索引所有顶点如下:

2) In the code, index all vertices as following:

int i=0;
BGL_FORALL_VERTICES(v, g, Graph)
{
   put(propmapIndex, v, i++);
}

3) 您现在可以使用 graphvize 来绘制/可视化您的图表,如下所示:

3) You can now use graphvize to drow/visualize your graph as following:

ofstream myfile;
myfile.open ("example.txt");
write_graphviz(myfile, g, default_writer(), default_writer(), default_writer(), propmapIndex);
myfile.close();

图表将在example.txt中进行描述,您可以使用graphviz对其进行可视化.

The graph will be described in example.txt, you can visualize it using graphviz.

相关文章