kpeter@758: /* -*- C++ -*- kpeter@758: * kpeter@758: * This file is a part of LEMON, a generic C++ optimization library kpeter@758: * kpeter@758: * Copyright (C) 2003-2008 kpeter@758: * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport kpeter@758: * (Egervary Research Group on Combinatorial Optimization, EGRES). kpeter@758: * kpeter@758: * Permission to use, modify and distribute this software is granted kpeter@758: * provided that this copyright notice appears in all copies. For kpeter@758: * precise terms see the accompanying LICENSE file. kpeter@758: * kpeter@758: * This software is provided "AS IS" with no warranty of any kind, kpeter@758: * express or implied, and with no claim as to its suitability for any kpeter@758: * purpose. kpeter@758: * kpeter@758: */ kpeter@758: kpeter@764: #ifndef LEMON_HOWARD_H kpeter@764: #define LEMON_HOWARD_H kpeter@758: kpeter@768: /// \ingroup min_mean_cycle kpeter@758: /// kpeter@758: /// \file kpeter@758: /// \brief Howard's algorithm for finding a minimum mean cycle. kpeter@758: kpeter@758: #include kpeter@763: #include kpeter@758: #include kpeter@758: #include kpeter@758: #include kpeter@758: #include kpeter@758: kpeter@758: namespace lemon { kpeter@758: kpeter@764: /// \brief Default traits class of Howard class. kpeter@761: /// kpeter@764: /// Default traits class of Howard class. kpeter@761: /// \tparam GR The type of the digraph. kpeter@761: /// \tparam LEN The type of the length map. kpeter@761: /// It must conform to the \ref concepts::ReadMap "ReadMap" concept. kpeter@761: #ifdef DOXYGEN kpeter@761: template kpeter@761: #else kpeter@761: template ::is_integer> kpeter@761: #endif kpeter@764: struct HowardDefaultTraits kpeter@761: { kpeter@761: /// The type of the digraph kpeter@761: typedef GR Digraph; kpeter@761: /// The type of the length map kpeter@761: typedef LEN LengthMap; kpeter@761: /// The type of the arc lengths kpeter@761: typedef typename LengthMap::Value Value; kpeter@761: kpeter@761: /// \brief The large value type used for internal computations kpeter@761: /// kpeter@761: /// The large value type used for internal computations. kpeter@761: /// It is \c long \c long if the \c Value type is integer, kpeter@761: /// otherwise it is \c double. kpeter@761: /// \c Value must be convertible to \c LargeValue. kpeter@761: typedef double LargeValue; kpeter@761: kpeter@761: /// The tolerance type used for internal computations kpeter@761: typedef lemon::Tolerance Tolerance; kpeter@761: kpeter@761: /// \brief The path type of the found cycles kpeter@761: /// kpeter@761: /// The path type of the found cycles. kpeter@761: /// It must conform to the \ref lemon::concepts::Path "Path" concept kpeter@761: /// and it must have an \c addBack() function. kpeter@761: typedef lemon::Path Path; kpeter@761: }; kpeter@761: kpeter@761: // Default traits class for integer value types kpeter@761: template kpeter@764: struct HowardDefaultTraits kpeter@761: { kpeter@761: typedef GR Digraph; kpeter@761: typedef LEN LengthMap; kpeter@761: typedef typename LengthMap::Value Value; kpeter@761: #ifdef LEMON_HAVE_LONG_LONG kpeter@761: typedef long long LargeValue; kpeter@761: #else kpeter@761: typedef long LargeValue; kpeter@761: #endif kpeter@761: typedef lemon::Tolerance Tolerance; kpeter@761: typedef lemon::Path Path; kpeter@761: }; kpeter@761: kpeter@761: kpeter@768: /// \addtogroup min_mean_cycle kpeter@758: /// @{ kpeter@758: kpeter@758: /// \brief Implementation of Howard's algorithm for finding a minimum kpeter@758: /// mean cycle. kpeter@758: /// kpeter@764: /// This class implements Howard's policy iteration algorithm for finding kpeter@771: /// a directed cycle of minimum mean length (cost) in a digraph kpeter@771: /// \ref amo93networkflows, \ref dasdan98minmeancycle. kpeter@768: /// This class provides the most efficient algorithm for the kpeter@768: /// minimum mean cycle problem, though the best known theoretical kpeter@768: /// bound on its running time is exponential. kpeter@758: /// kpeter@758: /// \tparam GR The type of the digraph the algorithm runs on. kpeter@758: /// \tparam LEN The type of the length map. The default kpeter@758: /// map type is \ref concepts::Digraph::ArcMap "GR::ArcMap". kpeter@758: #ifdef DOXYGEN kpeter@761: template kpeter@758: #else kpeter@758: template < typename GR, kpeter@761: typename LEN = typename GR::template ArcMap, kpeter@764: typename TR = HowardDefaultTraits > kpeter@758: #endif kpeter@764: class Howard kpeter@758: { kpeter@758: public: kpeter@758: kpeter@761: /// The type of the digraph kpeter@761: typedef typename TR::Digraph Digraph; kpeter@758: /// The type of the length map kpeter@761: typedef typename TR::LengthMap LengthMap; kpeter@758: /// The type of the arc lengths kpeter@761: typedef typename TR::Value Value; kpeter@761: kpeter@761: /// \brief The large value type kpeter@761: /// kpeter@761: /// The large value type used for internal computations. kpeter@764: /// Using the \ref HowardDefaultTraits "default traits class", kpeter@761: /// it is \c long \c long if the \c Value type is integer, kpeter@761: /// otherwise it is \c double. kpeter@761: typedef typename TR::LargeValue LargeValue; kpeter@761: kpeter@761: /// The tolerance type kpeter@761: typedef typename TR::Tolerance Tolerance; kpeter@761: kpeter@761: /// \brief The path type of the found cycles kpeter@761: /// kpeter@761: /// The path type of the found cycles. kpeter@764: /// Using the \ref HowardDefaultTraits "default traits class", kpeter@761: /// it is \ref lemon::Path "Path". kpeter@761: typedef typename TR::Path Path; kpeter@761: kpeter@764: /// The \ref HowardDefaultTraits "traits class" of the algorithm kpeter@761: typedef TR Traits; kpeter@758: kpeter@758: private: kpeter@758: kpeter@758: TEMPLATE_DIGRAPH_TYPEDEFS(Digraph); kpeter@758: kpeter@758: // The digraph the algorithm runs on kpeter@758: const Digraph &_gr; kpeter@758: // The length of the arcs kpeter@758: const LengthMap &_length; kpeter@758: kpeter@760: // Data for the found cycles kpeter@760: bool _curr_found, _best_found; kpeter@761: LargeValue _curr_length, _best_length; kpeter@760: int _curr_size, _best_size; kpeter@760: Node _curr_node, _best_node; kpeter@760: kpeter@758: Path *_cycle_path; kpeter@760: bool _local_path; kpeter@758: kpeter@760: // Internal data used by the algorithm kpeter@760: typename Digraph::template NodeMap _policy; kpeter@760: typename Digraph::template NodeMap _reached; kpeter@760: typename Digraph::template NodeMap _level; kpeter@761: typename Digraph::template NodeMap _dist; kpeter@758: kpeter@760: // Data for storing the strongly connected components kpeter@760: int _comp_num; kpeter@758: typename Digraph::template NodeMap _comp; kpeter@760: std::vector > _comp_nodes; kpeter@760: std::vector* _nodes; kpeter@760: typename Digraph::template NodeMap > _in_arcs; kpeter@760: kpeter@760: // Queue used for BFS search kpeter@760: std::vector _queue; kpeter@760: int _qfront, _qback; kpeter@761: kpeter@761: Tolerance _tolerance; kpeter@761: kpeter@767: // Infinite constant kpeter@767: const LargeValue INF; kpeter@767: kpeter@761: public: kpeter@761: kpeter@761: /// \name Named Template Parameters kpeter@761: /// @{ kpeter@761: kpeter@761: template kpeter@761: struct SetLargeValueTraits : public Traits { kpeter@761: typedef T LargeValue; kpeter@761: typedef lemon::Tolerance Tolerance; kpeter@761: }; kpeter@761: kpeter@761: /// \brief \ref named-templ-param "Named parameter" for setting kpeter@761: /// \c LargeValue type. kpeter@761: /// kpeter@761: /// \ref named-templ-param "Named parameter" for setting \c LargeValue kpeter@761: /// type. It is used for internal computations in the algorithm. kpeter@761: template kpeter@761: struct SetLargeValue kpeter@764: : public Howard > { kpeter@764: typedef Howard > Create; kpeter@761: }; kpeter@761: kpeter@761: template kpeter@761: struct SetPathTraits : public Traits { kpeter@761: typedef T Path; kpeter@761: }; kpeter@761: kpeter@761: /// \brief \ref named-templ-param "Named parameter" for setting kpeter@761: /// \c %Path type. kpeter@761: /// kpeter@761: /// \ref named-templ-param "Named parameter" for setting the \c %Path kpeter@761: /// type of the found cycles. kpeter@761: /// It must conform to the \ref lemon::concepts::Path "Path" concept kpeter@761: /// and it must have an \c addBack() function. kpeter@761: template kpeter@761: struct SetPath kpeter@764: : public Howard > { kpeter@764: typedef Howard > Create; kpeter@761: }; kpeter@760: kpeter@761: /// @} kpeter@758: kpeter@758: public: kpeter@758: kpeter@758: /// \brief Constructor. kpeter@758: /// kpeter@758: /// The constructor of the class. kpeter@758: /// kpeter@758: /// \param digraph The digraph the algorithm runs on. kpeter@758: /// \param length The lengths (costs) of the arcs. kpeter@764: Howard( const Digraph &digraph, kpeter@764: const LengthMap &length ) : kpeter@767: _gr(digraph), _length(length), _best_found(false), kpeter@767: _best_length(0), _best_size(1), _cycle_path(NULL), _local_path(false), kpeter@760: _policy(digraph), _reached(digraph), _level(digraph), _dist(digraph), kpeter@767: _comp(digraph), _in_arcs(digraph), kpeter@767: INF(std::numeric_limits::has_infinity ? kpeter@767: std::numeric_limits::infinity() : kpeter@767: std::numeric_limits::max()) kpeter@758: {} kpeter@758: kpeter@758: /// Destructor. kpeter@764: ~Howard() { kpeter@758: if (_local_path) delete _cycle_path; kpeter@758: } kpeter@758: kpeter@758: /// \brief Set the path structure for storing the found cycle. kpeter@758: /// kpeter@758: /// This function sets an external path structure for storing the kpeter@758: /// found cycle. kpeter@758: /// kpeter@758: /// If you don't call this function before calling \ref run() or kpeter@759: /// \ref findMinMean(), it will allocate a local \ref Path "path" kpeter@758: /// structure. The destuctor deallocates this automatically kpeter@758: /// allocated object, of course. kpeter@758: /// kpeter@758: /// \note The algorithm calls only the \ref lemon::Path::addBack() kpeter@758: /// "addBack()" function of the given path structure. kpeter@758: /// kpeter@758: /// \return (*this) kpeter@764: Howard& cycle(Path &path) { kpeter@758: if (_local_path) { kpeter@758: delete _cycle_path; kpeter@758: _local_path = false; kpeter@758: } kpeter@758: _cycle_path = &path; kpeter@758: return *this; kpeter@758: } kpeter@758: kpeter@769: /// \brief Set the tolerance used by the algorithm. kpeter@769: /// kpeter@769: /// This function sets the tolerance object used by the algorithm. kpeter@769: /// kpeter@769: /// \return (*this) kpeter@769: Howard& tolerance(const Tolerance& tolerance) { kpeter@769: _tolerance = tolerance; kpeter@769: return *this; kpeter@769: } kpeter@769: kpeter@769: /// \brief Return a const reference to the tolerance. kpeter@769: /// kpeter@769: /// This function returns a const reference to the tolerance object kpeter@769: /// used by the algorithm. kpeter@769: const Tolerance& tolerance() const { kpeter@769: return _tolerance; kpeter@769: } kpeter@769: kpeter@758: /// \name Execution control kpeter@758: /// The simplest way to execute the algorithm is to call the \ref run() kpeter@758: /// function.\n kpeter@759: /// If you only need the minimum mean length, you may call kpeter@759: /// \ref findMinMean(). kpeter@758: kpeter@758: /// @{ kpeter@758: kpeter@758: /// \brief Run the algorithm. kpeter@758: /// kpeter@758: /// This function runs the algorithm. kpeter@759: /// It can be called more than once (e.g. if the underlying digraph kpeter@759: /// and/or the arc lengths have been modified). kpeter@758: /// kpeter@758: /// \return \c true if a directed cycle exists in the digraph. kpeter@758: /// kpeter@759: /// \note mmc.run() is just a shortcut of the following code. kpeter@758: /// \code kpeter@759: /// return mmc.findMinMean() && mmc.findCycle(); kpeter@758: /// \endcode kpeter@758: bool run() { kpeter@758: return findMinMean() && findCycle(); kpeter@758: } kpeter@758: kpeter@759: /// \brief Find the minimum cycle mean. kpeter@758: /// kpeter@759: /// This function finds the minimum mean length of the directed kpeter@759: /// cycles in the digraph. kpeter@758: /// kpeter@759: /// \return \c true if a directed cycle exists in the digraph. kpeter@759: bool findMinMean() { kpeter@760: // Initialize and find strongly connected components kpeter@760: init(); kpeter@760: findComponents(); kpeter@760: kpeter@759: // Find the minimum cycle mean in the components kpeter@758: for (int comp = 0; comp < _comp_num; ++comp) { kpeter@760: // Find the minimum mean cycle in the current component kpeter@760: if (!buildPolicyGraph(comp)) continue; kpeter@758: while (true) { kpeter@760: findPolicyCycle(); kpeter@758: if (!computeNodeDistances()) break; kpeter@758: } kpeter@760: // Update the best cycle (global minimum mean cycle) kpeter@767: if ( _curr_found && (!_best_found || kpeter@760: _curr_length * _best_size < _best_length * _curr_size) ) { kpeter@760: _best_found = true; kpeter@760: _best_length = _curr_length; kpeter@760: _best_size = _curr_size; kpeter@760: _best_node = _curr_node; kpeter@760: } kpeter@758: } kpeter@760: return _best_found; kpeter@758: } kpeter@758: kpeter@758: /// \brief Find a minimum mean directed cycle. kpeter@758: /// kpeter@758: /// This function finds a directed cycle of minimum mean length kpeter@758: /// in the digraph using the data computed by findMinMean(). kpeter@758: /// kpeter@758: /// \return \c true if a directed cycle exists in the digraph. kpeter@758: /// kpeter@759: /// \pre \ref findMinMean() must be called before using this function. kpeter@758: bool findCycle() { kpeter@760: if (!_best_found) return false; kpeter@760: _cycle_path->addBack(_policy[_best_node]); kpeter@760: for ( Node v = _best_node; kpeter@760: (v = _gr.target(_policy[v])) != _best_node; ) { kpeter@758: _cycle_path->addBack(_policy[v]); kpeter@758: } kpeter@758: return true; kpeter@758: } kpeter@758: kpeter@758: /// @} kpeter@758: kpeter@758: /// \name Query Functions kpeter@759: /// The results of the algorithm can be obtained using these kpeter@758: /// functions.\n kpeter@758: /// The algorithm should be executed before using them. kpeter@758: kpeter@758: /// @{ kpeter@758: kpeter@758: /// \brief Return the total length of the found cycle. kpeter@758: /// kpeter@758: /// This function returns the total length of the found cycle. kpeter@758: /// kpeter@760: /// \pre \ref run() or \ref findMinMean() must be called before kpeter@758: /// using this function. kpeter@761: LargeValue cycleLength() const { kpeter@760: return _best_length; kpeter@758: } kpeter@758: kpeter@758: /// \brief Return the number of arcs on the found cycle. kpeter@758: /// kpeter@758: /// This function returns the number of arcs on the found cycle. kpeter@758: /// kpeter@760: /// \pre \ref run() or \ref findMinMean() must be called before kpeter@758: /// using this function. kpeter@758: int cycleArcNum() const { kpeter@760: return _best_size; kpeter@758: } kpeter@758: kpeter@758: /// \brief Return the mean length of the found cycle. kpeter@758: /// kpeter@758: /// This function returns the mean length of the found cycle. kpeter@758: /// kpeter@760: /// \note alg.cycleMean() is just a shortcut of the kpeter@758: /// following code. kpeter@758: /// \code kpeter@760: /// return static_cast(alg.cycleLength()) / alg.cycleArcNum(); kpeter@758: /// \endcode kpeter@758: /// kpeter@758: /// \pre \ref run() or \ref findMinMean() must be called before kpeter@758: /// using this function. kpeter@758: double cycleMean() const { kpeter@760: return static_cast(_best_length) / _best_size; kpeter@758: } kpeter@758: kpeter@758: /// \brief Return the found cycle. kpeter@758: /// kpeter@758: /// This function returns a const reference to the path structure kpeter@758: /// storing the found cycle. kpeter@758: /// kpeter@758: /// \pre \ref run() or \ref findCycle() must be called before using kpeter@758: /// this function. kpeter@758: const Path& cycle() const { kpeter@758: return *_cycle_path; kpeter@758: } kpeter@758: kpeter@758: ///@} kpeter@758: kpeter@758: private: kpeter@758: kpeter@760: // Initialize kpeter@760: void init() { kpeter@760: if (!_cycle_path) { kpeter@760: _local_path = true; kpeter@760: _cycle_path = new Path; kpeter@758: } kpeter@760: _queue.resize(countNodes(_gr)); kpeter@760: _best_found = false; kpeter@760: _best_length = 0; kpeter@760: _best_size = 1; kpeter@760: _cycle_path->clear(); kpeter@760: } kpeter@760: kpeter@760: // Find strongly connected components and initialize _comp_nodes kpeter@760: // and _in_arcs kpeter@760: void findComponents() { kpeter@760: _comp_num = stronglyConnectedComponents(_gr, _comp); kpeter@760: _comp_nodes.resize(_comp_num); kpeter@760: if (_comp_num == 1) { kpeter@760: _comp_nodes[0].clear(); kpeter@760: for (NodeIt n(_gr); n != INVALID; ++n) { kpeter@760: _comp_nodes[0].push_back(n); kpeter@760: _in_arcs[n].clear(); kpeter@760: for (InArcIt a(_gr, n); a != INVALID; ++a) { kpeter@760: _in_arcs[n].push_back(a); kpeter@760: } kpeter@760: } kpeter@760: } else { kpeter@760: for (int i = 0; i < _comp_num; ++i) kpeter@760: _comp_nodes[i].clear(); kpeter@760: for (NodeIt n(_gr); n != INVALID; ++n) { kpeter@760: int k = _comp[n]; kpeter@760: _comp_nodes[k].push_back(n); kpeter@760: _in_arcs[n].clear(); kpeter@760: for (InArcIt a(_gr, n); a != INVALID; ++a) { kpeter@760: if (_comp[_gr.source(a)] == k) _in_arcs[n].push_back(a); kpeter@760: } kpeter@760: } kpeter@758: } kpeter@760: } kpeter@760: kpeter@760: // Build the policy graph in the given strongly connected component kpeter@760: // (the out-degree of every node is 1) kpeter@760: bool buildPolicyGraph(int comp) { kpeter@760: _nodes = &(_comp_nodes[comp]); kpeter@760: if (_nodes->size() < 1 || kpeter@760: (_nodes->size() == 1 && _in_arcs[(*_nodes)[0]].size() == 0)) { kpeter@760: return false; kpeter@758: } kpeter@760: for (int i = 0; i < int(_nodes->size()); ++i) { kpeter@767: _dist[(*_nodes)[i]] = INF; kpeter@760: } kpeter@760: Node u, v; kpeter@760: Arc e; kpeter@760: for (int i = 0; i < int(_nodes->size()); ++i) { kpeter@760: v = (*_nodes)[i]; kpeter@760: for (int j = 0; j < int(_in_arcs[v].size()); ++j) { kpeter@760: e = _in_arcs[v][j]; kpeter@760: u = _gr.source(e); kpeter@760: if (_length[e] < _dist[u]) { kpeter@760: _dist[u] = _length[e]; kpeter@760: _policy[u] = e; kpeter@760: } kpeter@758: } kpeter@758: } kpeter@758: return true; kpeter@758: } kpeter@758: kpeter@760: // Find the minimum mean cycle in the policy graph kpeter@760: void findPolicyCycle() { kpeter@760: for (int i = 0; i < int(_nodes->size()); ++i) { kpeter@760: _level[(*_nodes)[i]] = -1; kpeter@760: } kpeter@761: LargeValue clength; kpeter@758: int csize; kpeter@758: Node u, v; kpeter@760: _curr_found = false; kpeter@760: for (int i = 0; i < int(_nodes->size()); ++i) { kpeter@760: u = (*_nodes)[i]; kpeter@760: if (_level[u] >= 0) continue; kpeter@760: for (; _level[u] < 0; u = _gr.target(_policy[u])) { kpeter@760: _level[u] = i; kpeter@760: } kpeter@760: if (_level[u] == i) { kpeter@760: // A cycle is found kpeter@760: clength = _length[_policy[u]]; kpeter@760: csize = 1; kpeter@760: for (v = u; (v = _gr.target(_policy[v])) != u; ) { kpeter@760: clength += _length[_policy[v]]; kpeter@760: ++csize; kpeter@758: } kpeter@760: if ( !_curr_found || kpeter@760: (clength * _curr_size < _curr_length * csize) ) { kpeter@760: _curr_found = true; kpeter@760: _curr_length = clength; kpeter@760: _curr_size = csize; kpeter@760: _curr_node = u; kpeter@758: } kpeter@758: } kpeter@758: } kpeter@758: } kpeter@758: kpeter@760: // Contract the policy graph and compute node distances kpeter@758: bool computeNodeDistances() { kpeter@760: // Find the component of the main cycle and compute node distances kpeter@760: // using reverse BFS kpeter@760: for (int i = 0; i < int(_nodes->size()); ++i) { kpeter@760: _reached[(*_nodes)[i]] = false; kpeter@760: } kpeter@760: _qfront = _qback = 0; kpeter@760: _queue[0] = _curr_node; kpeter@760: _reached[_curr_node] = true; kpeter@760: _dist[_curr_node] = 0; kpeter@758: Node u, v; kpeter@760: Arc e; kpeter@760: while (_qfront <= _qback) { kpeter@760: v = _queue[_qfront++]; kpeter@760: for (int j = 0; j < int(_in_arcs[v].size()); ++j) { kpeter@760: e = _in_arcs[v][j]; kpeter@758: u = _gr.source(e); kpeter@760: if (_policy[u] == e && !_reached[u]) { kpeter@760: _reached[u] = true; kpeter@761: _dist[u] = _dist[v] + _length[e] * _curr_size - _curr_length; kpeter@760: _queue[++_qback] = u; kpeter@758: } kpeter@758: } kpeter@758: } kpeter@760: kpeter@760: // Connect all other nodes to this component and compute node kpeter@760: // distances using reverse BFS kpeter@760: _qfront = 0; kpeter@760: while (_qback < int(_nodes->size())-1) { kpeter@760: v = _queue[_qfront++]; kpeter@760: for (int j = 0; j < int(_in_arcs[v].size()); ++j) { kpeter@760: e = _in_arcs[v][j]; kpeter@760: u = _gr.source(e); kpeter@760: if (!_reached[u]) { kpeter@760: _reached[u] = true; kpeter@760: _policy[u] = e; kpeter@761: _dist[u] = _dist[v] + _length[e] * _curr_size - _curr_length; kpeter@760: _queue[++_qback] = u; kpeter@760: } kpeter@760: } kpeter@760: } kpeter@760: kpeter@760: // Improve node distances kpeter@758: bool improved = false; kpeter@760: for (int i = 0; i < int(_nodes->size()); ++i) { kpeter@760: v = (*_nodes)[i]; kpeter@760: for (int j = 0; j < int(_in_arcs[v].size()); ++j) { kpeter@760: e = _in_arcs[v][j]; kpeter@760: u = _gr.source(e); kpeter@761: LargeValue delta = _dist[v] + _length[e] * _curr_size - _curr_length; kpeter@761: if (_tolerance.less(delta, _dist[u])) { kpeter@760: _dist[u] = delta; kpeter@760: _policy[u] = e; kpeter@760: improved = true; kpeter@760: } kpeter@758: } kpeter@758: } kpeter@758: return improved; kpeter@758: } kpeter@758: kpeter@764: }; //class Howard kpeter@758: kpeter@758: ///@} kpeter@758: kpeter@758: } //namespace lemon kpeter@758: kpeter@764: #endif //LEMON_HOWARD_H