alpar@103: /* -*- C++ -*- alpar@103: * alpar@103: * This file is a part of LEMON, a generic C++ optimization library alpar@103: * alpar@103: * Copyright (C) 2003-2008 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 alpar@103: #include alpar@103: #include alpar@103: // #include alpar@103: #include alpar@103: alpar@103: // #include alpar@103: alpar@103: #include alpar@103: #include alpar@103: alpar@103: ///\ingroup spantree alpar@103: ///\file alpar@103: ///\brief Kruskal's algorithm to compute a minimum cost tree alpar@103: /// alpar@103: ///Kruskal's algorithm to compute a minimum cost 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 alpar@103: typename disable_if, alpar@103: 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 IndexMap; alpar@103: typedef typename Digraph::Node Node; alpar@103: alpar@103: IndexMap index(digraph); alpar@103: UnionFind uf(index); alpar@103: for (typename Digraph::NodeIt it(digraph); it != INVALID; ++it) { alpar@103: uf.insert(it); alpar@103: } alpar@103: 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 alpar@103: typename enable_if, alpar@103: 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 IndexMap; alpar@103: typedef typename Graph::Node Node; alpar@103: alpar@103: IndexMap index(graph); alpar@103: UnionFind uf(index); alpar@103: for (typename Graph::NodeIt it(graph); it != INVALID; ++it) { alpar@103: uf.insert(it); alpar@103: } alpar@103: 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 alpar@103: struct PairComp { alpar@103: typedef typename Sequence::value_type Value; alpar@103: bool operator()(const Value& left, const Value& right) { alpar@103: return left.second < right.second; alpar@103: } alpar@103: }; alpar@103: alpar@103: template alpar@103: struct SequenceInputIndicator { alpar@103: static const bool value = false; alpar@103: }; alpar@103: alpar@103: template alpar@103: struct SequenceInputIndicator::type> { alpar@103: static const bool value = true; alpar@103: }; alpar@103: alpar@103: template alpar@103: struct MapInputIndicator { alpar@103: static const bool value = false; alpar@103: }; alpar@103: alpar@103: template alpar@103: struct MapInputIndicator::type> { alpar@103: static const bool value = true; alpar@103: }; alpar@103: alpar@103: template alpar@103: struct SequenceOutputIndicator { alpar@103: static const bool value = false; alpar@103: }; alpar@103: alpar@103: template alpar@103: struct SequenceOutputIndicator::type> { alpar@103: static const bool value = true; alpar@103: }; alpar@103: alpar@103: template alpar@103: struct MapOutputIndicator { alpar@103: static const bool value = false; alpar@103: }; alpar@103: alpar@103: template alpar@103: struct MapOutputIndicator::type> { alpar@103: static const bool value = true; alpar@103: }; alpar@103: alpar@103: template alpar@103: struct KruskalValueSelector {}; alpar@103: alpar@103: template alpar@103: struct KruskalValueSelector, void>::type> alpar@103: { alpar@103: typedef typename In::value_type::second_type Value; alpar@103: }; alpar@103: alpar@103: template alpar@103: struct KruskalValueSelector, void>::type> alpar@103: { alpar@103: typedef typename In::Value Value; alpar@103: }; alpar@103: alpar@103: template alpar@103: struct KruskalInputSelector {}; alpar@103: alpar@103: template alpar@103: struct KruskalOutputSelector {}; alpar@103: alpar@103: template alpar@103: struct KruskalInputSelector, 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:: alpar@103: kruskal(graph, in, out); alpar@103: } alpar@103: alpar@103: }; alpar@103: alpar@103: template alpar@103: struct KruskalInputSelector, 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::ItemIt MapArcIt; alpar@103: typedef std::vector > Sequence; alpar@103: Sequence seq; alpar@103: 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()); alpar@103: return KruskalOutputSelector:: alpar@103: kruskal(graph, seq, out); alpar@103: } alpar@103: }; alpar@103: deba@136: template deba@136: struct RemoveConst { deba@136: typedef T type; deba@136: }; deba@136: deba@136: template deba@136: struct RemoveConst { deba@136: typedef T type; deba@136: }; deba@136: alpar@103: template alpar@103: struct KruskalOutputSelector, 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) { deba@136: typedef StoreBoolMap::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 alpar@103: struct KruskalOutputSelector, 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: /// alpar@103: /// \brief Kruskal's algorithm to find a minimum cost tree of a graph. alpar@103: /// alpar@103: /// This function runs Kruskal's algorithm to find a minimum cost tree. 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@103: /// It can be either \ref concepts::Digraph "directed" or alpar@103: /// \ref concepts::Graph "undirected". alpar@103: /// If the graph is directed, the algorithm consider it to be alpar@103: /// undirected by disregarding the direction of the arcs. alpar@103: /// alpar@103: /// \param in This object is used to describe the arc costs. It can be one alpar@103: /// of the following choices. alpar@103: /// - An STL compatible 'Forward Container' with alpar@103: /// std::pair or alpar@103: /// std::pair as its value_type, where alpar@103: /// \c X is the type of the costs. The pairs indicates the arcs alpar@103: /// along with the assigned cost. They must be in a alpar@103: /// cost-ascending order. alpar@103: /// - Any readable Arc map. The values of the map indicate the arc costs. alpar@103: /// alpar@103: /// \retval out Here we also have a choise. alpar@103: /// - It can be a writable \c bool arc map. After running the alpar@103: /// algorithm this will contain the found minimum cost spanning alpar@103: /// tree: the value of an arc 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 alpar@103: /// each arc will be set exactly once. alpar@103: /// - It can also be an iteraror of an STL Container with alpar@103: /// GR::Edge or GR::Arc as its alpar@103: /// value_type. 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 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@103: /// write this. alpar@103: ///\code std::vector tree; alpar@103: /// kruskal(g,cost,std::back_inserter(tree)); alpar@103: ///\endcode alpar@103: /// alpar@103: /// \return The total cost of the found tree. alpar@103: /// alpar@103: /// \warning If kruskal runs on an be consistent of using the same alpar@103: /// Arc type for input and output. alpar@103: /// alpar@103: alpar@103: #ifdef DOXYGEN alpar@103: template alpar@103: Value kruskal(GR const& g, const In& in, Out& out) alpar@103: #else alpar@103: template alpar@103: inline typename _kruskal_bits::KruskalValueSelector::Value alpar@103: kruskal(const Graph& graph, const In& in, Out& out) alpar@103: #endif alpar@103: { alpar@103: return _kruskal_bits::KruskalInputSelector:: alpar@103: kruskal(graph, in, out); alpar@103: } alpar@103: alpar@103: alpar@103: alpar@103: alpar@103: template alpar@103: inline typename _kruskal_bits::KruskalValueSelector::Value alpar@103: kruskal(const Graph& graph, const In& in, const Out& out) alpar@103: { alpar@103: return _kruskal_bits::KruskalInputSelector:: alpar@103: kruskal(graph, in, out); alpar@103: } alpar@103: alpar@103: } //namespace lemon alpar@103: alpar@103: #endif //LEMON_KRUSKAL_H