c++ - 如何使用 boost 图库广度优先搜索创建遍历顶点的队列?

标签 c++ boost breadth-first-search

我想使用 boost 图库广度优先搜索来返回在节点 1 上启动时访问的顶点队列。我阅读了文档,但仍然无法理解如何实现这一点。

下面的结果将按顺序返回一个队列:1,2,3,4 或 1,3,2,4

typedef boost::property<boost::edge_weight_t, unsigned int> EdgeWeightProperty;
typedef boost::adjacency_list<boost::setS, boost::vecS, boost::undirectedS, 
                              boost::no_property, EdgeWeightProperty> Graph;

//create graph and add edge
Graph g;
boost::add_edge(1,2,6,g);
boost::add_edge(2,3,6,g);
boost::add_edge(3,1,6,g);
boost::add_edge(3,4,6,g);

//Perform breadth first using boost and return result in a queue.

最佳答案

boost 图形库文档定义了 BFS Visitor concept其中指出

Users can define a class with the BFS Visitor interface and pass an object of the class to breadth_first_search(), thereby augmenting the actions taken during the graph search.

基于此和 bfs.cpp example完成您所要求的首选方法是将 boost::visitor 进行子类化或boost::default_bfs_visitor并覆盖其中的一个或多个

  • initialize_vertex
  • discover_vertex
  • examine_vertex
  • examine_edge
  • tree_edge
  • non_tree_edge
  • gray_target
  • black_target
  • finish_vertex

对于您的用例,我认为最适合子类 boost::default_bfs_visitor并覆盖discover_vertex :

discover_vertex(s, g)

Invoked when a vertex is encountered for the first time. s is An object of type boost::graph_traits<Graph>::vertex_descriptor and g is an object of type Graph.

下面是具体如何做到这一点的示例,其中适当处理 std::queue对象来记录顶点。

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

typedef boost::property<boost::edge_weight_t, unsigned int> EdgeWeightProperty;
typedef boost::adjacency_list<boost::setS, boost::vecS, boost::undirectedS, 
                              boost::no_property, EdgeWeightProperty> Graph;

class BFSVisitor : public boost::default_bfs_visitor {
public:    
    BFSVisitor(std::queue<Graph::vertex_descriptor>& _v) : visited(_v){}
    void discover_vertex(Graph::vertex_descriptor s, const Graph &g) {
         visited.push(s); 
    }
    std::queue<Graph::vertex_descriptor>& visited;
};

int main() {
    Graph g;
    boost::add_edge(0,1,6,g);
    boost::add_edge(1,2,6,g);
    boost::add_edge(2,3,6,g);
    boost::add_edge(3,1,6,g);
    boost::add_edge(3,4,6,g);

    Graph::vertex_descriptor s = *(boost::vertices(g).first);
    std::queue<Graph::vertex_descriptor> q;
    BFSVisitor vis(q);
    boost::breadth_first_search(g, s, boost::visitor(vis));
    while (!vis.visited.empty()) {
        std::cout << vis.visited.front() << std::endl;
        vis.visited.pop();
    }
}

将输出

0
1
2
3
4

注意 - Boost 预计图表从顶点 0 开始,不是1 - 因此这条线

boost::add_edge(0,1,6,g);

如果从顶点 1 开始将不会有任何输出。

关于c++ - 如何使用 boost 图库广度优先搜索创建遍历顶点的队列?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59331950/

相关文章:

c++ - 当不存在可选子字符串时,避免匹配中的空元素

c++ - DirectShow DVD 播放

c++ - 如何使用 boost::flyweight 作为 GOF 模式?

algorithm - 维基百科的广度优先搜索示例 : How is a parentless node reached?

algorithm - 为什么说深度优先搜索会遇到无限循环?

c++:新返回一个类型但编译正常

c++ - QMessageBox 中的 HTML

c++ - Boost::Function 和 Boost::Bind 的替代方案

c++ - Boost 单元测试编译通过 Eclipse 失败

algorithm - 骑士以最少的步数俘获女王和车