【问题标题】:boost graph create graph in loop from txt fileboost graph 从 txt 文件循环创建图
【发布时间】:2014-09-09 14:08:22
【问题描述】:

问题可能与: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

由于文章中讨论的原因而不起作用。我还浏览了各种示例,包括来自 boost graph 纪录片:http://www.boost.org/doc/libs/1_36_0/libs/graph/example/family-tree-eg.cpp(都是“手工”制作的),

或来自“Boost Graph Library”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 文件创建图表,我将不胜感激。

在此先感谢你们,并致以最良好的祝愿。

【问题讨论】:

    标签: c++ boost graph


    【解决方案1】:

    正如这里已经讨论过的:增强图形理解顶点创建行为 由于文章中讨论的原因不起作用

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

    否则你可以

    • 保留id -&gt; vertex_descriptor 的 (bi)map
    • 使用顶点属性doc

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-01-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-09-02
      • 1970-01-01
      相关资源
      最近更新 更多