lemon/kruskal.h
author Alpar Juttner <alpar@cs.elte.hu>
Sat, 12 Apr 2008 19:41:48 +0100
changeset 121 91c0fed3181e
child 136 b82dc494bafc
permissions -rw-r--r--
Change SubNoCounter to NoSubCounter
alpar@103
     1
/* -*- C++ -*-
alpar@103
     2
 *
alpar@103
     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
alpar@103
    35
///\brief Kruskal's algorithm to compute a minimum cost tree
alpar@103
    36
///
alpar@103
    37
///Kruskal's algorithm to compute a minimum cost 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@103
    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@103
    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@103
    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@103
    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@103
    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@103
    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@103
   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@103
   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@103
   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@103
   137
 
alpar@103
   138
    template <typename Out>
alpar@103
   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@103
   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@103
   160
      typename enable_if<SequenceInputIndicator<In>, void>::type> 
alpar@103
   161
    {
alpar@103
   162
      typedef typename In::value_type::second_type Value;
alpar@103
   163
    };    
alpar@103
   164
alpar@103
   165
    template <typename In>
alpar@103
   166
    struct KruskalValueSelector<In,
alpar@103
   167
      typename enable_if<MapInputIndicator<In>, void>::type> 
alpar@103
   168
    {
alpar@103
   169
      typedef typename In::Value Value;
alpar@103
   170
    };    
alpar@103
   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@103
   179
    
alpar@103
   180
    template <typename Graph, typename In, typename Out>
alpar@103
   181
    struct KruskalInputSelector<Graph, In, Out,
alpar@103
   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@103
   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@103
   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
alpar@103
   215
    template <typename Graph, typename In, typename Out>
alpar@103
   216
    struct KruskalOutputSelector<Graph, In, Out,
alpar@103
   217
      typename enable_if<SequenceOutputIndicator<Out>, void>::type > 
alpar@103
   218
    {
alpar@103
   219
      typedef typename In::value_type::second_type Value;
alpar@103
   220
alpar@103
   221
      static Value kruskal(const Graph& graph, const In& in, Out& out) {
alpar@103
   222
        typedef StoreBoolMap<Out> Map;
alpar@103
   223
        Map map(out);
alpar@103
   224
        return _kruskal_bits::kruskal(graph, in, map);
alpar@103
   225
      }
alpar@103
   226
alpar@103
   227
    };
alpar@103
   228
alpar@103
   229
    template <typename Graph, typename In, typename Out>
alpar@103
   230
    struct KruskalOutputSelector<Graph, In, Out,
alpar@103
   231
      typename enable_if<MapOutputIndicator<Out>, void>::type > 
alpar@103
   232
    {
alpar@103
   233
      typedef typename In::value_type::second_type Value;
alpar@103
   234
alpar@103
   235
      static Value kruskal(const Graph& graph, const In& in, Out& out) {
alpar@103
   236
        return _kruskal_bits::kruskal(graph, in, out);
alpar@103
   237
      }
alpar@103
   238
    };
alpar@103
   239
alpar@103
   240
  }
alpar@103
   241
alpar@103
   242
  /// \ingroup spantree
alpar@103
   243
  ///
alpar@103
   244
  /// \brief Kruskal's algorithm to find a minimum cost tree of a graph.
alpar@103
   245
  ///
alpar@103
   246
  /// This function runs Kruskal's algorithm to find a minimum cost tree.
alpar@103
   247
  /// Due to some C++ hacking, it accepts various input and output types.
alpar@103
   248
  ///
alpar@103
   249
  /// \param g The graph the algorithm runs on.
alpar@103
   250
  /// It can be either \ref concepts::Digraph "directed" or 
alpar@103
   251
  /// \ref concepts::Graph "undirected".
alpar@103
   252
  /// If the graph is directed, the algorithm consider it to be 
alpar@103
   253
  /// undirected by disregarding the direction of the arcs.
alpar@103
   254
  ///
alpar@103
   255
  /// \param in This object is used to describe the arc costs. It can be one
alpar@103
   256
  /// of the following choices.
alpar@103
   257
  /// - An STL compatible 'Forward Container' with
alpar@103
   258
  /// <tt>std::pair<GR::Edge,X></tt> or
alpar@103
   259
  /// <tt>std::pair<GR::Arc,X></tt> as its <tt>value_type</tt>, where
alpar@103
   260
  /// \c X is the type of the costs. The pairs indicates the arcs
alpar@103
   261
  /// along with the assigned cost. <em>They must be in a
alpar@103
   262
  /// cost-ascending order.</em>
alpar@103
   263
  /// - Any readable Arc map. The values of the map indicate the arc costs.
alpar@103
   264
  ///
alpar@103
   265
  /// \retval out Here we also have a choise.
alpar@103
   266
  /// - It can be a writable \c bool arc map.  After running the
alpar@103
   267
  /// algorithm this will contain the found minimum cost spanning
alpar@103
   268
  /// tree: the value of an arc will be set to \c true if it belongs
alpar@103
   269
  /// to the tree, otherwise it will be set to \c false. The value of
alpar@103
   270
  /// each arc will be set exactly once.
alpar@103
   271
  /// - It can also be an iteraror of an STL Container with
alpar@103
   272
  /// <tt>GR::Edge</tt> or <tt>GR::Arc</tt> as its
alpar@103
   273
  /// <tt>value_type</tt>.  The algorithm copies the elements of the
alpar@103
   274
  /// found tree into this sequence.  For example, if we know that the
alpar@103
   275
  /// spanning tree of the graph \c g has say 53 arcs, then we can
alpar@103
   276
  /// put its arcs into an STL vector \c tree with a code like this.
alpar@103
   277
  ///\code
alpar@103
   278
  /// std::vector<Arc> tree(53);
alpar@103
   279
  /// kruskal(g,cost,tree.begin());
alpar@103
   280
  ///\endcode
alpar@103
   281
  /// Or if we don't know in advance the size of the tree, we can
alpar@103
   282
  /// write this.  
alpar@103
   283
  ///\code std::vector<Arc> tree;
alpar@103
   284
  /// kruskal(g,cost,std::back_inserter(tree)); 
alpar@103
   285
  ///\endcode
alpar@103
   286
  ///
alpar@103
   287
  /// \return The total cost of the found tree.
alpar@103
   288
  ///
alpar@103
   289
  /// \warning If kruskal runs on an be consistent of using the same
alpar@103
   290
  /// Arc type for input and output.
alpar@103
   291
  ///
alpar@103
   292
alpar@103
   293
#ifdef DOXYGEN
alpar@103
   294
  template <class Graph, class In, class Out>
alpar@103
   295
  Value kruskal(GR const& g, const In& in, Out& out)
alpar@103
   296
#else 
alpar@103
   297
  template <class Graph, class In, class Out>
alpar@103
   298
  inline typename _kruskal_bits::KruskalValueSelector<In>::Value 
alpar@103
   299
  kruskal(const Graph& graph, const In& in, Out& out) 
alpar@103
   300
#endif
alpar@103
   301
  {
alpar@103
   302
    return _kruskal_bits::KruskalInputSelector<Graph, In, Out>::
alpar@103
   303
      kruskal(graph, in, out);
alpar@103
   304
  }
alpar@103
   305
alpar@103
   306
 
alpar@103
   307
  
alpar@103
   308
alpar@103
   309
  template <class Graph, class In, class Out>
alpar@103
   310
  inline typename _kruskal_bits::KruskalValueSelector<In>::Value
alpar@103
   311
  kruskal(const Graph& graph, const In& in, const Out& out)
alpar@103
   312
  {
alpar@103
   313
    return _kruskal_bits::KruskalInputSelector<Graph, In, const Out>::
alpar@103
   314
      kruskal(graph, in, out);
alpar@103
   315
  }  
alpar@103
   316
alpar@103
   317
} //namespace lemon
alpar@103
   318
alpar@103
   319
#endif //LEMON_KRUSKAL_H