如何为Boost DFS创建内部且可修改的color_map?

时间:2019-07-19 08:17:56

标签: c++ boost depth-first-search boost-property-map

问题是创建可在增强Depth_First_Search中使用的颜色图。我的一个特殊功能是我想更改DFS的顶点颜色以修改DFS。当前,这仅在全局定义了颜色映射的情况下有效。

我的dfs访问者的示例部分:

template < typename Vertex, typename Graph >
    void finish_vertex(Vertex u, const Graph & g)
    {
        diGraph::vertex_descriptor vertex;
        vertex = boost::vertex(u, g);

        if (u == m_destination)
        {
            *m_result = true;
            //save current path
            m_allPaths->insert(std::make_pair(m_numPaths, m_spath));
            m_numPaths++;
        }

        if (u == m_source)
        {
            for (auto vd : boost::make_iterator_range(vertices(g)))
            {
                vertex_coloring[vd] = boost::black_color;
            }
        }
    }

如果我将全局颜色映射表传递给访问者,则一切正常:

//global version (working)
typedef boost::property< boost::vertex_name_t, std::string, boost::property<boost::vertex_index_t, int> > vertex_properties;
typedef boost::property< boost::edge_weight_t, float, boost::property<boost::edge_index_t, int > > edge_properties;
typedef boost::adjacency_list < boost::vecS, boost::vecS, boost::directedS, vertex_properties, edge_properties> diGraph;

//global version (working)
typedef std::map<diGraph::vertex_descriptor, default_color_type> colormap;
colormap vertex_coloring;

my_vis vis(0, 3, &foundDestination, &tempPaths);
//global version (working version)
//depth_first_search(ee_archi, vis, boost::make_assoc_property_map(vertex_coloring),0);

如果颜色图不是全局的而是外部的,我可以编写该图,但是它与DFS颜色图不同,因此我无法操纵DFS算法:

//external map
typedef boost::property< boost::vertex_color_t, boost::default_color_type> color_properties;
typedef boost::property< boost::vertex_name_t, std::string, boost::property<boost::vertex_index_t, int > > vertex_properties;
typedef boost::property< boost::edge_weight_t, float, boost::property<boost::edge_index_t, int > > edge_properties;
typedef boost::adjacency_list < boost::vecS, boost::vecS, boost::directedS, vertex_properties, edge_properties, color_properties> diGraph;

struct my_vis : default_dfs_visitor {
    //internal map
    using colormap = std::map<diGraph::vertex_descriptor, default_color_type>;
    colormap vertex_coloring;
….};

//not working
my_vis vis(0, 3, &foundDestination, &tempPaths);
depth_first_search(ee_archi, vis, boost::make_assoc_property_map(vis.vertex_coloring),0);

然后我尝试创建一个内部映射并使用get()和put()函数,但未成功:

//internal map
typedef boost::property< boost::vertex_name_t, std::string, boost::property<boost::vertex_index_t, int, boost::property< boost::vertex_color_t, boost::default_color_type> > > vertex_properties;
typedef boost::property< boost::edge_weight_t, float, boost::property<boost::edge_index_t, int > > edge_properties;
typedef boost::adjacency_list < boost::vecS, boost::vecS, boost::directedS, vertex_properties, edge_properties> diGraph;

0 个答案:

没有答案