【问题标题】:Can anybody give me an example of use the smallest_last_vertex_ordering in boost::graph?谁能给我一个在 boost::graph 中使用 minimum_last_vertex_ordering 的例子吗?
【发布时间】:2013-04-28 06:42:09
【问题描述】:

我是boost编程的新手,但我希望在顶点着色上使用boost图库。

我已经阅读了 boost/graph/smallest_last_ordering.hpp 中 minimum_last_vertex_ordering 的帮助文档和源代码。但是我不知道如何构造函数 minimum_last_vertex_ordering 的参数。

这是 minimum_last_vertex_ordering 的定义:

template <class VertexListGraph, class Order, class Degree, class Marker>
  void 
  smallest_last_vertex_ordering(const VertexListGraph& G, Order order, 
                            Degree degree, Marker marker) {
    typedef typename boost::graph_traits<VertexListGraph> GraphTraits;
    typedef typename GraphTraits::vertex_descriptor Vertex;
    //typedef typename GraphTraits::size_type size_type;
    typedef std::size_t size_type;

    const size_type num = num_vertices(G);

    typedef typename boost::property_map<VertexListGraph, vertex_index_t>::type ID;
    typedef bucket_sorter<size_type, Vertex, Degree, ID> BucketSorter;

    BucketSorter degree_bucket_sorter(num, num, degree,  
                                  get(vertex_index,G));

    smallest_last_vertex_ordering(G, order, degree, marker, degree_bucket_sorter);
 }

您能告诉我如何创建正确的 Order、degree 和 Marker 类吗?

非常感谢。

【问题讨论】:

    标签: c++ boost boost-graph


    【解决方案1】:

    查看Order 的实现似乎是一个property map,它以std::size_t 作为key,以vertex_descriptor 作为value_type。 DegreeMarker 是属性映射,其中 vertex_descriptor 作为 key, std::size_t 作为 value_type。最后两个映射仅在内部需要,这就是只有两个参数(图形和顺序属性映射)的重载的原因。

    这是一个使用图 here 的重载和定向版本(以避免此算法显然不支持的循环)的示例:

    #include <iostream>
    #include <string>
    
    #include <boost/graph/adjacency_list.hpp>
    #include <boost/property_map/shared_array_property_map.hpp> //this should be included from smallest_last_ordering.hpp
    #include <boost/graph/smallest_last_ordering.hpp>
    
    int main()
    {
        typedef boost::adjacency_list<boost::vecS,boost::vecS,boost::directedS> Graph;
        typedef boost::graph_traits<Graph>::vertex_descriptor VertexDesc;
    
        Graph g;
        VertexDesc A=add_vertex(g);
        VertexDesc B=add_vertex(g);
        VertexDesc C=add_vertex(g);
        VertexDesc D=add_vertex(g);
        VertexDesc E=add_vertex(g);
    
        add_edge(A,C,g);
        add_edge(A,E,g);
        add_edge(C,B,g);
        add_edge(E,B,g);
        add_edge(C,D,g);
        add_edge(B,D,g);
        add_edge(E,D,g);
    
        boost::vector_property_map<VertexDesc> order;
    
        smallest_last_vertex_ordering(g, order);
    
        std::string names[]={"A","B","C","D","E"};
    
        for(std::size_t index=0; index<num_vertices(g); ++index)
        {
            std::cout << names[order[index]] <<  " ";
        }
        std::cout << std::endl;
    }
    

    这是一个使用四参数重载的版本,因此您可以看到另一种定义属性映射的方法:

    #include <iostream>
    #include <string>
    #include <map>
    
    #include <boost/graph/adjacency_list.hpp>
    #include <boost/property_map/shared_array_property_map.hpp> //this should be included from smallest_last_ordering.hpp
    #include <boost/graph/smallest_last_ordering.hpp>
    
    int main()
    {
        typedef boost::adjacency_list<boost::vecS,boost::vecS,boost::directedS> Graph;
        typedef boost::graph_traits<Graph>::vertex_descriptor VertexDesc;
    
        Graph g;
        VertexDesc A=add_vertex(g);
        VertexDesc B=add_vertex(g);
        VertexDesc C=add_vertex(g);
        VertexDesc D=add_vertex(g);
        VertexDesc E=add_vertex(g);
    
        add_edge(A,C,g);
        add_edge(A,E,g);
        add_edge(C,B,g);
        add_edge(E,B,g);
        add_edge(C,D,g);
        add_edge(B,D,g);
        add_edge(E,D,g);
    
        typedef std::map<std::size_t,VertexDesc> OrderMap;
        OrderMap order;
        boost::associative_property_map<OrderMap> order_prop_map(order);
    
        typedef std::map<VertexDesc,std::size_t> Map;
        Map degree;
        Map marker;
        boost::associative_property_map<Map> degree_prop_map(degree);
        boost::associative_property_map<Map> marker_prop_map(marker);
    
        smallest_last_vertex_ordering(g, order_prop_map, degree_prop_map, marker_prop_map);
    
        //another alternative
        // std::vector<VertexDesc> order(num_vertices(g));
        // std::vector<std::size_t> degree(num_vertices(g));
        // std::vector<std::size_t> marker(num_vertices(g));
        // smallest_last_vertex_ordering(g, make_iterator_property_map(&order[0],boost::identity_property_map()), make_iterator_property_map(&degree[0],get(boost::vertex_index,g)), make_iterator_property_map(&marker[0],get(boost::vertex_index,g)));
    
        std::string names[]={"A","B","C","D","E"};
    
        for(std::size_t index=0; index<num_vertices(g); ++index)
        {
            std::cout << names[order[index]] <<  "(" << degree[order[index]] << "," << marker[order[index]] << ") ";
        }
        std::cout << std::endl;
    }
    

    【讨论】:

      猜你喜欢
      • 2011-05-28
      • 2013-06-06
      • 2020-09-19
      • 1970-01-01
      • 1970-01-01
      • 2011-02-28
      • 1970-01-01
      • 2010-12-28
      • 1970-01-01
      相关资源
      最近更新 更多