【问题标题】:Print boost graph with custom vertex label使用自定义顶点标签打印提升图
【发布时间】:2013-12-04 04:46:58
【问题描述】:

我想打印一个具有自定义(字符串)顶点标签而不是默认顶点编号标签 0,1,2...的 Boost Graph...

我已将图形初始化为:

typedef adjacency_list <vecS, vecS, directedS, 
                        property<vertex_name_t,string>
                       > Graph;

Graph g;
set<string> names; 
map<string,Graph::vertex_descriptor> vertex ;

for(auto it = names.begin() ; it != names.end(); ++it )
       vertex[*it] =  add_vertex(*it,g) ;

现在我应该如何打印这个图表,以便我得到 abc -&gt; xyz; 而不是 1-&gt;2; 形式的边

【问题讨论】:

    标签: c++ boost graph


    【解决方案1】:

    例如,您可以从顶点描述符中get 该名称

    graph_traits<Graph>::edge_iterator it, end;
    for(tie(it, end) = edges(g); it != end; ++it)
        std::cout << get(vertex_name, g)[source(*it, g)] << " -> "
                  << get(vertex_name, g)[target(*it, g)] << '\n';
    

    【讨论】:

      【解决方案2】:

      我不是真正精通 BGL 的用户,但我发现您的方法存在一些问题:

      1) 您似乎声明了一个内部属性vertex_name,但随后使用了一个外部存储set&lt;string&gt;

      2) 除此之外,我看不到任何实际填充名称的代码,无论是在内部还是外部的情况下——但我假设你只是为了简洁而省略了它。

      但是现在提出一个解决方案:BGL 实际上提供了一种方法来做你想做的事:boost::print_graph

      using namespace boost;
      typedef adjacency_list <vecS, vecS, directedS, 
                              property<vertex_name_t,string>
                             > Graph;
      Graph g;
      
      std::set<string> names;
      // somehow populating names here...
      
      // creating the vertices and storing the names as internal properties
      for(auto it = names.begin() ; it != names.end(); ++it )
      { 
        auto v = add_vertex(g);
        put(vertex_name,g,/*vertex descriptor*/ *v,/*vertex name*/ *it);
      }
      print_graph(g,get(vertex_name,g)); //NOTE: you could also use an external property map or an adaptor
      

      【讨论】:

        猜你喜欢
        • 2011-03-07
        • 1970-01-01
        • 2011-12-24
        • 2012-01-23
        • 2018-02-01
        • 2023-03-31
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多