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@758: #ifndef LEMON_MIN_MEAN_CYCLE_H kpeter@758: #define LEMON_MIN_MEAN_CYCLE_H kpeter@758: kpeter@758: /// \ingroup shortest_path kpeter@758: /// kpeter@758: /// \file kpeter@758: /// \brief Howard's algorithm for finding a minimum mean cycle. kpeter@758: kpeter@758: #include kpeter@758: #include kpeter@758: #include kpeter@758: #include kpeter@758: #include kpeter@758: kpeter@758: namespace lemon { kpeter@758: kpeter@758: /// \addtogroup shortest_path kpeter@758: /// @{ kpeter@758: kpeter@758: /// \brief Implementation of Howard's algorithm for finding a minimum kpeter@758: /// mean cycle. kpeter@758: /// kpeter@758: /// \ref MinMeanCycle implements Howard's algorithm for finding a kpeter@758: /// directed cycle of minimum mean length (cost) in a digraph. 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: /// kpeter@758: /// \warning \c LEN::Value must be convertible to \c double. kpeter@758: #ifdef DOXYGEN kpeter@758: template kpeter@758: #else kpeter@758: template < typename GR, kpeter@758: typename LEN = typename GR::template ArcMap > kpeter@758: #endif kpeter@758: class MinMeanCycle kpeter@758: { kpeter@758: public: kpeter@758: kpeter@758: /// The type of the digraph the algorithm runs on kpeter@758: typedef GR Digraph; kpeter@758: /// The type of the length map kpeter@758: typedef LEN LengthMap; kpeter@758: /// The type of the arc lengths kpeter@758: typedef typename LengthMap::Value Value; kpeter@758: /// The type of the paths kpeter@758: typedef lemon::Path Path; 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@758: // The total length of the found cycle kpeter@758: Value _cycle_length; kpeter@758: // The number of arcs on the found cycle kpeter@758: int _cycle_size; kpeter@758: // The found cycle kpeter@758: Path *_cycle_path; kpeter@758: kpeter@758: bool _local_path; kpeter@758: bool _cycle_found; kpeter@758: Node _cycle_node; kpeter@758: kpeter@758: typename Digraph::template NodeMap _reached; kpeter@758: typename Digraph::template NodeMap _dist; kpeter@758: typename Digraph::template NodeMap _policy; kpeter@758: kpeter@758: typename Digraph::template NodeMap _comp; kpeter@758: int _comp_num; kpeter@758: kpeter@758: std::vector _nodes; kpeter@758: std::vector _arcs; kpeter@758: Tolerance _tol; 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@758: MinMeanCycle( const Digraph &digraph, kpeter@758: const LengthMap &length ) : kpeter@758: _gr(digraph), _length(length), _cycle_length(0), _cycle_size(-1), kpeter@758: _cycle_path(NULL), _local_path(false), _reached(digraph), kpeter@758: _dist(digraph), _policy(digraph), _comp(digraph) kpeter@758: {} kpeter@758: kpeter@758: /// Destructor. kpeter@758: ~MinMeanCycle() { 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@758: /// \ref init(), 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@758: /// kpeter@758: /// \sa cycle() kpeter@758: MinMeanCycle& cyclePath(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@758: /// \name Execution control kpeter@758: /// The simplest way to execute the algorithm is to call the \ref run() kpeter@758: /// function.\n kpeter@758: /// If you only need the minimum mean length, you may call \ref init() kpeter@758: /// and \ref findMinMean(). kpeter@758: /// If you would like to run the algorithm again (e.g. the underlying kpeter@758: /// digraph and/or the arc lengths has been modified), you may not kpeter@758: /// create a new instance of the class, rather call \ref reset(), kpeter@758: /// \ref findMinMean() and \ref findCycle() instead. kpeter@758: kpeter@758: /// @{ kpeter@758: kpeter@758: /// \brief Run the algorithm. kpeter@758: /// kpeter@758: /// This function runs the algorithm. kpeter@758: /// kpeter@758: /// \return \c true if a directed cycle exists in the digraph. kpeter@758: /// kpeter@758: /// \note Apart from the return value, mmc.run() is just a kpeter@758: /// shortcut of the following code. kpeter@758: /// \code kpeter@758: /// mmc.init(); kpeter@758: /// mmc.findMinMean(); kpeter@758: /// mmc.findCycle(); kpeter@758: /// \endcode kpeter@758: bool run() { kpeter@758: init(); kpeter@758: return findMinMean() && findCycle(); kpeter@758: } kpeter@758: kpeter@758: /// \brief Initialize the internal data structures. kpeter@758: /// kpeter@758: /// This function initializes the internal data structures. kpeter@758: /// kpeter@758: /// \sa reset() kpeter@758: void init() { kpeter@758: _tol.epsilon(1e-6); kpeter@758: if (!_cycle_path) { kpeter@758: _local_path = true; kpeter@758: _cycle_path = new Path; kpeter@758: } kpeter@758: _cycle_found = false; kpeter@758: _comp_num = stronglyConnectedComponents(_gr, _comp); kpeter@758: } kpeter@758: kpeter@758: /// \brief Reset the internal data structures. kpeter@758: /// kpeter@758: /// This function resets the internal data structures so that kpeter@758: /// findMinMean() and findCycle() can be called again (e.g. when the kpeter@758: /// underlying digraph and/or the arc lengths has been modified). kpeter@758: /// kpeter@758: /// \sa init() kpeter@758: void reset() { kpeter@758: if (_cycle_path) _cycle_path->clear(); kpeter@758: _cycle_found = false; kpeter@758: _comp_num = stronglyConnectedComponents(_gr, _comp); kpeter@758: } kpeter@758: kpeter@758: /// \brief Find the minimum cycle mean. kpeter@758: /// kpeter@758: /// This function computes all the required data and finds the kpeter@758: /// minimum mean length of the directed cycles in the digraph. kpeter@758: /// kpeter@758: /// \return \c true if a directed cycle exists in the digraph. kpeter@758: /// kpeter@758: /// \pre \ref init() must be called before using this function. kpeter@758: bool findMinMean() { kpeter@758: // Find the minimum cycle mean in the components kpeter@758: for (int comp = 0; comp < _comp_num; ++comp) { kpeter@758: if (!initCurrentComponent(comp)) continue; kpeter@758: while (true) { kpeter@758: if (!findPolicyCycles()) break; kpeter@758: contractPolicyGraph(comp); kpeter@758: if (!computeNodeDistances()) break; kpeter@758: } kpeter@758: } kpeter@758: return _cycle_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@758: /// \pre \ref init() and \ref findMinMean() must be called before kpeter@758: /// using this function. kpeter@758: bool findCycle() { kpeter@758: if (!_cycle_found) return false; kpeter@758: _cycle_path->addBack(_policy[_cycle_node]); kpeter@758: for ( Node v = _cycle_node; kpeter@758: (v = _gr.target(_policy[v])) != _cycle_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@758: /// The result 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@758: /// \pre \ref run() or \ref findCycle() must be called before kpeter@758: /// using this function. kpeter@758: Value cycleLength() const { kpeter@758: return _cycle_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@758: /// \pre \ref run() or \ref findCycle() must be called before kpeter@758: /// using this function. kpeter@758: int cycleArcNum() const { kpeter@758: return _cycle_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@758: /// \note mmc.cycleMean() is just a shortcut of the kpeter@758: /// following code. kpeter@758: /// \code kpeter@758: /// return double(mmc.cycleLength()) / mmc.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@758: return double(_cycle_length) / _cycle_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: /// kpeter@758: /// \sa cyclePath() 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@758: // Initialize the internal data structures for the current strongly kpeter@758: // connected component and create the policy graph. kpeter@758: // The policy graph can be represented by the _policy map because kpeter@758: // the out-degree of every node is 1. kpeter@758: bool initCurrentComponent(int comp) { kpeter@758: // Find the nodes of the current component kpeter@758: _nodes.clear(); kpeter@758: for (NodeIt n(_gr); n != INVALID; ++n) { kpeter@758: if (_comp[n] == comp) _nodes.push_back(n); kpeter@758: } kpeter@758: if (_nodes.size() <= 1) return false; kpeter@758: // Find the arcs of the current component kpeter@758: _arcs.clear(); kpeter@758: for (ArcIt e(_gr); e != INVALID; ++e) { kpeter@758: if ( _comp[_gr.source(e)] == comp && kpeter@758: _comp[_gr.target(e)] == comp ) kpeter@758: _arcs.push_back(e); kpeter@758: } kpeter@758: // Initialize _reached, _dist, _policy maps kpeter@758: for (int i = 0; i < int(_nodes.size()); ++i) { kpeter@758: _reached[_nodes[i]] = false; kpeter@758: _policy[_nodes[i]] = INVALID; kpeter@758: } kpeter@758: Node u; Arc e; kpeter@758: for (int j = 0; j < int(_arcs.size()); ++j) { kpeter@758: e = _arcs[j]; kpeter@758: u = _gr.source(e); kpeter@758: if (!_reached[u] || _length[e] < _dist[u]) { kpeter@758: _dist[u] = _length[e]; kpeter@758: _policy[u] = e; kpeter@758: _reached[u] = true; kpeter@758: } kpeter@758: } kpeter@758: return true; kpeter@758: } kpeter@758: kpeter@758: // Find all cycles in the policy graph. kpeter@758: // Set _cycle_found to true if a cycle is found and set kpeter@758: // _cycle_length, _cycle_size, _cycle_node to represent the minimum kpeter@758: // mean cycle in the policy graph. kpeter@758: bool findPolicyCycles() { kpeter@758: typename Digraph::template NodeMap level(_gr, -1); kpeter@758: bool curr_cycle_found = false; kpeter@758: Value clength; kpeter@758: int csize; kpeter@758: int path_cnt = 0; kpeter@758: Node u, v; kpeter@758: // Searching for cycles kpeter@758: for (int i = 0; i < int(_nodes.size()); ++i) { kpeter@758: if (level[_nodes[i]] < 0) { kpeter@758: u = _nodes[i]; kpeter@758: level[u] = path_cnt; kpeter@758: while (level[u = _gr.target(_policy[u])] < 0) kpeter@758: level[u] = path_cnt; kpeter@758: if (level[u] == path_cnt) { kpeter@758: // A cycle is found kpeter@758: curr_cycle_found = true; kpeter@758: clength = _length[_policy[u]]; kpeter@758: csize = 1; kpeter@758: for (v = u; (v = _gr.target(_policy[v])) != u; ) { kpeter@758: clength += _length[_policy[v]]; kpeter@758: ++csize; kpeter@758: } kpeter@758: if ( !_cycle_found || kpeter@758: clength * _cycle_size < _cycle_length * csize ) { kpeter@758: _cycle_found = true; kpeter@758: _cycle_length = clength; kpeter@758: _cycle_size = csize; kpeter@758: _cycle_node = u; kpeter@758: } kpeter@758: } kpeter@758: ++path_cnt; kpeter@758: } kpeter@758: } kpeter@758: return curr_cycle_found; kpeter@758: } kpeter@758: kpeter@758: // Contract the policy graph to be connected by cutting all cycles kpeter@758: // except for the main cycle (i.e. the minimum mean cycle). kpeter@758: void contractPolicyGraph(int comp) { kpeter@758: // Find the component of the main cycle using reverse BFS search kpeter@758: typename Digraph::template NodeMap found(_gr, false); kpeter@758: std::deque queue; kpeter@758: queue.push_back(_cycle_node); kpeter@758: found[_cycle_node] = true; kpeter@758: Node u, v; kpeter@758: while (!queue.empty()) { kpeter@758: v = queue.front(); queue.pop_front(); kpeter@758: for (InArcIt e(_gr, v); e != INVALID; ++e) { kpeter@758: u = _gr.source(e); kpeter@758: if (_policy[u] == e && !found[u]) { kpeter@758: found[u] = true; kpeter@758: queue.push_back(u); kpeter@758: } kpeter@758: } kpeter@758: } kpeter@758: // Connect all other nodes to this component using reverse BFS search kpeter@758: queue.clear(); kpeter@758: for (int i = 0; i < int(_nodes.size()); ++i) kpeter@758: if (found[_nodes[i]]) queue.push_back(_nodes[i]); kpeter@758: int found_cnt = queue.size(); kpeter@758: while (found_cnt < int(_nodes.size())) { kpeter@758: v = queue.front(); queue.pop_front(); kpeter@758: for (InArcIt e(_gr, v); e != INVALID; ++e) { kpeter@758: u = _gr.source(e); kpeter@758: if (_comp[u] == comp && !found[u]) { kpeter@758: found[u] = true; kpeter@758: ++found_cnt; kpeter@758: _policy[u] = e; kpeter@758: queue.push_back(u); kpeter@758: } kpeter@758: } kpeter@758: } kpeter@758: } kpeter@758: kpeter@758: // Compute node distances in the policy graph and update the kpeter@758: // policy graph if the node distances can be improved. kpeter@758: bool computeNodeDistances() { kpeter@758: // Compute node distances using reverse BFS search kpeter@758: double cycle_mean = double(_cycle_length) / _cycle_size; kpeter@758: typename Digraph::template NodeMap found(_gr, false); kpeter@758: std::deque queue; kpeter@758: queue.push_back(_cycle_node); kpeter@758: found[_cycle_node] = true; kpeter@758: _dist[_cycle_node] = 0; kpeter@758: Node u, v; kpeter@758: while (!queue.empty()) { kpeter@758: v = queue.front(); queue.pop_front(); kpeter@758: for (InArcIt e(_gr, v); e != INVALID; ++e) { kpeter@758: u = _gr.source(e); kpeter@758: if (_policy[u] == e && !found[u]) { kpeter@758: found[u] = true; kpeter@758: _dist[u] = _dist[v] + _length[e] - cycle_mean; kpeter@758: queue.push_back(u); kpeter@758: } kpeter@758: } kpeter@758: } kpeter@758: // Improving node distances kpeter@758: bool improved = false; kpeter@758: for (int j = 0; j < int(_arcs.size()); ++j) { kpeter@758: Arc e = _arcs[j]; kpeter@758: u = _gr.source(e); v = _gr.target(e); kpeter@758: double delta = _dist[v] + _length[e] - cycle_mean; kpeter@758: if (_tol.less(delta, _dist[u])) { kpeter@758: improved = true; kpeter@758: _dist[u] = delta; kpeter@758: _policy[u] = e; kpeter@758: } kpeter@758: } kpeter@758: return improved; kpeter@758: } kpeter@758: kpeter@758: }; //class MinMeanCycle kpeter@758: kpeter@758: ///@} kpeter@758: kpeter@758: } //namespace lemon kpeter@758: kpeter@758: #endif //LEMON_MIN_MEAN_CYCLE_H