【发布时间】:2017-02-28 06:41:56
【问题描述】:
我正在尝试开始使用非常好的 Boost 图形库。
第一步是从文件中读取图形,其中每行包含由空格分隔的边。
我似乎无法正确完成它。我当前的代码如下所示:
..include stuff..
using namespace boost;
typedef boost::adjacency_list<listS, vecS, undirectedS> Graph;
Graph get_graph(char* str1){
Graph g;
std::ifstream infile(str1);
std::string line;
int e1;
int e2;
//std::map<int,g> VertexList;
while (std::getline(infile, line)){
std::istringstream iss(line);
//std::cout<<line<<"\n";
std::vector<std::string> strs;
split(strs, line, is_any_of(" "));
//std::cout<<" Adding edge: " << strs[0]<< " to " << strs[1] << "\n";
std::istringstream ss(strs[0].substr(0,5));
ss >> e1;
std::istringstream s2(strs[1].substr(0,5));
s2 >> e2;
//ADD e1 and e2 to a graph and connect them with an edge somehow!
// VertexList[123]=boost::add_vertex(g);
//Graph::vertex_descriptor v2 = boost::add_vertex(g);
//add individual vertices to the graph..
//e1 = add_vertex(g);
//e2 = add_vertex(g);
//add the corresponding edge..
}
std::cout<<num_vertices(g)<<std::endl;
return g;
}
int main(int argc, char *argv[]){
Graph g = get_graph(argv[1]);
//degree_vec(g);
return 0;
}
文件示例:
214328887 34428380
17116707 28465635
380580781 18996905
221036078 153460275
107830991 17868918
151338729 222261763
19705747 34428380
222261763 88323281
19933035 149538028
158419434 17434613
我尝试过并且效果不错的方法:
Using boost graph library: how to create a graph by reading edge lists from file,但它读取边缘列表,这对我没有帮助,因为我无法调用函数,绑定到邻接列表(或者我可以吗?)
使用
add_edge(e1,e2,g)不起作用,因为在这种情况下e1和e2只能是长度为1(?)的无符号整数,如果我使用add_edge(123,32,g)会抛出一些奇怪的结果. (就像我计算顶点时,有 122 个,而不是 1 个)使用
vertex_descriptors,例如Graph::vertex_descriptor e1 = boost::add_vertex(g);然后使用这些描述符添加边,但我似乎无法使其工作(正如我所说,这对我来说相对较新)
【问题讨论】: