src/hugo/kruskal.h
author ladanyi
Thu, 16 Sep 2004 19:51:28 +0000
changeset 875 fda944f15ca7
parent 812 182d3a4d7ddb
child 881 a9f19f38970b
permissions -rw-r--r--
Changed to conform to the new iterator style.
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@824
    24
///\weakgroup spantree
alpar@810
    25
namespace hugo {
alpar@810
    26
alpar@810
    27
  /// \addtogroup spantree
alpar@810
    28
  /// @{
alpar@810
    29
alpar@810
    30
  /// Kruskal's algorithm to find a minimum cost tree of a graph.
alpar@810
    31
alpar@810
    32
  /// This function runs Kruskal's algorithm to find a minimum cost tree.
alpar@810
    33
  /// \param G The graph the algorithm runs on. The algorithm considers the
alpar@810
    34
  /// graph to be undirected, the direction of the edges are not used.
alpar@810
    35
  ///
alpar@810
    36
  /// \param in This object is used to describe the edge costs. It must
alpar@810
    37
  /// be an STL compatible 'Forward Container'
alpar@824
    38
  /// with <tt>std::pair<GR::Edge,X></tt> as its <tt>value_type</tt>,
alpar@810
    39
  /// where X is the type of the costs. It must contain every edge in
alpar@810
    40
  /// cost-ascending order.
alpar@810
    41
  ///\par
alpar@810
    42
  /// For the sake of simplicity, there is a helper class KruskalMapInput,
alpar@810
    43
  /// which converts a
alpar@810
    44
  /// simple edge map to an input of this form. Alternatively, you can use
alpar@810
    45
  /// the function \ref kruskalEdgeMap to compute the minimum cost tree if
alpar@810
    46
  /// the edge costs are given by an edge map.
alpar@810
    47
  ///
alpar@810
    48
  /// \retval out This must be a writable \c bool edge map.
alpar@810
    49
  /// After running the algorithm
alpar@810
    50
  /// this will contain the found minimum cost spanning tree: the value of an
alpar@810
    51
  /// edge will be set to \c true if it belongs to the tree, otherwise it will
alpar@810
    52
  /// be set to \c false. The value of each edge will be set exactly once.
alpar@810
    53
  ///
alpar@810
    54
  /// \return The cost of the found tree.
alpar@810
    55
alpar@824
    56
  template <class GR, class IN, class OUT>
alpar@824
    57
  typename IN::value_type::second_type
alpar@824
    58
  kruskal(GR const& G, IN const& in, 
alpar@824
    59
		 OUT& out)
alpar@810
    60
  {
alpar@824
    61
    typedef typename IN::value_type::second_type EdgeCost;
alpar@824
    62
    typedef typename GR::template NodeMap<int> NodeIntMap;
alpar@824
    63
    typedef typename GR::Node Node;
alpar@810
    64
alpar@810
    65
    NodeIntMap comp(G, -1);
alpar@810
    66
    UnionFind<Node,NodeIntMap> uf(comp); 
alpar@810
    67
      
alpar@810
    68
    EdgeCost tot_cost = 0;
alpar@824
    69
    for (typename IN::const_iterator p = in.begin(); 
alpar@810
    70
	 p!=in.end(); ++p ) {
alpar@810
    71
      if ( uf.join(G.head((*p).first),
alpar@810
    72
		   G.tail((*p).first)) ) {
alpar@810
    73
	out.set((*p).first, true);
alpar@810
    74
	tot_cost += (*p).second;
alpar@810
    75
      }
alpar@810
    76
      else {
alpar@810
    77
	out.set((*p).first, false);
alpar@810
    78
      }
alpar@810
    79
    }
alpar@810
    80
    return tot_cost;
alpar@810
    81
  }
alpar@810
    82
alpar@810
    83
  /* A work-around for running Kruskal with const-reference bool maps... */
alpar@810
    84
alpar@812
    85
  ///\bug What is this? Or why doesn't it work?
alpar@810
    86
  ///
alpar@824
    87
  template<class Map>
alpar@810
    88
  class NonConstMapWr {
alpar@810
    89
    const Map &m;
alpar@810
    90
  public:
alpar@810
    91
    typedef typename Map::ValueType ValueType;
alpar@810
    92
alpar@810
    93
    NonConstMapWr(const Map &_m) : m(_m) {}
alpar@810
    94
alpar@824
    95
    template<class KeyType>
alpar@810
    96
    void set(KeyType const& k, ValueType const &v) const { m.set(k,v); }
alpar@810
    97
  };
alpar@810
    98
alpar@824
    99
  template <class GR, class IN, class OUT>
alpar@810
   100
  inline
alpar@824
   101
  typename IN::ValueType
alpar@824
   102
  kruskal(GR const& G, IN const& edges, 
alpar@824
   103
	  OUT const& out_map)
alpar@810
   104
  {
alpar@824
   105
    NonConstMapWr<OUT> map_wr(out_map);
alpar@810
   106
    return kruskal(G, edges, map_wr);
alpar@810
   107
  }  
alpar@810
   108
alpar@810
   109
  /* ** ** Input-objects ** ** */
alpar@810
   110
alpar@810
   111
  /// Kruskal input source.
alpar@810
   112
alpar@810
   113
  /// Kruskal input source.
alpar@810
   114
  ///
alpar@810
   115
  /// In most cases you possibly want to use the \ref kruskalEdgeMap() instead.
alpar@810
   116
  ///
alpar@810
   117
  /// \sa makeKruskalMapInput()
alpar@810
   118
  ///
alpar@824
   119
  ///\param GR The type of the graph the algorithm runs on.
alpar@810
   120
  ///\param Map An edge map containing the cost of the edges.
alpar@810
   121
  ///\par
alpar@810
   122
  ///The cost type can be any type satisfying
alpar@810
   123
  ///the STL 'LessThan comparable'
alpar@810
   124
  ///concept if it also has an operator+() implemented. (It is necessary for
alpar@810
   125
  ///computing the total cost of the tree).
alpar@810
   126
  ///
alpar@824
   127
  template<class GR, class Map>
alpar@810
   128
  class KruskalMapInput
alpar@824
   129
    : public std::vector< std::pair<typename GR::Edge,
alpar@810
   130
				    typename Map::ValueType> > {
alpar@810
   131
    
alpar@810
   132
  public:
alpar@824
   133
    typedef std::vector< std::pair<typename GR::Edge,
alpar@810
   134
				   typename Map::ValueType> > Parent;
alpar@810
   135
    typedef typename Parent::value_type value_type;
alpar@810
   136
alpar@810
   137
  private:
alpar@810
   138
    class comparePair {
alpar@810
   139
    public:
alpar@810
   140
      bool operator()(const value_type& a,
alpar@810
   141
		      const value_type& b) {
alpar@810
   142
	return a.second < b.second;
alpar@810
   143
      }
alpar@810
   144
    };
alpar@810
   145
alpar@810
   146
  public:
alpar@810
   147
alpar@810
   148
    void sort() {
alpar@810
   149
      std::sort(this->begin(), this->end(), comparePair());
alpar@810
   150
    }
alpar@810
   151
alpar@824
   152
    KruskalMapInput(GR const& G, Map const& m) {
alpar@824
   153
      typedef typename GR::EdgeIt EdgeIt;
alpar@810
   154
      
alpar@810
   155
      this->clear();
alpar@810
   156
      for(EdgeIt e(G);e!=INVALID;++e) push_back(make_pair(e, m[e]));
alpar@810
   157
      sort();
alpar@810
   158
    }
alpar@810
   159
  };
alpar@810
   160
alpar@810
   161
  /// Creates a KruskalMapInput object for \ref kruskal()
alpar@810
   162
alpar@810
   163
  /// It makes is easier to use 
alpar@810
   164
  /// \ref KruskalMapInput by making it unnecessary 
alpar@810
   165
  /// to explicitly give the type of the parameters.
alpar@810
   166
  ///
alpar@810
   167
  /// In most cases you possibly
alpar@810
   168
  /// want to use the function kruskalEdgeMap() instead.
alpar@810
   169
  ///
alpar@810
   170
  ///\param G The type of the graph the algorithm runs on.
alpar@810
   171
  ///\param m An edge map containing the cost of the edges.
alpar@810
   172
  ///\par
alpar@810
   173
  ///The cost type can be any type satisfying the
alpar@810
   174
  ///STL 'LessThan Comparable'
alpar@810
   175
  ///concept if it also has an operator+() implemented. (It is necessary for
alpar@810
   176
  ///computing the total cost of the tree).
alpar@810
   177
  ///
alpar@810
   178
  ///\return An appropriate input source for \ref kruskal().
alpar@810
   179
  ///
alpar@824
   180
  template<class GR, class Map>
alpar@810
   181
  inline
alpar@824
   182
  KruskalMapInput<GR,Map> makeKruskalMapInput(const GR &G,const Map &m)
alpar@810
   183
  {
alpar@824
   184
    return KruskalMapInput<GR,Map>(G,m);
alpar@810
   185
  }
alpar@810
   186
  
alpar@810
   187
  
alpar@810
   188
  /* ** ** Output-objects: simple writable bool maps** ** */
alpar@810
   189
  
alpar@810
   190
  /// A writable bool-map that makes a sequence of "true" keys
alpar@810
   191
alpar@810
   192
  /// A writable bool-map that creates a sequence out of keys that receives
alpar@810
   193
  /// the value "true".
alpar@810
   194
  /// \warning Not a regular property map, as it doesn't know its KeyType
alpar@810
   195
  /// \bug Missing documentation.
alpar@810
   196
  /// \todo This class may be of wider usage, therefore it could move to
alpar@810
   197
  /// <tt>maps.h</tt>
alpar@824
   198
  template<class Iterator>
alpar@810
   199
  class SequenceOutput {
alpar@810
   200
    mutable Iterator it;
alpar@810
   201
alpar@810
   202
  public:
alpar@810
   203
    typedef bool ValueType;
alpar@810
   204
alpar@810
   205
    SequenceOutput(Iterator const &_it) : it(_it) {}
alpar@810
   206
alpar@810
   207
    template<typename KeyType>
alpar@810
   208
    void set(KeyType const& k, bool v) const { if(v) {*it=k; ++it;} }
alpar@810
   209
  };
alpar@810
   210
alpar@824
   211
  template<class Iterator>
alpar@810
   212
  inline
alpar@810
   213
  SequenceOutput<Iterator>
alpar@810
   214
  makeSequenceOutput(Iterator it) {
alpar@810
   215
    return SequenceOutput<Iterator>(it);
alpar@810
   216
  }
alpar@810
   217
alpar@810
   218
  /* ** ** Wrapper funtions ** ** */
alpar@810
   219
alpar@810
   220
alpar@810
   221
  /// \brief Wrapper function to kruskal().
alpar@810
   222
  /// Input is from an edge map, output is a plain bool map.
alpar@810
   223
  ///
alpar@810
   224
  /// Wrapper function to kruskal().
alpar@810
   225
  /// Input is from an edge map, output is a plain bool map.
alpar@810
   226
  ///
alpar@810
   227
  ///\param G The type of the graph the algorithm runs on.
alpar@810
   228
  ///\param in An edge map containing the cost of the edges.
alpar@810
   229
  ///\par
alpar@810
   230
  ///The cost type can be any type satisfying the
alpar@810
   231
  ///STL 'LessThan Comparable'
alpar@810
   232
  ///concept if it also has an operator+() implemented. (It is necessary for
alpar@810
   233
  ///computing the total cost of the tree).
alpar@810
   234
  ///
alpar@810
   235
  /// \retval out This must be a writable \c bool edge map.
alpar@810
   236
  /// After running the algorithm
alpar@810
   237
  /// this will contain the found minimum cost spanning tree: the value of an
alpar@810
   238
  /// edge will be set to \c true if it belongs to the tree, otherwise it will
alpar@810
   239
  /// be set to \c false. The value of each edge will be set exactly once.
alpar@810
   240
  ///
alpar@810
   241
  /// \return The cost of the found tree.
alpar@810
   242
alpar@810
   243
alpar@824
   244
  template <class GR, class IN, class RET>
alpar@810
   245
  inline
alpar@824
   246
  typename IN::ValueType
alpar@824
   247
  kruskalEdgeMap(GR const& G,
alpar@824
   248
		 IN const& in,
alpar@824
   249
		 RET &out) {
alpar@810
   250
    return kruskal(G,
alpar@824
   251
		   KruskalMapInput<GR,IN>(G,in),
alpar@810
   252
		   out);
alpar@810
   253
  }
alpar@810
   254
alpar@810
   255
  /// \brief Wrapper function to kruskal().
alpar@810
   256
  /// Input is from an edge map, output is an STL Sequence.
alpar@810
   257
  ///
alpar@810
   258
  /// Wrapper function to kruskal().
alpar@810
   259
  /// Input is from an edge map, output is an STL Sequence.
alpar@810
   260
  ///
alpar@810
   261
  ///\param G The type of the graph the algorithm runs on.
alpar@810
   262
  ///\param in An edge map containing the cost of the edges.
alpar@810
   263
  ///\par
alpar@810
   264
  ///The cost type can be any type satisfying the
alpar@810
   265
  ///STL 'LessThan Comparable'
alpar@810
   266
  ///concept if it also has an operator+() implemented. (It is necessary for
alpar@810
   267
  ///computing the total cost of the tree).
alpar@810
   268
  ///
alpar@810
   269
  /// \retval out This must be an iteraror of an STL Container with
alpar@824
   270
  /// <tt>GR::Edge</tt> as its <tt>value_type</tt>.
alpar@810
   271
  /// The algorithm copies the elements of the found tree into this sequence.
alpar@810
   272
  /// For example, if we know that the spanning tree of the graph \c G has
alpar@810
   273
  /// say 53 edges then
alpar@824
   274
  /// we can put its edges into a STL vector \c tree with a code like this.
alpar@810
   275
  /// \code
alpar@810
   276
  /// std::vector<Edge> tree(53);
alpar@810
   277
  /// kruskalEdgeMap_IteratorOut(G,cost,tree.begin());
alpar@810
   278
  /// \endcode
alpar@810
   279
  /// Or if we don't know in advance the size of the tree, we can write this.
alpar@810
   280
  /// \code
alpar@810
   281
  /// std::vector<Edge> tree;
alpar@810
   282
  /// kruskalEdgeMap_IteratorOut(G,cost,std::back_inserter(tree));
alpar@810
   283
  /// \endcode
alpar@810
   284
  ///
alpar@810
   285
  /// \return The cost of the found tree.
alpar@810
   286
  ///
alpar@810
   287
  /// \bug its name does not follow the coding style.
alpar@824
   288
  template <class GR, class IN, class RET>
alpar@810
   289
  inline
alpar@824
   290
  typename IN::ValueType
alpar@824
   291
  kruskalEdgeMap_IteratorOut(const GR& G,
alpar@824
   292
			     const IN& in,
alpar@824
   293
			     RET out)
alpar@810
   294
  {
alpar@824
   295
    SequenceOutput<RET> _out(out);
alpar@810
   296
    return kruskal(G,
alpar@824
   297
		   KruskalMapInput<GR,IN>(G, in),
alpar@810
   298
		   _out);
alpar@810
   299
  }
alpar@810
   300
alpar@810
   301
  /// @}
alpar@810
   302
alpar@810
   303
} //namespace hugo
alpar@810
   304
alpar@810
   305
#endif //HUGO_KRUSKAL_H