source: NonGTP/Boost/boost/graph/kruskal_min_spanning_tree.hpp @ 857

Revision 857, 5.6 KB checked in by igarcia, 19 years ago (diff)
Line 
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
33namespace 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      typedef typename graph_traits<Graph>::vertex_descriptor Vertex;
52      typedef typename graph_traits<Graph>::edge_descriptor Edge;
53      function_requires<VertexListGraphConcept<Graph> >();
54      function_requires<EdgeListGraphConcept<Graph> >();
55      function_requires<OutputIteratorConcept<OutputIterator, Edge> >();
56      function_requires<ReadWritePropertyMapConcept<Rank, Vertex> >();
57      function_requires<ReadWritePropertyMapConcept<Parent, Vertex> >();
58      function_requires<ReadablePropertyMapConcept<Weight, Edge> >();
59      typedef typename property_traits<Weight>::value_type W_value;
60      typedef typename property_traits<Rank>::value_type R_value;
61      typedef typename property_traits<Parent>::value_type P_value;
62      function_requires<ComparableConcept<W_value> >();
63      function_requires<ConvertibleConcept<P_value, Vertex> >();
64      function_requires<IntegerConcept<R_value> >();
65
66      disjoint_sets<Rank, Parent>  dset(rank, parent);
67
68      typename graph_traits<Graph>::vertex_iterator ui, uiend;
69      for (boost::tie(ui, uiend) = vertices(G); ui != uiend; ++ui)
70        dset.make_set(*ui);
71
72      typedef indirect_cmp<Weight, std::greater<W_value> > weight_greater;
73      weight_greater wl(weight);
74      std::priority_queue<Edge, std::vector<Edge>, weight_greater> Q(wl);
75      /*push all edge into Q*/
76      typename graph_traits<Graph>::edge_iterator ei, eiend;
77      for (boost::tie(ei, eiend) = edges(G); ei != eiend; ++ei)
78        Q.push(*ei);
79
80      while (! Q.empty()) {
81        Edge e = Q.top();
82        Q.pop();
83        Vertex u = dset.find_set(source(e, G));
84        Vertex v = dset.find_set(target(e, G));
85        if ( u != v ) {
86          *spanning_tree_edges++ = e;
87          dset.link(u, v);
88        }
89      }
90    }
91
92  } // namespace detail
93
94  // Named Parameters Variants
95
96  template <class Graph, class OutputIterator>
97  inline void
98  kruskal_minimum_spanning_tree(const Graph& g,
99                                OutputIterator spanning_tree_edges)
100  {
101    typedef typename graph_traits<Graph>::vertices_size_type size_type;
102    typedef typename graph_traits<Graph>::vertex_descriptor vertex_t;
103    typedef typename property_map<Graph, vertex_index_t>::type index_map_t;
104    typename graph_traits<Graph>::vertices_size_type
105      n = num_vertices(g);
106    std::vector<size_type> rank_map(n);
107    std::vector<vertex_t> pred_map(n);
108
109    detail::kruskal_mst_impl
110      (g, spanning_tree_edges,
111       make_iterator_property_map(rank_map.begin(), get(vertex_index, g), rank_map[0]),
112       make_iterator_property_map(pred_map.begin(), get(vertex_index, g), pred_map[0]),
113       get(edge_weight, g));
114  }
115
116  template <class Graph, class OutputIterator, class P, class T, class R>
117  inline void
118  kruskal_minimum_spanning_tree(const Graph& g,
119                                OutputIterator spanning_tree_edges,
120                                const bgl_named_params<P, T, R>& params)
121  {
122    typedef typename graph_traits<Graph>::vertices_size_type size_type;
123    typedef typename graph_traits<Graph>::vertex_descriptor vertex_t;
124    typename graph_traits<Graph>::vertices_size_type n;
125    n = is_default_param(get_param(params, vertex_rank))
126                                   ? num_vertices(g) : 1;
127    std::vector<size_type> rank_map(n);
128    n = is_default_param(get_param(params, vertex_predecessor))
129                                   ? num_vertices(g) : 1;
130    std::vector<vertex_t> pred_map(n);
131   
132    detail::kruskal_mst_impl
133      (g, spanning_tree_edges,
134       choose_param
135       (get_param(params, vertex_rank),
136        make_iterator_property_map
137        (rank_map.begin(),
138         choose_pmap(get_param(params, vertex_index), g, vertex_index), rank_map[0])),
139       choose_param
140       (get_param(params, vertex_predecessor),
141        make_iterator_property_map
142        (pred_map.begin(),
143         choose_const_pmap(get_param(params, vertex_index), g, vertex_index),
144         pred_map[0])),
145       choose_const_pmap(get_param(params, edge_weight), g, edge_weight));
146  }
147   
148} // namespace boost
149
150
151#endif // BOOST_GRAPH_MST_KRUSKAL_HPP
152
Note: See TracBrowser for help on using the repository browser.