gpt4 book ai didi

c++ - Boost Graph Library - 仅通过连接的顶点进行深度优先搜索

转载 作者:行者123 更新时间:2023-11-30 05:10:11 28 4
gpt4 key购买 nike

我正在尝试通过我的图表运行 DFS,该图表具有多个不相交的图表。我想指定起始顶点并让 DFS 仅遍历 that 树,将访问的顶点 ID 保持在 vector 中。为此,我需要 depth_first_visit 函数 http://www.boost.org/doc/libs/1_55_0/libs/graph/doc/depth_first_visit.html

该函数要求我初始化一个颜色图,但我对此一无所知,因为我还有自定义顶点和自定义边。我得到的最好的例子来自 thisthis讨论深度优先搜索而不是深度优先访问的帖子。如果我要用我使用的结构替换顶点,那么它将是下面的代码。

重申一下,我对如何初始化颜色贴图并为自定义顶点类型设置起始顶点一无所知。我希望有人能给我一个简单的例子。过去几个小时一直在谷歌搜索,但找不到示例。

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

最佳答案

最简单的方法是创建一个 vector 以包含每个顶点的颜色。

实现的最简单方法是:

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

Live On Coliru

#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++ - Boost Graph Library - 仅通过连接的顶点进行深度优先搜索,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45821365/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com