Chris@16: // Chris@16: //======================================================================= Chris@16: // Copyright 1997, 1998, 1999, 2000 University of Notre Dame. Chris@16: // Authors: Andrew Lumsdaine, Lie-Quan Lee, Jeremy G. Siek Chris@16: // Chris@16: // Distributed under the Boost Software License, Version 1.0. (See Chris@16: // accompanying file LICENSE_1_0.txt or copy at Chris@16: // http://www.boost.org/LICENSE_1_0.txt) Chris@16: //======================================================================= Chris@16: // Chris@16: #ifndef BOOST_GRAPH_MST_KRUSKAL_HPP Chris@16: #define BOOST_GRAPH_MST_KRUSKAL_HPP Chris@16: Chris@16: /* Chris@16: *Minimum Spanning Tree Chris@16: * Kruskal Algorithm Chris@16: * Chris@16: *Requirement: Chris@16: * undirected graph Chris@16: */ Chris@16: Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: Chris@16: Chris@16: namespace boost { Chris@16: Chris@16: // Kruskal's algorithm for Minimum Spanning Tree Chris@16: // Chris@16: // This is a greedy algorithm to calculate the Minimum Spanning Tree Chris@16: // for an undirected graph with weighted edges. The output will be a Chris@16: // set of edges. Chris@16: // Chris@16: Chris@16: namespace detail { Chris@16: Chris@16: template Chris@16: void Chris@16: kruskal_mst_impl(const Graph& G, Chris@16: OutputIterator spanning_tree_edges, Chris@16: Rank rank, Parent parent, Weight weight) Chris@16: { Chris@16: if (num_vertices(G) == 0) return; // Nothing to do in this case Chris@16: typedef typename graph_traits::vertex_descriptor Vertex; Chris@16: typedef typename graph_traits::edge_descriptor Edge; Chris@16: BOOST_CONCEPT_ASSERT(( VertexListGraphConcept )); Chris@16: BOOST_CONCEPT_ASSERT(( EdgeListGraphConcept )); Chris@16: BOOST_CONCEPT_ASSERT(( OutputIteratorConcept )); Chris@16: BOOST_CONCEPT_ASSERT(( ReadWritePropertyMapConcept )); Chris@16: BOOST_CONCEPT_ASSERT(( ReadWritePropertyMapConcept )); Chris@16: BOOST_CONCEPT_ASSERT(( ReadablePropertyMapConcept )); Chris@16: typedef typename property_traits::value_type W_value; Chris@16: typedef typename property_traits::value_type R_value; Chris@16: typedef typename property_traits::value_type P_value; Chris@16: BOOST_CONCEPT_ASSERT(( ComparableConcept )); Chris@16: BOOST_CONCEPT_ASSERT(( ConvertibleConcept )); Chris@16: BOOST_CONCEPT_ASSERT(( IntegerConcept )); Chris@16: Chris@16: disjoint_sets dset(rank, parent); Chris@16: Chris@16: typename graph_traits::vertex_iterator ui, uiend; Chris@16: for (boost::tie(ui, uiend) = vertices(G); ui != uiend; ++ui) Chris@16: dset.make_set(*ui); Chris@16: Chris@16: typedef indirect_cmp > weight_greater; Chris@16: weight_greater wl(weight); Chris@16: std::priority_queue, weight_greater> Q(wl); Chris@16: /*push all edge into Q*/ Chris@16: typename graph_traits::edge_iterator ei, eiend; Chris@16: for (boost::tie(ei, eiend) = edges(G); ei != eiend; ++ei) Chris@16: Q.push(*ei); Chris@16: Chris@16: while (! Q.empty()) { Chris@16: Edge e = Q.top(); Chris@16: Q.pop(); Chris@16: Vertex u = dset.find_set(source(e, G)); Chris@16: Vertex v = dset.find_set(target(e, G)); Chris@16: if ( u != v ) { Chris@16: *spanning_tree_edges++ = e; Chris@16: dset.link(u, v); Chris@16: } Chris@16: } Chris@16: } Chris@16: Chris@16: } // namespace detail Chris@16: Chris@16: // Named Parameters Variants Chris@16: Chris@16: template Chris@16: inline void Chris@16: kruskal_minimum_spanning_tree(const Graph& g, Chris@16: OutputIterator spanning_tree_edges) Chris@16: { Chris@16: typedef typename graph_traits::vertices_size_type size_type; Chris@16: typedef typename graph_traits::vertex_descriptor vertex_t; Chris@16: if (num_vertices(g) == 0) return; // Nothing to do in this case Chris@16: typename graph_traits::vertices_size_type Chris@16: n = num_vertices(g); Chris@16: std::vector rank_map(n); Chris@16: std::vector pred_map(n); Chris@16: Chris@16: detail::kruskal_mst_impl Chris@16: (g, spanning_tree_edges, Chris@16: make_iterator_property_map(rank_map.begin(), get(vertex_index, g), rank_map[0]), Chris@16: make_iterator_property_map(pred_map.begin(), get(vertex_index, g), pred_map[0]), Chris@16: get(edge_weight, g)); Chris@16: } Chris@16: Chris@16: template Chris@16: inline void Chris@16: kruskal_minimum_spanning_tree(const Graph& g, Chris@16: OutputIterator spanning_tree_edges, Chris@16: const bgl_named_params& params) Chris@16: { Chris@16: typedef typename graph_traits::vertices_size_type size_type; Chris@16: typedef typename graph_traits::vertex_descriptor vertex_t; Chris@16: if (num_vertices(g) == 0) return; // Nothing to do in this case Chris@16: typename graph_traits::vertices_size_type n; Chris@16: n = is_default_param(get_param(params, vertex_rank)) Chris@16: ? num_vertices(g) : 1; Chris@16: std::vector rank_map(n); Chris@16: n = is_default_param(get_param(params, vertex_predecessor)) Chris@16: ? num_vertices(g) : 1; Chris@16: std::vector pred_map(n); Chris@16: Chris@16: detail::kruskal_mst_impl Chris@16: (g, spanning_tree_edges, Chris@16: choose_param Chris@16: (get_param(params, vertex_rank), Chris@16: make_iterator_property_map Chris@16: (rank_map.begin(), Chris@16: choose_pmap(get_param(params, vertex_index), g, vertex_index), rank_map[0])), Chris@16: choose_param Chris@16: (get_param(params, vertex_predecessor), Chris@16: make_iterator_property_map Chris@16: (pred_map.begin(), Chris@16: choose_const_pmap(get_param(params, vertex_index), g, vertex_index), Chris@16: pred_map[0])), Chris@16: choose_const_pmap(get_param(params, edge_weight), g, edge_weight)); Chris@16: } Chris@16: Chris@16: } // namespace boost Chris@16: Chris@16: Chris@16: #endif // BOOST_GRAPH_MST_KRUSKAL_HPP Chris@16: