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@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: deba@220: #include alpar@103: #include 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 alpar@103: typename disable_if, 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 IndexMap; alpar@103: typedef typename Digraph::Node Node; alpar@209: 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@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 alpar@103: typename enable_if, 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 IndexMap; alpar@103: typedef typename Graph::Node Node; alpar@209: 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@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 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 alpar@103: struct SequenceInputIndicator { alpar@103: static const bool value = false; alpar@103: }; alpar@103: alpar@103: template alpar@209: 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@209: 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@209: alpar@103: template alpar@209: 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@209: 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@209: }; alpar@103: alpar@103: template alpar@103: struct KruskalValueSelector, void>::type> alpar@103: { alpar@103: typedef typename In::Value Value; alpar@209: }; alpar@209: alpar@103: template alpar@103: struct KruskalInputSelector {}; alpar@103: alpar@103: template alpar@103: struct KruskalOutputSelector {}; alpar@209: 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@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()); 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) { kpeter@167: typedef LoggerBoolMap::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: /// kpeter@194: /// \brief Kruskal algorithm to find 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@194: /// spanning 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@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@194: /// std::pair or kpeter@194: /// std::pair as its value_type, where kpeter@194: /// \c X is the type of the costs. The pairs indicates the arcs/edges alpar@103: /// along with the assigned cost. They must be in a alpar@103: /// cost-ascending order. 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@194: /// - It can be a writable \c bool arc/edge map. After running the kpeter@194: /// 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: /// GR::Arc or GR::Edge 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@209: /// write this. kpeter@194: ///\code kpeter@194: /// std::vector 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 alpar@103: template alpar@103: Value kruskal(GR const& g, const In& in, Out& out) alpar@209: #else alpar@103: template alpar@209: inline typename _kruskal_bits::KruskalValueSelector::Value alpar@209: 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@209: alpar@209: 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@209: } alpar@103: alpar@103: } //namespace lemon alpar@103: alpar@103: #endif //LEMON_KRUSKAL_H