boost图形从txt文件循环创建图形

时间:2014-09-09 14:08:23

标签: c++ boost graph

问题可能与:Using boost graph library: how to create a graph by reading edge lists from file

有关 然而,答案并没有真正帮助我。

我想使用boost的adjacency_list类创建一个图表。数据位于包含两列的.txt文件中。

样品:

5 14
7 2
3 18
21 207
...

如果我能做到以下几点会很棒:

std::ifstream data("data.txt");
typedef adjacency_list<> Graph;
Graph g; 
while (data >> nodeA >> nodeB){
   add_edge(nodeA, nodeB, g);
} 

,如前所述:boost graph understanding vertex creation behaviour

由于文章中讨论的原因,

不起作用。我还经历过各种各样的例子,包括来自增强图纪录片:http://www.boost.org/doc/libs/1_36_0/libs/graph/example/family-tree-eg.cpp(全部由&#34;手&#34;),

或来自书籍#34; Boost Graph Library&#34; p.44(我似乎无法编译)。因此,经过几天的反复试验并查看上述来源后,我仍然无法弄清楚如何从文件中正确创建图表。

理想情况下,我想按照以下步骤进行操作:

while (data >> nodeA >> nodeB){
   graph_traits<Graph>::vertex_descriptor *newVertex = new graph_traits<Graph>::vertex_descriptor;
   newVertex = vertex(nodeA,g); 
   graph_traits<Graph>::vertex_descriptor *otherVertex = new graph_traits<Graph>::vertex_descriptor;
   otherVertex = vertex(nodeB,g); 
   add_edge(newVertex, otherVertex,g);
}

类似于:www.boost.org/doc/libs/1_56_0/libs/graph/example/undirected_adjacency_list.cpp

但是我收到了编译错误。

无论如何,我真的很感激帮助弄清楚如何从txt文件创建图表。

提前谢谢你们,祝福你们。

1 个答案:

答案 0 :(得分:1)

  

,正如这里已经讨论的那样:提升图了解顶点创建行为   由于文章

中讨论的原因不起作用

我没有看到眼前的问题。除非您的顶点非常稀疏(例如,您有一个节点ID,例如99,999,999,999,并且memery使用会造成问题)。

否则,你可以

  • 保留id -> vertex_descriptor
  • 的(bi)地图
  • 使用顶点属性doc