【问题标题】:Simplest way to define ColorMap for depth_first_search为 depth_first_search 定义 ColorMap 的最简单方法
【发布时间】:2018-12-20 19:08:33
【问题描述】:

我想使用 depth_first_search 算法。既然我需要 指定起始顶点的 DFS 版本,我也 必须定义一个 ColorMap。这就是我想要的功能 使用:

template <class Graph, class DFSVisitor, class ColorMap>
void depth_first_search(const Graph& g, DFSVisitor vis, ColorMap color, 
             typename graph_traits<Graph>::vertex_descriptor start)

https://www.boost.org/doc/libs/1_67_0/libs/graph/doc/depth_first_search.html

由于地图与我无关,默认的ColorMap 就足够了。 您能否给我一个提示,如何在 depth_first_search 中创建并将其作为参数传递?

【问题讨论】:

    标签: c++ boost boost-graph


    【解决方案1】:

    只需使用命名参数重载,这将允许您在仅使用默认颜色映射时指定起始顶点。

    template <class Graph, class class P, class T, class R>
    void depth_first_search(Graph& G, const bgl_named_params<P, T, R>& params);
    

    例子:

    boost::depth_first_search(graph, boost::root_vertex(start_vertex));
    

    【讨论】:

      【解决方案2】:

      我同意 0x5453 的回答。使用命名参数重载更容易。但是,如果您确实想知道如何初始化 ColorMap 对象,这里就是答案。

      默认值:从大小为 num_vertices(g) 的 default_color_type 的 std::vector 创建的 iterator_property_map,并使用 i_map 作为索引图。

      Graph g;
      
      /*
       * do something with graph
       */
      
      // number of colors should be the same as number of vertices.
      std::vector<boost::default_color_type> colors(boost::num_vertices(g));
      
      // create a ColorMap object (cpp_version < 17)
      auto color_map = boost::make_iterator_property_map(colors.begin(),
          boost::get(boost::vertex_index, g));
      
      // In C++17, make_iterator_property_map isn't necessary, as we have class 
      // template argument deduction
      boost::iterator_property_map color_map(colors.begin(),
          boost::get(boost::vertex_index, g));
      

      这里make_iterator_property_map 接受两个参数并返回一个iterator_property_map。第一个参数是颜色值的迭代器,第二个参数是图的顶点索引和colors索引之间的映射。

      【讨论】:

        猜你喜欢
        • 2010-11-11
        • 1970-01-01
        • 2013-05-16
        • 1970-01-01
        • 1970-01-01
        • 2016-07-06
        • 1970-01-01
        • 1970-01-01
        • 2011-04-16
        相关资源
        最近更新 更多