如何迭代提升图以获得顶点的传入和传出边?

How to iterate over boost graph to get incoming and outgoing edges of vertex?

我正在尝试迭代增强图。在迭代时,我试图从该顶点找到传入边和传出边。但是我遇到了分段错误。
我尝试调试并找到它抛出分段错误的行。

(顶点->需要找出出入边的图的顶点)
(图 -> 增强图)

//Finding out edges of vertex
    boost::graph_traits<BGType>::out_edge_iterator ei, ei_end;
    boost::tie(ei, ei_end) = out_edges( vertex, graph ); // error at this line
    for( boost::tie(ei, ei_end) = out_edges(vertex, graph); ei != ei_end; ++ei)
    {
        auto target = boost::target ( *ei, graph );
        graph[target]._isVisible = false;
    }

    //Finding in edges of vertex
    boost::graph_traits<BGType>::in_edge_iterator ein, ein_end;
    boost::tie(ein, ein_end) = in_edges( vertex, graph ); // error at this line
    for( boost::tie(ein, ein_end) = in_edges(vertex, graph); ein != ein_end; ++ein)
    {
        auto source = boost::source ( *ein, graph ); 
        graph[source]._isVisible = false;
    }
boost::tie(ei, ei_end) = out_edges( vertex, graph ); // error at this line
boost::tie(ein, ein_end) = in_edges( vertex, graph ); // error at this line

两条评论都表明 graphvertex 无效。检查 graph 是否仍然是对对象的有效引用。

如果是这样,那么“显然”vertex 是不正确的。可能是 out-of-range。考虑检查一下:

void hide_neighbours(BGV vertex, BGType& graph) {
    assert(vertex < num_vertices(graph));

当你在做的时候,我们可以简化实施:

void hide_neighbours(BGV vertex, BGType& graph) {
    assert(vertex < num_vertices(graph));
    using boost::make_iterator_range;

    for (auto e : make_iterator_range(out_edges(vertex, graph)))
        graph[target(e, graph)]._isVisible = false;
    for (auto e : make_iterator_range(in_edges(vertex, graph)))
        graph[source(e, graph)]._isVisible = false;
}

现场演示

Live On Compiler Explorer

#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/graph_utility.hpp>
#include <boost/property_map/function_property_map.hpp>

struct VertexProps {
    bool _isVisible = true;
};

using BGType = boost::adjacency_list< //
    boost::vecS,                      //
    boost::vecS,                      //
    boost::bidirectionalS,            //
    VertexProps>;
using BGV = BGType::vertex_descriptor;

void hide_neighbours(BGV vertex, BGType& graph) {
    assert(vertex < num_vertices(graph));
    using boost::make_iterator_range;

    for (auto e : make_iterator_range(out_edges(vertex, graph)))
        graph[target(e, graph)]._isVisible = false;
    for (auto e : make_iterator_range(in_edges(vertex, graph)))
        graph[source(e, graph)]._isVisible = false;
}

int main() {
    BGType graph(8);
    add_edge(1, 5, graph);
    add_edge(5, 3, graph);
    add_edge(3, 7, graph);
    add_edge(7, 4, graph);

    add_edge(6, 5, graph);
    add_edge(2, 5, graph);

    auto annotate = boost::make_function_property_map<BGV>([&graph](auto v) {
        auto id = std::to_string(v);
        return graph[v]._isVisible ? id : "(" + id + ")";
    });

    print_graph(graph, annotate, std::cout << std::boolalpha);

    hide_neighbours(4, graph);
    print_graph(graph, annotate, std::cout << "\nNeigbours of 4 hidden:\n");

    hide_neighbours(5, graph);
    print_graph(graph, annotate, std::cout << "\nNeigbours of 5 hidden:\n");
}

版画

0 --> 
1 --> 5 
2 --> 5 
3 --> 7 
4 --> 
5 --> 3 
6 --> 5 
7 --> 4 

Neigbours of 4 hidden:
0 --> 
1 --> 5 
2 --> 5 
3 --> (7) 
4 --> 
5 --> 3 
6 --> 5 
(7) --> 4 

Neigbours of 5 hidden:
0 --> 
(1) --> 5 
(2) --> 5 
(3) --> (7) 
4 --> 
5 --> (3) 
(6) --> 5 
(7) --> 4 

请注意,如果您指定了一个无效的顶点,您将得到:

sotest: /home/sehe/Projects/Whosebug/test.cpp:17: void hide_neighbours(BGV, BGType&): Assertion `vertex < num_vertices(graph)' failed.

其他提示

如果上述没有立即 remove/expose 问题,那么您遇到了 Undefined Behaviour。也许 graph 确实是一个悬空引用,或者 graph 已被非法修改(例如,删除未清除的顶点)。

如果您在查找此类原因方面需要帮助,请随时 post 另一个问题,其中包含更多相关代码。