增强图形breadth_first_visit中的颜色图 [英] Color map in boost graph breadth_first_visit
问题描述
我想使用boosts breadth_first_visit
方法,我想为它提供我自己的外部颜色图。
我定义图如下
I want to use boosts breadth_first_visit
method and i'd like to provide it with my own "external" color map.
I defined the graph as follows
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我自己的颜色图。我想将顶点颜色存储在向量中,所以我的定义看起来像
where Node_t
is a struct, defining the properties for the vertices. However, i can't find out how i can provide the BFS with my own color map. I'd like to store the vertex colors in a vector, so my definition looks like
std::vector<boost::default_color_type> colors;
但我无法弄清楚,如何使用这个bfs。
but i can't figure it out, how to use this for the 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。
is working. While the first gives me a bunch of different compiler errors (e.g. default-int is not supported, "boost::color_traits" use of class type requires type argument list) the second compile aborts with only C2664 : 'boost::put' cannot convert parameter 2 from 'void*' to 'ptrdiff_t'.
所以问题是:如何使用我自己的颜色映射结构。另外一个问题是:我如何获得特定vertex_descriptor的颜色值?
So the question is: How can i use my own color-mapping-structure. An additional question would be: How can i get the color-value for a specific vertex_descriptor?
推荐答案
但解决了我的问题。对于那些像我一样困惑的人,对于boost中的颜色映射或者感兴趣的颜色映射:
Ok, i used another approach but solved my problem. For those who are as confused as i was about color maps in boost or those who are interested:
bfs使用的颜色映射的类型是: p>
The Type of the color map, as bfs uses it is:
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将是
This maps vertex_descriptor
to (in my case) default_color_type
. The appropriate call to boost's bfs would be
boost::breadth_first_visit(g, *boost::vertices(g).first, boost::color_map(colorMap));
给定一个color_names结构,用于映射颜色数字,如
Given a color_names structure that maps the color number like
const char* color_names[] = {"white", "gray", "green", "red", "black"};
可以通过遍历图中的所有顶点并使用当前的vertex_descriptor顶点作为颜色映射中[]操作符的参数:
one could iterate through the colors by iterating over all vertices in the graph and using the vertex_descriptor of the current vertex as an argument for the []-operator in the color map:
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;
}
这篇关于增强图形breadth_first_visit中的颜色图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!