首页 文章

增强图中的彩色 Map breadth_first_visit

提问于
浏览
5

我想使用boosts breadth_first_visit 方法,我想为它提供我自己的"external"彩色 Map . 我将图表定义如下

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 是一个结构,定义顶点的属性 . 但是,我可以__645443_喜欢将顶点颜色存储在矢量中,所以我的定义看起来像

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' .

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

1 回答

  • 4

    好吧,我使用了另一种方法,但解决了我的问题 . 对于那些和我一样迷茫的人来说,有关激励中的色彩图或者有兴趣的人:

    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作为颜色映射中[] -operator的参数来迭代颜色:

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

相关问题