alpar@877: /* -*- mode: C++; indent-tabs-mode: nil; -*- kpeter@758: * alpar@877: * This file is a part of LEMON, a generic C++ optimization library. kpeter@758: * alpar@877: * Copyright (C) 2003-2010 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@864: #ifndef LEMON_HOWARD_MMC_H kpeter@864: #define LEMON_HOWARD_MMC_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@864: /// \brief Default traits class of HowardMmc class. kpeter@761: /// kpeter@864: /// Default traits class of HowardMmc class. kpeter@761: /// \tparam GR The type of the digraph. kpeter@864: /// \tparam CM The type of the cost map. kpeter@761: /// It must conform to the \ref concepts::ReadMap "ReadMap" concept. kpeter@761: #ifdef DOXYGEN kpeter@864: template kpeter@761: #else kpeter@864: template ::is_integer> kpeter@761: #endif kpeter@864: struct HowardMmcDefaultTraits kpeter@761: { kpeter@761: /// The type of the digraph kpeter@761: typedef GR Digraph; kpeter@864: /// The type of the cost map kpeter@864: typedef CM CostMap; kpeter@864: /// The type of the arc costs kpeter@864: typedef typename CostMap::Value Cost; kpeter@761: kpeter@864: /// \brief The large cost type used for internal computations kpeter@761: /// kpeter@864: /// The large cost type used for internal computations. kpeter@864: /// It is \c long \c long if the \c Cost type is integer, kpeter@761: /// otherwise it is \c double. kpeter@864: /// \c Cost must be convertible to \c LargeCost. kpeter@864: typedef double LargeCost; kpeter@761: kpeter@761: /// The tolerance type used for internal computations kpeter@864: 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@864: // Default traits class for integer cost types kpeter@864: template kpeter@864: struct HowardMmcDefaultTraits kpeter@761: { kpeter@761: typedef GR Digraph; kpeter@864: typedef CM CostMap; kpeter@864: typedef typename CostMap::Value Cost; kpeter@761: #ifdef LEMON_HAVE_LONG_LONG kpeter@864: typedef long long LargeCost; kpeter@761: #else kpeter@864: typedef long LargeCost; kpeter@761: #endif kpeter@864: 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@864: /// a directed cycle of minimum mean cost in a digraph kpeter@1002: /// \ref dasdan98minmeancycle, \ref dasdan04experimental. 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@864: /// \tparam CM The type of the cost map. The default kpeter@758: /// map type is \ref concepts::Digraph::ArcMap "GR::ArcMap". kpeter@825: /// \tparam TR The traits class that defines various types used by the kpeter@864: /// algorithm. By default, it is \ref HowardMmcDefaultTraits kpeter@864: /// "HowardMmcDefaultTraits". kpeter@825: /// In most cases, this parameter should not be set directly, kpeter@825: /// consider to use the named template parameters instead. kpeter@758: #ifdef DOXYGEN kpeter@864: template kpeter@758: #else kpeter@758: template < typename GR, kpeter@864: typename CM = typename GR::template ArcMap, kpeter@864: typename TR = HowardMmcDefaultTraits > kpeter@758: #endif kpeter@864: class HowardMmc kpeter@758: { kpeter@758: public: alpar@877: kpeter@761: /// The type of the digraph kpeter@761: typedef typename TR::Digraph Digraph; kpeter@864: /// The type of the cost map kpeter@864: typedef typename TR::CostMap CostMap; kpeter@864: /// The type of the arc costs kpeter@864: typedef typename TR::Cost Cost; kpeter@761: kpeter@864: /// \brief The large cost type kpeter@761: /// kpeter@864: /// The large cost type used for internal computations. kpeter@864: /// By default, it is \c long \c long if the \c Cost type is integer, kpeter@761: /// otherwise it is \c double. kpeter@864: typedef typename TR::LargeCost LargeCost; 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@864: /// Using the \ref HowardMmcDefaultTraits "default traits class", kpeter@761: /// it is \ref lemon::Path "Path". kpeter@761: typedef typename TR::Path Path; kpeter@761: kpeter@864: /// The \ref HowardMmcDefaultTraits "traits class" of the algorithm kpeter@761: typedef TR Traits; kpeter@758: kpeter@1012: /// \brief Constants for the causes of search termination. kpeter@1012: /// kpeter@1012: /// Enum type containing constants for the different causes of search kpeter@1012: /// termination. The \ref findCycleMean() function returns one of kpeter@1012: /// these values. kpeter@1012: enum TerminationCause { kpeter@1012: kpeter@1012: /// No directed cycle can be found in the digraph. kpeter@1012: NO_CYCLE = 0, kpeter@1012: kpeter@1012: /// Optimal solution (minimum cycle mean) is found. kpeter@1012: OPTIMAL = 1, kpeter@1012: kpeter@1012: /// The iteration count limit is reached. kpeter@1012: ITERATION_LIMIT kpeter@1012: }; kpeter@1012: kpeter@758: private: kpeter@758: kpeter@758: TEMPLATE_DIGRAPH_TYPEDEFS(Digraph); alpar@877: kpeter@758: // The digraph the algorithm runs on kpeter@758: const Digraph &_gr; kpeter@864: // The cost of the arcs kpeter@864: const CostMap &_cost; kpeter@758: kpeter@760: // Data for the found cycles kpeter@760: bool _curr_found, _best_found; kpeter@864: LargeCost _curr_cost, _best_cost; 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@864: 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; alpar@877: kpeter@760: // Queue used for BFS search kpeter@760: std::vector _queue; kpeter@760: int _qfront, _qback; kpeter@761: kpeter@761: Tolerance _tolerance; alpar@877: kpeter@767: // Infinite constant kpeter@864: const LargeCost INF; kpeter@767: kpeter@761: public: alpar@877: kpeter@761: /// \name Named Template Parameters kpeter@761: /// @{ kpeter@761: kpeter@761: template kpeter@864: struct SetLargeCostTraits : public Traits { kpeter@864: typedef T LargeCost; kpeter@761: typedef lemon::Tolerance Tolerance; kpeter@761: }; kpeter@761: kpeter@761: /// \brief \ref named-templ-param "Named parameter" for setting kpeter@864: /// \c LargeCost type. kpeter@761: /// kpeter@864: /// \ref named-templ-param "Named parameter" for setting \c LargeCost kpeter@761: /// type. It is used for internal computations in the algorithm. kpeter@761: template kpeter@864: struct SetLargeCost kpeter@864: : public HowardMmc > { kpeter@864: typedef HowardMmc > 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@864: : public HowardMmc > { kpeter@864: typedef HowardMmc > Create; kpeter@761: }; alpar@877: kpeter@761: /// @} kpeter@758: kpeter@863: protected: kpeter@863: kpeter@864: HowardMmc() {} kpeter@863: 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@864: /// \param cost The costs of the arcs. kpeter@864: HowardMmc( const Digraph &digraph, kpeter@864: const CostMap &cost ) : kpeter@864: _gr(digraph), _cost(cost), _best_found(false), kpeter@864: _best_cost(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@864: INF(std::numeric_limits::has_infinity ? kpeter@864: std::numeric_limits::infinity() : kpeter@864: std::numeric_limits::max()) kpeter@758: {} kpeter@758: kpeter@758: /// Destructor. kpeter@864: ~HowardMmc() { 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@1049: /// \ref findCycleMean(), a local \ref Path "path" structure kpeter@1049: /// will be allocated. 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@864: HowardMmc& 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@864: HowardMmc& 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@864: /// If you only need the minimum mean cost, you may call kpeter@864: /// \ref findCycleMean(). 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@864: /// and/or the arc costs 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@864: /// return mmc.findCycleMean() && mmc.findCycle(); kpeter@758: /// \endcode kpeter@758: bool run() { kpeter@864: return findCycleMean() && findCycle(); kpeter@758: } kpeter@758: kpeter@1012: /// \brief Find the minimum cycle mean (or an upper bound). kpeter@758: /// kpeter@864: /// This function finds the minimum mean cost of the directed kpeter@1012: /// cycles in the digraph (or an upper bound for it). kpeter@758: /// kpeter@1012: /// By default, the function finds the exact minimum cycle mean, kpeter@1012: /// but an optional limit can also be specified for the number of kpeter@1012: /// iterations performed during the search process. kpeter@1012: /// The return value indicates if the optimal solution is found kpeter@1012: /// or the iteration limit is reached. In the latter case, an kpeter@1012: /// approximate solution is provided, which corresponds to a directed kpeter@1012: /// cycle whose mean cost is relatively small, but not necessarily kpeter@1012: /// minimal. kpeter@1012: /// kpeter@1012: /// \param limit The maximum allowed number of iterations during kpeter@1012: /// the search process. Its default value implies that the algorithm kpeter@1012: /// runs until it finds the exact optimal solution. kpeter@1012: /// kpeter@1012: /// \return The termination cause of the search process. kpeter@1012: /// For more information, see \ref TerminationCause. kpeter@1012: TerminationCause findCycleMean(int limit = std::numeric_limits::max()) { kpeter@760: // Initialize and find strongly connected components kpeter@760: init(); kpeter@760: findComponents(); alpar@877: kpeter@759: // Find the minimum cycle mean in the components kpeter@1012: int iter_count = 0; kpeter@1012: bool iter_limit_reached = false; 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@1012: if (++iter_count > limit) { kpeter@1012: iter_limit_reached = true; kpeter@1012: break; kpeter@1012: } kpeter@760: findPolicyCycle(); kpeter@758: if (!computeNodeDistances()) break; kpeter@758: } kpeter@1012: kpeter@760: // Update the best cycle (global minimum mean cycle) kpeter@767: if ( _curr_found && (!_best_found || kpeter@864: _curr_cost * _best_size < _best_cost * _curr_size) ) { kpeter@760: _best_found = true; kpeter@864: _best_cost = _curr_cost; kpeter@760: _best_size = _curr_size; kpeter@760: _best_node = _curr_node; kpeter@760: } kpeter@1012: kpeter@1012: if (iter_limit_reached) break; kpeter@758: } kpeter@1012: kpeter@1012: if (iter_limit_reached) { kpeter@1012: return ITERATION_LIMIT; kpeter@1012: } else { kpeter@1012: return _best_found ? OPTIMAL : NO_CYCLE; kpeter@1012: } kpeter@758: } kpeter@758: kpeter@758: /// \brief Find a minimum mean directed cycle. kpeter@758: /// kpeter@864: /// This function finds a directed cycle of minimum mean cost kpeter@864: /// in the digraph using the data computed by findCycleMean(). kpeter@758: /// kpeter@758: /// \return \c true if a directed cycle exists in the digraph. kpeter@758: /// kpeter@864: /// \pre \ref findCycleMean() 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@864: /// \brief Return the total cost of the found cycle. kpeter@758: /// kpeter@864: /// This function returns the total cost of the found cycle. kpeter@758: /// kpeter@864: /// \pre \ref run() or \ref findCycleMean() must be called before kpeter@758: /// using this function. kpeter@864: Cost cycleCost() const { kpeter@864: return static_cast(_best_cost); 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@864: /// \pre \ref run() or \ref findCycleMean() must be called before kpeter@758: /// using this function. kpeter@864: int cycleSize() const { kpeter@760: return _best_size; kpeter@758: } kpeter@758: kpeter@864: /// \brief Return the mean cost of the found cycle. kpeter@758: /// kpeter@864: /// This function returns the mean cost 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@864: /// return static_cast(alg.cycleCost()) / alg.cycleSize(); kpeter@758: /// \endcode kpeter@758: /// kpeter@864: /// \pre \ref run() or \ref findCycleMean() must be called before kpeter@758: /// using this function. kpeter@758: double cycleMean() const { kpeter@864: return static_cast(_best_cost) / _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@864: _best_cost = 0; kpeter@760: _best_size = 1; kpeter@760: _cycle_path->clear(); kpeter@760: } alpar@877: 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@864: if (_cost[e] < _dist[u]) { kpeter@864: _dist[u] = _cost[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@864: LargeCost ccost; 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@864: ccost = _cost[_policy[u]]; kpeter@760: csize = 1; kpeter@760: for (v = u; (v = _gr.target(_policy[v])) != u; ) { kpeter@864: ccost += _cost[_policy[v]]; kpeter@760: ++csize; kpeter@758: } kpeter@760: if ( !_curr_found || kpeter@864: (ccost * _curr_size < _curr_cost * csize) ) { kpeter@760: _curr_found = true; kpeter@864: _curr_cost = ccost; 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@864: _dist[u] = _dist[v] + _cost[e] * _curr_size - _curr_cost; 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@864: _dist[u] = _dist[v] + _cost[e] * _curr_size - _curr_cost; 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@864: LargeCost delta = _dist[v] + _cost[e] * _curr_size - _curr_cost; 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@864: }; //class HowardMmc kpeter@758: kpeter@758: ///@} kpeter@758: kpeter@758: } //namespace lemon kpeter@758: kpeter@864: #endif //LEMON_HOWARD_MMC_H