src/work/johanna/kruskal.h
author jacint
Fri, 30 Jul 2004 10:24:05 +0000
changeset 749 8e933219691e
parent 682 1ea8162ce638
child 755 a8c2e828ce0b
permissions -rw-r--r--
bug fixing
beckerjc@150
     1
// -*- c++ -*- //
beckerjc@150
     2
#ifndef HUGO_KRUSKAL_H
beckerjc@150
     3
#define HUGO_KRUSKAL_H
beckerjc@150
     4
beckerjc@150
     5
#include <algorithm>
alpar@737
     6
#include <hugo/unionfind.h>
beckerjc@150
     7
alpar@682
     8
///\file
alpar@682
     9
///\brief Kruskal's algorithm to compute a minimum cost tree
alpar@682
    10
beckerjc@150
    11
namespace hugo {
beckerjc@150
    12
alpar@737
    13
  /// Kruskal's algorithm to find a minimum cost tree of a graph.
alpar@737
    14
alpar@737
    15
  /// This function runs Kruskal's algorithm to find a minimum cost tree.
alpar@737
    16
  /// \param G The graph the algorithm runs on.
alpar@737
    17
  /// \param in This object is used to describe the edge costs. It must
alpar@737
    18
  /// be an STL 'forward container'
alpar@737
    19
  /// with value_type <tt> std::pair<Graph::Edge,X> </tt>,
alpar@737
    20
  /// where X is the type of the costs. It must contain every edge in
alpar@737
    21
  /// cost-ascending order.
alpar@737
    22
  /// \retval out This must be a writeable EdgeMap. After running the algorithm
alpar@737
    23
  /// this will contain the found minimum cost spanning tree: the value of an
alpar@737
    24
  /// edge will be set to \c true if it belongs to the tree, otherwise it will
alpar@737
    25
  /// be set to \c false. The value of each edge will be set exactly once.\n
alpar@737
    26
  /// For the sake of simplicity, there is a helper class KruskalPairVec,
alpar@737
    27
  /// which converts a
alpar@737
    28
  /// simple EdgeMap to an input of this form. Alternatively, you can use
alpar@737
    29
  /// the function \ref kruskalEdgeMap to compute the minimum cost tree if
alpar@737
    30
  /// the edge costs are given by an EdgeMap.
alpar@737
    31
  /// \return The cost of the found tree.
beckerjc@150
    32
beckerjc@349
    33
  template <typename Graph, typename InputEdgeOrder, typename OutBoolMap>
alpar@737
    34
  typename InputEdgeOrder::value_type::second_type
alpar@737
    35
  kruskal(Graph const& G, InputEdgeOrder const& in, 
alpar@737
    36
		 OutBoolMap& out)
beckerjc@349
    37
  {
alpar@737
    38
    typedef typename InputEdgeOrder::value_type::second_type EdgeCost;
alpar@737
    39
    typedef typename Graph::template NodeMap<int> NodeIntMap;
beckerjc@349
    40
    typedef typename Graph::Node Node;
beckerjc@246
    41
alpar@737
    42
    NodeIntMap comp(G, -1);
alpar@737
    43
    UnionFind<Node,NodeIntMap> uf(comp); 
beckerjc@349
    44
      
beckerjc@349
    45
    EdgeCost tot_cost = 0;
alpar@737
    46
    for (typename InputEdgeOrder::const_iterator p = in.begin(); 
alpar@737
    47
	 p!=in.end(); ++p ) {
alpar@737
    48
      if ( uf.join(G.head((*p).first),
alpar@737
    49
		   G.tail((*p).first)) ) {
alpar@737
    50
	out.set((*p).first, true);
alpar@737
    51
	tot_cost += (*p).second;
beckerjc@349
    52
      }
beckerjc@349
    53
      else {
alpar@737
    54
	out.set((*p).first, false);
beckerjc@349
    55
      }
beckerjc@349
    56
    }
beckerjc@349
    57
    return tot_cost;
beckerjc@349
    58
  }
beckerjc@246
    59
beckerjc@352
    60
  /* A work-around for running Kruskal with const-reference bool maps... */
beckerjc@352
    61
beckerjc@352
    62
  template<typename Map>
beckerjc@352
    63
  class NonConstMapWr {
beckerjc@352
    64
    const Map &m;
beckerjc@352
    65
  public:
beckerjc@352
    66
    typedef typename Map::ValueType ValueType;
beckerjc@352
    67
beckerjc@352
    68
    NonConstMapWr(const Map &_m) : m(_m) {}
beckerjc@352
    69
beckerjc@352
    70
    template<typename KeyType>
beckerjc@352
    71
    void set(KeyType const& k, ValueType const &v) const { m.set(k,v); }
beckerjc@352
    72
  };
beckerjc@352
    73
beckerjc@352
    74
  template <typename Graph, typename InputEdgeOrder, typename OutBoolMap>
beckerjc@352
    75
  inline
beckerjc@352
    76
  typename InputEdgeOrder::ValueType
alpar@737
    77
  kruskal(Graph const& G, InputEdgeOrder const& edges, 
beckerjc@352
    78
	  OutBoolMap const& out_map)
beckerjc@352
    79
  {
beckerjc@352
    80
    NonConstMapWr<OutBoolMap> map_wr(out_map);
alpar@737
    81
    return kruskal(G, edges, map_wr);
beckerjc@352
    82
  }  
beckerjc@246
    83
beckerjc@349
    84
  
beckerjc@349
    85
  /* ** ** Output-objektumok: egyszeruen extra bool mapek ** ** */
beckerjc@349
    86
  
beckerjc@349
    87
  /// A writable bool-map that makes a sequence of "true" keys
beckerjc@246
    88
alpar@737
    89
  /// A writable bool-map that creates a sequence out of keys that receives
beckerjc@349
    90
  /// the value "true".
beckerjc@349
    91
  /// \warning Not a regular property map, as it doesn't know its KeyType
beckerjc@246
    92
beckerjc@349
    93
  template<typename Iterator>
beckerjc@349
    94
  class SequenceOutput {
beckerjc@352
    95
    mutable Iterator it;
beckerjc@150
    96
beckerjc@218
    97
  public:
beckerjc@349
    98
    typedef bool ValueType;
beckerjc@150
    99
beckerjc@349
   100
    SequenceOutput(Iterator const &_it) : it(_it) {}
beckerjc@150
   101
beckerjc@349
   102
    template<typename KeyType>
beckerjc@352
   103
    void set(KeyType const& k, bool v) const { if(v) {*it=k; ++it;} }
beckerjc@218
   104
  };
beckerjc@150
   105
beckerjc@349
   106
  template<typename Iterator>
beckerjc@349
   107
  inline
beckerjc@349
   108
  SequenceOutput<Iterator>
beckerjc@349
   109
  makeSequenceOutput(Iterator it) {
beckerjc@349
   110
    return SequenceOutput<Iterator>(it);
beckerjc@349
   111
  }
beckerjc@246
   112
beckerjc@246
   113
  /* ** ** InputSource -ok ** ** */
beckerjc@246
   114
alpar@737
   115
  /// Kruskal input source.
beckerjc@246
   116
alpar@737
   117
  /// Kruskal input source.
alpar@737
   118
  ///
alpar@737
   119
  template<typename Graph, typename Map>
alpar@737
   120
  class KruskalPairVec
alpar@737
   121
    : public std::vector< std::pair<typename Graph::Edge,
alpar@737
   122
				    typename Map::ValueType> > {
alpar@737
   123
    
beckerjc@246
   124
  public:
alpar@737
   125
    typedef std::vector< std::pair<typename Graph::Edge,
alpar@737
   126
				   typename Map::ValueType> > Parent;
alpar@737
   127
    typedef typename Parent::value_type value_type;
alpar@737
   128
//     typedef Key KeyType;
alpar@737
   129
//     typedef Val ValueType;
alpar@737
   130
//     typedef std::pair<Key,Val> PairType;
alpar@737
   131
//     typedef typename Parent::iterator iterator;
alpar@737
   132
//     typedef typename Parent::const_iterator const_iterator;
beckerjc@246
   133
beckerjc@246
   134
  private:
beckerjc@246
   135
    class comparePair {
beckerjc@246
   136
    public:
alpar@737
   137
      bool operator()(const value_type& a,
alpar@737
   138
		      const value_type& b) {
beckerjc@246
   139
	return a.second < b.second;
beckerjc@246
   140
      }
beckerjc@246
   141
    };
beckerjc@246
   142
beckerjc@246
   143
  public:
beckerjc@246
   144
beckerjc@246
   145
    // FIXME: kell ez?
beckerjc@349
   146
    // KruskalPairVec(Parent const& p) : Parent(p) {}
beckerjc@246
   147
    
beckerjc@246
   148
    void sort() {
alpar@737
   149
      std::sort(this->begin(), this->end(), comparePair());
beckerjc@246
   150
    }
beckerjc@246
   151
beckerjc@246
   152
    // FIXME: nem nagyon illik ez ide...
beckerjc@349
   153
    KruskalPairVec(Graph const& G, Map const& m) {
beckerjc@246
   154
      typedef typename Graph::EdgeIt EdgeIt;
alpar@737
   155
      
alpar@737
   156
      this->clear();
alpar@737
   157
      for(EdgeIt e(G);G.valid(e);G.next(e)) {
alpar@737
   158
	// for (EdgeIt e=G.template first<EdgeIt>(); G.valid(e); G.next(e)) {
beckerjc@246
   159
	push_back(make_pair(e, m[e]));
beckerjc@246
   160
      }
beckerjc@246
   161
      sort();
beckerjc@246
   162
    }
beckerjc@246
   163
alpar@737
   164
//     Key const& first(const_iterator i) const { return i->first; }
alpar@737
   165
//     Key& first(iterator i) { return i->first; }
beckerjc@246
   166
alpar@737
   167
//     Val const& second(const_iterator i) const { return i->second; }
alpar@737
   168
//     Val& second(iterator i) { return i->second; }
beckerjc@246
   169
  };
beckerjc@246
   170
beckerjc@349
   171
alpar@737
   172
//   template <typename Map>
alpar@737
   173
//   class KruskalMapVec : public std::vector<typename Map::KeyType> {
alpar@737
   174
//   public:
alpar@737
   175
    
alpar@737
   176
//     typedef typename Map::KeyType KeyType;
alpar@737
   177
//     typedef typename Map::ValueType ValueType;
beckerjc@349
   178
alpar@737
   179
//     typedef typename std::vector<KeyType> Parent;
alpar@737
   180
//     typedef typename Parent::iterator iterator;
alpar@737
   181
//     typedef typename Parent::const_iterator const_iterator;
beckerjc@349
   182
alpar@737
   183
//   private:
beckerjc@349
   184
alpar@737
   185
//     const Map &m;
beckerjc@349
   186
alpar@737
   187
//     class compareKeys {
alpar@737
   188
//       const Map &m;
alpar@737
   189
//     public:
alpar@737
   190
//       compareKeys(Map const &_m) : m(_m) {}
alpar@737
   191
//       bool operator()(KeyType const& a, KeyType const& b) {
alpar@737
   192
// 	return m[a] < m[b];
alpar@737
   193
//       }
alpar@737
   194
//     };
beckerjc@349
   195
alpar@737
   196
//   public:
beckerjc@349
   197
alpar@737
   198
//     KruskalMapVec(Map const& _m) : m(_m) {}
beckerjc@349
   199
alpar@737
   200
//     void sort() {
alpar@737
   201
//       std::sort(begin(), end(), compareKeys(m));
alpar@737
   202
//     }
beckerjc@349
   203
alpar@737
   204
//     // FIXME: nem nagyon illik ez ide...
alpar@737
   205
//     template<typename Graph>
alpar@737
   206
//     KruskalMapVec(Graph const& G, Map const& _m) : m(_m) {
alpar@737
   207
//       typedef typename Graph::EdgeIt EdgeIt;
beckerjc@349
   208
alpar@737
   209
//       clear();
alpar@737
   210
//       for(EdgeIt e(G);G.valid(e);G.next(e)) {
alpar@737
   211
//       // for (EdgeIt e=G.template first<EdgeIt>(); G.valid(e); G.next(e)) 
alpar@737
   212
// 	push_back(e);
alpar@737
   213
//       }
alpar@737
   214
//       sort();
alpar@737
   215
//     }
beckerjc@349
   216
alpar@737
   217
//     KeyType const& first(const_iterator i) const { return *i; }
alpar@737
   218
//     KeyType& first(iterator i) { return *i; }
beckerjc@349
   219
alpar@737
   220
//     ValueType const& second(const_iterator i) const { return m[*i]; }
alpar@737
   221
//     ValueType& second(iterator i) { return m[*i]; }
alpar@737
   222
//   };
beckerjc@349
   223
beckerjc@246
   224
  /* ** ** Wrapper fuggvenyek ** ** */
beckerjc@246
   225
beckerjc@349
   226
beckerjc@349
   227
  /// \brief Wrapper to Kruskal().
beckerjc@349
   228
  /// Input is from an EdgeMap, output is a plain boolmap.
alpar@737
   229
alpar@737
   230
  ///\todo some more words would be nice here.
alpar@737
   231
  ///
beckerjc@246
   232
  template <typename Graph, typename EdgeCostMap, typename RetEdgeBoolMap>
beckerjc@352
   233
  inline
beckerjc@246
   234
  typename EdgeCostMap::ValueType
alpar@737
   235
  kruskalEdgeMap(Graph const& G,
alpar@737
   236
			EdgeCostMap const& edge_costs,
alpar@737
   237
			RetEdgeBoolMap &ret_bool_map) {
alpar@737
   238
    
alpar@737
   239
    typedef KruskalPairVec<Graph,EdgeCostMap> InputVec;
alpar@737
   240
    
beckerjc@349
   241
    InputVec iv(G, edge_costs);
alpar@737
   242
    return kruskal(G, iv, ret_bool_map);
beckerjc@246
   243
  }
beckerjc@246
   244
beckerjc@349
   245
beckerjc@349
   246
  /// \brief Wrapper to Kruskal().
beckerjc@349
   247
  /// Input is from an EdgeMap, output is to a sequence.
alpar@737
   248
alpar@737
   249
  ///\todo it does not follow the naming convention.
alpar@737
   250
  ///
beckerjc@246
   251
  template <typename Graph, typename EdgeCostMap, typename RetIterator>
beckerjc@352
   252
  inline
beckerjc@246
   253
  typename EdgeCostMap::ValueType
alpar@737
   254
  kruskalEdgeMap_IteratorOut(const Graph& G,
alpar@737
   255
			     const EdgeCostMap& edge_costs,
alpar@737
   256
			     RetIterator ret_iterator)
alpar@737
   257
  {
alpar@737
   258
    typedef typename EdgeCostMap::ValueType ValueType;
alpar@737
   259
    
beckerjc@349
   260
    typedef SequenceOutput<RetIterator> OutMap;
beckerjc@349
   261
    OutMap out(ret_iterator);
beckerjc@246
   262
alpar@737
   263
    typedef KruskalPairVec<Graph, EdgeCostMap> InputVec;
alpar@737
   264
beckerjc@349
   265
    InputVec iv(G, edge_costs);
beckerjc@246
   266
alpar@737
   267
    return kruskal(G, iv, out);
beckerjc@246
   268
  }
beckerjc@246
   269
beckerjc@150
   270
beckerjc@150
   271
} //namespace hugo
beckerjc@150
   272
beckerjc@150
   273
#endif //HUGO_KRUSKAL_H