[2409] | 1 | /* -*- C++ -*- |
---|
| 2 | * |
---|
| 3 | * This file is a part of LEMON, a generic C++ optimization library |
---|
| 4 | * |
---|
[2553] | 5 | * Copyright (C) 2003-2008 |
---|
[2409] | 6 | * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
---|
| 7 | * (Egervary Research Group on Combinatorial Optimization, EGRES). |
---|
| 8 | * |
---|
| 9 | * Permission to use, modify and distribute this software is granted |
---|
| 10 | * provided that this copyright notice appears in all copies. For |
---|
| 11 | * precise terms see the accompanying LICENSE file. |
---|
| 12 | * |
---|
| 13 | * This software is provided "AS IS" with no warranty of any kind, |
---|
| 14 | * express or implied, and with no claim as to its suitability for any |
---|
| 15 | * purpose. |
---|
| 16 | * |
---|
| 17 | */ |
---|
| 18 | |
---|
| 19 | #ifndef LEMON_MIN_MEAN_CYCLE_H |
---|
| 20 | #define LEMON_MIN_MEAN_CYCLE_H |
---|
| 21 | |
---|
[2529] | 22 | /// \ingroup shortest_path |
---|
[2409] | 23 | /// |
---|
[2437] | 24 | /// \file |
---|
[2555] | 25 | /// \brief Howard's algorithm for finding a minimum mean directed cycle. |
---|
[2409] | 26 | |
---|
[2509] | 27 | #include <vector> |
---|
[2409] | 28 | #include <lemon/graph_utils.h> |
---|
| 29 | #include <lemon/path.h> |
---|
[2555] | 30 | #include <lemon/tolerance.h> |
---|
[2583] | 31 | #include <lemon/topology.h> |
---|
[2409] | 32 | |
---|
| 33 | namespace lemon { |
---|
| 34 | |
---|
[2529] | 35 | /// \addtogroup shortest_path |
---|
[2409] | 36 | /// @{ |
---|
| 37 | |
---|
[2555] | 38 | /// \brief Implementation of Howard's algorithm for finding a minimum |
---|
| 39 | /// mean directed cycle. |
---|
[2409] | 40 | /// |
---|
[2555] | 41 | /// \ref MinMeanCycle implements Howard's algorithm for finding a |
---|
| 42 | /// minimum mean directed cycle. |
---|
[2409] | 43 | /// |
---|
[2583] | 44 | /// \tparam Graph The directed graph type the algorithm runs on. |
---|
| 45 | /// \tparam LengthMap The type of the length (cost) map. |
---|
[2409] | 46 | /// |
---|
[2555] | 47 | /// \warning \c LengthMap::Value must be convertible to \c double. |
---|
| 48 | /// |
---|
[2409] | 49 | /// \author Peter Kovacs |
---|
| 50 | |
---|
[2555] | 51 | template < typename Graph, |
---|
| 52 | typename LengthMap = typename Graph::template EdgeMap<int> > |
---|
[2409] | 53 | class MinMeanCycle |
---|
| 54 | { |
---|
[2555] | 55 | GRAPH_TYPEDEFS(typename Graph); |
---|
[2409] | 56 | |
---|
| 57 | typedef typename LengthMap::Value Length; |
---|
[2618] | 58 | typedef lemon::Path<Graph> Path; |
---|
[2409] | 59 | |
---|
[2583] | 60 | private: |
---|
[2413] | 61 | |
---|
[2583] | 62 | // The directed graph the algorithm runs on |
---|
[2555] | 63 | const Graph &_graph; |
---|
[2583] | 64 | // The length of the edges |
---|
[2555] | 65 | const LengthMap &_length; |
---|
[2413] | 66 | |
---|
[2583] | 67 | // The total length of the found cycle |
---|
[2555] | 68 | Length _cycle_length; |
---|
[2583] | 69 | // The number of edges on the found cycle |
---|
[2555] | 70 | int _cycle_size; |
---|
[2583] | 71 | // The found cycle |
---|
[2555] | 72 | Path *_cycle_path; |
---|
[2413] | 73 | |
---|
[2555] | 74 | bool _local_path; |
---|
| 75 | bool _cycle_found; |
---|
| 76 | Node _cycle_node; |
---|
[2409] | 77 | |
---|
[2583] | 78 | typename Graph::template NodeMap<bool> _reached; |
---|
| 79 | typename Graph::template NodeMap<double> _dist; |
---|
| 80 | typename Graph::template NodeMap<Edge> _policy; |
---|
| 81 | |
---|
[2555] | 82 | typename Graph::template NodeMap<int> _component; |
---|
| 83 | int _component_num; |
---|
[2437] | 84 | |
---|
[2555] | 85 | std::vector<Node> _nodes; |
---|
| 86 | std::vector<Edge> _edges; |
---|
| 87 | Tolerance<double> _tolerance; |
---|
[2437] | 88 | |
---|
[2555] | 89 | public: |
---|
[2409] | 90 | |
---|
| 91 | /// \brief The constructor of the class. |
---|
| 92 | /// |
---|
| 93 | /// The constructor of the class. |
---|
| 94 | /// |
---|
[2555] | 95 | /// \param graph The directed graph the algorithm runs on. |
---|
| 96 | /// \param length The length (cost) of the edges. |
---|
| 97 | MinMeanCycle( const Graph &graph, |
---|
| 98 | const LengthMap &length ) : |
---|
[2583] | 99 | _graph(graph), _length(length), _cycle_length(0), _cycle_size(-1), |
---|
| 100 | _cycle_path(NULL), _local_path(false), _reached(graph), |
---|
| 101 | _dist(graph), _policy(graph), _component(graph) |
---|
| 102 | {} |
---|
[2409] | 103 | |
---|
[2555] | 104 | /// The destructor of the class. |
---|
[2437] | 105 | ~MinMeanCycle() { |
---|
[2555] | 106 | if (_local_path) delete _cycle_path; |
---|
[2409] | 107 | } |
---|
| 108 | |
---|
[2583] | 109 | /// \brief Sets the \ref Path "path" structure for storing the found |
---|
| 110 | /// cycle. |
---|
| 111 | /// |
---|
| 112 | /// Sets an external \ref Path "path" structure for storing the |
---|
| 113 | /// found cycle. |
---|
| 114 | /// |
---|
| 115 | /// If you don't call this function before calling \ref run() or |
---|
| 116 | /// \ref init(), it will allocate a local \ref Path "path" |
---|
| 117 | /// structure. |
---|
| 118 | /// The destuctor deallocates this automatically allocated map, |
---|
| 119 | /// of course. |
---|
| 120 | /// |
---|
| 121 | /// \note The algorithm calls only the \ref lemon::Path::addBack() |
---|
| 122 | /// "addBack()" function of the given \ref Path "path" structure. |
---|
| 123 | /// |
---|
| 124 | /// \return <tt>(*this)</tt> |
---|
| 125 | /// |
---|
| 126 | /// \sa cycle() |
---|
| 127 | MinMeanCycle& cyclePath(Path &path) { |
---|
| 128 | if (_local_path) { |
---|
| 129 | delete _cycle_path; |
---|
| 130 | _local_path = false; |
---|
[2409] | 131 | } |
---|
[2583] | 132 | _cycle_path = &path; |
---|
| 133 | return *this; |
---|
[2555] | 134 | } |
---|
| 135 | |
---|
[2583] | 136 | /// \name Execution control |
---|
[2588] | 137 | /// The simplest way to execute the algorithm is to call the run() |
---|
| 138 | /// function. |
---|
[2583] | 139 | /// \n |
---|
| 140 | /// If you only need the minimum mean value, you may call init() |
---|
| 141 | /// and findMinMean(). |
---|
| 142 | /// \n |
---|
| 143 | /// If you would like to run the algorithm again (e.g. the |
---|
| 144 | /// underlaying graph and/or the edge costs were modified), you may |
---|
| 145 | /// not create a new instance of the class, rather call reset(), |
---|
| 146 | /// findMinMean(), and findCycle() instead. |
---|
[2555] | 147 | |
---|
[2583] | 148 | /// @{ |
---|
[2437] | 149 | |
---|
[2409] | 150 | /// \brief Runs the algorithm. |
---|
| 151 | /// |
---|
| 152 | /// Runs the algorithm. |
---|
| 153 | /// |
---|
[2555] | 154 | /// \return Returns \c true if a directed cycle exists in the graph. |
---|
[2413] | 155 | /// |
---|
[2555] | 156 | /// \note Apart from the return value, <tt>mmc.run()</tt> is just a |
---|
[2517] | 157 | /// shortcut of the following code. |
---|
[2413] | 158 | /// \code |
---|
[2555] | 159 | /// mmc.init(); |
---|
| 160 | /// mmc.findMinMean(); |
---|
| 161 | /// mmc.findCycle(); |
---|
[2413] | 162 | /// \endcode |
---|
[2409] | 163 | bool run() { |
---|
| 164 | init(); |
---|
[2555] | 165 | return findMinMean() && findCycle(); |
---|
[2413] | 166 | } |
---|
[2437] | 167 | |
---|
[2413] | 168 | /// \brief Initializes the internal data structures. |
---|
[2517] | 169 | /// |
---|
| 170 | /// Initializes the internal data structures. |
---|
| 171 | /// |
---|
| 172 | /// \sa reset() |
---|
[2413] | 173 | void init() { |
---|
[2583] | 174 | _tolerance.epsilon(1e-6); |
---|
[2555] | 175 | if (!_cycle_path) { |
---|
| 176 | _local_path = true; |
---|
| 177 | _cycle_path = new Path; |
---|
[2413] | 178 | } |
---|
[2555] | 179 | _cycle_found = false; |
---|
| 180 | _component_num = stronglyConnectedComponents(_graph, _component); |
---|
[2413] | 181 | } |
---|
| 182 | |
---|
| 183 | /// \brief Resets the internal data structures. |
---|
| 184 | /// |
---|
[2437] | 185 | /// Resets the internal data structures so that \ref findMinMean() |
---|
| 186 | /// and \ref findCycle() can be called again (e.g. when the |
---|
[2413] | 187 | /// underlaying graph has been modified). |
---|
[2517] | 188 | /// |
---|
| 189 | /// \sa init() |
---|
[2413] | 190 | void reset() { |
---|
[2555] | 191 | if (_cycle_path) _cycle_path->clear(); |
---|
| 192 | _cycle_found = false; |
---|
| 193 | _component_num = stronglyConnectedComponents(_graph, _component); |
---|
| 194 | } |
---|
| 195 | |
---|
| 196 | /// \brief Finds the minimum cycle mean length in the graph. |
---|
| 197 | /// |
---|
| 198 | /// Computes all the required data and finds the minimum cycle mean |
---|
| 199 | /// length in the graph. |
---|
| 200 | /// |
---|
| 201 | /// \return Returns \c true if a directed cycle exists in the graph. |
---|
| 202 | /// |
---|
| 203 | /// \pre \ref init() must be called before using this function. |
---|
| 204 | bool findMinMean() { |
---|
| 205 | // Finding the minimum mean cycle in the components |
---|
| 206 | for (int comp = 0; comp < _component_num; ++comp) { |
---|
| 207 | if (!initCurrentComponent(comp)) continue; |
---|
| 208 | while (true) { |
---|
[2583] | 209 | if (!findPolicyCycles()) break; |
---|
[2555] | 210 | contractPolicyGraph(comp); |
---|
[2583] | 211 | if (!computeNodeDistances(comp)) break; |
---|
[2555] | 212 | } |
---|
| 213 | } |
---|
[2583] | 214 | return _cycle_found; |
---|
[2555] | 215 | } |
---|
| 216 | |
---|
| 217 | /// \brief Finds a critical (minimum mean) directed cycle. |
---|
| 218 | /// |
---|
| 219 | /// Finds a critical (minimum mean) directed cycle using the data |
---|
| 220 | /// computed in the \ref findMinMean() function. |
---|
| 221 | /// |
---|
| 222 | /// \return Returns \c true if a directed cycle exists in the graph. |
---|
| 223 | /// |
---|
| 224 | /// \pre \ref init() and \ref findMinMean() must be called before |
---|
| 225 | /// using this function. |
---|
| 226 | bool findCycle() { |
---|
| 227 | if (!_cycle_found) return false; |
---|
| 228 | _cycle_path->addBack(_policy[_cycle_node]); |
---|
| 229 | for ( Node v = _cycle_node; |
---|
| 230 | (v = _graph.target(_policy[v])) != _cycle_node; ) { |
---|
| 231 | _cycle_path->addBack(_policy[v]); |
---|
| 232 | } |
---|
| 233 | return true; |
---|
[2413] | 234 | } |
---|
[2583] | 235 | |
---|
| 236 | /// @} |
---|
[2437] | 237 | |
---|
[2583] | 238 | /// \name Query Functions |
---|
| 239 | /// The result of the algorithm can be obtained using these |
---|
| 240 | /// functions. |
---|
[2588] | 241 | /// \n The algorithm should be executed before using them. |
---|
[2583] | 242 | |
---|
| 243 | /// @{ |
---|
| 244 | |
---|
[2413] | 245 | /// \brief Returns the total length of the found cycle. |
---|
| 246 | /// |
---|
| 247 | /// Returns the total length of the found cycle. |
---|
[2409] | 248 | /// |
---|
[2555] | 249 | /// \pre \ref run() or \ref findMinMean() must be called before |
---|
| 250 | /// using this function. |
---|
[2437] | 251 | Length cycleLength() const { |
---|
[2555] | 252 | return _cycle_length; |
---|
[2409] | 253 | } |
---|
[2437] | 254 | |
---|
[2555] | 255 | /// \brief Returns the number of edges on the found cycle. |
---|
[2409] | 256 | /// |
---|
[2555] | 257 | /// Returns the number of edges on the found cycle. |
---|
[2409] | 258 | /// |
---|
[2555] | 259 | /// \pre \ref run() or \ref findMinMean() must be called before |
---|
| 260 | /// using this function. |
---|
[2437] | 261 | int cycleEdgeNum() const { |
---|
[2555] | 262 | return _cycle_size; |
---|
[2409] | 263 | } |
---|
[2437] | 264 | |
---|
[2413] | 265 | /// \brief Returns the mean length of the found cycle. |
---|
[2409] | 266 | /// |
---|
[2413] | 267 | /// Returns the mean length of the found cycle. |
---|
[2409] | 268 | /// |
---|
[2517] | 269 | /// \pre \ref run() or \ref findMinMean() must be called before |
---|
| 270 | /// using this function. |
---|
[2409] | 271 | /// |
---|
[2555] | 272 | /// \note <tt>mmc.cycleMean()</tt> is just a shortcut of the |
---|
| 273 | /// following code. |
---|
[2413] | 274 | /// \code |
---|
[2555] | 275 | /// return double(mmc.cycleLength()) / mmc.cycleEdgeNum(); |
---|
[2413] | 276 | /// \endcode |
---|
[2555] | 277 | double cycleMean() const { |
---|
[2583] | 278 | return double(_cycle_length) / _cycle_size; |
---|
[2409] | 279 | } |
---|
| 280 | |
---|
[2555] | 281 | /// \brief Returns a const reference to the \ref Path "path" |
---|
| 282 | /// structure storing the found cycle. |
---|
[2409] | 283 | /// |
---|
[2555] | 284 | /// Returns a const reference to the \ref Path "path" |
---|
| 285 | /// structure storing the found cycle. |
---|
[2409] | 286 | /// |
---|
[2555] | 287 | /// \pre \ref run() or \ref findCycle() must be called before using |
---|
| 288 | /// this function. |
---|
[2409] | 289 | /// |
---|
[2555] | 290 | /// \sa cyclePath() |
---|
[2437] | 291 | const Path& cycle() const { |
---|
[2555] | 292 | return *_cycle_path; |
---|
[2409] | 293 | } |
---|
[2583] | 294 | |
---|
| 295 | ///@} |
---|
| 296 | |
---|
| 297 | private: |
---|
[2437] | 298 | |
---|
[2583] | 299 | // Initializes the internal data structures for the current strongly |
---|
| 300 | // connected component and creating the policy graph. |
---|
| 301 | // The policy graph can be represented by the _policy map because |
---|
| 302 | // the out degree of every node is 1. |
---|
| 303 | bool initCurrentComponent(int comp) { |
---|
| 304 | // Finding the nodes of the current component |
---|
| 305 | _nodes.clear(); |
---|
| 306 | for (NodeIt n(_graph); n != INVALID; ++n) { |
---|
| 307 | if (_component[n] == comp) _nodes.push_back(n); |
---|
[2409] | 308 | } |
---|
[2583] | 309 | if (_nodes.size() <= 1) return false; |
---|
| 310 | // Finding the edges of the current component |
---|
| 311 | _edges.clear(); |
---|
| 312 | for (EdgeIt e(_graph); e != INVALID; ++e) { |
---|
| 313 | if ( _component[_graph.source(e)] == comp && |
---|
| 314 | _component[_graph.target(e)] == comp ) |
---|
| 315 | _edges.push_back(e); |
---|
| 316 | } |
---|
| 317 | // Initializing _reached, _dist, _policy maps |
---|
| 318 | for (int i = 0; i < int(_nodes.size()); ++i) { |
---|
| 319 | _reached[_nodes[i]] = false; |
---|
| 320 | _policy[_nodes[i]] = INVALID; |
---|
| 321 | } |
---|
| 322 | Node u; Edge e; |
---|
| 323 | for (int j = 0; j < int(_edges.size()); ++j) { |
---|
| 324 | e = _edges[j]; |
---|
| 325 | u = _graph.source(e); |
---|
| 326 | if (!_reached[u] || _length[e] < _dist[u]) { |
---|
| 327 | _dist[u] = _length[e]; |
---|
| 328 | _policy[u] = e; |
---|
| 329 | _reached[u] = true; |
---|
| 330 | } |
---|
| 331 | } |
---|
| 332 | return true; |
---|
| 333 | } |
---|
| 334 | |
---|
| 335 | // Finds all cycles in the policy graph. |
---|
| 336 | // Sets _cycle_found to true if a cycle is found and sets |
---|
| 337 | // _cycle_length, _cycle_size, _cycle_node to represent the minimum |
---|
| 338 | // mean cycle in the policy graph. |
---|
| 339 | bool findPolicyCycles() { |
---|
| 340 | typename Graph::template NodeMap<int> level(_graph, -1); |
---|
| 341 | bool curr_cycle_found = false; |
---|
| 342 | Length clength; |
---|
| 343 | int csize; |
---|
| 344 | int path_cnt = 0; |
---|
| 345 | Node u, v; |
---|
| 346 | // Searching for cycles |
---|
| 347 | for (int i = 0; i < int(_nodes.size()); ++i) { |
---|
| 348 | if (level[_nodes[i]] < 0) { |
---|
| 349 | u = _nodes[i]; |
---|
| 350 | level[u] = path_cnt; |
---|
| 351 | while (level[u = _graph.target(_policy[u])] < 0) |
---|
| 352 | level[u] = path_cnt; |
---|
| 353 | if (level[u] == path_cnt) { |
---|
| 354 | // A cycle is found |
---|
| 355 | curr_cycle_found = true; |
---|
| 356 | clength = _length[_policy[u]]; |
---|
| 357 | csize = 1; |
---|
| 358 | for (v = u; (v = _graph.target(_policy[v])) != u; ) { |
---|
| 359 | clength += _length[_policy[v]]; |
---|
| 360 | ++csize; |
---|
| 361 | } |
---|
| 362 | if ( !_cycle_found || |
---|
| 363 | clength * _cycle_size < _cycle_length * csize ) { |
---|
| 364 | _cycle_found = true; |
---|
| 365 | _cycle_length = clength; |
---|
| 366 | _cycle_size = csize; |
---|
| 367 | _cycle_node = u; |
---|
| 368 | } |
---|
| 369 | } |
---|
| 370 | ++path_cnt; |
---|
| 371 | } |
---|
| 372 | } |
---|
| 373 | return curr_cycle_found; |
---|
| 374 | } |
---|
| 375 | |
---|
| 376 | // Contracts the policy graph to be connected by cutting all cycles |
---|
| 377 | // except for the main cycle (i.e. the minimum mean cycle). |
---|
| 378 | void contractPolicyGraph(int comp) { |
---|
| 379 | // Finding the component of the main cycle using |
---|
| 380 | // reverse BFS search |
---|
| 381 | typename Graph::template NodeMap<int> found(_graph, false); |
---|
| 382 | std::deque<Node> queue; |
---|
| 383 | queue.push_back(_cycle_node); |
---|
| 384 | found[_cycle_node] = true; |
---|
| 385 | Node u, v; |
---|
| 386 | while (!queue.empty()) { |
---|
| 387 | v = queue.front(); queue.pop_front(); |
---|
| 388 | for (InEdgeIt e(_graph, v); e != INVALID; ++e) { |
---|
| 389 | u = _graph.source(e); |
---|
| 390 | if (_component[u] == comp && !found[u] && _policy[u] == e) { |
---|
| 391 | found[u] = true; |
---|
| 392 | queue.push_back(u); |
---|
| 393 | } |
---|
| 394 | } |
---|
| 395 | } |
---|
| 396 | // Connecting all other nodes to this component using |
---|
| 397 | // reverse BFS search |
---|
| 398 | queue.clear(); |
---|
| 399 | for (int i = 0; i < int(_nodes.size()); ++i) |
---|
| 400 | if (found[_nodes[i]]) queue.push_back(_nodes[i]); |
---|
| 401 | int found_cnt = queue.size(); |
---|
| 402 | while (found_cnt < int(_nodes.size()) && !queue.empty()) { |
---|
| 403 | v = queue.front(); queue.pop_front(); |
---|
| 404 | for (InEdgeIt e(_graph, v); e != INVALID; ++e) { |
---|
| 405 | u = _graph.source(e); |
---|
| 406 | if (_component[u] == comp && !found[u]) { |
---|
| 407 | found[u] = true; |
---|
| 408 | ++found_cnt; |
---|
| 409 | _policy[u] = e; |
---|
| 410 | queue.push_back(u); |
---|
| 411 | } |
---|
| 412 | } |
---|
| 413 | } |
---|
| 414 | } |
---|
| 415 | |
---|
| 416 | // Computes node distances in the policy graph and updates the |
---|
| 417 | // policy graph if the node distances can be improved. |
---|
| 418 | bool computeNodeDistances(int comp) { |
---|
| 419 | // Computing node distances using reverse BFS search |
---|
| 420 | double cycle_mean = double(_cycle_length) / _cycle_size; |
---|
| 421 | typename Graph::template NodeMap<int> found(_graph, false); |
---|
| 422 | std::deque<Node> queue; |
---|
| 423 | queue.push_back(_cycle_node); |
---|
| 424 | found[_cycle_node] = true; |
---|
| 425 | _dist[_cycle_node] = 0; |
---|
| 426 | Node u, v; |
---|
| 427 | while (!queue.empty()) { |
---|
| 428 | v = queue.front(); queue.pop_front(); |
---|
| 429 | for (InEdgeIt e(_graph, v); e != INVALID; ++e) { |
---|
| 430 | u = _graph.source(e); |
---|
| 431 | if (_component[u] == comp && !found[u] && _policy[u] == e) { |
---|
| 432 | found[u] = true; |
---|
| 433 | _dist[u] = _dist[v] + _length[e] - cycle_mean; |
---|
| 434 | queue.push_back(u); |
---|
| 435 | } |
---|
| 436 | } |
---|
| 437 | } |
---|
| 438 | // Improving node distances |
---|
| 439 | bool improved = false; |
---|
| 440 | for (int j = 0; j < int(_edges.size()); ++j) { |
---|
| 441 | Edge e = _edges[j]; |
---|
| 442 | u = _graph.source(e); v = _graph.target(e); |
---|
| 443 | double delta = _dist[v] + _length[e] - cycle_mean; |
---|
| 444 | if (_tolerance.less(delta, _dist[u])) { |
---|
| 445 | improved = true; |
---|
| 446 | _dist[u] = delta; |
---|
| 447 | _policy[u] = e; |
---|
| 448 | } |
---|
| 449 | } |
---|
| 450 | return improved; |
---|
[2409] | 451 | } |
---|
| 452 | |
---|
| 453 | }; //class MinMeanCycle |
---|
| 454 | |
---|
| 455 | ///@} |
---|
| 456 | |
---|
| 457 | } //namespace lemon |
---|
| 458 | |
---|
| 459 | #endif //LEMON_MIN_MEAN_CYCLE_H |
---|