lemon/kruskal.h
changeset 126 e1dd2a70737c
child 136 b82dc494bafc
equal deleted inserted replaced
-1:000000000000 0:bbfeab2f727c
       
     1 /* -*- C++ -*-
       
     2  *
       
     3  * This file is a part of LEMON, a generic C++ optimization library
       
     4  *
       
     5  * Copyright (C) 2003-2008
       
     6  * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
       
     7  * (Egervary Research Group on Combinatorial Optimization, EGRES).
       
     8  *
       
     9  * Permission to use, modify and distribute this software is granted
       
    10  * provided that this copyright notice appears in all copies. For
       
    11  * precise terms see the accompanying LICENSE file.
       
    12  *
       
    13  * This software is provided "AS IS" with no warranty of any kind,
       
    14  * express or implied, and with no claim as to its suitability for any
       
    15  * purpose.
       
    16  *
       
    17  */
       
    18 
       
    19 #ifndef LEMON_KRUSKAL_H
       
    20 #define LEMON_KRUSKAL_H
       
    21 
       
    22 #include <algorithm>
       
    23 #include <vector>
       
    24 #include <lemon/unionfind.h>
       
    25 // #include <lemon/graph_utils.h>
       
    26 #include <lemon/maps.h>
       
    27 
       
    28 // #include <lemon/radix_sort.h>
       
    29 
       
    30 #include <lemon/bits/utility.h>
       
    31 #include <lemon/bits/traits.h>
       
    32 
       
    33 ///\ingroup spantree
       
    34 ///\file
       
    35 ///\brief Kruskal's algorithm to compute a minimum cost tree
       
    36 ///
       
    37 ///Kruskal's algorithm to compute a minimum cost tree.
       
    38 ///
       
    39 
       
    40 namespace lemon {
       
    41 
       
    42   namespace _kruskal_bits {
       
    43 
       
    44     // Kruskal for directed graphs.
       
    45 
       
    46     template <typename Digraph, typename In, typename Out>
       
    47     typename disable_if<lemon::UndirectedTagIndicator<Digraph>,
       
    48 		       typename In::value_type::second_type >::type
       
    49     kruskal(const Digraph& digraph, const In& in, Out& out,dummy<0> = 0) {
       
    50       typedef typename In::value_type::second_type Value;
       
    51       typedef typename Digraph::template NodeMap<int> IndexMap;
       
    52       typedef typename Digraph::Node Node;
       
    53       
       
    54       IndexMap index(digraph);
       
    55       UnionFind<IndexMap> uf(index);
       
    56       for (typename Digraph::NodeIt it(digraph); it != INVALID; ++it) {
       
    57         uf.insert(it);
       
    58       }
       
    59       
       
    60       Value tree_value = 0;
       
    61       for (typename In::const_iterator it = in.begin(); it != in.end(); ++it) {
       
    62         if (uf.join(digraph.target(it->first),digraph.source(it->first))) {
       
    63           out.set(it->first, true);
       
    64           tree_value += it->second;
       
    65         }
       
    66         else {
       
    67           out.set(it->first, false);
       
    68         }
       
    69       }
       
    70       return tree_value;
       
    71     }
       
    72 
       
    73     // Kruskal for undirected graphs.
       
    74 
       
    75     template <typename Graph, typename In, typename Out>
       
    76     typename enable_if<lemon::UndirectedTagIndicator<Graph>,
       
    77 		       typename In::value_type::second_type >::type
       
    78     kruskal(const Graph& graph, const In& in, Out& out,dummy<1> = 1) {
       
    79       typedef typename In::value_type::second_type Value;
       
    80       typedef typename Graph::template NodeMap<int> IndexMap;
       
    81       typedef typename Graph::Node Node;
       
    82       
       
    83       IndexMap index(graph);
       
    84       UnionFind<IndexMap> uf(index);
       
    85       for (typename Graph::NodeIt it(graph); it != INVALID; ++it) {
       
    86         uf.insert(it);
       
    87       }
       
    88       
       
    89       Value tree_value = 0;
       
    90       for (typename In::const_iterator it = in.begin(); it != in.end(); ++it) {
       
    91         if (uf.join(graph.u(it->first),graph.v(it->first))) {
       
    92           out.set(it->first, true);
       
    93           tree_value += it->second;
       
    94         }
       
    95         else {
       
    96           out.set(it->first, false);
       
    97         }
       
    98       }
       
    99       return tree_value;
       
   100     }
       
   101 
       
   102 
       
   103     template <typename Sequence>
       
   104     struct PairComp {
       
   105       typedef typename Sequence::value_type Value;
       
   106       bool operator()(const Value& left, const Value& right) {
       
   107 	return left.second < right.second;
       
   108       }
       
   109     };
       
   110 
       
   111     template <typename In, typename Enable = void>
       
   112     struct SequenceInputIndicator {
       
   113       static const bool value = false;
       
   114     };
       
   115 
       
   116     template <typename In>
       
   117     struct SequenceInputIndicator<In, 
       
   118       typename exists<typename In::value_type::first_type>::type> {
       
   119       static const bool value = true;
       
   120     };
       
   121 
       
   122     template <typename In, typename Enable = void>
       
   123     struct MapInputIndicator {
       
   124       static const bool value = false;
       
   125     };
       
   126 
       
   127     template <typename In>
       
   128     struct MapInputIndicator<In, 
       
   129       typename exists<typename In::Value>::type> {
       
   130       static const bool value = true;
       
   131     };
       
   132 
       
   133     template <typename In, typename Enable = void>
       
   134     struct SequenceOutputIndicator {
       
   135       static const bool value = false;
       
   136     };
       
   137  
       
   138     template <typename Out>
       
   139     struct SequenceOutputIndicator<Out, 
       
   140       typename exists<typename Out::value_type>::type> {
       
   141       static const bool value = true;
       
   142     };
       
   143 
       
   144     template <typename Out, typename Enable = void>
       
   145     struct MapOutputIndicator {
       
   146       static const bool value = false;
       
   147     };
       
   148 
       
   149     template <typename Out>
       
   150     struct MapOutputIndicator<Out, 
       
   151       typename exists<typename Out::Value>::type> {
       
   152       static const bool value = true;
       
   153     };
       
   154 
       
   155     template <typename In, typename InEnable = void>
       
   156     struct KruskalValueSelector {};
       
   157 
       
   158     template <typename In>
       
   159     struct KruskalValueSelector<In,
       
   160       typename enable_if<SequenceInputIndicator<In>, void>::type> 
       
   161     {
       
   162       typedef typename In::value_type::second_type Value;
       
   163     };    
       
   164 
       
   165     template <typename In>
       
   166     struct KruskalValueSelector<In,
       
   167       typename enable_if<MapInputIndicator<In>, void>::type> 
       
   168     {
       
   169       typedef typename In::Value Value;
       
   170     };    
       
   171     
       
   172     template <typename Graph, typename In, typename Out,
       
   173               typename InEnable = void>
       
   174     struct KruskalInputSelector {};
       
   175 
       
   176     template <typename Graph, typename In, typename Out,
       
   177               typename InEnable = void>
       
   178     struct KruskalOutputSelector {};
       
   179     
       
   180     template <typename Graph, typename In, typename Out>
       
   181     struct KruskalInputSelector<Graph, In, Out,
       
   182       typename enable_if<SequenceInputIndicator<In>, void>::type > 
       
   183     {
       
   184       typedef typename In::value_type::second_type Value;
       
   185 
       
   186       static Value kruskal(const Graph& graph, const In& in, Out& out) {
       
   187         return KruskalOutputSelector<Graph, In, Out>::
       
   188           kruskal(graph, in, out);
       
   189       }
       
   190 
       
   191     };
       
   192 
       
   193     template <typename Graph, typename In, typename Out>
       
   194     struct KruskalInputSelector<Graph, In, Out,
       
   195       typename enable_if<MapInputIndicator<In>, void>::type > 
       
   196     {
       
   197       typedef typename In::Value Value;
       
   198       static Value kruskal(const Graph& graph, const In& in, Out& out) {
       
   199         typedef typename In::Key MapArc;
       
   200         typedef typename In::Value Value;
       
   201         typedef typename ItemSetTraits<Graph, MapArc>::ItemIt MapArcIt;
       
   202         typedef std::vector<std::pair<MapArc, Value> > Sequence;
       
   203         Sequence seq;
       
   204         
       
   205         for (MapArcIt it(graph); it != INVALID; ++it) {
       
   206           seq.push_back(std::make_pair(it, in[it]));
       
   207         }
       
   208 
       
   209         std::sort(seq.begin(), seq.end(), PairComp<Sequence>());
       
   210         return KruskalOutputSelector<Graph, Sequence, Out>::
       
   211           kruskal(graph, seq, out);
       
   212       }
       
   213     };
       
   214 
       
   215     template <typename Graph, typename In, typename Out>
       
   216     struct KruskalOutputSelector<Graph, In, Out,
       
   217       typename enable_if<SequenceOutputIndicator<Out>, void>::type > 
       
   218     {
       
   219       typedef typename In::value_type::second_type Value;
       
   220 
       
   221       static Value kruskal(const Graph& graph, const In& in, Out& out) {
       
   222         typedef StoreBoolMap<Out> Map;
       
   223         Map map(out);
       
   224         return _kruskal_bits::kruskal(graph, in, map);
       
   225       }
       
   226 
       
   227     };
       
   228 
       
   229     template <typename Graph, typename In, typename Out>
       
   230     struct KruskalOutputSelector<Graph, In, Out,
       
   231       typename enable_if<MapOutputIndicator<Out>, void>::type > 
       
   232     {
       
   233       typedef typename In::value_type::second_type Value;
       
   234 
       
   235       static Value kruskal(const Graph& graph, const In& in, Out& out) {
       
   236         return _kruskal_bits::kruskal(graph, in, out);
       
   237       }
       
   238     };
       
   239 
       
   240   }
       
   241 
       
   242   /// \ingroup spantree
       
   243   ///
       
   244   /// \brief Kruskal's algorithm to find a minimum cost tree of a graph.
       
   245   ///
       
   246   /// This function runs Kruskal's algorithm to find a minimum cost tree.
       
   247   /// Due to some C++ hacking, it accepts various input and output types.
       
   248   ///
       
   249   /// \param g The graph the algorithm runs on.
       
   250   /// It can be either \ref concepts::Digraph "directed" or 
       
   251   /// \ref concepts::Graph "undirected".
       
   252   /// If the graph is directed, the algorithm consider it to be 
       
   253   /// undirected by disregarding the direction of the arcs.
       
   254   ///
       
   255   /// \param in This object is used to describe the arc costs. It can be one
       
   256   /// of the following choices.
       
   257   /// - An STL compatible 'Forward Container' with
       
   258   /// <tt>std::pair<GR::Edge,X></tt> or
       
   259   /// <tt>std::pair<GR::Arc,X></tt> as its <tt>value_type</tt>, where
       
   260   /// \c X is the type of the costs. The pairs indicates the arcs
       
   261   /// along with the assigned cost. <em>They must be in a
       
   262   /// cost-ascending order.</em>
       
   263   /// - Any readable Arc map. The values of the map indicate the arc costs.
       
   264   ///
       
   265   /// \retval out Here we also have a choise.
       
   266   /// - It can be a writable \c bool arc map.  After running the
       
   267   /// algorithm this will contain the found minimum cost spanning
       
   268   /// tree: the value of an arc will be set to \c true if it belongs
       
   269   /// to the tree, otherwise it will be set to \c false. The value of
       
   270   /// each arc will be set exactly once.
       
   271   /// - It can also be an iteraror of an STL Container with
       
   272   /// <tt>GR::Edge</tt> or <tt>GR::Arc</tt> as its
       
   273   /// <tt>value_type</tt>.  The algorithm copies the elements of the
       
   274   /// found tree into this sequence.  For example, if we know that the
       
   275   /// spanning tree of the graph \c g has say 53 arcs, then we can
       
   276   /// put its arcs into an STL vector \c tree with a code like this.
       
   277   ///\code
       
   278   /// std::vector<Arc> tree(53);
       
   279   /// kruskal(g,cost,tree.begin());
       
   280   ///\endcode
       
   281   /// Or if we don't know in advance the size of the tree, we can
       
   282   /// write this.  
       
   283   ///\code std::vector<Arc> tree;
       
   284   /// kruskal(g,cost,std::back_inserter(tree)); 
       
   285   ///\endcode
       
   286   ///
       
   287   /// \return The total cost of the found tree.
       
   288   ///
       
   289   /// \warning If kruskal runs on an be consistent of using the same
       
   290   /// Arc type for input and output.
       
   291   ///
       
   292 
       
   293 #ifdef DOXYGEN
       
   294   template <class Graph, class In, class Out>
       
   295   Value kruskal(GR const& g, const In& in, Out& out)
       
   296 #else 
       
   297   template <class Graph, class In, class Out>
       
   298   inline typename _kruskal_bits::KruskalValueSelector<In>::Value 
       
   299   kruskal(const Graph& graph, const In& in, Out& out) 
       
   300 #endif
       
   301   {
       
   302     return _kruskal_bits::KruskalInputSelector<Graph, In, Out>::
       
   303       kruskal(graph, in, out);
       
   304   }
       
   305 
       
   306  
       
   307   
       
   308 
       
   309   template <class Graph, class In, class Out>
       
   310   inline typename _kruskal_bits::KruskalValueSelector<In>::Value
       
   311   kruskal(const Graph& graph, const In& in, const Out& out)
       
   312   {
       
   313     return _kruskal_bits::KruskalInputSelector<Graph, In, const Out>::
       
   314       kruskal(graph, in, out);
       
   315   }  
       
   316 
       
   317 } //namespace lemon
       
   318 
       
   319 #endif //LEMON_KRUSKAL_H