alpar@209: /* -*- mode: C++; indent-tabs-mode: nil; -*- alpar@103: * alpar@209: * This file is a part of LEMON, a generic C++ optimization library. alpar@103: * alpar@440: * Copyright (C) 2003-2009 alpar@103: * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport alpar@103: * (Egervary Research Group on Combinatorial Optimization, EGRES). alpar@103: * alpar@103: * Permission to use, modify and distribute this software is granted alpar@103: * provided that this copyright notice appears in all copies. For alpar@103: * precise terms see the accompanying LICENSE file. alpar@103: * alpar@103: * This software is provided "AS IS" with no warranty of any kind, alpar@103: * express or implied, and with no claim as to its suitability for any alpar@103: * purpose. alpar@103: * alpar@103: */ alpar@103: alpar@103: #ifndef LEMON_KRUSKAL_H alpar@103: #define LEMON_KRUSKAL_H alpar@103: alpar@103: #include <algorithm> alpar@103: #include <vector> alpar@103: #include <lemon/unionfind.h> alpar@103: #include <lemon/maps.h> alpar@103: deba@220: #include <lemon/core.h> alpar@103: #include <lemon/bits/traits.h> alpar@103: alpar@103: ///\ingroup spantree alpar@103: ///\file kpeter@194: ///\brief Kruskal's algorithm to compute a minimum cost spanning tree alpar@103: /// kpeter@194: ///Kruskal's algorithm to compute a minimum cost spanning tree. alpar@103: /// alpar@103: alpar@103: namespace lemon { alpar@103: alpar@103: namespace _kruskal_bits { alpar@103: alpar@103: // Kruskal for directed graphs. alpar@103: alpar@103: template <typename Digraph, typename In, typename Out> alpar@103: typename disable_if<lemon::UndirectedTagIndicator<Digraph>, alpar@209: typename In::value_type::second_type >::type alpar@103: kruskal(const Digraph& digraph, const In& in, Out& out,dummy<0> = 0) { alpar@103: typedef typename In::value_type::second_type Value; alpar@103: typedef typename Digraph::template NodeMap<int> IndexMap; alpar@103: typedef typename Digraph::Node Node; alpar@209: alpar@103: IndexMap index(digraph); alpar@103: UnionFind<IndexMap> uf(index); alpar@103: for (typename Digraph::NodeIt it(digraph); it != INVALID; ++it) { alpar@103: uf.insert(it); alpar@103: } alpar@209: alpar@103: Value tree_value = 0; alpar@103: for (typename In::const_iterator it = in.begin(); it != in.end(); ++it) { alpar@103: if (uf.join(digraph.target(it->first),digraph.source(it->first))) { alpar@103: out.set(it->first, true); alpar@103: tree_value += it->second; alpar@103: } alpar@103: else { alpar@103: out.set(it->first, false); alpar@103: } alpar@103: } alpar@103: return tree_value; alpar@103: } alpar@103: alpar@103: // Kruskal for undirected graphs. alpar@103: alpar@103: template <typename Graph, typename In, typename Out> alpar@103: typename enable_if<lemon::UndirectedTagIndicator<Graph>, alpar@209: typename In::value_type::second_type >::type alpar@103: kruskal(const Graph& graph, const In& in, Out& out,dummy<1> = 1) { alpar@103: typedef typename In::value_type::second_type Value; alpar@103: typedef typename Graph::template NodeMap<int> IndexMap; alpar@103: typedef typename Graph::Node Node; alpar@209: alpar@103: IndexMap index(graph); alpar@103: UnionFind<IndexMap> uf(index); alpar@103: for (typename Graph::NodeIt it(graph); it != INVALID; ++it) { alpar@103: uf.insert(it); alpar@103: } alpar@209: alpar@103: Value tree_value = 0; alpar@103: for (typename In::const_iterator it = in.begin(); it != in.end(); ++it) { alpar@103: if (uf.join(graph.u(it->first),graph.v(it->first))) { alpar@103: out.set(it->first, true); alpar@103: tree_value += it->second; alpar@103: } alpar@103: else { alpar@103: out.set(it->first, false); alpar@103: } alpar@103: } alpar@103: return tree_value; alpar@103: } alpar@103: alpar@103: alpar@103: template <typename Sequence> alpar@103: struct PairComp { alpar@103: typedef typename Sequence::value_type Value; alpar@103: bool operator()(const Value& left, const Value& right) { alpar@209: return left.second < right.second; alpar@103: } alpar@103: }; alpar@103: alpar@103: template <typename In, typename Enable = void> alpar@103: struct SequenceInputIndicator { alpar@103: static const bool value = false; alpar@103: }; alpar@103: alpar@103: template <typename In> alpar@209: struct SequenceInputIndicator<In, alpar@103: typename exists<typename In::value_type::first_type>::type> { alpar@103: static const bool value = true; alpar@103: }; alpar@103: alpar@103: template <typename In, typename Enable = void> alpar@103: struct MapInputIndicator { alpar@103: static const bool value = false; alpar@103: }; alpar@103: alpar@103: template <typename In> alpar@209: struct MapInputIndicator<In, alpar@103: typename exists<typename In::Value>::type> { alpar@103: static const bool value = true; alpar@103: }; alpar@103: alpar@103: template <typename In, typename Enable = void> alpar@103: struct SequenceOutputIndicator { alpar@103: static const bool value = false; alpar@103: }; alpar@209: alpar@103: template <typename Out> alpar@209: struct SequenceOutputIndicator<Out, alpar@103: typename exists<typename Out::value_type>::type> { alpar@103: static const bool value = true; alpar@103: }; alpar@103: alpar@103: template <typename Out, typename Enable = void> alpar@103: struct MapOutputIndicator { alpar@103: static const bool value = false; alpar@103: }; alpar@103: alpar@103: template <typename Out> alpar@209: struct MapOutputIndicator<Out, alpar@103: typename exists<typename Out::Value>::type> { alpar@103: static const bool value = true; alpar@103: }; alpar@103: alpar@103: template <typename In, typename InEnable = void> alpar@103: struct KruskalValueSelector {}; alpar@103: alpar@103: template <typename In> alpar@103: struct KruskalValueSelector<In, alpar@209: typename enable_if<SequenceInputIndicator<In>, void>::type> alpar@103: { alpar@103: typedef typename In::value_type::second_type Value; alpar@209: }; alpar@103: alpar@103: template <typename In> alpar@103: struct KruskalValueSelector<In, alpar@209: typename enable_if<MapInputIndicator<In>, void>::type> alpar@103: { alpar@103: typedef typename In::Value Value; alpar@209: }; alpar@209: alpar@103: template <typename Graph, typename In, typename Out, alpar@103: typename InEnable = void> alpar@103: struct KruskalInputSelector {}; alpar@103: alpar@103: template <typename Graph, typename In, typename Out, alpar@103: typename InEnable = void> alpar@103: struct KruskalOutputSelector {}; alpar@209: alpar@103: template <typename Graph, typename In, typename Out> alpar@103: struct KruskalInputSelector<Graph, In, Out, alpar@209: typename enable_if<SequenceInputIndicator<In>, void>::type > alpar@103: { alpar@103: typedef typename In::value_type::second_type Value; alpar@103: alpar@103: static Value kruskal(const Graph& graph, const In& in, Out& out) { alpar@103: return KruskalOutputSelector<Graph, In, Out>:: alpar@103: kruskal(graph, in, out); alpar@103: } alpar@103: alpar@103: }; alpar@103: alpar@103: template <typename Graph, typename In, typename Out> alpar@103: struct KruskalInputSelector<Graph, In, Out, alpar@209: typename enable_if<MapInputIndicator<In>, void>::type > alpar@103: { alpar@103: typedef typename In::Value Value; alpar@103: static Value kruskal(const Graph& graph, const In& in, Out& out) { alpar@103: typedef typename In::Key MapArc; alpar@103: typedef typename In::Value Value; alpar@103: typedef typename ItemSetTraits<Graph, MapArc>::ItemIt MapArcIt; alpar@103: typedef std::vector<std::pair<MapArc, Value> > Sequence; alpar@103: Sequence seq; alpar@209: alpar@103: for (MapArcIt it(graph); it != INVALID; ++it) { alpar@103: seq.push_back(std::make_pair(it, in[it])); alpar@103: } alpar@103: alpar@103: std::sort(seq.begin(), seq.end(), PairComp<Sequence>()); alpar@103: return KruskalOutputSelector<Graph, Sequence, Out>:: alpar@103: kruskal(graph, seq, out); alpar@103: } alpar@103: }; alpar@103: deba@136: template <typename T> deba@136: struct RemoveConst { deba@136: typedef T type; deba@136: }; deba@136: deba@136: template <typename T> deba@136: struct RemoveConst<const T> { deba@136: typedef T type; deba@136: }; deba@136: alpar@103: template <typename Graph, typename In, typename Out> alpar@103: struct KruskalOutputSelector<Graph, In, Out, alpar@209: typename enable_if<SequenceOutputIndicator<Out>, void>::type > alpar@103: { alpar@103: typedef typename In::value_type::second_type Value; alpar@103: alpar@103: static Value kruskal(const Graph& graph, const In& in, Out& out) { kpeter@167: typedef LoggerBoolMap<typename RemoveConst<Out>::type> Map; alpar@103: Map map(out); alpar@103: return _kruskal_bits::kruskal(graph, in, map); alpar@103: } alpar@103: alpar@103: }; alpar@103: alpar@103: template <typename Graph, typename In, typename Out> alpar@103: struct KruskalOutputSelector<Graph, In, Out, alpar@209: typename enable_if<MapOutputIndicator<Out>, void>::type > alpar@103: { alpar@103: typedef typename In::value_type::second_type Value; alpar@103: alpar@103: static Value kruskal(const Graph& graph, const In& in, Out& out) { alpar@103: return _kruskal_bits::kruskal(graph, in, out); alpar@103: } alpar@103: }; alpar@103: alpar@103: } alpar@103: alpar@103: /// \ingroup spantree alpar@103: /// kpeter@576: /// \brief Kruskal's algorithm for finding a minimum cost spanning tree of kpeter@194: /// a graph. alpar@103: /// alpar@209: /// This function runs Kruskal's algorithm to find a minimum cost kpeter@576: /// spanning tree of a graph. alpar@103: /// Due to some C++ hacking, it accepts various input and output types. alpar@103: /// alpar@103: /// \param g The graph the algorithm runs on. alpar@209: /// It can be either \ref concepts::Digraph "directed" or alpar@103: /// \ref concepts::Graph "undirected". alpar@209: /// If the graph is directed, the algorithm consider it to be alpar@103: /// undirected by disregarding the direction of the arcs. alpar@103: /// alpar@209: /// \param in This object is used to describe the arc/edge costs. kpeter@194: /// It can be one of the following choices. alpar@103: /// - An STL compatible 'Forward Container' with kpeter@576: /// <tt>std::pair<GR::Arc,C></tt> or kpeter@576: /// <tt>std::pair<GR::Edge,C></tt> as its <tt>value_type</tt>, where kpeter@576: /// \c C is the type of the costs. The pairs indicates the arcs/edges alpar@103: /// along with the assigned cost. <em>They must be in a alpar@103: /// cost-ascending order.</em> alpar@209: /// - Any readable arc/edge map. The values of the map indicate the kpeter@194: /// arc/edge costs. alpar@103: /// kpeter@194: /// \retval out Here we also have a choice. kpeter@576: /// - It can be a writable arc/edge map with \c bool value type. After kpeter@576: /// running the algorithm it will contain the found minimum cost spanning kpeter@194: /// tree: the value of an arc/edge will be set to \c true if it belongs alpar@103: /// to the tree, otherwise it will be set to \c false. The value of kpeter@194: /// each arc/edge will be set exactly once. alpar@103: /// - It can also be an iteraror of an STL Container with kpeter@194: /// <tt>GR::Arc</tt> or <tt>GR::Edge</tt> as its alpar@103: /// <tt>value_type</tt>. The algorithm copies the elements of the alpar@103: /// found tree into this sequence. For example, if we know that the alpar@103: /// spanning tree of the graph \c g has say 53 arcs, then we can alpar@103: /// put its arcs into an STL vector \c tree with a code like this. alpar@103: ///\code alpar@103: /// std::vector<Arc> tree(53); alpar@103: /// kruskal(g,cost,tree.begin()); alpar@103: ///\endcode alpar@103: /// Or if we don't know in advance the size of the tree, we can alpar@209: /// write this. kpeter@194: ///\code kpeter@194: /// std::vector<Arc> tree; alpar@209: /// kruskal(g,cost,std::back_inserter(tree)); alpar@103: ///\endcode alpar@103: /// kpeter@194: /// \return The total cost of the found spanning tree. alpar@103: /// deba@220: /// \note If the input graph is not (weakly) connected, a spanning kpeter@216: /// forest is calculated instead of a spanning tree. alpar@103: alpar@103: #ifdef DOXYGEN kpeter@576: template <typename Graph, typename In, typename Out> kpeter@576: Value kruskal(const Graph& g, const In& in, Out& out) alpar@209: #else alpar@103: template <class Graph, class In, class Out> alpar@209: inline typename _kruskal_bits::KruskalValueSelector<In>::Value alpar@209: kruskal(const Graph& graph, const In& in, Out& out) alpar@103: #endif alpar@103: { alpar@103: return _kruskal_bits::KruskalInputSelector<Graph, In, Out>:: alpar@103: kruskal(graph, in, out); alpar@103: } alpar@103: alpar@209: alpar@103: template <class Graph, class In, class Out> alpar@103: inline typename _kruskal_bits::KruskalValueSelector<In>::Value alpar@103: kruskal(const Graph& graph, const In& in, const Out& out) alpar@103: { alpar@103: return _kruskal_bits::KruskalInputSelector<Graph, In, const Out>:: alpar@103: kruskal(graph, in, out); alpar@209: } alpar@103: alpar@103: } //namespace lemon alpar@103: alpar@103: #endif //LEMON_KRUSKAL_H