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