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