boost::metis_reader实例代码
在进行图论相关算法研究时,常常需要使用到METIS软件包对图进行划分。然而,METIS的输入输出数据格式与其他常用的格式存在区别,因此编写转换程序十分必要。
这里推荐使用 boost::metis_reader 进行 METIS 数据格式到 boost::adjacency_list 格式的转换。下面是一个示例代码,具体使用请参考注释。
#include <iostream>
#include <boost/graph/adjacency_list.hpp>
#include <boost/property_map/property_map.hpp>
#include <boost/graph/metis.hpp>
int main(int argc, char** argv)
{
// 读入 METIS 格式文件
std::string input_file = "example.graph";
boost::read_metis_graph(input_file, g);
// 稀疏图,使用邻接表存储
typedef boost::adjacency_list<boost::vecS, boost::vecS, boost::undirectedS> Graph;
typedef boost::graph_traits<Graph>::vertex_descriptor Vertex;
Graph g; // 定义稀疏图
// 生成 property map,用于访问节点属性
std::vector<Vertex> id_vec(boost::num_vertices(g));
boost::iterator_property_map<std::vector<Vertex>::iterator, boost::property_map<Graph,