增强图中的彩色地图breadth_first_visit

Aqu*_*pax 5 c++ boost properties graph

我想使用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;
Run Code Online (Sandbox Code Playgroud)

where Node_t是结构,定义顶点的属性.但是,我无法找到如何为BFS提供我自己的颜色图.我想将顶点颜色存储在矢量中,所以我的定义看起来像

std::vector<boost::default_color_type> colors;
Run Code Online (Sandbox Code Playgroud)

但我无法弄清楚,如何将此用于bfs.

也不

boost::breadth_first_search(g, *boost::vertices(g).first, 
  boost::color_map(colors));
Run Code Online (Sandbox Code Playgroud)

也不

boost::breadth_first_search(g, *boost::vertices(g).first, 
  boost::color_map(&colors[0]));
Run Code Online (Sandbox Code Playgroud)

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

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

Aqu*_*pax 5

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

bfs 使用的颜色图的类型是:

typedef boost::property_map<GraphType, boost::vertex_color_t>::type color_map_t;
color_map_t colorMap; //Create a color map
Run Code Online (Sandbox Code Playgroud)

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

boost::breadth_first_visit(g, *boost::vertices(g).first, boost::color_map(colorMap));
Run Code Online (Sandbox Code Playgroud)

给定一个 color_names 结构来映射颜色编号,例如

const char* color_names[] = {"white", "gray", "green", "red", "black"};
Run Code Online (Sandbox Code Playgroud)

可以通过迭代图中的所有顶点并使用当前顶点的 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;
}
Run Code Online (Sandbox Code Playgroud)