Boost Graph initialize_vertex change vertex color (visitor)

不羁岁月 提交于 2019-12-13 07:10:34

问题


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.


回答1:


What you want is probably the follows:

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

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

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.



来源:https://stackoverflow.com/questions/21111567/boost-graph-initialize-vertex-change-vertex-color-visitor

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!