Boost Graph initialize_vertex更改顶点颜色(访客) [英] Boost Graph initialize_vertex change vertex color (visitor)
问题描述
我想用initialise_vertex作为'color map'修饰符来构建一个访问者(用于dikstra). 我想根据条件从搜索中排除一些顶点. 因此,我想在算法的初始部分设置一些黑色"顶点.
I would like to build a visitor (for dikstra) with the initialise_vertex acting as 'colour map' modifier. I want to exclude some vertices from the search based on a condition. So I want to set some vertices 'black' in the init part of the algorithm.
class dijkstra_2step : public boost::default_dijkstra_visitor
{
public:
dijkstra_2step(std::vector<Weight> dists, double threshold): distances(dists), threshold(threshold) {}
// THIS PART IS NOT CORRECT!!!! //
void initialize_vertex(boost::graph_traits <unGraph>::vertex_descriptor u, const unGraph& g){
if( distances[u] > threshold ) color[u] = black; // ??????
}
//////////
std::vector<Weight> distances;
double threshold;
};
对上述访客有帮助吗?如何访问颜色图? 我在网上找不到东西.
Any help for the above visitor? How to I access the colour map? I couldn't find something online.
推荐答案
您想要的可能是以下内容:
What you want is probably the follows:
对于Dijkstra,您实际上可以传递任意容器(例如std :: map甚至std :: vector)作为颜色图;您只需要适当包装即可:
In the case of Dijkstra you can actually pass an arbitrary container (e.g. std::map or even std::vector) as a color map; you just need to wrap it properly:
#include "boost/graph/properties.hpp"
std::vector<int> colorMap(num_vertices(g), boost::white_color);
之后,您可以在此容器中将某些顶点标记为黑色".然后,您必须调用Dijkstra的dijkstra_shortest_paths_no_init
变体.
After that you can mark some vertices as "black" in this container. Then you have to call dijkstra_shortest_paths_no_init
variant of Dijkstra.
dijkstra_shortest_paths_no_init(g, src, ..., ..., &colorMap[0]);
仅作记录,获取颜色图的标准方法是使用类似的代码
Just for the record, a standard way to get a color map is with code like
boost::property_map< unGraph, boost::vertex_color_t >::type colorMap =
boost::get(boost::vertex_color, g);
(前提是为给定的图形类型定义了这种映射).
(provided such map is defined for given graph type).
顺便说一句,或者,您可以使用 filtered_graph 作为输入而不是unGraph;您将必须提供一个顶点过滤器,用于指定图中的顶点.
BTW, alternatively, you can use filtered_graph as your input instead of unGraph; you will have to provide a vertex filter which specifies which vertices are in the graph.
这篇关于Boost Graph initialize_vertex更改顶点颜色(访客)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!