使用BFS查找Boost BGL图中的所有可达顶点

时间:2018-12-20 12:21:21

标签: c++ boost graph boost-graph

我已经构建了一个Boost BGL图:

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;

并且我试图查找(收集)从某个起点(顶点)可到达的所有顶点,这些顶点按其距离排序。这意味着我想创建一个从某个起始顶点可到达的所有顶点的列表,其中“较近”的顶点存储在列表/向量中的较早位置。因此,我(我想)需要BFS。

不幸的是,我没有找到如何解决这个问题而没有编译错误:

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);

这会导致以下错误:

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>>>>'

我的问题:

  1. 有人能说明我的错误吗?或者也许是指向 例子吗?
  2. 是否可能有更好的(就效率而言)或不同的方法来实现该目标?

(尽管我首先要使用“ connected_components” ...但是它使用的DFS无法满足我所拥有的距离/排序标准)。

1 个答案:

答案 0 :(得分:1)

文档说Buffer必须是vertex_descriptors的队列。您不小心声明了它具有vertex_t(顶点属性束)作为值类型。

解决此问题:

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

boost::queue<vertex_descriptor> Q;

它会编译:

Live On Coliru

#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/breadth_first_search.hpp>
#include <variant>
#include <queue>

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);
}