| [810] | 1 | // -*- c++ -*- // | 
|---|
|  | 2 | #ifndef HUGO_KRUSKAL_H | 
|---|
|  | 3 | #define HUGO_KRUSKAL_H | 
|---|
|  | 4 |  | 
|---|
|  | 5 | #include <algorithm> | 
|---|
|  | 6 | #include <hugo/unionfind.h> | 
|---|
|  | 7 |  | 
|---|
|  | 8 | /** | 
|---|
|  | 9 | @defgroup spantree Minimum Cost Spanning Tree Algorithms | 
|---|
|  | 10 | @ingroup galgs | 
|---|
|  | 11 | \brief This group containes the algorithms for finding a minimum cost spanning | 
|---|
|  | 12 | tree in a graph | 
|---|
|  | 13 |  | 
|---|
|  | 14 | This group containes the algorithms for finding a minimum cost spanning | 
|---|
|  | 15 | tree in a graph | 
|---|
|  | 16 | */ | 
|---|
|  | 17 |  | 
|---|
|  | 18 | ///\ingroup spantree | 
|---|
|  | 19 | ///\file | 
|---|
|  | 20 | ///\brief Kruskal's algorithm to compute a minimum cost tree | 
|---|
|  | 21 | /// | 
|---|
|  | 22 | ///Kruskal's algorithm to compute a minimum cost tree. | 
|---|
|  | 23 |  | 
|---|
|  | 24 | namespace hugo { | 
|---|
|  | 25 |  | 
|---|
|  | 26 | /// \addtogroup spantree | 
|---|
|  | 27 | /// @{ | 
|---|
|  | 28 |  | 
|---|
|  | 29 | /// Kruskal's algorithm to find a minimum cost tree of a graph. | 
|---|
|  | 30 |  | 
|---|
|  | 31 | /// This function runs Kruskal's algorithm to find a minimum cost tree. | 
|---|
|  | 32 | /// \param G The graph the algorithm runs on. The algorithm considers the | 
|---|
|  | 33 | /// graph to be undirected, the direction of the edges are not used. | 
|---|
|  | 34 | /// | 
|---|
|  | 35 | /// \param in This object is used to describe the edge costs. It must | 
|---|
|  | 36 | /// be an STL compatible 'Forward Container' | 
|---|
| [824] | 37 | /// with <tt>std::pair<GR::Edge,X></tt> as its <tt>value_type</tt>, | 
|---|
| [810] | 38 | /// where X is the type of the costs. It must contain every edge in | 
|---|
|  | 39 | /// cost-ascending order. | 
|---|
|  | 40 | ///\par | 
|---|
|  | 41 | /// For the sake of simplicity, there is a helper class KruskalMapInput, | 
|---|
|  | 42 | /// which converts a | 
|---|
|  | 43 | /// simple edge map to an input of this form. Alternatively, you can use | 
|---|
|  | 44 | /// the function \ref kruskalEdgeMap to compute the minimum cost tree if | 
|---|
|  | 45 | /// the edge costs are given by an edge map. | 
|---|
|  | 46 | /// | 
|---|
|  | 47 | /// \retval out This must be a writable \c bool edge map. | 
|---|
|  | 48 | /// After running the algorithm | 
|---|
|  | 49 | /// this will contain the found minimum cost spanning tree: the value of an | 
|---|
|  | 50 | /// edge will be set to \c true if it belongs to the tree, otherwise it will | 
|---|
|  | 51 | /// be set to \c false. The value of each edge will be set exactly once. | 
|---|
|  | 52 | /// | 
|---|
|  | 53 | /// \return The cost of the found tree. | 
|---|
|  | 54 |  | 
|---|
| [824] | 55 | template <class GR, class IN, class OUT> | 
|---|
|  | 56 | typename IN::value_type::second_type | 
|---|
|  | 57 | kruskal(GR const& G, IN const& in, | 
|---|
|  | 58 | OUT& out) | 
|---|
| [810] | 59 | { | 
|---|
| [824] | 60 | typedef typename IN::value_type::second_type EdgeCost; | 
|---|
|  | 61 | typedef typename GR::template NodeMap<int> NodeIntMap; | 
|---|
|  | 62 | typedef typename GR::Node Node; | 
|---|
| [810] | 63 |  | 
|---|
|  | 64 | NodeIntMap comp(G, -1); | 
|---|
|  | 65 | UnionFind<Node,NodeIntMap> uf(comp); | 
|---|
|  | 66 |  | 
|---|
|  | 67 | EdgeCost tot_cost = 0; | 
|---|
| [824] | 68 | for (typename IN::const_iterator p = in.begin(); | 
|---|
| [810] | 69 | p!=in.end(); ++p ) { | 
|---|
|  | 70 | if ( uf.join(G.head((*p).first), | 
|---|
|  | 71 | G.tail((*p).first)) ) { | 
|---|
|  | 72 | out.set((*p).first, true); | 
|---|
|  | 73 | tot_cost += (*p).second; | 
|---|
|  | 74 | } | 
|---|
|  | 75 | else { | 
|---|
|  | 76 | out.set((*p).first, false); | 
|---|
|  | 77 | } | 
|---|
|  | 78 | } | 
|---|
|  | 79 | return tot_cost; | 
|---|
|  | 80 | } | 
|---|
|  | 81 |  | 
|---|
|  | 82 | /* A work-around for running Kruskal with const-reference bool maps... */ | 
|---|
|  | 83 |  | 
|---|
| [885] | 84 | /// Helper class for calling kruskal with "constant" output map. | 
|---|
|  | 85 |  | 
|---|
|  | 86 | /// Helper class for calling kruskal with output maps constructed | 
|---|
|  | 87 | /// on-the-fly. | 
|---|
| [810] | 88 | /// | 
|---|
| [885] | 89 | /// A typical examle is the following call: | 
|---|
|  | 90 | /// <tt>kruskal(G, some_input, makeSequenceOutput(iterator))</tt>. | 
|---|
|  | 91 | /// Here, the third argument is a temporary object (which wraps around an | 
|---|
|  | 92 | /// iterator with a writable bool map interface), and thus by rules of C++ | 
|---|
|  | 93 | /// is a \c const object. To enable call like this exist this class and | 
|---|
|  | 94 | /// the prototype of the \ref kruskal() function with <tt>const& OUT</tt> | 
|---|
|  | 95 | /// third argument. | 
|---|
| [824] | 96 | template<class Map> | 
|---|
| [810] | 97 | class NonConstMapWr { | 
|---|
|  | 98 | const Map &m; | 
|---|
|  | 99 | public: | 
|---|
|  | 100 | typedef typename Map::ValueType ValueType; | 
|---|
|  | 101 |  | 
|---|
|  | 102 | NonConstMapWr(const Map &_m) : m(_m) {} | 
|---|
|  | 103 |  | 
|---|
| [824] | 104 | template<class KeyType> | 
|---|
| [810] | 105 | void set(KeyType const& k, ValueType const &v) const { m.set(k,v); } | 
|---|
|  | 106 | }; | 
|---|
|  | 107 |  | 
|---|
| [824] | 108 | template <class GR, class IN, class OUT> | 
|---|
| [810] | 109 | inline | 
|---|
| [885] | 110 | typename IN::value_type::second_type | 
|---|
|  | 111 | kruskal(GR const& G, IN const& edges, OUT const& out_map) | 
|---|
| [810] | 112 | { | 
|---|
| [824] | 113 | NonConstMapWr<OUT> map_wr(out_map); | 
|---|
| [810] | 114 | return kruskal(G, edges, map_wr); | 
|---|
|  | 115 | } | 
|---|
|  | 116 |  | 
|---|
|  | 117 | /* ** ** Input-objects ** ** */ | 
|---|
|  | 118 |  | 
|---|
|  | 119 | /// Kruskal input source. | 
|---|
|  | 120 |  | 
|---|
|  | 121 | /// Kruskal input source. | 
|---|
|  | 122 | /// | 
|---|
|  | 123 | /// In most cases you possibly want to use the \ref kruskalEdgeMap() instead. | 
|---|
|  | 124 | /// | 
|---|
|  | 125 | /// \sa makeKruskalMapInput() | 
|---|
|  | 126 | /// | 
|---|
| [824] | 127 | ///\param GR The type of the graph the algorithm runs on. | 
|---|
| [810] | 128 | ///\param Map An edge map containing the cost of the edges. | 
|---|
|  | 129 | ///\par | 
|---|
|  | 130 | ///The cost type can be any type satisfying | 
|---|
|  | 131 | ///the STL 'LessThan comparable' | 
|---|
|  | 132 | ///concept if it also has an operator+() implemented. (It is necessary for | 
|---|
|  | 133 | ///computing the total cost of the tree). | 
|---|
|  | 134 | /// | 
|---|
| [824] | 135 | template<class GR, class Map> | 
|---|
| [810] | 136 | class KruskalMapInput | 
|---|
| [824] | 137 | : public std::vector< std::pair<typename GR::Edge, | 
|---|
| [810] | 138 | typename Map::ValueType> > { | 
|---|
|  | 139 |  | 
|---|
|  | 140 | public: | 
|---|
| [824] | 141 | typedef std::vector< std::pair<typename GR::Edge, | 
|---|
| [810] | 142 | typename Map::ValueType> > Parent; | 
|---|
|  | 143 | typedef typename Parent::value_type value_type; | 
|---|
|  | 144 |  | 
|---|
|  | 145 | private: | 
|---|
|  | 146 | class comparePair { | 
|---|
|  | 147 | public: | 
|---|
|  | 148 | bool operator()(const value_type& a, | 
|---|
|  | 149 | const value_type& b) { | 
|---|
|  | 150 | return a.second < b.second; | 
|---|
|  | 151 | } | 
|---|
|  | 152 | }; | 
|---|
|  | 153 |  | 
|---|
|  | 154 | public: | 
|---|
|  | 155 |  | 
|---|
|  | 156 | void sort() { | 
|---|
|  | 157 | std::sort(this->begin(), this->end(), comparePair()); | 
|---|
|  | 158 | } | 
|---|
|  | 159 |  | 
|---|
| [824] | 160 | KruskalMapInput(GR const& G, Map const& m) { | 
|---|
|  | 161 | typedef typename GR::EdgeIt EdgeIt; | 
|---|
| [810] | 162 |  | 
|---|
| [885] | 163 | for(EdgeIt e(G);e!=INVALID;++e) push_back(value_type(e, m[e])); | 
|---|
| [810] | 164 | sort(); | 
|---|
|  | 165 | } | 
|---|
|  | 166 | }; | 
|---|
|  | 167 |  | 
|---|
|  | 168 | /// Creates a KruskalMapInput object for \ref kruskal() | 
|---|
|  | 169 |  | 
|---|
|  | 170 | /// It makes is easier to use | 
|---|
|  | 171 | /// \ref KruskalMapInput by making it unnecessary | 
|---|
|  | 172 | /// to explicitly give the type of the parameters. | 
|---|
|  | 173 | /// | 
|---|
|  | 174 | /// In most cases you possibly | 
|---|
|  | 175 | /// want to use the function kruskalEdgeMap() instead. | 
|---|
|  | 176 | /// | 
|---|
|  | 177 | ///\param G The type of the graph the algorithm runs on. | 
|---|
|  | 178 | ///\param m An edge map containing the cost of the edges. | 
|---|
|  | 179 | ///\par | 
|---|
|  | 180 | ///The cost type can be any type satisfying the | 
|---|
|  | 181 | ///STL 'LessThan Comparable' | 
|---|
|  | 182 | ///concept if it also has an operator+() implemented. (It is necessary for | 
|---|
|  | 183 | ///computing the total cost of the tree). | 
|---|
|  | 184 | /// | 
|---|
|  | 185 | ///\return An appropriate input source for \ref kruskal(). | 
|---|
|  | 186 | /// | 
|---|
| [824] | 187 | template<class GR, class Map> | 
|---|
| [810] | 188 | inline | 
|---|
| [824] | 189 | KruskalMapInput<GR,Map> makeKruskalMapInput(const GR &G,const Map &m) | 
|---|
| [810] | 190 | { | 
|---|
| [824] | 191 | return KruskalMapInput<GR,Map>(G,m); | 
|---|
| [810] | 192 | } | 
|---|
|  | 193 |  | 
|---|
|  | 194 |  | 
|---|
| [885] | 195 |  | 
|---|
|  | 196 | /* ** ** Output-objects: simple writable bool maps ** ** */ | 
|---|
| [810] | 197 |  | 
|---|
| [885] | 198 |  | 
|---|
|  | 199 |  | 
|---|
| [810] | 200 | /// A writable bool-map that makes a sequence of "true" keys | 
|---|
|  | 201 |  | 
|---|
|  | 202 | /// A writable bool-map that creates a sequence out of keys that receives | 
|---|
|  | 203 | /// the value "true". | 
|---|
| [885] | 204 | /// | 
|---|
|  | 205 | /// \sa makeKruskalSequenceOutput() | 
|---|
|  | 206 | /// | 
|---|
|  | 207 | /// Very often, when looking for a min cost spanning tree, we want as | 
|---|
|  | 208 | /// output a container containing the edges of the found tree. For this | 
|---|
|  | 209 | /// purpose exist this class that wraps around an STL iterator with a | 
|---|
|  | 210 | /// writable bool map interface. When a key gets value "true" this key | 
|---|
|  | 211 | /// is added to sequence pointed by the iterator. | 
|---|
|  | 212 | /// | 
|---|
|  | 213 | /// A typical usage: | 
|---|
|  | 214 | /// \code | 
|---|
|  | 215 | /// std::vector<Graph::Edge> v; | 
|---|
|  | 216 | /// kruskal(g, input, makeKruskalSequenceOutput(back_inserter(v))); | 
|---|
|  | 217 | /// \endcode | 
|---|
|  | 218 | /// | 
|---|
|  | 219 | /// For the most common case, when the input is given by a simple edge | 
|---|
|  | 220 | /// map and the output is a sequence of the tree edges, a special | 
|---|
|  | 221 | /// wrapper function exists: \ref kruskalEdgeMap_IteratorOut(). | 
|---|
|  | 222 | /// | 
|---|
| [810] | 223 | /// \warning Not a regular property map, as it doesn't know its KeyType | 
|---|
| [885] | 224 |  | 
|---|
| [824] | 225 | template<class Iterator> | 
|---|
| [885] | 226 | class KruskalSequenceOutput { | 
|---|
| [810] | 227 | mutable Iterator it; | 
|---|
|  | 228 |  | 
|---|
|  | 229 | public: | 
|---|
|  | 230 | typedef bool ValueType; | 
|---|
|  | 231 |  | 
|---|
| [885] | 232 | KruskalSequenceOutput(Iterator const &_it) : it(_it) {} | 
|---|
| [810] | 233 |  | 
|---|
|  | 234 | template<typename KeyType> | 
|---|
|  | 235 | void set(KeyType const& k, bool v) const { if(v) {*it=k; ++it;} } | 
|---|
|  | 236 | }; | 
|---|
|  | 237 |  | 
|---|
| [824] | 238 | template<class Iterator> | 
|---|
| [810] | 239 | inline | 
|---|
| [885] | 240 | KruskalSequenceOutput<Iterator> | 
|---|
|  | 241 | makeKruskalSequenceOutput(Iterator it) { | 
|---|
|  | 242 | return KruskalSequenceOutput<Iterator>(it); | 
|---|
| [810] | 243 | } | 
|---|
|  | 244 |  | 
|---|
| [885] | 245 |  | 
|---|
|  | 246 |  | 
|---|
| [810] | 247 | /* ** ** Wrapper funtions ** ** */ | 
|---|
|  | 248 |  | 
|---|
|  | 249 |  | 
|---|
| [885] | 250 |  | 
|---|
| [810] | 251 | /// \brief Wrapper function to kruskal(). | 
|---|
|  | 252 | /// Input is from an edge map, output is a plain bool map. | 
|---|
|  | 253 | /// | 
|---|
|  | 254 | /// Wrapper function to kruskal(). | 
|---|
|  | 255 | /// Input is from an edge map, output is a plain bool map. | 
|---|
|  | 256 | /// | 
|---|
|  | 257 | ///\param G The type of the graph the algorithm runs on. | 
|---|
|  | 258 | ///\param in An edge map containing the cost of the edges. | 
|---|
|  | 259 | ///\par | 
|---|
|  | 260 | ///The cost type can be any type satisfying the | 
|---|
|  | 261 | ///STL 'LessThan Comparable' | 
|---|
|  | 262 | ///concept if it also has an operator+() implemented. (It is necessary for | 
|---|
|  | 263 | ///computing the total cost of the tree). | 
|---|
|  | 264 | /// | 
|---|
|  | 265 | /// \retval out This must be a writable \c bool edge map. | 
|---|
|  | 266 | /// After running the algorithm | 
|---|
|  | 267 | /// this will contain the found minimum cost spanning tree: the value of an | 
|---|
|  | 268 | /// edge will be set to \c true if it belongs to the tree, otherwise it will | 
|---|
|  | 269 | /// be set to \c false. The value of each edge will be set exactly once. | 
|---|
|  | 270 | /// | 
|---|
|  | 271 | /// \return The cost of the found tree. | 
|---|
|  | 272 |  | 
|---|
| [824] | 273 | template <class GR, class IN, class RET> | 
|---|
| [810] | 274 | inline | 
|---|
| [824] | 275 | typename IN::ValueType | 
|---|
|  | 276 | kruskalEdgeMap(GR const& G, | 
|---|
|  | 277 | IN const& in, | 
|---|
|  | 278 | RET &out) { | 
|---|
| [810] | 279 | return kruskal(G, | 
|---|
| [824] | 280 | KruskalMapInput<GR,IN>(G,in), | 
|---|
| [810] | 281 | out); | 
|---|
|  | 282 | } | 
|---|
|  | 283 |  | 
|---|
|  | 284 | /// \brief Wrapper function to kruskal(). | 
|---|
|  | 285 | /// Input is from an edge map, output is an STL Sequence. | 
|---|
|  | 286 | /// | 
|---|
|  | 287 | /// Wrapper function to kruskal(). | 
|---|
|  | 288 | /// Input is from an edge map, output is an STL Sequence. | 
|---|
|  | 289 | /// | 
|---|
|  | 290 | ///\param G The type of the graph the algorithm runs on. | 
|---|
|  | 291 | ///\param in An edge map containing the cost of the edges. | 
|---|
|  | 292 | ///\par | 
|---|
|  | 293 | ///The cost type can be any type satisfying the | 
|---|
|  | 294 | ///STL 'LessThan Comparable' | 
|---|
|  | 295 | ///concept if it also has an operator+() implemented. (It is necessary for | 
|---|
|  | 296 | ///computing the total cost of the tree). | 
|---|
|  | 297 | /// | 
|---|
|  | 298 | /// \retval out This must be an iteraror of an STL Container with | 
|---|
| [824] | 299 | /// <tt>GR::Edge</tt> as its <tt>value_type</tt>. | 
|---|
| [810] | 300 | /// The algorithm copies the elements of the found tree into this sequence. | 
|---|
|  | 301 | /// For example, if we know that the spanning tree of the graph \c G has | 
|---|
|  | 302 | /// say 53 edges then | 
|---|
| [824] | 303 | /// we can put its edges into a STL vector \c tree with a code like this. | 
|---|
| [810] | 304 | /// \code | 
|---|
|  | 305 | /// std::vector<Edge> tree(53); | 
|---|
|  | 306 | /// kruskalEdgeMap_IteratorOut(G,cost,tree.begin()); | 
|---|
|  | 307 | /// \endcode | 
|---|
|  | 308 | /// Or if we don't know in advance the size of the tree, we can write this. | 
|---|
|  | 309 | /// \code | 
|---|
|  | 310 | /// std::vector<Edge> tree; | 
|---|
|  | 311 | /// kruskalEdgeMap_IteratorOut(G,cost,std::back_inserter(tree)); | 
|---|
|  | 312 | /// \endcode | 
|---|
|  | 313 | /// | 
|---|
|  | 314 | /// \return The cost of the found tree. | 
|---|
|  | 315 | /// | 
|---|
|  | 316 | /// \bug its name does not follow the coding style. | 
|---|
| [885] | 317 |  | 
|---|
| [824] | 318 | template <class GR, class IN, class RET> | 
|---|
| [810] | 319 | inline | 
|---|
| [824] | 320 | typename IN::ValueType | 
|---|
|  | 321 | kruskalEdgeMap_IteratorOut(const GR& G, | 
|---|
|  | 322 | const IN& in, | 
|---|
|  | 323 | RET out) | 
|---|
| [810] | 324 | { | 
|---|
| [885] | 325 | KruskalSequenceOutput<RET> _out(out); | 
|---|
|  | 326 | return kruskal(G, KruskalMapInput<GR,IN>(G, in), _out); | 
|---|
| [810] | 327 | } | 
|---|
|  | 328 |  | 
|---|
|  | 329 | /// @} | 
|---|
|  | 330 |  | 
|---|
|  | 331 | } //namespace hugo | 
|---|
|  | 332 |  | 
|---|
|  | 333 | #endif //HUGO_KRUSKAL_H | 
|---|