lemon/kruskal.h
author Alpar Juttner <alpar@cs.elte.hu>
Fri, 18 Jul 2008 16:36:54 +0100
changeset 223 cfadfeef706f
parent 209 765619b7cbb2
child 220 a5d8c039f218
permissions -rw-r--r--
Fix compilation error when NDEBUG is defined.
alpar@209
     1
/* -*- mode: C++; indent-tabs-mode: nil; -*-
alpar@103
     2
 *
alpar@209
     3
 * This file is a part of LEMON, a generic C++ optimization library.
alpar@103
     4
 *
alpar@103
     5
 * Copyright (C) 2003-2008
alpar@103
     6
 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
alpar@103
     7
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
alpar@103
     8
 *
alpar@103
     9
 * Permission to use, modify and distribute this software is granted
alpar@103
    10
 * provided that this copyright notice appears in all copies. For
alpar@103
    11
 * precise terms see the accompanying LICENSE file.
alpar@103
    12
 *
alpar@103
    13
 * This software is provided "AS IS" with no warranty of any kind,
alpar@103
    14
 * express or implied, and with no claim as to its suitability for any
alpar@103
    15
 * purpose.
alpar@103
    16
 *
alpar@103
    17
 */
alpar@103
    18
alpar@103
    19
#ifndef LEMON_KRUSKAL_H
alpar@103
    20
#define LEMON_KRUSKAL_H
alpar@103
    21
alpar@103
    22
#include <algorithm>
alpar@103
    23
#include <vector>
alpar@103
    24
#include <lemon/unionfind.h>
alpar@103
    25
// #include <lemon/graph_utils.h>
alpar@103
    26
#include <lemon/maps.h>
alpar@103
    27
alpar@103
    28
// #include <lemon/radix_sort.h>
alpar@103
    29
alpar@103
    30
#include <lemon/bits/utility.h>
alpar@103
    31
#include <lemon/bits/traits.h>
alpar@103
    32
alpar@103
    33
///\ingroup spantree
alpar@103
    34
///\file
kpeter@194
    35
///\brief Kruskal's algorithm to compute a minimum cost spanning tree
alpar@103
    36
///
kpeter@194
    37
///Kruskal's algorithm to compute a minimum cost spanning tree.
alpar@103
    38
///
alpar@103
    39
alpar@103
    40
