大佬教程收集整理的这篇文章主要介绍了c – 如何在boost使用BFS中遍历图形,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
#include <stdint.h> #include <iostream> #include <vector> #include <boost/graph/adjacency_list.hpp> #include <boost/graph/breadth_first_search.hpp> int main() { typedef boost::adjacency_list<boost::vecS,boost::hash_setS,boost::undirectedS,uint32_t,boost::no_property> graph_t; graph_t graph(4); graph_t::vertex_descriptor a = boost::vertex(0,graph); graph_t::vertex_descriptor b = boost::vertex(1,graph); graph_t::vertex_descriptor c = boost::vertex(2,graph); graph_t::vertex_descriptor d = boost::vertex(3,graph); graph[a] = 0; graph[b] = 1; graph[c] = 2; graph[d] = 3; std::pair<graph_t::edge_descriptor,bool> result = boost::add_edge(a,b,graph); result = boost::add_edge(a,c,1,graph); result = boost::add_edge(c,2,graph); class { public: void initialize_vertex(const graph_t::vertex_descriptor &s,graph_t &g) { std::cout << "Initialize: " << g[s] << std::endl; } void discover_vertex(const graph_t::vertex_descriptor &s,graph_t &g) { std::cout << "Discover: " << g[s] << std::endl; } void examine_vertex(const graph_t::vertex_descriptor &s,graph_t &g) { std::cout << "Examine vertex: " << g[s] << std::endl; } void examine_edge(const graph_t::edge_descriptor &e,graph_t &g) { std::cout << "Examine edge: " << g[e] << std::endl; } void tree_edge(const graph_t::edge_descriptor &e,graph_t &g) { std::cout << "Tree edge: " << g[e] << std::endl; } void non_tree_edge(const graph_t::edge_descriptor &e,graph_t &g) { std::cout << "Non-Tree edge: " << g[e] << std::endl; } void gray_target(const graph_t::edge_descriptor &e,graph_t &g) { std::cout << "Gray target: " << g[e] << std::endl; } void black_target(const graph_t::edge_descriptor &e,graph_t &g) { std::cout << "Black target: " << g[e] << std::endl; } void finish_vertex(const graph_t::vertex_descriptor &s,graph_t &g) { std::cout << "Finish vertex: " << g[s] << std::endl; } } bfs_visitor; boost::breadth_first_search(graph,a,bfs_visitor); return 0; }
如何使用bfs_visitor访问图表?
Ps.我已经看到并编译了“How to create a C++ Boost undirected graph and traverse it in depth first search (DFS) order?”,但它没有帮助.
breadth_first_search(graph,boost::visitor(bfs_visitor));
如果您已在图形定义中使用vecS作为VertexList存储,或者已构建并初始化内部vertex_index属性映射,则此方法可以正常工作.由于您使用的是hash_setS,因此需要将调用更改为:
breath_first_search(graph,boost::visitor(bfs_visitor).vertex_index_map(my_index_map));
您已在uint32_t捆绑属性中使用索引映射.您可以使用get(boost :: vertex_bundle,graph)来访问它.
你的访客也有问题.您应该从boost :: defaulT_Bfs_visitor派生它,并且您的成员函数的graph_t参数需要是const限定的.
完整代码:
#include <stdint.h> #include <iostream> #include <vector> #include <boost/graph/adjacency_list.hpp> #include <boost/graph/breadth_first_search.hpp> typedef boost::adjacency_list<boost::vecS,boost::no_property> graph_t; struct my_visitor : boost::defaulT_Bfs_visitor{ void initialize_vertex(const graph_t::vertex_descriptor &s,const graph_t &g) const { std::cout << "Initialize: " << g[s] << std::endl; } void discover_vertex(const graph_t::vertex_descriptor &s,const graph_t &g) const { std::cout << "Discover: " << g[s] << std::endl; } void examine_vertex(const graph_t::vertex_descriptor &s,const graph_t &g) const { std::cout << "Examine vertex: " << g[s] << std::endl; } void examine_edge(const graph_t::edge_descriptor &e,const graph_t &g) const { std::cout << "Examine edge: " << g[e] << std::endl; } void tree_edge(const graph_t::edge_descriptor &e,const graph_t &g) const { std::cout << "Tree edge: " << g[e] << std::endl; } void non_tree_edge(const graph_t::edge_descriptor &e,const graph_t &g) const { std::cout << "Non-Tree edge: " << g[e] << std::endl; } void gray_target(const graph_t::edge_descriptor &e,const graph_t &g) const { std::cout << "Gray target: " << g[e] << std::endl; } void black_target(const graph_t::edge_descriptor &e,const graph_t &g) const { std::cout << "Black target: " << g[e] << std::endl; } void finish_vertex(const graph_t::vertex_descriptor &s,const graph_t &g) const { std::cout << "Finish vertex: " << g[s] << std::endl; } }; int main() { graph_t graph(4); graph_t::vertex_descriptor a = boost::vertex(0,graph); my_visitor vis; breadth_first_search(graph,boost::visitor(vis).vertex_index_map(get(boost::vertex_bundle,graph))); return 0; }
以上是大佬教程为你收集整理的c – 如何在boost使用BFS中遍历图形全部内容,希望文章能够帮你解决c – 如何在boost使用BFS中遍历图形所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。