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