1 /* -*- mode: C++; indent-tabs-mode: nil; -*-
3 * This file is a part of LEMON, a generic C++ optimization library.
5 * Copyright (C) 2003-2009
6 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
7 * (Egervary Research Group on Combinatorial Optimization, EGRES).
9 * Permission to use, modify and distribute this software is granted
10 * provided that this copyright notice appears in all copies. For
11 * precise terms see the accompanying LICENSE file.
13 * This software is provided "AS IS" with no warranty of any kind,
14 * express or implied, and with no claim as to its suitability for any
19 #ifndef LEMON_KRUSKAL_H
20 #define LEMON_KRUSKAL_H
24 #include <lemon/unionfind.h>
25 #include <lemon/maps.h>
27 #include <lemon/core.h>
28 #include <lemon/bits/traits.h>
32 ///\brief Kruskal's algorithm to compute a minimum cost spanning tree
34 ///Kruskal's algorithm to compute a minimum cost spanning tree.
39 namespace _kruskal_bits {
41 // Kruskal for directed graphs.
43 template <typename Digraph, typename In, typename Out>
44 typename disable_if<lemon::UndirectedTagIndicator<Digraph>,
45 typename In::value_type::second_type >::type
46 kruskal(const Digraph& digraph, const In& in, Out& out,dummy<0> = 0) {
47 typedef typename In::value_type::second_type Value;
48 typedef typename Digraph::template NodeMap<int> IndexMap;
49 typedef typename Digraph::Node Node;
51 IndexMap index(digraph);
52 UnionFind<IndexMap> uf(index);
53 for (typename Digraph::NodeIt it(digraph); it != INVALID; ++it) {
58 for (typename In::const_iterator it = in.begin(); it != in.end(); ++it) {
59 if (uf.join(digraph.target(it->first),digraph.source(it->first))) {
60 out.set(it->first, true);
61 tree_value += it->second;
64 out.set(it->first, false);
70 // Kruskal for undirected graphs.
72 template <typename Graph, typename In, typename Out>
73 typename enable_if<lemon::UndirectedTagIndicator<Graph>,
74 typename In::value_type::second_type >::type
75 kruskal(const Graph& graph, const In& in, Out& out,dummy<1> = 1) {
76 typedef typename In::value_type::second_type Value;
77 typedef typename Graph::template NodeMap<int> IndexMap;
78 typedef typename Graph::Node Node;
80 IndexMap index(graph);
81 UnionFind<IndexMap> uf(index);
82 for (typename Graph::NodeIt it(graph); it != INVALID; ++it) {
87 for (typename In::const_iterator it = in.begin(); it != in.end(); ++it) {
88 if (uf.join(graph.u(it->first),graph.v(it->first))) {
89 out.set(it->first, true);
90 tree_value += it->second;
93 out.set(it->first, false);
100 template <typename Sequence>
102 typedef typename Sequence::value_type Value;
103 bool operator()(const Value& left, const Value& right) {
104 return left.second < right.second;
108 template <typename In, typename Enable = void>
109 struct SequenceInputIndicator {
110 static const bool value = false;
113 template <typename In>
114 struct SequenceInputIndicator<In,
115 typename exists<typename In::value_type::first_type>::type> {
116 static const bool value = true;
119 template <typename In, typename Enable = void>
120 struct MapInputIndicator {
121 static const bool value = false;
124 template <typename In>
125 struct MapInputIndicator<In,
126 typename exists<typename In::Value>::type> {
127 static const bool value = true;
130 template <typename In, typename Enable = void>
131 struct SequenceOutputIndicator {
132 static const bool value = false;
135 template <typename Out>
136 struct SequenceOutputIndicator<Out,
137 typename exists<typename Out::value_type>::type> {
138 static const bool value = true;
141 template <typename Out, typename Enable = void>
142 struct MapOutputIndicator {
143 static const bool value = false;
146 template <typename Out>
147 struct MapOutputIndicator<Out,
148 typename exists<typename Out::Value>::type> {
149 static const bool value = true;
152 template <typename In, typename InEnable = void>
153 struct KruskalValueSelector {};
155 template <typename In>
156 struct KruskalValueSelector<In,
157 typename enable_if<SequenceInputIndicator<In>, void>::type>
159 typedef typename In::value_type::second_type Value;
162 template <typename In>
163 struct KruskalValueSelector<In,
164 typename enable_if<MapInputIndicator<In>, void>::type>
166 typedef typename In::Value Value;
169 template <typename Graph, typename In, typename Out,
170 typename InEnable = void>
171 struct KruskalInputSelector {};
173 template <typename Graph, typename In, typename Out,
174 typename InEnable = void>
175 struct KruskalOutputSelector {};
177 template <typename Graph, typename In, typename Out>
178 struct KruskalInputSelector<Graph, In, Out,
179 typename enable_if<SequenceInputIndicator<In>, void>::type >
181 typedef typename In::value_type::second_type Value;
183 static Value kruskal(const Graph& graph, const In& in, Out& out) {
184 return KruskalOutputSelector<Graph, In, Out>::
185 kruskal(graph, in, out);
190 template <typename Graph, typename In, typename Out>
191 struct KruskalInputSelector<Graph, In, Out,
192 typename enable_if<MapInputIndicator<In>, void>::type >
194 typedef typename In::Value Value;
195 static Value kruskal(const Graph& graph, const In& in, Out& out) {
196 typedef typename In::Key MapArc;
197 typedef typename In::Value Value;
198 typedef typename ItemSetTraits<Graph, MapArc>::ItemIt MapArcIt;
199 typedef std::vector<std::pair<MapArc, Value> > Sequence;
202 for (MapArcIt it(graph); it != INVALID; ++it) {
203 seq.push_back(std::make_pair(it, in[it]));
206 std::sort(seq.begin(), seq.end(), PairComp<Sequence>());
207 return KruskalOutputSelector<Graph, Sequence, Out>::
208 kruskal(graph, seq, out);
212 template <typename T>
217 template <typename T>
218 struct RemoveConst<const T> {
222 template <typename Graph, typename In, typename Out>
223 struct KruskalOutputSelector<Graph, In, Out,
224 typename enable_if<SequenceOutputIndicator<Out>, void>::type >
226 typedef typename In::value_type::second_type Value;
228 static Value kruskal(const Graph& graph, const In& in, Out& out) {
229 typedef LoggerBoolMap<typename RemoveConst<Out>::type> Map;
231 return _kruskal_bits::kruskal(graph, in, map);
236 template <typename Graph, typename In, typename Out>
237 struct KruskalOutputSelector<Graph, In, Out,
238 typename enable_if<MapOutputIndicator<Out>, void>::type >
240 typedef typename In::value_type::second_type Value;
242 static Value kruskal(const Graph& graph, const In& in, Out& out) {
243 return _kruskal_bits::kruskal(graph, in, out);
249 /// \ingroup spantree
251 /// \brief Kruskal algorithm to find a minimum cost spanning tree of
254 /// This function runs Kruskal's algorithm to find a minimum cost
256 /// Due to some C++ hacking, it accepts various input and output types.
258 /// \param g The graph the algorithm runs on.
259 /// It can be either \ref concepts::Digraph "directed" or
260 /// \ref concepts::Graph "undirected".
261 /// If the graph is directed, the algorithm consider it to be
262 /// undirected by disregarding the direction of the arcs.
264 /// \param in This object is used to describe the arc/edge costs.
265 /// It can be one of the following choices.
266 /// - An STL compatible 'Forward Container' with
267 /// <tt>std::pair<GR::Arc,X></tt> or
268 /// <tt>std::pair<GR::Edge,X></tt> as its <tt>value_type</tt>, where
269 /// \c X is the type of the costs. The pairs indicates the arcs/edges
270 /// along with the assigned cost. <em>They must be in a
271 /// cost-ascending order.</em>
272 /// - Any readable arc/edge map. The values of the map indicate the
275 /// \retval out Here we also have a choice.
276 /// - It can be a writable \c bool arc/edge map. After running the
277 /// algorithm it will contain the found minimum cost spanning
278 /// tree: the value of an arc/edge will be set to \c true if it belongs
279 /// to the tree, otherwise it will be set to \c false. The value of
280 /// each arc/edge will be set exactly once.
281 /// - It can also be an iteraror of an STL Container with
282 /// <tt>GR::Arc</tt> or <tt>GR::Edge</tt> as its
283 /// <tt>value_type</tt>. The algorithm copies the elements of the
284 /// found tree into this sequence. For example, if we know that the
285 /// spanning tree of the graph \c g has say 53 arcs, then we can
286 /// put its arcs into an STL vector \c tree with a code like this.
288 /// std::vector<Arc> tree(53);
289 /// kruskal(g,cost,tree.begin());
291 /// Or if we don't know in advance the size of the tree, we can
294 /// std::vector<Arc> tree;
295 /// kruskal(g,cost,std::back_inserter(tree));
298 /// \return The total cost of the found spanning tree.
300 /// \note If the input graph is not (weakly) connected, a spanning
301 /// forest is calculated instead of a spanning tree.
304 template <class Graph, class In, class Out>
305 Value kruskal(GR const& g, const In& in, Out& out)
307 template <class Graph, class In, class Out>
308 inline typename _kruskal_bits::KruskalValueSelector<In>::Value
309 kruskal(const Graph& graph, const In& in, Out& out)
312 return _kruskal_bits::KruskalInputSelector<Graph, In, Out>::
313 kruskal(graph, in, out);
319 template <class Graph, class In, class Out>
320 inline typename _kruskal_bits::KruskalValueSelector<In>::Value
321 kruskal(const Graph& graph, const In& in, const Out& out)
323 return _kruskal_bits::KruskalInputSelector<Graph, In, const Out>::
324 kruskal(graph, in, out);
329 #endif //LEMON_KRUSKAL_H