Search code examples
c++boostgraphboost-graph

Iterate over boost graph without Visitor


I need to iterate over a graph(DFS), but without using the standard DFS visitor callback technique.

Is there a way to iteratively traverse the graph in this manner ?

for(each edge in my graph visited in dfs) {
    do some complicated stuff;
}

Solution

  • Yes, depending on the concrete graph type, you can just do

    auto e = edges(g);
    for (auto it = e.first; it != e.second; ++it)
    {
    }
    

    You might want in_edges(g) or out_edges(g) if your graph models different concepts: http://www.boost.org/doc/libs/1_55_0/libs/graph/doc/graph_concepts.html

    Edit Update to the comments:

    You'll have to wrap it yourself then. You could force a pull interface using Boost Coroutine. Or you can use the visitor to fill a queue, which you consumer after the DFS completed.