gravatar
alpar (Alpar Juttner)
alpar@cs.elte.hu
Merge
0 1 0
merge default
0 files changed with 2 insertions and 3 deletions:
↑ Collapse diff ↑
Ignore white space 768 line context
1 1
/* -*- mode: C++; indent-tabs-mode: nil; -*-
2 2
 *
3 3
 * This file is a part of LEMON, a generic C++ optimization library.
4 4
 *
5 5
 * Copyright (C) 2003-2008
6 6
 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
7 7
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
8 8
 *
9 9
 * Permission to use, modify and distribute this software is granted
10 10
 * provided that this copyright notice appears in all copies. For
11 11
 * precise terms see the accompanying LICENSE file.
12 12
 *
13 13
 * This software is provided "AS IS" with no warranty of any kind,
14 14
 * express or implied, and with no claim as to its suitability for any
15 15
 * purpose.
16 16
 *
17 17
 */
18 18

	
19 19
#ifndef LEMON_KRUSKAL_H
20 20
#define LEMON_KRUSKAL_H
21 21

	
22 22
#include <algorithm>
23 23
#include <vector>
24 24
#include <lemon/unionfind.h>
25 25
// #include <lemon/graph_utils.h>
26 26
#include <lemon/maps.h>
27 27

	
28 28
// #include <lemon/radix_sort.h>
29 29

	
30 30
#include <lemon/bits/utility.h>
31 31
#include <lemon/bits/traits.h>
32 32

	
33 33
///\ingroup spantree
34 34
///\file
35 35
///\brief Kruskal's algorithm to compute a minimum cost spanning tree
36 36
///
37 37
///Kruskal's algorithm to compute a minimum cost spanning tree.
38 38
///
39 39

	
40 40
namespace lemon {
41 41

	
42 42
  namespace _kruskal_bits {
43 43

	
44 44
    // Kruskal for directed graphs.
45 45

	
46 46
    template <typename Digraph, typename In, typename Out>
47 47
    typename disable_if<lemon::UndirectedTagIndicator<Digraph>,
48 48
                       typename In::value_type::second_type >::type
49 49
    kruskal(const Digraph& digraph, const In& in, Out& out,dummy<0> = 0) {
50 50
      typedef typename In::value_type::second_type Value;
51 51
      typedef typename Digraph::template NodeMap<int> IndexMap;
52 52
      typedef typename Digraph::Node Node;
53 53

	
54 54
      IndexMap index(digraph);
55 55
      UnionFind<IndexMap> uf(index);
56 56
      for (typename Digraph::NodeIt it(digraph); it != INVALID; ++it) {
57 57
        uf.insert(it);
58 58
      }
59 59

	
60 60
      Value tree_value = 0;
61 61
      for (typename In::const_iterator it = in.begin(); it != in.end(); ++it) {
62 62
        if (uf.join(digraph.target(it->first),digraph.source(it->first))) {
63 63
          out.set(it->first, true);
64 64
          tree_value += it->second;
65 65
        }
66 66
        else {
67 67
          out.set(it->first, false);
68 68
        }
69 69
      }
70 70
      return tree_value;
71 71
    }
72 72

	
73 73
    // Kruskal for undirected graphs.
74 74

	
75 75
    template <typename Graph, typename In, typename Out>
76 76
    typename enable_if<lemon::UndirectedTagIndicator<Graph>,
77 77
                       typename In::value_type::second_type >::type
78 78
    kruskal(const Graph& graph, const In& in, Out& out,dummy<1> = 1) {
79 79
      typedef typename In::value_type::second_type Value;
80 80
      typedef typename Graph::template NodeMap<int> IndexMap;
81 81
      typedef typename Graph::Node Node;
82 82

	
83 83
      IndexMap index(graph);
84 84
      UnionFind<IndexMap> uf(index);
85 85
      for (typename Graph::NodeIt it(graph); it != INVALID; ++it) {
86 86
        uf.insert(it);
87 87
      }
88 88

	
89 89
      Value tree_value = 0;
90 90
      for (typename In::const_iterator it = in.begin(); it != in.end(); ++it) {
91 91
        if (uf.join(graph.u(it->first),graph.v(it->first))) {
92 92
          out.set(it->first, true);
93 93
          tree_value += it->second;
94 94
        }
95 95
        else {
96 96
          out.set(it->first, false);
97 97
        }
98 98
      }
99 99
      return tree_value;
100 100
    }
101 101

	
102 102

	
103 103
    template <typename Sequence>
104 104
    struct PairComp {
105 105
      typedef typename Sequence::value_type Value;
106 106
      bool operator()(const Value& left, const Value& right) {
107 107
        return left.second < right.second;
108 108
      }
109 109
    };
110 110

	
111 111
    template <typename In, typename Enable = void>
112 112
    struct SequenceInputIndicator {
113 113
      static const bool value = false;
114 114
    };
115 115

	
116 116
    template <typename In>
117 117
    struct SequenceInputIndicator<In,
118 118
      typename exists<typename In::value_type::first_type>::type> {
119 119
      static const bool value = true;
120 120
    };
121 121

	
122 122
    template <typename In, typename Enable = void>
123 123
    struct MapInputIndicator {
124 124
      static const bool value = false;
125 125
    };
126 126

	
127 127
    template <typename In>
128 128
    struct MapInputIndicator<In,
129 129
      typename exists<typename In::Value>::type> {
130 130
      static const bool value = true;
131 131
    };
132 132

	
133 133
    template <typename In, typename Enable = void>
134 134
    struct SequenceOutputIndicator {
135 135
      static const bool value = false;
136 136
    };
137 137

	
138 138
    template <typename Out>
139 139
    struct SequenceOutputIndicator<Out,
140 140
      typename exists<typename Out::value_type>::type> {
141 141
      static const bool value = true;
142 142
    };
143 143

	
144 144
    template <typename Out, typename Enable = void>
145 145
    struct MapOutputIndicator {
146 146
      static const bool value = false;
147 147
    };
148 148

	
149 149
    template <typename Out>
150 150
    struct MapOutputIndicator<Out,
151 151
      typename exists<typename Out::Value>::type> {
152 152
      static const bool value = true;
153 153
    };
154 154

	
155 155
    template <typename In, typename InEnable = void>
156 156
    struct KruskalValueSelector {};
157 157

	
158 158
    template <typename In>
159 159
    struct KruskalValueSelector<In,
160 160
      typename enable_if<SequenceInputIndicator<In>, void>::type>
161 161
    {
162 162
      typedef typename In::value_type::second_type Value;
163 163
    };
164 164

	
165 165
    template <typename In>
166 166
    struct KruskalValueSelector<In,
167 167
      typename enable_if<MapInputIndicator<In>, void>::type>
168 168
    {
169 169
      typedef typename In::Value Value;
170 170
    };
171 171

	
172 172
    template <typename Graph, typename In, typename Out,
173 173
              typename InEnable = void>
174 174
    struct KruskalInputSelector {};
175 175

	
176 176
    template <typename Graph, typename In, typename Out,
177 177
              typename InEnable = void>
178 178
    struct KruskalOutputSelector {};
179 179

	
180 180
    template <typename Graph, typename In, typename Out>
181 181
    struct KruskalInputSelector<Graph, In, Out,
182 182
      typename enable_if<SequenceInputIndicator<In>, void>::type >
183 183
    {
184 184
      typedef typename In::value_type::second_type Value;
185 185

	
186 186
      static Value kruskal(const Graph& graph, const In& in, Out& out) {
187 187
        return KruskalOutputSelector<Graph, In, Out>::
188 188
          kruskal(graph, in, out);
189 189
      }
190 190

	
191 191
    };
192 192

	
193 193
    template <typename Graph, typename In, typename Out>
194 194
    struct KruskalInputSelector<Graph, In, Out,
195 195
      typename enable_if<MapInputIndicator<In>, void>::type >
196 196
    {
197 197
      typedef typename In::Value Value;
198 198
      static Value kruskal(const Graph& graph, const In& in, Out& out) {
199 199
        typedef typename In::Key MapArc;
200 200
        typedef typename In::Value Value;
201 201
        typedef typename ItemSetTraits<Graph, MapArc>::ItemIt MapArcIt;
202 202
        typedef std::vector<std::pair<MapArc, Value> > Sequence;
203 203
        Sequence seq;
204 204

	
205 205
        for (MapArcIt it(graph); it != INVALID; ++it) {
206 206
          seq.push_back(std::make_pair(it, in[it]));
207 207
        }
208 208

	
209 209
        std::sort(seq.begin(), seq.end(), PairComp<Sequence>());
210 210
        return KruskalOutputSelector<Graph, Sequence, Out>::
211 211
          kruskal(graph, seq, out);
212 212
      }
213 213
    };
214 214

	
215 215
    template <typename T>
216 216
    struct RemoveConst {
217 217
      typedef T type;
218 218
    };
219 219

	
220 220
    template <typename T>
221 221
    struct RemoveConst<const T> {
222 222
      typedef T type;
223 223
    };
224 224

	
225 225
    template <typename Graph, typename In, typename Out>
226 226
    struct KruskalOutputSelector<Graph, In, Out,
227 227
      typename enable_if<SequenceOutputIndicator<Out>, void>::type >
228 228
    {
229 229
      typedef typename In::value_type::second_type Value;
230 230

	
231 231
      static Value kruskal(const Graph& graph, const In& in, Out& out) {
232 232
        typedef LoggerBoolMap<typename RemoveConst<Out>::type> Map;
233 233
        Map map(out);
234 234
        return _kruskal_bits::kruskal(graph, in, map);
235 235
      }
236 236

	
237 237
    };
238 238

	
239 239
    template <typename Graph, typename In, typename Out>
240 240
    struct KruskalOutputSelector<Graph, In, Out,
241 241
      typename enable_if<MapOutputIndicator<Out>, void>::type >
242 242
    {
243 243
      typedef typename In::value_type::second_type Value;
244 244

	
245 245
      static Value kruskal(const Graph& graph, const In& in, Out& out) {
246 246
        return _kruskal_bits::kruskal(graph, in, out);
247 247
      }
248 248
    };
249 249

	
250 250
  }
251 251

	
252 252
  /// \ingroup spantree
253 253
  ///
254 254
  /// \brief Kruskal algorithm to find a minimum cost spanning tree of
255 255
  /// a graph.
256 256
  ///
257 257
  /// This function runs Kruskal's algorithm to find a minimum cost
258 258
  /// spanning tree.
259 259
  /// Due to some C++ hacking, it accepts various input and output types.
260 260
  ///
261 261
  /// \param g The graph the algorithm runs on.
262 262
  /// It can be either \ref concepts::Digraph "directed" or
263 263
  /// \ref concepts::Graph "undirected".
264 264
  /// If the graph is directed, the algorithm consider it to be
265 265
  /// undirected by disregarding the direction of the arcs.
266 266
  ///
267 267
  /// \param in This object is used to describe the arc/edge costs.
268 268
  /// It can be one of the following choices.
269 269
  /// - An STL compatible 'Forward Container' with
270 270
  /// <tt>std::pair<GR::Arc,X></tt> or
271 271
  /// <tt>std::pair<GR::Edge,X></tt> as its <tt>value_type</tt>, where
272 272
  /// \c X is the type of the costs. The pairs indicates the arcs/edges
273 273
  /// along with the assigned cost. <em>They must be in a
274 274
  /// cost-ascending order.</em>
275 275
  /// - Any readable arc/edge map. The values of the map indicate the
276 276
  /// arc/edge costs.
277 277
  ///
278 278
  /// \retval out Here we also have a choice.
279 279
  /// - It can be a writable \c bool arc/edge map. After running the
280 280
  /// algorithm it will contain the found minimum cost spanning
281 281
  /// tree: the value of an arc/edge will be set to \c true if it belongs
282 282
  /// to the tree, otherwise it will be set to \c false. The value of
283 283
  /// each arc/edge will be set exactly once.
284 284
  /// - It can also be an iteraror of an STL Container with
285 285
  /// <tt>GR::Arc</tt> or <tt>GR::Edge</tt> as its
286 286
  /// <tt>value_type</tt>.  The algorithm copies the elements of the
287 287
  /// found tree into this sequence.  For example, if we know that the
288 288
  /// spanning tree of the graph \c g has say 53 arcs, then we can
289 289
  /// put its arcs into an STL vector \c tree with a code like this.
290 290
  ///\code
291 291
  /// std::vector<Arc> tree(53);
292 292
  /// kruskal(g,cost,tree.begin());
293 293
  ///\endcode
294 294
  /// Or if we don't know in advance the size of the tree, we can
295 295
  /// write this.
296 296
  ///\code
297 297
  /// std::vector<Arc> tree;
298 298
  /// kruskal(g,cost,std::back_inserter(tree));
299 299
  ///\endcode
300 300
  ///
301 301
  /// \return The total cost of the found spanning tree.
302 302
  ///
303
  /// \warning If Kruskal runs on an be consistent of using the same
304
  /// Arc type for input and output.
305
  ///
303
  /// \note If the input graph is not (weakly) connected, a spanning 
304
  /// forest is calculated instead of a spanning tree.
306 305

	
307 306
#ifdef DOXYGEN
308 307
  template <class Graph, class In, class Out>
309 308
  Value kruskal(GR const& g, const In& in, Out& out)
310 309
#else
311 310
  template <class Graph, class In, class Out>
312 311
  inline typename _kruskal_bits::KruskalValueSelector<In>::Value
313 312
  kruskal(const Graph& graph, const In& in, Out& out)
314 313
#endif
315 314
  {
316 315
    return _kruskal_bits::KruskalInputSelector<Graph, In, Out>::
317 316
      kruskal(graph, in, out);
318 317
  }
319 318

	
320 319

	
321 320

	
322 321

	
323 322
  template <class Graph, class In, class Out>
324 323
  inline typename _kruskal_bits::KruskalValueSelector<In>::Value
325 324
  kruskal(const Graph& graph, const In& in, const Out& out)
326 325
  {
327 326
    return _kruskal_bits::KruskalInputSelector<Graph, In, const Out>::
328 327
      kruskal(graph, in, out);
329 328
  }
330 329

	
331 330
} //namespace lemon
332 331

	
333 332
#endif //LEMON_KRUSKAL_H
0 comments (0 inline)