提升图形库 - 深度优先 仅通过连接的顶点进行搜索
Boost Graph Library - Depth First Search only through connected vertices
我正在尝试通过具有多个不相交图的图形运行DFS。我想指定起始顶点,并让 DFS 仅遍历该树,保留在向量中访问的顶点 ID。为此,我需要depth_first_visit函数 http://www.boost.org/doc/libs/1_55_0/libs/graph/doc/depth_first_visit.html
该函数要求我初始化颜色图,而我对此一无所知,因为我还有一个自定义顶点和一个自定义边。我得到的最好的例子是这篇文章和这篇文章,它谈论的是深度优先搜索而不是深度首次访问。如果我用我使用的结构替换顶点,那么它将是下面的代码。
因此,重申一下,我对如何初始化颜色图并为自定义顶点类型设置起始顶点一无所知。我希望有人能给我一个简单的例子。过去几个小时一直在谷歌搜索,但找不到一个例子。
// 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;
//======================================================================================================
//information representing each vertex
struct Vertex {
public:
//id
int id = 0;
int parent_id = -1;
int l_child = -1, r_child = -1;
Vertex(int id = -1) : id(id) {}
};
//======================================================================================================
//information representing each weight
//it carries the boundary length and also the distance
struct Edge {
//distance
float boundary_length = 0;
float weight = 1;
//float L, a, b = 1;
Edge(float boundary_length = 1) : boundary_length(boundary_length) {}
};
typedef boost::adjacency_list<boost::listS, boost::vecS, boost::undirectedS, Vertex, Edge> Graph;
class MyVisitor : public boost::default_dfs_visitor
{
public:
MyVisitor() : vv(new std::vector<int>()) {}
void discover_vertex(int v, const Graph& g) const
{
vv->push_back(g[v].id);
return;
}
std::vector<int>& GetVector() const { return *vv; }
private:
boost::shared_ptr< std::vector<int> > vv;
};
int main()
{
Graph 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);
boost::add_edge(5, 6, g);
boost::add_edge(5, 8, g);
MyVisitor vis;
boost::depth_first_search(g, boost::visitor(vis));
std::vector<int> vctr = vis.GetVector();
return 0;
}
最简单的方法是创建一个矢量来包含每个顶点的颜色。
实现这一目标的最简单方法是:
auto indexmap = boost::get(boost::vertex_index, g);
auto colormap = boost::make_vector_property_map<boost::default_color_type>(indexmap);
然后你像
boost::depth_first_search(g, vis, colormap);
这相当于使用命名参数习惯用法,如下所示:
boost::depth_first_search(g, boost::visitor(vis) .color_map(colormap));
要同时提供起始顶点,只需使用该重载:
boost::depth_first_search(g, vis, colormap, 1);
住在科里鲁
#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/depth_first_search.hpp>
#include <iostream>
using namespace std;
//======================================================================================================
// information representing each vertex
struct Vertex {
int id = 0;
int parent_id = -1;
int l_child = -1, r_child = -1;
Vertex(int id = -1) : id(id) {}
};
//======================================================================================================
// information representing each weight
// it carries the boundary length and also the distance
struct Edge {
// distance
float boundary_length = 0;
float weight = 1;
// float L, a, b = 1;
Edge(float boundary_length = 1) : boundary_length(boundary_length) {}
};
typedef boost::adjacency_list<boost::listS, boost::vecS, boost::undirectedS, Vertex, Edge> Graph;
class MyVisitor : public boost::default_dfs_visitor {
public:
MyVisitor() : vv(new std::vector<int>()) {}
void discover_vertex(int v, const Graph &g) const {
vv->push_back(g[v].id);
return;
}
std::vector<int> &GetVector() const { return *vv; }
private:
boost::shared_ptr<std::vector<int> > vv;
};
int main() {
Graph 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);
boost::add_edge(5, 6, g);
boost::add_edge(5, 8, g);
for (auto v : boost::make_iterator_range(boost::vertices(g)))
g[v] = Vertex(v);
auto indexmap = boost::get(boost::vertex_index, g);
auto colormap = boost::make_vector_property_map<boost::default_color_type>(indexmap);
MyVisitor vis;
boost::depth_first_search(g, vis, colormap, 1);
std::vector<int> vctr = vis.GetVector();
for(auto id : vctr)
std::cout << id << " ";
}
指纹
1 0 2 3 4 5 6 8 7
相关文章:
- 如何循环打印顶点结构
- 当套接字连接断开时检测C/C++Unix
- 无法在windows上使用mingw将sqlite3与c连接
- 到连接组件算法的问题(递归)
- D3D11-将混合权重和索引传递到顶点着色器
- QTcpSocket在不阻塞GUI的情况下重新连接到服务器
- 无法在C++中建立与MySQL数据库的连接
- PC中的程序和PHONE中的本机描述应用程序之间的数据连接
- 在Qt Creator中,如何在连接到正在运行的进程后查看控制台输出
- 从返回的顶点缓冲区查询顶点结构
- Vulkan 中的动态顶点缓冲区格式设置
- 连接 dockerized 模型和 dockerized 数据库时出现"无法 SQLConnect"错误
- 在顶点着色器中使用 OpenGl 的未声明标识符,我在顶点着色器中绘制三角形时遇到问题
- 使用 bfs 解决连接组件问题时得到错误的答案
- 在C++的两个字符串中连接以逗号分隔的唯一值
- 如何修复valgrind启动时的致命错误(与libc6-dbg和libc6-dbg:i386连接)
- 提升图形库 - 深度优先 仅通过连接的顶点进行搜索
- 如何在C++中将边列表转换为邻接矩阵,其中包含连接到顶点的边数而不是一条边?
- 在图形中移除连接到源顶点的边时获取额外的边
- 给定一组顶点,如何生成具有几乎最少边量的强连接有向图