Chris@16
|
1 //=======================================================================
|
Chris@16
|
2 // Copyright 1997, 1998, 1999, 2000 University of Notre Dame.
|
Chris@16
|
3 // Authors: Andrew Lumsdaine, Lie-Quan Lee, Jeremy G. Siek
|
Chris@16
|
4 //
|
Chris@16
|
5 // Distributed under the Boost Software License, Version 1.0. (See
|
Chris@16
|
6 // accompanying file LICENSE_1_0.txt or copy at
|
Chris@16
|
7 // http://www.boost.org/LICENSE_1_0.txt)
|
Chris@16
|
8 //=======================================================================
|
Chris@16
|
9 //
|
Chris@16
|
10 #ifndef BOOST_GRAPH_MST_PRIM_HPP
|
Chris@16
|
11 #define BOOST_GRAPH_MST_PRIM_HPP
|
Chris@16
|
12
|
Chris@16
|
13 #include <functional>
|
Chris@16
|
14 #include <boost/graph/graph_traits.hpp>
|
Chris@16
|
15 #include <boost/graph/dijkstra_shortest_paths.hpp>
|
Chris@16
|
16
|
Chris@16
|
17 namespace boost {
|
Chris@16
|
18
|
Chris@16
|
19 namespace detail {
|
Chris@16
|
20 // this should be somewhere else in boost...
|
Chris@16
|
21 template <class U, class V> struct _project2nd {
|
Chris@16
|
22 V operator()(U, V v) const { return v; }
|
Chris@16
|
23 };
|
Chris@16
|
24 }
|
Chris@16
|
25
|
Chris@16
|
26 namespace detail {
|
Chris@16
|
27
|
Chris@16
|
28 // This is Prim's algorithm to calculate the Minimum Spanning Tree
|
Chris@16
|
29 // for an undirected graph with weighted edges.
|
Chris@16
|
30
|
Chris@16
|
31 template <class Graph, class P, class T, class R, class Weight>
|
Chris@16
|
32 inline void
|
Chris@16
|
33 prim_mst_impl(const Graph& G,
|
Chris@16
|
34 typename graph_traits<Graph>::vertex_descriptor s,
|
Chris@16
|
35 const bgl_named_params<P,T,R>& params,
|
Chris@16
|
36 Weight)
|
Chris@16
|
37 {
|
Chris@16
|
38 typedef typename property_traits<Weight>::value_type W;
|
Chris@16
|
39 std::less<W> compare;
|
Chris@16
|
40 detail::_project2nd<W,W> combine;
|
Chris@16
|
41 dijkstra_shortest_paths(G, s, params.distance_compare(compare).
|
Chris@16
|
42 distance_combine(combine));
|
Chris@16
|
43 }
|
Chris@16
|
44 } // namespace detail
|
Chris@16
|
45
|
Chris@16
|
46 template <class VertexListGraph, class DijkstraVisitor,
|
Chris@16
|
47 class PredecessorMap, class DistanceMap,
|
Chris@16
|
48 class WeightMap, class IndexMap>
|
Chris@16
|
49 inline void
|
Chris@16
|
50 prim_minimum_spanning_tree
|
Chris@16
|
51 (const VertexListGraph& g,
|
Chris@16
|
52 typename graph_traits<VertexListGraph>::vertex_descriptor s,
|
Chris@16
|
53 PredecessorMap predecessor, DistanceMap distance, WeightMap weight,
|
Chris@16
|
54 IndexMap index_map,
|
Chris@16
|
55 DijkstraVisitor vis)
|
Chris@16
|
56 {
|
Chris@16
|
57 typedef typename property_traits<WeightMap>::value_type W;
|
Chris@16
|
58 std::less<W> compare;
|
Chris@16
|
59 detail::_project2nd<W,W> combine;
|
Chris@16
|
60 dijkstra_shortest_paths(g, s, predecessor, distance, weight, index_map,
|
Chris@16
|
61 compare, combine, (std::numeric_limits<W>::max)(), 0,
|
Chris@16
|
62 vis);
|
Chris@16
|
63 }
|
Chris@16
|
64
|
Chris@16
|
65 template <class VertexListGraph, class PredecessorMap,
|
Chris@16
|
66 class P, class T, class R>
|
Chris@16
|
67 inline void prim_minimum_spanning_tree
|
Chris@16
|
68 (const VertexListGraph& g,
|
Chris@16
|
69 PredecessorMap p_map,
|
Chris@16
|
70 const bgl_named_params<P,T,R>& params)
|
Chris@16
|
71 {
|
Chris@16
|
72 detail::prim_mst_impl
|
Chris@16
|
73 (g,
|
Chris@16
|
74 choose_param(get_param(params, root_vertex_t()), *vertices(g).first),
|
Chris@16
|
75 params.predecessor_map(p_map),
|
Chris@16
|
76 choose_const_pmap(get_param(params, edge_weight), g, edge_weight));
|
Chris@16
|
77 }
|
Chris@16
|
78
|
Chris@16
|
79 template <class VertexListGraph, class PredecessorMap>
|
Chris@16
|
80 inline void prim_minimum_spanning_tree
|
Chris@16
|
81 (const VertexListGraph& g, PredecessorMap p_map)
|
Chris@16
|
82 {
|
Chris@16
|
83 detail::prim_mst_impl
|
Chris@16
|
84 (g, *vertices(g).first, predecessor_map(p_map).
|
Chris@16
|
85 weight_map(get(edge_weight, g)),
|
Chris@16
|
86 get(edge_weight, g));
|
Chris@16
|
87 }
|
Chris@16
|
88
|
Chris@16
|
89 } // namespace boost
|
Chris@16
|
90
|
Chris@16
|
91 #endif // BOOST_GRAPH_MST_PRIM_HPP
|