gpt4 book ai didi

c++ - boost 图中的颜色图 breadth_first_visit

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:36:00 26 4
gpt4 key购买 nike

我想使用 boosts breadth_first_visit 方法,我想为它提供我自己的“外部”颜色图。我定义的图如下

typedef boost::adjacency_list<boost::setS, boost::listS, boost::undirectedS, 
boost::property<boost::vertex_index_t, int,
boost::property<boost::vertex_color_t, boost::default_color_type,
Node_t>>> GraphType;

其中 Node_t 是一个结构体,用于定义顶点的属性。但是,我无法找到如何为 BFS 提供我自己的颜色图。我想将顶点颜色存储在一个 vector 中,所以我的定义看起来像

std::vector<boost::default_color_type> colors;

但我想不通,如何将其用于 bfs。

都不是

boost::breadth_first_search(g, *boost::vertices(g).first, 
boost::color_map(colors));

也不是

boost::breadth_first_search(g, *boost::vertices(g).first, 
boost::color_map(&colors[0]));

正在工作。虽然第一个给了我一堆不同的编译器错误(例如不支持 default-int,“boost::color_traits”使用类类型需要类型参数列表)第二个编译中止只有 C2664:'boost::put' cannot将参数 2 从“void*”转换为“ptrdiff_t”。

所以问题是:如何使用我自己的颜色映射结构。另一个问题是:如何获取特定 vertex_descriptor 的颜色值?

最佳答案

好的,我使用了另一种方法但解决了我的问题。对于像我一样对 boost 中的彩色 map 感到困惑的人或感兴趣的人:

颜色图的类型,如 bfs 使用的那样:

typedef boost::property_map<GraphType, boost::vertex_color_t>::type color_map_t;
color_map_t colorMap; //Create a color map

这将 vertex_descriptor 映射到(在我的例子中)default_color_type。对 boost 的 bfs 的适当调用是

boost::breadth_first_visit(g, *boost::vertices(g).first, boost::color_map(colorMap));

给定一个映射颜色编号的 color_names 结构

const char* color_names[] = {"white", "gray", "green", "red", "black"};

可以通过迭代图中的所有顶点并使用当前顶点的 vertex_descriptor 作为颜色图中 [] 运算符的参数来迭代颜色:

GraphType::vertex_iterator it, itEnd;
for (boost::tie(it, itEnd) = boost::vertices(g); it != itEnd; it++)
{
std::cout << "Color of node " << *it << " is " << color_names[colorMap[*it]] << std::endl;
}

关于c++ - boost 图中的颜色图 breadth_first_visit,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11666131/

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