alpar@810
|
1 |
// -*- c++ -*- //
|
alpar@810
|
2 |
#ifndef HUGO_KRUSKAL_H
|
alpar@810
|
3 |
#define HUGO_KRUSKAL_H
|
alpar@810
|
4 |
|
alpar@810
|
5 |
#include <algorithm>
|
alpar@810
|
6 |
#include <hugo/unionfind.h>
|
alpar@810
|
7 |
|
alpar@810
|
8 |
/**
|
alpar@810
|
9 |
@defgroup spantree Minimum Cost Spanning Tree Algorithms
|
alpar@810
|
10 |
@ingroup galgs
|
alpar@810
|
11 |
\brief This group containes the algorithms for finding a minimum cost spanning
|
alpar@810
|
12 |
tree in a graph
|
alpar@810
|
13 |
|
alpar@810
|
14 |
This group containes the algorithms for finding a minimum cost spanning
|
alpar@810
|
15 |
tree in a graph
|
alpar@810
|
16 |
*/
|
alpar@810
|
17 |
|
alpar@810
|
18 |
///\ingroup spantree
|
alpar@810
|
19 |
///\file
|
alpar@810
|
20 |
///\brief Kruskal's algorithm to compute a minimum cost tree
|
alpar@810
|
21 |
///
|
alpar@810
|
22 |
///Kruskal's algorithm to compute a minimum cost tree.
|
alpar@810
|
23 |
|
alpar@810
|
24 |
namespace hugo {
|
alpar@810
|
25 |
|
alpar@810
|
26 |
/// \addtogroup spantree
|
alpar@810
|
27 |
/// @{
|
alpar@810
|
28 |
|
alpar@810
|
29 |
/// Kruskal's algorithm to find a minimum cost tree of a graph.
|
alpar@810
|
30 |
|
alpar@810
|
31 |
/// This function runs Kruskal's algorithm to find a minimum cost tree.
|
alpar@810
|
32 |
/// \param G The graph the algorithm runs on. The algorithm considers the
|
alpar@810
|
33 |
/// graph to be undirected, the direction of the edges are not used.
|
alpar@810
|
34 |
///
|
alpar@810
|
35 |
/// \param in This object is used to describe the edge costs. It must
|
alpar@810
|
36 |
/// be an STL compatible 'Forward Container'
|
alpar@824
|
37 |
/// with <tt>std::pair<GR::Edge,X></tt> as its <tt>value_type</tt>,
|
alpar@810
|
38 |
/// where X is the type of the costs. It must contain every edge in
|
alpar@810
|
39 |
/// cost-ascending order.
|
alpar@810
|
40 |
///\par
|
alpar@810
|
41 |
/// For the sake of simplicity, there is a helper class KruskalMapInput,
|
alpar@810
|
42 |
/// which converts a
|
alpar@810
|
43 |
/// simple edge map to an input of this form. Alternatively, you can use
|
alpar@810
|
44 |
/// the function \ref kruskalEdgeMap to compute the minimum cost tree if
|
alpar@810
|
45 |
/// the edge costs are given by an edge map.
|
alpar@810
|
46 |
///
|
alpar@810
|
47 |
/// \retval out This must be a writable \c bool edge map.
|
alpar@810
|
48 |
/// After running the algorithm
|
alpar@810
|
49 |
/// this will contain the found minimum cost spanning tree: the value of an
|
alpar@810
|
50 |
/// edge will be set to \c true if it belongs to the tree, otherwise it will
|
alpar@810
|
51 |
/// be set to \c false. The value of each edge will be set exactly once.
|
alpar@810
|
52 |
///
|
alpar@810
|
53 |
/// \return The cost of the found tree.
|
alpar@810
|
54 |
|
alpar@824
|
55 |
template <class GR, class IN, class OUT>
|
alpar@824
|
56 |
typename IN::value_type::second_type
|
alpar@824
|
57 |
kruskal(GR const& G, IN const& in,
|
alpar@824
|
58 |
OUT& out)
|
alpar@810
|
59 |
{
|
alpar@824
|
60 |
typedef typename IN::value_type::second_type EdgeCost;
|
alpar@824
|
61 |
typedef typename GR::template NodeMap<int> NodeIntMap;
|
alpar@824
|
62 |
typedef typename GR::Node Node;
|
alpar@810
|
63 |
|
alpar@810
|
64 |
NodeIntMap comp(G, -1);
|
alpar@810
|
65 |
UnionFind<Node,NodeIntMap> uf(comp);
|
alpar@810
|
66 |
|
alpar@810
|
67 |
EdgeCost tot_cost = 0;
|
alpar@824
|
68 |
for (typename IN::const_iterator p = in.begin();
|
alpar@810
|
69 |
p!=in.end(); ++p ) {
|
alpar@810
|
70 |
if ( uf.join(G.head((*p).first),
|
alpar@810
|
71 |
G.tail((*p).first)) ) {
|
alpar@810
|
72 |
out.set((*p).first, true);
|
alpar@810
|
73 |
tot_cost += (*p).second;
|
alpar@810
|
74 |
}
|
alpar@810
|
75 |
else {
|
alpar@810
|
76 |
out.set((*p).first, false);
|
alpar@810
|
77 |
}
|
alpar@810
|
78 |
}
|
alpar@810
|
79 |
return tot_cost;
|
alpar@810
|
80 |
}
|
alpar@810
|
81 |
|
alpar@810
|
82 |
/* A work-around for running Kruskal with const-reference bool maps... */
|
alpar@810
|
83 |
|
klao@885
|
84 |
/// Helper class for calling kruskal with "constant" output map.
|
klao@885
|
85 |
|
klao@885
|
86 |
/// Helper class for calling kruskal with output maps constructed
|
klao@885
|
87 |
/// on-the-fly.
|
alpar@810
|
88 |
///
|
klao@885
|
89 |
/// A typical examle is the following call:
|
klao@885
|
90 |
/// <tt>kruskal(G, some_input, makeSequenceOutput(iterator))</tt>.
|
klao@885
|
91 |
/// Here, the third argument is a temporary object (which wraps around an
|
klao@885
|
92 |
/// iterator with a writable bool map interface), and thus by rules of C++
|
klao@885
|
93 |
/// is a \c const object. To enable call like this exist this class and
|
klao@885
|
94 |
/// the prototype of the \ref kruskal() function with <tt>const& OUT</tt>
|
klao@885
|
95 |
/// third argument.
|
alpar@824
|
96 |
template<class Map>
|
alpar@810
|
97 |
class NonConstMapWr {
|
alpar@810
|
98 |
const Map &m;
|
alpar@810
|
99 |
public:
|
alpar@810
|
100 |
typedef typename Map::ValueType ValueType;
|
alpar@810
|
101 |
|
alpar@810
|
102 |
NonConstMapWr(const Map &_m) : m(_m) {}
|
alpar@810
|
103 |
|
alpar@824
|
104 |
template<class KeyType>
|
alpar@810
|
105 |
void set(KeyType const& k, ValueType const &v) const { m.set(k,v); }
|
alpar@810
|
106 |
};
|
alpar@810
|
107 |
|
alpar@824
|
108 |
template <class GR, class IN, class OUT>
|
alpar@810
|
109 |
inline
|
klao@885
|
110 |
typename IN::value_type::second_type
|
klao@885
|
111 |
kruskal(GR const& G, IN const& edges, OUT const& out_map)
|
alpar@810
|
112 |
{
|
alpar@824
|
113 |
NonConstMapWr<OUT> map_wr(out_map);
|
alpar@810
|
114 |
return kruskal(G, edges, map_wr);
|
alpar@810
|
115 |
}
|
alpar@810
|
116 |
|
alpar@810
|
117 |
/* ** ** Input-objects ** ** */
|
alpar@810
|
118 |
|
alpar@810
|
119 |
/// Kruskal input source.
|
alpar@810
|
120 |
|
alpar@810
|
121 |
/// Kruskal input source.
|
alpar@810
|
122 |
///
|
alpar@810
|
123 |
/// In most cases you possibly want to use the \ref kruskalEdgeMap() instead.
|
alpar@810
|
124 |
///
|
alpar@810
|
125 |
/// \sa makeKruskalMapInput()
|
alpar@810
|
126 |
///
|
alpar@824
|
127 |
///\param GR The type of the graph the algorithm runs on.
|
alpar@810
|
128 |
///\param Map An edge map containing the cost of the edges.
|
alpar@810
|
129 |
///\par
|
alpar@810
|
130 |
///The cost type can be any type satisfying
|
alpar@810
|
131 |
///the STL 'LessThan comparable'
|
alpar@810
|
132 |
///concept if it also has an operator+() implemented. (It is necessary for
|
alpar@810
|
133 |
///computing the total cost of the tree).
|
alpar@810
|
134 |
///
|
alpar@824
|
135 |
template<class GR, class Map>
|
alpar@810
|
136 |
class KruskalMapInput
|
alpar@824
|
137 |
: public std::vector< std::pair<typename GR::Edge,
|
alpar@810
|
138 |
typename Map::ValueType> > {
|
alpar@810
|
139 |
|
alpar@810
|
140 |
public:
|
alpar@824
|
141 |
typedef std::vector< std::pair<typename GR::Edge,
|
alpar@810
|
142 |
typename Map::ValueType> > Parent;
|
alpar@810
|
143 |
typedef typename Parent::value_type value_type;
|
alpar@810
|
144 |
|
alpar@810
|
145 |
private:
|
alpar@810
|
146 |
class comparePair {
|
alpar@810
|
147 |
public:
|
alpar@810
|
148 |
bool operator()(const value_type& a,
|
alpar@810
|
149 |
const value_type& b) {
|
alpar@810
|
150 |
return a.second < b.second;
|
alpar@810
|
151 |
}
|
alpar@810
|
152 |
};
|
alpar@810
|
153 |
|
alpar@810
|
154 |
public:
|
alpar@810
|
155 |
|
alpar@810
|
156 |
void sort() {
|
alpar@810
|
157 |
std::sort(this->begin(), this->end(), comparePair());
|
alpar@810
|
158 |
}
|
alpar@810
|
159 |
|
alpar@824
|
160 |
KruskalMapInput(GR const& G, Map const& m) {
|
alpar@824
|
161 |
typedef typename GR::EdgeIt EdgeIt;
|
alpar@810
|
162 |
|
klao@885
|
163 |
for(EdgeIt e(G);e!=INVALID;++e) push_back(value_type(e, m[e]));
|
alpar@810
|
164 |
sort();
|
alpar@810
|
165 |
}
|
alpar@810
|
166 |
};
|
alpar@810
|
167 |
|
alpar@810
|
168 |
/// Creates a KruskalMapInput object for \ref kruskal()
|
alpar@810
|
169 |
|
alpar@810
|
170 |
/// It makes is easier to use
|
alpar@810
|
171 |
/// \ref KruskalMapInput by making it unnecessary
|
alpar@810
|
172 |
/// to explicitly give the type of the parameters.
|
alpar@810
|
173 |
///
|
alpar@810
|
174 |
/// In most cases you possibly
|
alpar@810
|
175 |
/// want to use the function kruskalEdgeMap() instead.
|
alpar@810
|
176 |
///
|
alpar@810
|
177 |
///\param G The type of the graph the algorithm runs on.
|
alpar@810
|
178 |
///\param m An edge map containing the cost of the edges.
|
alpar@810
|
179 |
///\par
|
alpar@810
|
180 |
///The cost type can be any type satisfying the
|
alpar@810
|
181 |
///STL 'LessThan Comparable'
|
alpar@810
|
182 |
///concept if it also has an operator+() implemented. (It is necessary for
|
alpar@810
|
183 |
///computing the total cost of the tree).
|
alpar@810
|
184 |
///
|
alpar@810
|
185 |
///\return An appropriate input source for \ref kruskal().
|
alpar@810
|
186 |
///
|
alpar@824
|
187 |
template<class GR, class Map>
|
alpar@810
|
188 |
inline
|
alpar@824
|
189 |
KruskalMapInput<GR,Map> makeKruskalMapInput(const GR &G,const Map &m)
|
alpar@810
|
190 |
{
|
alpar@824
|
191 |
return KruskalMapInput<GR,Map>(G,m);
|
alpar@810
|
192 |
}
|
alpar@810
|
193 |
|
alpar@810
|
194 |
|
klao@885
|
195 |
|
klao@885
|
196 |
/* ** ** Output-objects: simple writable bool maps ** ** */
|
alpar@810
|
197 |
|
klao@885
|
198 |
|
klao@885
|
199 |
|
alpar@810
|
200 |
/// A writable bool-map that makes a sequence of "true" keys
|
alpar@810
|
201 |
|
alpar@810
|
202 |
/// A writable bool-map that creates a sequence out of keys that receives
|
alpar@810
|
203 |
/// the value "true".
|
klao@885
|
204 |
///
|
klao@885
|
205 |
/// \sa makeKruskalSequenceOutput()
|
klao@885
|
206 |
///
|
klao@885
|
207 |
/// Very often, when looking for a min cost spanning tree, we want as
|
klao@885
|
208 |
/// output a container containing the edges of the found tree. For this
|
klao@885
|
209 |
/// purpose exist this class that wraps around an STL iterator with a
|
klao@885
|
210 |
/// writable bool map interface. When a key gets value "true" this key
|
klao@885
|
211 |
/// is added to sequence pointed by the iterator.
|
klao@885
|
212 |
///
|
klao@885
|
213 |
/// A typical usage:
|
klao@885
|
214 |
/// \code
|
klao@885
|
215 |
/// std::vector<Graph::Edge> v;
|
klao@885
|
216 |
/// kruskal(g, input, makeKruskalSequenceOutput(back_inserter(v)));
|
klao@885
|
217 |
/// \endcode
|
klao@885
|
218 |
///
|
klao@885
|
219 |
/// For the most common case, when the input is given by a simple edge
|
klao@885
|
220 |
/// map and the output is a sequence of the tree edges, a special
|
klao@885
|
221 |
/// wrapper function exists: \ref kruskalEdgeMap_IteratorOut().
|
klao@885
|
222 |
///
|
alpar@810
|
223 |
/// \warning Not a regular property map, as it doesn't know its KeyType
|
klao@885
|
224 |
|
alpar@824
|
225 |
template<class Iterator>
|
klao@885
|
226 |
class KruskalSequenceOutput {
|
alpar@810
|
227 |
mutable Iterator it;
|
alpar@810
|
228 |
|
alpar@810
|
229 |
public:
|
alpar@810
|
230 |
typedef bool ValueType;
|
alpar@810
|
231 |
|
klao@885
|
232 |
KruskalSequenceOutput(Iterator const &_it) : it(_it) {}
|
alpar@810
|
233 |
|
alpar@810
|
234 |
template<typename KeyType>
|
alpar@810
|
235 |
void set(KeyType const& k, bool v) const { if(v) {*it=k; ++it;} }
|
alpar@810
|
236 |
};
|
alpar@810
|
237 |
|
alpar@824
|
238 |
template<class Iterator>
|
alpar@810
|
239 |
inline
|
klao@885
|
240 |
KruskalSequenceOutput<Iterator>
|
klao@885
|
241 |
makeKruskalSequenceOutput(Iterator it) {
|
klao@885
|
242 |
return KruskalSequenceOutput<Iterator>(it);
|
alpar@810
|
243 |
}
|
alpar@810
|
244 |
|
klao@885
|
245 |
|
klao@885
|
246 |
|
alpar@810
|
247 |
/* ** ** Wrapper funtions ** ** */
|
alpar@810
|
248 |
|
alpar@810
|
249 |
|
klao@885
|
250 |
|
alpar@810
|
251 |
/// \brief Wrapper function to kruskal().
|
alpar@810
|
252 |
/// Input is from an edge map, output is a plain bool map.
|
alpar@810
|
253 |
///
|
alpar@810
|
254 |
/// Wrapper function to kruskal().
|
alpar@810
|
255 |
/// Input is from an edge map, output is a plain bool map.
|
alpar@810
|
256 |
///
|
alpar@810
|
257 |
///\param G The type of the graph the algorithm runs on.
|
alpar@810
|
258 |
///\param in An edge map containing the cost of the edges.
|
alpar@810
|
259 |
///\par
|
alpar@810
|
260 |
///The cost type can be any type satisfying the
|
alpar@810
|
261 |
///STL 'LessThan Comparable'
|
alpar@810
|
262 |
///concept if it also has an operator+() implemented. (It is necessary for
|
alpar@810
|
263 |
///computing the total cost of the tree).
|
alpar@810
|
264 |
///
|
alpar@810
|
265 |
/// \retval out This must be a writable \c bool edge map.
|
alpar@810
|
266 |
/// After running the algorithm
|
alpar@810
|
267 |
/// this will contain the found minimum cost spanning tree: the value of an
|
alpar@810
|
268 |
/// edge will be set to \c true if it belongs to the tree, otherwise it will
|
alpar@810
|
269 |
/// be set to \c false. The value of each edge will be set exactly once.
|
alpar@810
|
270 |
///
|
alpar@810
|
271 |
/// \return The cost of the found tree.
|
alpar@810
|
272 |
|
alpar@824
|
273 |
template <class GR, class IN, class RET>
|
alpar@810
|
274 |
inline
|
alpar@824
|
275 |
typename IN::ValueType
|
alpar@824
|
276 |
kruskalEdgeMap(GR const& G,
|
alpar@824
|
277 |
IN const& in,
|
alpar@824
|
278 |
RET &out) {
|
alpar@810
|
279 |
return kruskal(G,
|
alpar@824
|
280 |
KruskalMapInput<GR,IN>(G,in),
|
alpar@810
|
281 |
out);
|
alpar@810
|
282 |
}
|
alpar@810
|
283 |
|
alpar@810
|
284 |
/// \brief Wrapper function to kruskal().
|
alpar@810
|
285 |
/// Input is from an edge map, output is an STL Sequence.
|
alpar@810
|
286 |
///
|
alpar@810
|
287 |
/// Wrapper function to kruskal().
|
alpar@810
|
288 |
/// Input is from an edge map, output is an STL Sequence.
|
alpar@810
|
289 |
///
|
alpar@810
|
290 |
///\param G The type of the graph the algorithm runs on.
|
alpar@810
|
291 |
///\param in An edge map containing the cost of the edges.
|
alpar@810
|
292 |
///\par
|
alpar@810
|
293 |
///The cost type can be any type satisfying the
|
alpar@810
|
294 |
///STL 'LessThan Comparable'
|
alpar@810
|
295 |
///concept if it also has an operator+() implemented. (It is necessary for
|
alpar@810
|
296 |
///computing the total cost of the tree).
|
alpar@810
|
297 |
///
|
alpar@810
|
298 |
/// \retval out This must be an iteraror of an STL Container with
|
alpar@824
|
299 |
/// <tt>GR::Edge</tt> as its <tt>value_type</tt>.
|
alpar@810
|
300 |
/// The algorithm copies the elements of the found tree into this sequence.
|
alpar@810
|
301 |
/// For example, if we know that the spanning tree of the graph \c G has
|
alpar@810
|
302 |
/// say 53 edges then
|
alpar@824
|
303 |
/// we can put its edges into a STL vector \c tree with a code like this.
|
alpar@810
|
304 |
/// \code
|
alpar@810
|
305 |
/// std::vector<Edge> tree(53);
|
alpar@810
|
306 |
/// kruskalEdgeMap_IteratorOut(G,cost,tree.begin());
|
alpar@810
|
307 |
/// \endcode
|
alpar@810
|
308 |
/// Or if we don't know in advance the size of the tree, we can write this.
|
alpar@810
|
309 |
/// \code
|
alpar@810
|
310 |
/// std::vector<Edge> tree;
|
alpar@810
|
311 |
/// kruskalEdgeMap_IteratorOut(G,cost,std::back_inserter(tree));
|
alpar@810
|
312 |
/// \endcode
|
alpar@810
|
313 |
///
|
alpar@810
|
314 |
/// \return The cost of the found tree.
|
alpar@810
|
315 |
///
|
alpar@810
|
316 |
/// \bug its name does not follow the coding style.
|
klao@885
|
317 |
|
alpar@824
|
318 |
template <class GR, class IN, class RET>
|
alpar@810
|
319 |
inline
|
alpar@824
|
320 |
typename IN::ValueType
|
alpar@824
|
321 |
kruskalEdgeMap_IteratorOut(const GR& G,
|
alpar@824
|
322 |
const IN& in,
|
alpar@824
|
323 |
RET out)
|
alpar@810
|
324 |
{
|
klao@885
|
325 |
KruskalSequenceOutput<RET> _out(out);
|
klao@885
|
326 |
return kruskal(G, KruskalMapInput<GR,IN>(G, in), _out);
|
alpar@810
|
327 |
}
|
alpar@810
|
328 |
|
alpar@810
|
329 |
/// @}
|
alpar@810
|
330 |
|
alpar@810
|
331 |
} //namespace hugo
|
alpar@810
|
332 |
|
alpar@810
|
333 |
#endif //HUGO_KRUSKAL_H
|