升压图breath_first_visit中的颜色图

Color map in boost graph breadth_first_visit

本文关键字:颜色 visit first breath      更新时间:2023-10-16

我想使用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提供我自己的颜色图。我想把顶点颜色存储在一个向量中,所以我的定义看起来像

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

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

所以问题是:我怎样才能使用我自己的颜色映射结构。另一个问题是:如何获得特定顶点描述符的颜色值?

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

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。对助推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"};

可以通过迭代图中的所有顶点来迭代颜色,并使用当前顶点的顶点描述符作为颜色图中[]运算符的自变量:

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