c++boostgraphboost-graph

Find all reachable vertices in a Boost BGL graph using BFS


I have constructed a boost BGL graph:

using vertex_t = std::variant<node_t, specialNode_t>; // structs
using edge_t = std::variant<TerminalType>;            // enum

using Graph_t = boost::adjacency_list<
    boost::vecS,
    boost::vecS,
    boost::undirectedS,
    vertex_t,
    edge_t>;

Graph_t myGraph;

and I'm trying to find (collect) all vertices reachable from a certain starting point (vertex) sorted by their distance. That means I'd like to create a list of all vertices reachable from a certain starting vertex where "nearer" vertices are stored earlier in the list/vector. Therefore I (think I) need BFS.

Unfortunately I failed to find out how to do that without compile error:

boost::queue<vertex_t> Q;
boost::default_bfs_visitor vis; // Will do my collecting visitor later...

auto indexmap = boost::get(boost::vertex_index, myGraph);
auto colormap = boost::make_vector_property_map<boost::default_color_type>(indexmap);

boost::breadth_first_visit(myGraph, start, std::ref(Q), vis, colormap);

this leads to the following errors:

Error   C2039   'push': is not a member of 'std::reference_wrapper<boost::queue<ListSim::vertex_t,std::deque<_Tp,std::allocator<_Ty>>>>'
Error   C2039   'empty': is not a member of 'std::reference_wrapper<boost::queue<ListSim::vertex_t,std::deque<_Tp,std::allocator<_Ty>>>>'
Error   C2039   'top': is not a member of 'std::reference_wrapper<boost::queue<ListSim::vertex_t,std::deque<_Tp,std::allocator<_Ty>>>>'
Error   C2039   'pop': is not a member of 'std::reference_wrapper<boost::queue<ListSim::vertex_t,std::deque<_Tp,std::allocator<_Ty>>>>'
Error   C2039   'push': is not a member of 'std::reference_wrapper<boost::queue<ListSim::vertex_t,std::deque<_Tp,std::allocator<_Ty>>>>'

My questions:

  1. Can anyone shed some light on my mistake? Or maybe a pointer to an example?
  2. Is there possibly a better (in terms of efficiency) or different approach to reach that goal?

(I though about using "connected_components" first... but it uses DFS which cannot fulfill the distance/sorting criteria I have).


Solution

  • The docs say that the Buffer needs to be a queue of vertex_descriptors. You accidentally declared it to have vertex_t (the vertex property bundle) as the value type.

    Fix it:

    using vertex_descriptor = boost::graph_traits<Graph_t>::vertex_descriptor;
    
    boost::queue<vertex_descriptor> Q;
    

    And it compiles:

    Live On Coliru

    #include <boost/graph/adjacency_list.hpp>
    #include <boost/graph/breadth_first_search.hpp>
    #include <variant>
    
    struct node_t {
    };
    struct specialNode_t {
    };
    enum class TerminalType {
    };
    
    using vertex_t = std::variant<node_t, specialNode_t>; // structs
    using edge_t = std::variant<TerminalType>;            // enum
    
    using Graph_t = boost::adjacency_list<boost::vecS, boost::vecS, boost::undirectedS, vertex_t, edge_t>;
    
    int main() {
        Graph_t myGraph(5);
        boost::default_bfs_visitor vis; // Will do my collecting visitor later...
    
        auto indexmap = boost::get(boost::vertex_index, myGraph);
        auto colormap = boost::make_vector_property_map<boost::default_color_type>(indexmap);
    
        using vertex_descriptor = boost::graph_traits<Graph_t>::vertex_descriptor;
    
        boost::queue<vertex_descriptor> Q;
        boost::breadth_first_visit(myGraph, 0, Q, vis, colormap);
    }