epoc32/include/stdapis/boost/graph/kruskal_min_spanning_tree.hpp
author William Roberts <williamr@symbian.org>
Wed, 31 Mar 2010 12:33:34 +0100
branchSymbian3
changeset 4 837f303aceeb
permissions -rw-r--r--
Current Symbian^3 public API header files (from PDK 3.0.h)
This is the epoc32/include tree with the "platform" subtrees removed, and
all but a selected few mbg and rsg files removed.
     1 //
     2 //=======================================================================
     3 // Copyright 1997, 1998, 1999, 2000 University of Notre Dame.
     4 // Authors: Andrew Lumsdaine, Lie-Quan Lee, Jeremy G. Siek
     5 //
     6 // Distributed under the Boost Software License, Version 1.0. (See
     7 // accompanying file LICENSE_1_0.txt or copy at
     8 // http://www.boost.org/LICENSE_1_0.txt)
     9 //=======================================================================
    10 //
    11 #ifndef BOOST_GRAPH_MST_KRUSKAL_HPP
    12 #define BOOST_GRAPH_MST_KRUSKAL_HPP
    13 
    14 /*
    15  *Minimum Spanning Tree 
    16  *         Kruskal Algorithm
    17  *
    18  *Requirement:
    19  *      undirected graph
    20  */
    21 
    22 #include <vector>
    23 #include <queue>
    24 #include <functional>
    25 
    26 #include <boost/property_map.hpp>
    27 #include <boost/graph/graph_concepts.hpp>
    28 #include <boost/graph/named_function_params.hpp>
    29 #include <boost/pending/disjoint_sets.hpp>
    30 #include <boost/pending/indirect_cmp.hpp>
    31 
    32 
    33 namespace boost {
    34 
    35   // Kruskal's algorithm for Minimum Spanning Tree
    36   //
    37   // This is a greedy algorithm to calculate the Minimum Spanning Tree
    38   // for an undirected graph with weighted edges. The output will be a
    39   // set of edges.
    40   //
    41 
    42   namespace detail {
    43 
    44     template <class Graph, class OutputIterator, 
    45               class Rank, class Parent, class Weight>
    46     void
    47     kruskal_mst_impl(const Graph& G, 
    48                      OutputIterator spanning_tree_edges, 
    49                      Rank rank, Parent parent, Weight weight)
    50     {
    51       if (num_vertices(G) == 0) return; // Nothing to do in this case
    52       typedef typename graph_traits<Graph>::vertex_descriptor Vertex;
    53       typedef typename graph_traits<Graph>::edge_descriptor Edge;
    54       function_requires<VertexListGraphConcept<Graph> >();
    55       function_requires<EdgeListGraphConcept<Graph> >();
    56       function_requires<OutputIteratorConcept<OutputIterator, Edge> >();
    57       function_requires<ReadWritePropertyMapConcept<Rank, Vertex> >();
    58       function_requires<ReadWritePropertyMapConcept<Parent, Vertex> >();
    59       function_requires<ReadablePropertyMapConcept<Weight, Edge> >();
    60       typedef typename property_traits<Weight>::value_type W_value;
    61       typedef typename property_traits<Rank>::value_type R_value;
    62       typedef typename property_traits<Parent>::value_type P_value;
    63       function_requires<ComparableConcept<W_value> >();
    64       function_requires<ConvertibleConcept<P_value, Vertex> >();
    65       function_requires<IntegerConcept<R_value> >();
    66 
    67       disjoint_sets<Rank, Parent>  dset(rank, parent);
    68 
    69       typename graph_traits<Graph>::vertex_iterator ui, uiend;
    70       for (boost::tie(ui, uiend) = vertices(G); ui != uiend; ++ui)
    71         dset.make_set(*ui);
    72 
    73       typedef indirect_cmp<Weight, std::greater<W_value> > weight_greater;
    74       weight_greater wl(weight);
    75       std::priority_queue<Edge, std::vector<Edge>, weight_greater> Q(wl);
    76       /*push all edge into Q*/
    77       typename graph_traits<Graph>::edge_iterator ei, eiend;
    78       for (boost::tie(ei, eiend) = edges(G); ei != eiend; ++ei) 
    79         Q.push(*ei);
    80 
    81       while (! Q.empty()) {
    82         Edge e = Q.top();
    83         Q.pop();
    84         Vertex u = dset.find_set(source(e, G));
    85         Vertex v = dset.find_set(target(e, G));
    86         if ( u != v ) {
    87           *spanning_tree_edges++ = e;
    88           dset.link(u, v);
    89         }
    90       }
    91     }
    92 
    93   } // namespace detail 
    94 
    95   // Named Parameters Variants
    96 
    97   template <class Graph, class OutputIterator>
    98   inline void 
    99   kruskal_minimum_spanning_tree(const Graph& g,
   100                                 OutputIterator spanning_tree_edges)
   101   {
   102     typedef typename graph_traits<Graph>::vertices_size_type size_type;
   103     typedef typename graph_traits<Graph>::vertex_descriptor vertex_t;
   104     typedef typename property_map<Graph, vertex_index_t>::type index_map_t;
   105     if (num_vertices(g) == 0) return; // Nothing to do in this case
   106     typename graph_traits<Graph>::vertices_size_type
   107       n = num_vertices(g);
   108     std::vector<size_type> rank_map(n);
   109     std::vector<vertex_t> pred_map(n);
   110 
   111     detail::kruskal_mst_impl
   112       (g, spanning_tree_edges, 
   113        make_iterator_property_map(rank_map.begin(), get(vertex_index, g), rank_map[0]),
   114        make_iterator_property_map(pred_map.begin(), get(vertex_index, g), pred_map[0]),
   115        get(edge_weight, g));
   116   }
   117 
   118   template <class Graph, class OutputIterator, class P, class T, class R>
   119   inline void
   120   kruskal_minimum_spanning_tree(const Graph& g,
   121                                 OutputIterator spanning_tree_edges, 
   122                                 const bgl_named_params<P, T, R>& params)
   123   {
   124     typedef typename graph_traits<Graph>::vertices_size_type size_type;
   125     typedef typename graph_traits<Graph>::vertex_descriptor vertex_t;
   126     if (num_vertices(g) == 0) return; // Nothing to do in this case
   127     typename graph_traits<Graph>::vertices_size_type n;
   128     n = is_default_param(get_param(params, vertex_rank))
   129                                    ? num_vertices(g) : 1;
   130     std::vector<size_type> rank_map(n);
   131     n = is_default_param(get_param(params, vertex_predecessor))
   132                                    ? num_vertices(g) : 1;
   133     std::vector<vertex_t> pred_map(n);
   134     
   135     detail::kruskal_mst_impl
   136       (g, spanning_tree_edges, 
   137        choose_param
   138        (get_param(params, vertex_rank), 
   139         make_iterator_property_map
   140         (rank_map.begin(), 
   141          choose_pmap(get_param(params, vertex_index), g, vertex_index), rank_map[0])),
   142        choose_param
   143        (get_param(params, vertex_predecessor), 
   144         make_iterator_property_map
   145         (pred_map.begin(), 
   146          choose_const_pmap(get_param(params, vertex_index), g, vertex_index), 
   147          pred_map[0])),
   148        choose_const_pmap(get_param(params, edge_weight), g, edge_weight));
   149   }
   150     
   151 } // namespace boost
   152 
   153 
   154 #endif // BOOST_GRAPH_MST_KRUSKAL_HPP
   155