【问题标题】:print or iterate over the vertices with vertex_descriptor boost graph使用 vertex_descriptor boost graph 打印或迭代顶点
【发布时间】:2018-08-07 00:04:41
【问题描述】:

构建一个提升图,其中添加了边缘区域(如下)并且 v1 定义为

gm->create_edge(v1,v2,1); 
boost::graph_traits<Graph>::vertex_descriptor v1 = gm-> create_vertex("one",1,1);

如何使用打印或枚举顶点?:

boost::graph_traits<Graph>::vertex_descriptor vd;

这允许我打印顶点 ID:

Graph::vertex_iterator v, vend, vnext; 
for (boost::tie(v, vend) = vertices(gm->g); v != vend; ++v)
    std::cout << gm->g[*v].id << ", "  << gm->g[*v].color << ", ";

我有一个节点度数的函数,它需要一个顶点描述符,那么,我该如何传递呢?

gm->degree( takes vertex_descriptor )?

links 已关闭。

【问题讨论】:

    标签: c++ boost graph vertex descriptor


    【解决方案1】:

    vertices(g) 返回一个迭代器对。间接迭代器给出描述符。在您自己的示例中,vvertex_iterator,所以 *vvertex_descriptor

    让我们使用 Boost 的 degree 函数 - 它也需要一个描述符:

    Live On Coliru

    #include <boost/graph/adjacency_list.hpp>
    
    namespace MyProgram {
        using namespace boost;
        struct VertexProps { int id; default_color_type color; };
    
        using Graph = adjacency_list<vecS, vecS, directedS, VertexProps>;
    
        Graph make_sample_graph();
    }
    
    #include <iostream>
    
    int main() {
        using MyProgram::Graph;
        Graph g = MyProgram::make_sample_graph();
    
        Graph::vertex_iterator v, vend;
        for (boost::tie(v, vend) = vertices(g); v != vend; ++v) {
            std::cout << "Vertex descriptor #" << *v 
                 << " degree:" << degree(*v, g)
                 << " id:"     << g[*v].id
                 << " color:"  << g[*v].color
                 << "\n";
        }
    }
    

    不过我更喜欢使用 ranged-for:

    Live On Coliru

    for (auto vd : boost::make_iterator_range(vertices(g))) {
        std::cout << "Vertex descriptor #" << vd 
             << " degree:" << degree(vd, g)
             << " id:"     << g[vd].id
             << " color:"  << g[vd].color
             << "\n";
    }
    

    为了完整性,make_sample_graph()

    namespace MyProgram {
        Graph make_sample_graph() {
            Graph g(10);
            for (auto vd : boost::make_iterator_range(vertices(g)))
                g[vd] = { int(vd)*100, {} };
    
            add_edge(1,2,g);
            add_edge(2,3,g);
            add_edge(4,5,g);
            add_edge(4,6,g);
            add_edge(6,7,g);
    
            return g;
        }
    }
    

    打印:

    Vertex descriptor #0 degree:0 id:0 color:0
    Vertex descriptor #1 degree:1 id:100 color:0
    Vertex descriptor #2 degree:2 id:200 color:0
    Vertex descriptor #3 degree:1 id:300 color:0
    Vertex descriptor #4 degree:2 id:400 color:0
    Vertex descriptor #5 degree:1 id:500 color:0
    Vertex descriptor #6 degree:2 id:600 color:0
    Vertex descriptor #7 degree:1 id:700 color:0
    Vertex descriptor #8 degree:0 id:800 color:0
    Vertex descriptor #9 degree:0 id:900 color:0
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-03-25
      • 2015-04-28
      • 2012-11-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多