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

标签 c++ boost indexing vertex boost-graph

由于我的图使用 setS 作为顶点,我必须为我的图提供一个 vertex_index 属性映射,或者为 write_graphviz 提供一个明确的 vertex_id 参数,以便能够使用 write_graphviz。 My graph is defined as: typedef adjacency_list<setS, setS, undirectedS, NodeData, EdgeData> Graph; 其中 NodeData 和 EdgeData 是结构体。 您能否给我一个非常简单的示例,说明如何为我的图形提供 vertex_index 属性映射?或者如何给 write_graphviz 一个显式的 vertex_id 参数?

谢谢

最佳答案

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

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

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

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

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

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

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

关于c++ - 如何为我的图形提供 vertex_index 属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7935417/

相关文章:

c++: STL 对迭代器使用通用的 "end()"值?

c++ - 是否将指针移动到结构成员 UB 的后面?并访问它?

c++ - 当ini文件不存在时使用Boost属性树读取INI文件

c++ - shared_ptr 的子数据(类似子字符串?)

python - 迭代 `np.where` 的输出

c++ - QListView 模型用户界面

c++ - 错误 C4430 : missing type specifier

c++ - boost .Asio : Async operations timeout

mysql - 索引 SET 字段

c - 使用 C 中的索引访问枚举