1 | /* -*- C++ -*- |
---|
2 | * |
---|
3 | * This file is a part of LEMON, a generic C++ optimization library |
---|
4 | * |
---|
5 | * Copyright (C) 2003-2006 |
---|
6 | * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
---|
7 | * (Egervary Research Group on Combinatorial Optimization, EGRES). |
---|
8 | * |
---|
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. |
---|
12 | * |
---|
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 |
---|
15 | * purpose. |
---|
16 | * |
---|
17 | */ |
---|
18 | |
---|
19 | #ifndef LEMON_KRUSKAL_H |
---|
20 | #define LEMON_KRUSKAL_H |
---|
21 | |
---|
22 | #include <algorithm> |
---|
23 | #include <vector> |
---|
24 | #include <lemon/unionfind.h> |
---|
25 | #include <lemon/bits/utility.h> |
---|
26 | #include <lemon/bits/traits.h> |
---|
27 | #include <lemon/time_measure.h> |
---|
28 | #include <iostream> |
---|
29 | |
---|
30 | ///\ingroup spantree |
---|
31 | ///\file |
---|
32 | ///\brief Kruskal's algorithm to compute a minimum cost tree |
---|
33 | /// |
---|
34 | ///Kruskal's algorithm to compute a minimum cost tree. |
---|
35 | /// |
---|
36 | ///\todo The file still needs some clean-up. |
---|
37 | |
---|
38 | namespace lemon { |
---|
39 | |
---|
40 | /// \addtogroup spantree |
---|
41 | /// @{ |
---|
42 | |
---|
43 | /// Kruskal's algorithm to find a minimum cost tree of a graph. |
---|
44 | |
---|
45 | /// This function runs Kruskal's algorithm to find a minimum cost tree. |
---|
46 | /// Due to hard C++ hacking, it accepts various input and output types. |
---|
47 | /// |
---|
48 | /// \param g The graph the algorithm runs on. |
---|
49 | /// It can be either \ref concept::Graph "directed" or |
---|
50 | /// \ref concept::UGraph "undirected". |
---|
51 | /// If the graph is directed, the algorithm consider it to be |
---|
52 | /// undirected by disregarding the direction of the edges. |
---|
53 | /// |
---|
54 | /// \param in This object is used to describe the edge costs. It can be one |
---|
55 | /// of the following choices. |
---|
56 | /// - An STL compatible 'Forward Container' |
---|
57 | /// with <tt>std::pair<GR::Edge,X></tt> as its <tt>value_type</tt>, |
---|
58 | /// where \c X is the type of the costs. The pairs indicates the edges along |
---|
59 | /// with the assigned cost. <em>They must be in a |
---|
60 | /// cost-ascending order.</em> |
---|
61 | /// - Any readable Edge map. The values of the map indicate the edge costs. |
---|
62 | /// |
---|
63 | /// \retval out Here we also have a choise. |
---|
64 | /// - Is can be a writable \c bool edge map. |
---|
65 | /// After running the algorithm |
---|
66 | /// this will contain the found minimum cost spanning tree: the value of an |
---|
67 | /// edge will be set to \c true if it belongs to the tree, otherwise it will |
---|
68 | /// be set to \c false. The value of each edge will be set exactly once. |
---|
69 | /// - It can also be an iteraror of an STL Container with |
---|
70 | /// <tt>GR::Edge</tt> as its <tt>value_type</tt>. |
---|
71 | /// The algorithm copies the elements of the found tree into this sequence. |
---|
72 | /// For example, if we know that the spanning tree of the graph \c g has |
---|
73 | /// say 53 edges, then |
---|
74 | /// we can put its edges into a STL vector \c tree with a code like this. |
---|
75 | ///\code |
---|
76 | /// std::vector<Edge> tree(53); |
---|
77 | /// kruskal(g,cost,tree.begin()); |
---|
78 | ///\endcode |
---|
79 | /// Or if we don't know in advance the size of the tree, we can write this. |
---|
80 | ///\code |
---|
81 | /// std::vector<Edge> tree; |
---|
82 | /// kruskal(g,cost,std::back_inserter(tree)); |
---|
83 | ///\endcode |
---|
84 | /// |
---|
85 | /// \return The cost of the found tree. |
---|
86 | /// |
---|
87 | /// \warning If kruskal is run on an |
---|
88 | /// \ref lemon::concept::UGraph "undirected graph", be sure that the |
---|
89 | /// map storing the tree is also undirected |
---|
90 | /// (e.g. ListUGraph::UEdgeMap<bool>, otherwise the values of the |
---|
91 | /// half of the edges will not be set. |
---|
92 | /// |
---|
93 | /// \todo Discuss the case of undirected graphs: In this case the algorithm |
---|
94 | /// also require <tt>Edge</tt>s instead of <tt>UEdge</tt>s, as some |
---|
95 | /// people would expect. So, one should be careful not to add both of the |
---|
96 | /// <tt>Edge</tt>s belonging to a certain <tt>UEdge</tt>. |
---|
97 | /// (\ref kruskal() and \ref KruskalMapInput are kind enough to do so.) |
---|
98 | |
---|
99 | #ifdef DOXYGEN |
---|
100 | template <class GR, class IN, class OUT> |
---|
101 | typename IN::value_type::second_type |
---|
102 | kruskal(GR const& g, IN const& in, |
---|
103 | OUT& out) |
---|
104 | #else |
---|
105 | template <class GR, class IN, class OUT> |
---|
106 | typename IN::value_type::second_type |
---|
107 | kruskal(GR const& g, IN const& in, |
---|
108 | OUT& out, |
---|
109 | // typename IN::value_type::first_type = typename GR::Edge() |
---|
110 | // ,typename OUT::Key = OUT::Key() |
---|
111 | // //,typename OUT::Key = typename GR::Edge() |
---|
112 | const typename IN::value_type::first_type * = |
---|
113 | (const typename IN::value_type::first_type *)(0), |
---|
114 | const typename OUT::Key * = (const typename OUT::Key *)(0) |
---|
115 | ) |
---|
116 | #endif |
---|
117 | { |
---|
118 | typedef typename IN::value_type::second_type EdgeCost; |
---|
119 | typedef typename GR::template NodeMap<int> NodeIntMap; |
---|
120 | typedef typename GR::Node Node; |
---|
121 | |
---|
122 | Timer timer; |
---|
123 | NodeIntMap comp(g); |
---|
124 | UnionFind<Node,NodeIntMap> uf(comp); |
---|
125 | for (typename GR::NodeIt it(g); it != INVALID; ++it) { |
---|
126 | uf.insert(it); |
---|
127 | } |
---|
128 | |
---|
129 | EdgeCost tot_cost = 0; |
---|
130 | for (typename IN::const_iterator p = in.begin(); |
---|
131 | p!=in.end(); ++p ) { |
---|
132 | if ( uf.join(g.target((*p).first), |
---|
133 | g.source((*p).first)) ) { |
---|
134 | out.set((*p).first, true); |
---|
135 | tot_cost += (*p).second; |
---|
136 | } |
---|
137 | else { |
---|
138 | out.set((*p).first, false); |
---|
139 | } |
---|
140 | } |
---|
141 | std::cout << timer << std::endl; |
---|
142 | return tot_cost; |
---|
143 | } |
---|
144 | |
---|
145 | |
---|
146 | /// @} |
---|
147 | |
---|
148 | |
---|
149 | /* A work-around for running Kruskal with const-reference bool maps... */ |
---|
150 | |
---|
151 | /// Helper class for calling kruskal with "constant" output map. |
---|
152 | |
---|
153 | /// Helper class for calling kruskal with output maps constructed |
---|
154 | /// on-the-fly. |
---|
155 | /// |
---|
156 | /// A typical examle is the following call: |
---|
157 | /// <tt>kruskal(g, some_input, makeSequenceOutput(iterator))</tt>. |
---|
158 | /// Here, the third argument is a temporary object (which wraps around an |
---|
159 | /// iterator with a writable bool map interface), and thus by rules of C++ |
---|
160 | /// is a \c const object. To enable call like this exist this class and |
---|
161 | /// the prototype of the \ref kruskal() function with <tt>const& OUT</tt> |
---|
162 | /// third argument. |
---|
163 | template<class Map> |
---|
164 | class NonConstMapWr { |
---|
165 | const Map &m; |
---|
166 | public: |
---|
167 | typedef typename Map::Key Key; |
---|
168 | typedef typename Map::Value Value; |
---|
169 | |
---|
170 | NonConstMapWr(const Map &_m) : m(_m) {} |
---|
171 | |
---|
172 | template<class Key> |
---|
173 | void set(Key const& k, Value const &v) const { m.set(k,v); } |
---|
174 | }; |
---|
175 | |
---|
176 | template <class GR, class IN, class OUT> |
---|
177 | inline |
---|
178 | typename IN::value_type::second_type |
---|
179 | kruskal(GR const& g, IN const& edges, OUT const& out_map, |
---|
180 | // typename IN::value_type::first_type = typename GR::Edge(), |
---|
181 | // typename OUT::Key = GR::Edge() |
---|
182 | const typename IN::value_type::first_type * = |
---|
183 | (const typename IN::value_type::first_type *)(0), |
---|
184 | const typename OUT::Key * = (const typename OUT::Key *)(0) |
---|
185 | ) |
---|
186 | { |
---|
187 | NonConstMapWr<OUT> map_wr(out_map); |
---|
188 | return kruskal(g, edges, map_wr); |
---|
189 | } |
---|
190 | |
---|
191 | /* ** ** Input-objects ** ** */ |
---|
192 | |
---|
193 | /// Kruskal's input source. |
---|
194 | |
---|
195 | /// Kruskal's input source. |
---|
196 | /// |
---|
197 | /// In most cases you possibly want to use the \ref kruskal() instead. |
---|
198 | /// |
---|
199 | /// \sa makeKruskalMapInput() |
---|
200 | /// |
---|
201 | ///\param GR The type of the graph the algorithm runs on. |
---|
202 | ///\param Map An edge map containing the cost of the edges. |
---|
203 | ///\par |
---|
204 | ///The cost type can be any type satisfying |
---|
205 | ///the STL 'LessThan comparable' |
---|
206 | ///concept if it also has an operator+() implemented. (It is necessary for |
---|
207 | ///computing the total cost of the tree). |
---|
208 | /// |
---|
209 | template<class GR, class Map> |
---|
210 | class KruskalMapInput |
---|
211 | : public std::vector< std::pair<typename GR::Edge, |
---|
212 | typename Map::Value> > { |
---|
213 | |
---|
214 | public: |
---|
215 | typedef std::vector< std::pair<typename GR::Edge, |
---|
216 | typename Map::Value> > Parent; |
---|
217 | typedef typename Parent::value_type value_type; |
---|
218 | |
---|
219 | private: |
---|
220 | class comparePair { |
---|
221 | public: |
---|
222 | bool operator()(const value_type& a, |
---|
223 | const value_type& b) { |
---|
224 | return a.second < b.second; |
---|
225 | } |
---|
226 | }; |
---|
227 | |
---|
228 | template<class _GR> |
---|
229 | typename enable_if<UndirectedTagIndicator<_GR>,void>::type |
---|
230 | fillWithEdges(const _GR& g, const Map& m,dummy<0> = 0) |
---|
231 | { |
---|
232 | for(typename GR::UEdgeIt e(g);e!=INVALID;++e) |
---|
233 | push_back(value_type(g.direct(e, true), m[e])); |
---|
234 | } |
---|
235 | |
---|
236 | template<class _GR> |
---|
237 | typename disable_if<UndirectedTagIndicator<_GR>,void>::type |
---|
238 | fillWithEdges(const _GR& g, const Map& m,dummy<1> = 1) |
---|
239 | { |
---|
240 | for(typename GR::EdgeIt e(g);e!=INVALID;++e) |
---|
241 | push_back(value_type(e, m[e])); |
---|
242 | } |
---|
243 | |
---|
244 | |
---|
245 | public: |
---|
246 | |
---|
247 | void sort() { |
---|
248 | std::sort(this->begin(), this->end(), comparePair()); |
---|
249 | } |
---|
250 | |
---|
251 | KruskalMapInput(GR const& g, Map const& m) { |
---|
252 | fillWithEdges(g,m); |
---|
253 | sort(); |
---|
254 | } |
---|
255 | }; |
---|
256 | |
---|
257 | /// Creates a KruskalMapInput object for \ref kruskal() |
---|
258 | |
---|
259 | /// It makes easier to use |
---|
260 | /// \ref KruskalMapInput by making it unnecessary |
---|
261 | /// to explicitly give the type of the parameters. |
---|
262 | /// |
---|
263 | /// In most cases you possibly |
---|
264 | /// want to use \ref kruskal() instead. |
---|
265 | /// |
---|
266 | ///\param g The type of the graph the algorithm runs on. |
---|
267 | ///\param m An edge map containing the cost of the edges. |
---|
268 | ///\par |
---|
269 | ///The cost type can be any type satisfying the |
---|
270 | ///STL 'LessThan Comparable' |
---|
271 | ///concept if it also has an operator+() implemented. (It is necessary for |
---|
272 | ///computing the total cost of the tree). |
---|
273 | /// |
---|
274 | ///\return An appropriate input source for \ref kruskal(). |
---|
275 | /// |
---|
276 | template<class GR, class Map> |
---|
277 | inline |
---|
278 | KruskalMapInput<GR,Map> makeKruskalMapInput(const GR &g,const Map &m) |
---|
279 | { |
---|
280 | return KruskalMapInput<GR,Map>(g,m); |
---|
281 | } |
---|
282 | |
---|
283 | |
---|
284 | |
---|
285 | /* ** ** Output-objects: simple writable bool maps ** ** */ |
---|
286 | |
---|
287 | |
---|
288 | |
---|
289 | /// A writable bool-map that makes a sequence of "true" keys |
---|
290 | |
---|
291 | /// A writable bool-map that creates a sequence out of keys that receives |
---|
292 | /// the value "true". |
---|
293 | /// |
---|
294 | /// \sa makeKruskalSequenceOutput() |
---|
295 | /// |
---|
296 | /// Very often, when looking for a min cost spanning tree, we want as |
---|
297 | /// output a container containing the edges of the found tree. For this |
---|
298 | /// purpose exist this class that wraps around an STL iterator with a |
---|
299 | /// writable bool map interface. When a key gets value "true" this key |
---|
300 | /// is added to sequence pointed by the iterator. |
---|
301 | /// |
---|
302 | /// A typical usage: |
---|
303 | ///\code |
---|
304 | /// std::vector<Graph::Edge> v; |
---|
305 | /// kruskal(g, input, makeKruskalSequenceOutput(back_inserter(v))); |
---|
306 | ///\endcode |
---|
307 | /// |
---|
308 | /// For the most common case, when the input is given by a simple edge |
---|
309 | /// map and the output is a sequence of the tree edges, a special |
---|
310 | /// wrapper function exists: \ref kruskalEdgeMap_IteratorOut(). |
---|
311 | /// |
---|
312 | /// \warning Not a regular property map, as it doesn't know its Key |
---|
313 | |
---|
314 | template<class Iterator> |
---|
315 | class KruskalSequenceOutput { |
---|
316 | mutable Iterator it; |
---|
317 | |
---|
318 | public: |
---|
319 | typedef typename std::iterator_traits<Iterator>::value_type Key; |
---|
320 | typedef bool Value; |
---|
321 | |
---|
322 | KruskalSequenceOutput(Iterator const &_it) : it(_it) {} |
---|
323 | |
---|
324 | template<typename Key> |
---|
325 | void set(Key const& k, bool v) const { if(v) {*it=k; ++it;} } |
---|
326 | }; |
---|
327 | |
---|
328 | template<class Iterator> |
---|
329 | inline |
---|
330 | KruskalSequenceOutput<Iterator> |
---|
331 | makeKruskalSequenceOutput(Iterator it) { |
---|
332 | return KruskalSequenceOutput<Iterator>(it); |
---|
333 | } |
---|
334 | |
---|
335 | |
---|
336 | |
---|
337 | /* ** ** Wrapper funtions ** ** */ |
---|
338 | |
---|
339 | // \brief Wrapper function to kruskal(). |
---|
340 | // Input is from an edge map, output is a plain bool map. |
---|
341 | // |
---|
342 | // Wrapper function to kruskal(). |
---|
343 | // Input is from an edge map, output is a plain bool map. |
---|
344 | // |
---|
345 | // \param g The type of the graph the algorithm runs on. |
---|
346 | // \param in An edge map containing the cost of the edges. |
---|
347 | // \par |
---|
348 | // The cost type can be any type satisfying the |
---|
349 | // STL 'LessThan Comparable' |
---|
350 | // concept if it also has an operator+() implemented. (It is necessary for |
---|
351 | // computing the total cost of the tree). |
---|
352 | // |
---|
353 | // \retval out This must be a writable \c bool edge map. |
---|
354 | // After running the algorithm |
---|
355 | // this will contain the found minimum cost spanning tree: the value of an |
---|
356 | // edge will be set to \c true if it belongs to the tree, otherwise it will |
---|
357 | // be set to \c false. The value of each edge will be set exactly once. |
---|
358 | // |
---|
359 | // \return The cost of the found tree. |
---|
360 | |
---|
361 | template <class GR, class IN, class RET> |
---|
362 | inline |
---|
363 | typename IN::Value |
---|
364 | kruskal(GR const& g, |
---|
365 | IN const& in, |
---|
366 | RET &out, |
---|
367 | // typename IN::Key = typename GR::Edge(), |
---|
368 | //typename IN::Key = typename IN::Key (), |
---|
369 | // typename RET::Key = typename GR::Edge() |
---|
370 | const typename IN::Key * = (const typename IN::Key *)(0), |
---|
371 | const typename RET::Key * = (const typename RET::Key *)(0) |
---|
372 | ) |
---|
373 | { |
---|
374 | return kruskal(g, |
---|
375 | KruskalMapInput<GR,IN>(g,in), |
---|
376 | out); |
---|
377 | } |
---|
378 | |
---|
379 | // \brief Wrapper function to kruskal(). |
---|
380 | // Input is from an edge map, output is an STL Sequence. |
---|
381 | // |
---|
382 | // Wrapper function to kruskal(). |
---|
383 | // Input is from an edge map, output is an STL Sequence. |
---|
384 | // |
---|
385 | // \param g The type of the graph the algorithm runs on. |
---|
386 | // \param in An edge map containing the cost of the edges. |
---|
387 | // \par |
---|
388 | // The cost type can be any type satisfying the |
---|
389 | // STL 'LessThan Comparable' |
---|
390 | // concept if it also has an operator+() implemented. (It is necessary for |
---|
391 | // computing the total cost of the tree). |
---|
392 | // |
---|
393 | // \retval out This must be an iteraror of an STL Container with |
---|
394 | // <tt>GR::Edge</tt> as its <tt>value_type</tt>. |
---|
395 | // The algorithm copies the elements of the found tree into this sequence. |
---|
396 | // For example, if we know that the spanning tree of the graph \c g has |
---|
397 | // say 53 edges, then |
---|
398 | // we can put its edges into a STL vector \c tree with a code like this. |
---|
399 | //\code |
---|
400 | // std::vector<Edge> tree(53); |
---|
401 | // kruskal(g,cost,tree.begin()); |
---|
402 | //\endcode |
---|
403 | // Or if we don't know in advance the size of the tree, we can write this. |
---|
404 | //\code |
---|
405 | // std::vector<Edge> tree; |
---|
406 | // kruskal(g,cost,std::back_inserter(tree)); |
---|
407 | //\endcode |
---|
408 | // |
---|
409 | // \return The cost of the found tree. |
---|
410 | // |
---|
411 | // \bug its name does not follow the coding style. |
---|
412 | |
---|
413 | template <class GR, class IN, class RET> |
---|
414 | inline |
---|
415 | typename IN::Value |
---|
416 | kruskal(const GR& g, |
---|
417 | const IN& in, |
---|
418 | RET out, |
---|
419 | const typename RET::value_type * = |
---|
420 | (const typename RET::value_type *)(0) |
---|
421 | ) |
---|
422 | { |
---|
423 | KruskalSequenceOutput<RET> _out(out); |
---|
424 | return kruskal(g, KruskalMapInput<GR,IN>(g, in), _out); |
---|
425 | } |
---|
426 | |
---|
427 | template <class GR, class IN, class RET> |
---|
428 | inline |
---|
429 | typename IN::Value |
---|
430 | kruskal(const GR& g, |
---|
431 | const IN& in, |
---|
432 | RET *out |
---|
433 | ) |
---|
434 | { |
---|
435 | KruskalSequenceOutput<RET*> _out(out); |
---|
436 | return kruskal(g, KruskalMapInput<GR,IN>(g, in), _out); |
---|
437 | } |
---|
438 | |
---|
439 | /// @} |
---|
440 | |
---|
441 | } //namespace lemon |
---|
442 | |
---|
443 | #endif //LEMON_KRUSKAL_H |
---|