6

我想使用 boostsbreadth_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;

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

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

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

4

1 回答 1

4

好的,我使用了另一种方法,但解决了我的问题。对于那些和我一样对 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。对 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;
}
于 2012-07-26T11:00:23.380 回答