namespace lemon {
alpar@103
    41
alpar@103
    42
  namespace _kruskal_bits {
alpar@103
    43
alpar@103
    44
    // Kruskal for directed graphs.
alpar@103
    45
alpar@103
    46
    template <typename Digraph, typename In, typename Out>
alpar@103
    47
    typename disable_if<lemon::UndirectedTagIndicator<Digraph>,
alpar@209
    48
                       typename In::value_type::second_type >::type
alpar@103
    49
    kruskal(const Digraph& digraph, const In& in, Out& out,dummy<0> = 0) {
alpar@103
    50
      typedef typename In::value_type::second_type Value;
alpar@103
    51
      typedef typename Digraph::template NodeMap<int> IndexMap;
alpar@103
    52
      typedef typename Digraph::Node Node;
alpar@209
    53
alpar@103
    54
      IndexMap index(digraph);
alpar@103
    55
      UnionFind<IndexMap> uf(index);
alpar@103
    56
      for (typename Digraph::NodeIt it(digraph); it != INVALID; ++it) {
alpar@103
    57
        uf.insert(it);
alpar@103
    58
      }
alpar@209
    59
alpar@103
    60
      Value tree_value = 0;
alpar@103
    61
      for (typename In::const_iterator it = in.begin(); it != in.end(); ++it) {
alpar@103
    62
        if (uf.join(digraph.target(it->first),digraph.source(it->first))) {
alpar@103
    63
          out.set(it->first, true);
alpar@103
    64
          tree_value += it->second;
alpar@103
    65
        }
alpar@103
    66
        else {
alpar@103
    67
          out.set(it->first, false);
alpar@103
    68
        }
alpar@103
    69
      }
alpar@103
    70
      return tree_value;
alpar@103
    71
    }
alpar@103
    72
alpar@103
    73
    // Kruskal for undirected graphs.
alpar@103
    74
alpar@103
    75
    template <typename Graph, typename In, typename Out>
alpar@103
    76
    typename enable_if<lemon::UndirectedTagIndicator<Graph>,
alpar@209
    77
                       typename In::value_type::second_type >::type
alpar@103
    78
    kruskal(const Graph& graph, const In& in, Out& out,dummy<1> = 1) {
alpar@103
    79
      typedef typename In::value_type::second_type Value;
alpar@103
    80
      typedef typename Graph::template NodeMap<int> IndexMap;
alpar@103
    81
      typedef typename Graph::Node Node;
alpar@209
    82
alpar@103
    83
      IndexMap index(graph);
alpar@103
    84
      UnionFind<IndexMap> uf(index);
alpar@103
    85
      for (typename Graph::NodeIt it(graph); it != INVALID; ++it) {
alpar@103
    86
        uf.insert(it);
alpar@103
    87
      }
alpar@209
    88
alpar@103
    89
      Value tree_value = 0;
alpar@103
    90
      for (typename In::const_iterator it = in.begin(); it != in.end(); ++it) {
alpar@103
    91
        if (uf.join(graph.u(it->first),graph.v(it->first))) {
alpar@103
    92
          out.set(it->first, true);
alpar@103
    93
          tree_value += it->second;
alpar@103
    94
        }
alpar@103
    95
        else {
alpar@103
    96
          out.set(it->first, false);
alpar@103
    97
        }
alpar@103
    98
      }
alpar@103
    99
      return tree_value;
alpar@103
   100
    }
alpar@103
   101
alpar@103
   102
alpar@103
   103
    template <typename Sequence>
alpar@103
   104
    struct PairComp {
alpar@103
   105
      typedef typename Sequence::value_type Value;
alpar@103
   106
      bool operator()(const Value& left, const Value& right) {
alpar@209
   107
        return left.second < right.second;
alpar@103
   108
      }
alpar@103
   109
    };
alpar@103
   110
alpar@103
   111
    template <typename In, typename Enable = void>
alpar@103
   112
    struct SequenceInputIndicator {
alpar@103
   113
      static const bool value = false;
alpar@103
   114
    };
alpar@103
   115
alpar@103
   116
    template <typename In>
alpar@209
   117
    struct SequenceInputIndicator<In,
alpar@103
   118
      typename exists<typename In::value_type::first_type>::type> {
alpar@103
   119
      static const bool value = true;
alpar@103
   120
    };
alpar@103
   121
alpar@103
   122
    template <typename In, typename Enable = void>
alpar@103
   123
    struct MapInputIndicator {
alpar@103
   124
      static const bool value = false;
alpar@103
   125
    };
alpar@103
   126
alpar@103
   127
    template <typename In>
alpar@209
   128
    struct MapInputIndicator<In,
alpar@103
   129
      typename exists<typename In::Value>::type> {
alpar@103
   130
      static const bool value = true;
alpar@103
   131
    };
alpar@103
   132
alpar@103
   133
    template <typename In, typename Enable = void>
alpar@103
   134
    struct SequenceOutputIndicator {
alpar@103
   135
      static const bool value = false;
alpar@103
   136
    };
alpar@209
   137
alpar@103
   138
    template <typename Out>
alpar@209
   139
    struct SequenceOutputIndicator<Out,
alpar@103
   140
      typename exists<typename Out::value_type>::type> {
alpar@103
   141
      static const bool value = true;
alpar@103
   142
    };
alpar@103
   143
alpar@103
   144
    template <typename Out, typename Enable = void>
alpar@103
   145
    struct MapOutputIndicator {
alpar@103
   146
      static const bool value = false;
alpar@103
   147
    };
alpar@103
   148
alpar@103
   149
    template <typename Out>
alpar@209
   150
    struct MapOutputIndicator<Out,
alpar@103
   151
      typename exists<typename Out::Value>::type> {
alpar@103
   152
      static const bool value = true;
alpar@103
   153
    };
alpar@103
   154
alpar@103
   155
    template <typename In, typename InEnable = void>
alpar@103
   156
    struct KruskalValueSelector {};
alpar@103
   157
alpar@103
   158
    template <typename In>
alpar@103
   159
    struct KruskalValueSelector<In,
alpar@209
   160
      typename enable_if<SequenceInputIndicator<In>, void>::type>
alpar@103
   161
    {
alpar@103
   162
      typedef typename In::value_type::second_type Value;
alpar@209
   163
    };
alpar@103
   164
alpar@103
   165
    template <typename In>
alpar@103
   166
    struct KruskalValueSelector<In,
alpar@209
   167
      typename enable_if<MapInputIndicator<In>, void>::type>
alpar@103
   168
    {
alpar@103
   169
      typedef typename In::Value Value;
alpar@209
   170
    };
alpar@209
   171
alpar@103
   172
    template <typename Graph, typename In, typename Out,
alpar@103
   173
              typename InEnable = void>
alpar@103
   174
    struct KruskalInputSelector {};
alpar@103
   175
alpar@103
   176
    template <typename Graph, typename In, typename Out,
alpar@103
   177
              typename InEnable = void>
alpar@103
   178
    struct KruskalOutputSelector {};
alpar@209
   179
alpar@103
   180
    template <typename Graph, typename In, typename Out>
alpar@103
   181
    struct KruskalInputSelector<Graph, In, Out,
alpar@209
   182
      typename enable_if<SequenceInputIndicator<In>, void>::type >
alpar@103
   183
    {
alpar@103
   184
      typedef typename In::value_type::second_type Value;
alpar@103
   185
alpar@103
   186
      static Value kruskal(const Graph& graph, const In& in, Out& out) {
alpar@103
   187
        return KruskalOutputSelector<Graph, In, Out>::
alpar@103
   188
          kruskal(graph, in, out);
alpar@103
   189
      }
alpar@103
   190
alpar@103
   191
    };
alpar@103
   192
alpar@103
   193
    template <typename Graph, typename In, typename Out>
alpar@103
   194
    struct KruskalInputSelector<Graph, In, Out,
alpar@209
   195
      typename enable_if<MapInputIndicator<In>, void>::type >
alpar@103
   196
    {
alpar@103
   197
      typedef typename In::Value Value;
alpar@103
   198
      static Value kruskal(const Graph& graph, const In& in, Out& out) {
alpar@103
   199
        typedef typename In::Key MapArc;
alpar@103
   200
        typedef typename In::Value Value;
alpar@103
   201
        typedef typename ItemSetTraits<Graph, MapArc>::ItemIt MapArcIt;
alpar@103
   202
        typedef std::vector<std::pair<MapArc, Value> > Sequence;
alpar@103
   203
        Sequence seq;
alpar@209
   204
alpar@103
   205
        for (MapArcIt it(graph); it != INVALID; ++it) {
alpar@103
   206
          seq.push_back(std::make_pair(it, in[it]));
alpar@103
   207
        }
alpar@103
   208
alpar@103
   209
        std::sort(seq.begin(), seq.end(), PairComp<Sequence>());
alpar@103
   210
        return KruskalOutputSelector<Graph, Sequence, Out>::
alpar@103
   211
          kruskal(graph, seq, out);
alpar@103
   212
      }
alpar@103
   213
    };
alpar@103
   214
deba@136
   215
    template <typename T>
deba@136
   216
    struct RemoveConst {
deba@136
   217
      typedef T type;
deba@136
   218
    };
deba@136
   219
deba@136
   220
    template <typename T>
deba@136
   221
    struct RemoveConst<const T> {
deba@136
   222
      typedef T type;
deba@136
   223
    };
deba@136
   224
alpar@103
   225
    template <typename Graph, typename In, typename Out>
alpar@103
   226
    struct KruskalOutputSelector<Graph, In, Out,
alpar@209
   227
      typename enable_if<SequenceOutputIndicator<Out>, void>::type >
alpar@103
   228
    {
alpar@103
   229
      typedef typename In::value_type::second_type Value;
alpar@103
   230
alpar@103
   231
      static Value kruskal(const Graph& graph, const In& in, Out& out) {
kpeter@167
   232
        typedef LoggerBoolMap<typename RemoveConst<Out>::type> Map;
alpar@103
   233
        Map map(out);
alpar@103
   234
        return _kruskal_bits::kruskal(graph, in, map);
alpar@103
   235
      }
alpar@103
   236
alpar@103
   237
    };
alpar@103
   238
alpar@103
   239
    template <typename Graph, typename In, typename Out>
alpar@103
   240
    struct KruskalOutputSelector<Graph, In, Out,
alpar@209
   241
      typename enable_if<MapOutputIndicator<Out>, void>::type >
alpar@103
   242
    {
alpar@103
   243
      typedef typename In::value_type::second_type Value;
alpar@103
   244
alpar@103
   245
      static Value kruskal(const Graph& graph, const In& in, Out& out) {
alpar@103
   246
        return _kruskal_bits::kruskal(graph, in, out);
alpar@103
   247
      }
alpar@103
   248
    };
alpar@103
   249
alpar@103
   250
  }
alpar@103
   251
alpar@103
   252
  /// \ingroup spantree
alpar@103
   253
  ///
kpeter@194
   254
  /// \brief Kruskal algorithm to find a minimum cost spanning tree of
kpeter@194
   255
  /// a graph.
alpar@103
   256
  ///
alpar@209
   257
  /// This function runs Kruskal's algorithm to find a minimum cost
kpeter@194
   258
  /// spanning tree.
alpar@103
   259
  /// Due to some C++ hacking, it accepts various input and output types.
alpar@103
   260
  ///
alpar@103
   261
  /// \param g The graph the algorithm runs on.
alpar@209
   262
  /// It can be either \ref concepts::Digraph "directed" or
alpar@103
   263
  /// \ref concepts::Graph "undirected".
alpar@209
   264
  /// If the graph is directed, the algorithm consider it to be
alpar@103
   265
  /// undirected by disregarding the direction of the arcs.
alpar@103
   266
  ///
alpar@209
   267
  /// \param in This object is used to describe the arc/edge costs.
kpeter@194
   268
  /// It can be one of the following choices.
alpar@103
   269
  /// - An STL compatible 'Forward Container' with
kpeter@194
   270
  /// <tt>std::pair<GR::Arc,X></tt> or
kpeter@194
   271
  /// <tt>std::pair<GR::Edge,X></tt> as its <tt>value_type</tt>, where
kpeter@194
   272
  /// \c X is the type of the costs. The pairs indicates the arcs/edges
alpar@103
   273
  /// along with the assigned cost. <em>They must be in a
alpar@103
   274
  /// cost-ascending order.</em>
alpar@209
   275
  /// - Any readable arc/edge map. The values of the map indicate the
kpeter@194
   276
  /// arc/edge costs.
alpar@103
   277
  ///
kpeter@194
   278
  /// \retval out Here we also have a choice.
kpeter@194
   279
  /// - It can be a writable \c bool arc/edge map. After running the
kpeter@194
   280
  /// algorithm it will contain the found minimum cost spanning
kpeter@194
   281
  /// tree: the value of an arc/edge will be set to \c true if it belongs
alpar@103
   282
  /// to the tree, otherwise it will be set to \c false. The value of
kpeter@194
   283
  /// each arc/edge will be set exactly once.
alpar@103
   284
  /// - It can also be an iteraror of an STL Container with
kpeter@194
   285
  /// <tt>GR::Arc</tt> or <tt>GR::Edge</tt> as its
alpar@103
   286
  /// <tt>value_type</tt>.  The algorithm copies the elements of the
alpar@103
   287
  /// found tree into this sequence.  For example, if we know that the
alpar@103
   288
  /// spanning tree of the graph \c g has say 53 arcs, then we can
alpar@103
   289
  /// put its arcs into an STL vector \c tree with a code like this.
alpar@103
   290
  ///\code
alpar@103
   291
  /// std::vector<Arc> tree(53);
alpar@103
   292
  /// kruskal(g,cost,tree.begin());
alpar@103
   293
  ///\endcode
alpar@103
   294
  /// Or if we don't know in advance the size of the tree, we can
alpar@209
   295
  /// write this.
kpeter@194
   296
  ///\code
kpeter@194
   297
  /// std::vector<Arc> tree;
alpar@209
   298
  /// kruskal(g,cost,std::back_inserter(tree));
alpar@103
   299
  ///\endcode
alpar@103
   300
  ///
kpeter@194
   301
  /// \return The total cost of the found spanning tree.
alpar@103
   302
  ///
kpeter@216
   303
  /// \note If the input graph is not (weakly) connected, a spanning 
kpeter@216
   304
  /// forest is calculated instead of a spanning tree.
alpar@103
   305
alpar@103
   306
#ifdef DOXYGEN
alpar@103
   307
  template <class Graph, class In, class Out>
alpar@103
   308
  Value kruskal(GR const& g, const In& in, Out& out)
alpar@209
   309
#else
alpar@103
   310
  template <class Graph, class In, class Out>
alpar@209
   311
  inline typename _kruskal_bits::KruskalValueSelector<In>::Value
alpar@209
   312
  kruskal(const Graph& graph, const In& in, Out& out)
alpar@103
   313
#endif
alpar@103
   314
  {
alpar@103
   315
    return _kruskal_bits::KruskalInputSelector<Graph, In, Out>::
alpar@103
   316
      kruskal(graph, in, out);
alpar@103
   317
  }
alpar@103
   318
alpar@209
   319
alpar@209
   320
alpar@103
   321
alpar@103
   322
  template <class Graph, class In, class Out>
alpar@103
   323
  inline typename _kruskal_bits::KruskalValueSelector<In>::Value
alpar@103
   324
  kruskal(const Graph& graph, const In& in, const Out& out)
alpar@103
   325
  {
alpar@103
   326
    return _kruskal_bits::KruskalInputSelector<Graph, In, const Out>::
alpar@103
   327
      kruskal(graph, in, out);
alpar@209
   328
  }
alpar@103
   329
alpar@103
   330
} //namespace lemon
alpar@103
   331
alpar@103
   332
#endif //LEMON_KRUSKAL_H