c++ - 如何创建 C++ Boost 无向图并以深度优先搜索 (DFS) 顺序遍历它?

标签 c++ boost-graph

如何创建 C++ Boost 无向图并以深度优先搜索 (DFS) 顺序对其进行遍历?

最佳答案

// Boost DFS example on an undirected graph.
// Create a sample graph, traverse its nodes
// in DFS order and print out their values.

#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/depth_first_search.hpp>
#include <iostream>
using namespace std;

typedef boost::adjacency_list<boost::listS, boost::vecS, boost::undirectedS> MyGraph;
typedef boost::graph_traits<MyGraph>::vertex_descriptor MyVertex;

class MyVisitor : public boost::default_dfs_visitor
{
public:
  void discover_vertex(MyVertex v, const MyGraph& g) const
  {
    cerr << v << endl;
    return;
  }
};

int main()
{
  MyGraph g;
  boost::add_edge(0, 1, g);
  boost::add_edge(0, 2, g);
  boost::add_edge(1, 2, g);
  boost::add_edge(1, 3, g);

  MyVisitor vis;
  boost::depth_first_search(g, boost::visitor(vis));

  return 0;
}

关于c++ - 如何创建 C++ Boost 无向图并以深度优先搜索 (DFS) 顺序遍历它?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14126/

相关文章:

c++ - 如何设置、清除和切换单个位?

c++ - Valgrind 下 Mac OS 上的 std::thread.join() SIGSEGV

c++ - 哈希表——哈希函数实现

c++ - 有效地在 Boost BGL 图中找到所有可达的顶点

c++ - Boost 图形库 C++/幂律

c++ - boost 图 : Test if two vertices are adjacent

c++ - BGL 中边的自定义属性

c++ - Boost Graph Library - 来自外部 vector 的权重属性

c++ - 帮我去掉一个Singleton : looking for an alternative

c++ - 不是 VS2017 中的编译时常量表达式