【问题标题】:c++ boost::graph get parent vertices from directed graphc++ boost::graph 从有向图中获取父顶点
【发布时间】:2012-12-10 09:49:31
【问题描述】:

我有一个有向图(通过 boost::graph 库中的 adjacency_graph 实现),我正在尝试查找某个顶点的父顶点。

过去(通过 pygraph)我只是简单地反转有向图,然后进行邻居搜索,但似乎使用 boost::reverse_graph 反转图会将我的有向图变成双向图,因此我不能使用不再使用相邻顶点方法。

有没有更好的方法来获取父顶点?

谢谢。

这是我当前的示例代码:

#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/reverse_graph.hpp>
#include <iostream>

typedef boost::adjacency_list< boost::setS, boost::vecS, boost::directedS > Graph;
typedef boost::reverse_graph<Graph> Rgraph;
typedef Graph::vertex_descriptor Vertex;

int main()
{
    Graph graph;
    Vertex v0 = boost::add_vertex(graph);
    Vertex v1 = boost::add_vertex(graph);
    Vertex v2 = boost::add_vertex(graph);
    Vertex v3 = boost::add_vertex(graph);
    Vertex v4 = boost::add_vertex(graph);
    Vertex v5 = boost::add_vertex(graph);
    Vertex v6 = boost::add_vertex(graph);

    boost::add_edge(v0,v1,graph);
    boost::add_edge(v1,v2,graph);
    boost::add_edge(v2,v3,graph);
    boost::add_edge(v2,v4,graph);
    boost::add_edge(v3,v5,graph);
    boost::add_edge(v4,v5,graph);
    boost::add_edge(v5,v6,graph);

    Graph::adjacency_iterator ibegin, iend;
    for (boost::tie(ibegin, iend) = boost::adjacent_vertices(v2, graph); ibegin != iend; ++ibegin)
    {
        std::cout << *ibegin << std::endl;
    }

    std::cout << std::endl << "############# RGRAPH #############" << std::endl << std::endl;

    Rgraph rgraph(graph);
    Rgraph::adjacency_iterator rbegin, rend;
    for (boost::tie(rbegin, rend) = boost::adjacent_vertices(v2, rgraph); rbegin != rend; ++rbegin)
    {
        std::cout << *rbegin << std::endl;
    }
    std::cout << std::endl;

    return 0;
}

【问题讨论】:

    标签: c++ boost graph boost-graph directed-acyclic-graphs


    【解决方案1】:

    reverse_graph 要求适配图是BidirectionalGraph 的模型。如果您将图表更改为typedef boost::adjacency_list&lt; boost::setS, boost::vecS, boost::bidirectionalS &gt; Graph;,您的程序将编译并给出结果:

    3
    4
    
    ############# RGRAPH #############
    
    1
    

    我相信这是你应该期待的。

    另一种不需要reverse_graph(但仍需要bidirectionalS)的方法是使用:

    Graph::out_edge_iterator out_begin, out_end;
    for (boost::tie(out_begin, out_end) = out_edges(v2,graph); out_begin != out_end; ++out_begin)
    {   
        std::cout << target(*out_begin,graph) << std::endl;
    }
    std::cout << std::endl;
    
    Graph::in_edge_iterator in_begin, in_end;
    for (boost::tie(in_begin, in_end) = in_edges(v2,graph); in_begin != in_end; ++in_begin)
    {   
        std::cout << source(*in_begin,graph) << std::endl;
    }
    std::cout << std::endl;
    

    【讨论】:

    • 我猜可以将双向图用作有向图,并且只反转它用于父顶点查找?
    • @Shootfast digraph 也使用了双向有向图。neighborsincidents 方法基本上是我用out_edgesin_edges 添加的。根据this 第三模板参数是“一个选择器,用于选择图形是有向、无向还是双向边访问(访问出边和入边)。选项有directedS、undirectedS 和bidirectionalS。”
    • @Shootfast 大写bidirectionalS的代码出错。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-25
    • 1970-01-01
    相关资源
    最近更新 更多