[877] | 1 | /* -*- mode: C++; indent-tabs-mode: nil; -*- |
---|
[765] | 2 | * |
---|
[877] | 3 | * This file is a part of LEMON, a generic C++ optimization library. |
---|
[765] | 4 | * |
---|
[877] | 5 | * Copyright (C) 2003-2010 |
---|
[765] | 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 | |
---|
[864] | 19 | #ifndef LEMON_KARP_MMC_H |
---|
| 20 | #define LEMON_KARP_MMC_H |
---|
[765] | 21 | |
---|
[768] | 22 | /// \ingroup min_mean_cycle |
---|
[765] | 23 | /// |
---|
| 24 | /// \file |
---|
| 25 | /// \brief Karp's algorithm for finding a minimum mean cycle. |
---|
| 26 | |
---|
| 27 | #include <vector> |
---|
| 28 | #include <limits> |
---|
| 29 | #include <lemon/core.h> |
---|
| 30 | #include <lemon/path.h> |
---|
| 31 | #include <lemon/tolerance.h> |
---|
| 32 | #include <lemon/connectivity.h> |
---|
| 33 | |
---|
| 34 | namespace lemon { |
---|
| 35 | |
---|
[864] | 36 | /// \brief Default traits class of KarpMmc class. |
---|
[765] | 37 | /// |
---|
[864] | 38 | /// Default traits class of KarpMmc class. |
---|
[765] | 39 | /// \tparam GR The type of the digraph. |
---|
[864] | 40 | /// \tparam CM The type of the cost map. |
---|
[765] | 41 | /// It must conform to the \ref concepts::ReadMap "ReadMap" concept. |
---|
| 42 | #ifdef DOXYGEN |
---|
[864] | 43 | template <typename GR, typename CM> |
---|
[765] | 44 | #else |
---|
[864] | 45 | template <typename GR, typename CM, |
---|
| 46 | bool integer = std::numeric_limits<typename CM::Value>::is_integer> |
---|
[765] | 47 | #endif |
---|
[864] | 48 | struct KarpMmcDefaultTraits |
---|
[765] | 49 | { |
---|
| 50 | /// The type of the digraph |
---|
| 51 | typedef GR Digraph; |
---|
[864] | 52 | /// The type of the cost map |
---|
| 53 | typedef CM CostMap; |
---|
| 54 | /// The type of the arc costs |
---|
| 55 | typedef typename CostMap::Value Cost; |
---|
[765] | 56 | |
---|
[864] | 57 | /// \brief The large cost type used for internal computations |
---|
[765] | 58 | /// |
---|
[864] | 59 | /// The large cost type used for internal computations. |
---|
| 60 | /// It is \c long \c long if the \c Cost type is integer, |
---|
[765] | 61 | /// otherwise it is \c double. |
---|
[864] | 62 | /// \c Cost must be convertible to \c LargeCost. |
---|
| 63 | typedef double LargeCost; |
---|
[765] | 64 | |
---|
| 65 | /// The tolerance type used for internal computations |
---|
[864] | 66 | typedef lemon::Tolerance<LargeCost> Tolerance; |
---|
[765] | 67 | |
---|
| 68 | /// \brief The path type of the found cycles |
---|
| 69 | /// |
---|
| 70 | /// The path type of the found cycles. |
---|
| 71 | /// It must conform to the \ref lemon::concepts::Path "Path" concept |
---|
[772] | 72 | /// and it must have an \c addFront() function. |
---|
[765] | 73 | typedef lemon::Path<Digraph> Path; |
---|
| 74 | }; |
---|
| 75 | |
---|
[864] | 76 | // Default traits class for integer cost types |
---|
| 77 | template <typename GR, typename CM> |
---|
| 78 | struct KarpMmcDefaultTraits<GR, CM, true> |
---|
[765] | 79 | { |
---|
| 80 | typedef GR Digraph; |
---|
[864] | 81 | typedef CM CostMap; |
---|
| 82 | typedef typename CostMap::Value Cost; |
---|
[765] | 83 | #ifdef LEMON_HAVE_LONG_LONG |
---|
[864] | 84 | typedef long long LargeCost; |
---|
[765] | 85 | #else |
---|
[864] | 86 | typedef long LargeCost; |
---|
[765] | 87 | #endif |
---|
[864] | 88 | typedef lemon::Tolerance<LargeCost> Tolerance; |
---|
[765] | 89 | typedef lemon::Path<Digraph> Path; |
---|
| 90 | }; |
---|
| 91 | |
---|
| 92 | |
---|
[768] | 93 | /// \addtogroup min_mean_cycle |
---|
[765] | 94 | /// @{ |
---|
| 95 | |
---|
| 96 | /// \brief Implementation of Karp's algorithm for finding a minimum |
---|
| 97 | /// mean cycle. |
---|
| 98 | /// |
---|
| 99 | /// This class implements Karp's algorithm for finding a directed |
---|
[864] | 100 | /// cycle of minimum mean cost in a digraph |
---|
[771] | 101 | /// \ref amo93networkflows, \ref dasdan98minmeancycle. |
---|
[768] | 102 | /// It runs in time O(ne) and uses space O(n<sup>2</sup>+e). |
---|
[765] | 103 | /// |
---|
| 104 | /// \tparam GR The type of the digraph the algorithm runs on. |
---|
[864] | 105 | /// \tparam CM The type of the cost map. The default |
---|
[765] | 106 | /// map type is \ref concepts::Digraph::ArcMap "GR::ArcMap<int>". |
---|
[825] | 107 | /// \tparam TR The traits class that defines various types used by the |
---|
[864] | 108 | /// algorithm. By default, it is \ref KarpMmcDefaultTraits |
---|
| 109 | /// "KarpMmcDefaultTraits<GR, CM>". |
---|
[825] | 110 | /// In most cases, this parameter should not be set directly, |
---|
| 111 | /// consider to use the named template parameters instead. |
---|
[765] | 112 | #ifdef DOXYGEN |
---|
[864] | 113 | template <typename GR, typename CM, typename TR> |
---|
[765] | 114 | #else |
---|
| 115 | template < typename GR, |
---|
[864] | 116 | typename CM = typename GR::template ArcMap<int>, |
---|
| 117 | typename TR = KarpMmcDefaultTraits<GR, CM> > |
---|
[765] | 118 | #endif |
---|
[864] | 119 | class KarpMmc |
---|
[765] | 120 | { |
---|
| 121 | public: |
---|
| 122 | |
---|
| 123 | /// The type of the digraph |
---|
| 124 | typedef typename TR::Digraph Digraph; |
---|
[864] | 125 | /// The type of the cost map |
---|
| 126 | typedef typename TR::CostMap CostMap; |
---|
| 127 | /// The type of the arc costs |
---|
| 128 | typedef typename TR::Cost Cost; |
---|
[765] | 129 | |
---|
[864] | 130 | /// \brief The large cost type |
---|
[765] | 131 | /// |
---|
[864] | 132 | /// The large cost type used for internal computations. |
---|
| 133 | /// By default, it is \c long \c long if the \c Cost type is integer, |
---|
[765] | 134 | /// otherwise it is \c double. |
---|
[864] | 135 | typedef typename TR::LargeCost LargeCost; |
---|
[765] | 136 | |
---|
| 137 | /// The tolerance type |
---|
| 138 | typedef typename TR::Tolerance Tolerance; |
---|
| 139 | |
---|
| 140 | /// \brief The path type of the found cycles |
---|
| 141 | /// |
---|
| 142 | /// The path type of the found cycles. |
---|
[864] | 143 | /// Using the \ref KarpMmcDefaultTraits "default traits class", |
---|
[765] | 144 | /// it is \ref lemon::Path "Path<Digraph>". |
---|
| 145 | typedef typename TR::Path Path; |
---|
| 146 | |
---|
[864] | 147 | /// The \ref KarpMmcDefaultTraits "traits class" of the algorithm |
---|
[765] | 148 | typedef TR Traits; |
---|
| 149 | |
---|
| 150 | private: |
---|
| 151 | |
---|
| 152 | TEMPLATE_DIGRAPH_TYPEDEFS(Digraph); |
---|
| 153 | |
---|
| 154 | // Data sturcture for path data |
---|
| 155 | struct PathData |
---|
| 156 | { |
---|
[864] | 157 | LargeCost dist; |
---|
[765] | 158 | Arc pred; |
---|
[864] | 159 | PathData(LargeCost d, Arc p = INVALID) : |
---|
[767] | 160 | dist(d), pred(p) {} |
---|
[765] | 161 | }; |
---|
| 162 | |
---|
| 163 | typedef typename Digraph::template NodeMap<std::vector<PathData> > |
---|
| 164 | PathDataNodeMap; |
---|
| 165 | |
---|
| 166 | private: |
---|
| 167 | |
---|
| 168 | // The digraph the algorithm runs on |
---|
| 169 | const Digraph &_gr; |
---|
[864] | 170 | // The cost of the arcs |
---|
| 171 | const CostMap &_cost; |
---|
[765] | 172 | |
---|
| 173 | // Data for storing the strongly connected components |
---|
| 174 | int _comp_num; |
---|
| 175 | typename Digraph::template NodeMap<int> _comp; |
---|
| 176 | std::vector<std::vector<Node> > _comp_nodes; |
---|
| 177 | std::vector<Node>* _nodes; |
---|
| 178 | typename Digraph::template NodeMap<std::vector<Arc> > _out_arcs; |
---|
| 179 | |
---|
| 180 | // Data for the found cycle |
---|
[864] | 181 | LargeCost _cycle_cost; |
---|
[765] | 182 | int _cycle_size; |
---|
| 183 | Node _cycle_node; |
---|
| 184 | |
---|
| 185 | Path *_cycle_path; |
---|
| 186 | bool _local_path; |
---|
| 187 | |
---|
| 188 | // Node map for storing path data |
---|
| 189 | PathDataNodeMap _data; |
---|
| 190 | // The processed nodes in the last round |
---|
| 191 | std::vector<Node> _process; |
---|
| 192 | |
---|
| 193 | Tolerance _tolerance; |
---|
[877] | 194 | |
---|
[767] | 195 | // Infinite constant |
---|
[864] | 196 | const LargeCost INF; |
---|
[765] | 197 | |
---|
| 198 | public: |
---|
| 199 | |
---|
| 200 | /// \name Named Template Parameters |
---|
| 201 | /// @{ |
---|
| 202 | |
---|
| 203 | template <typename T> |
---|
[864] | 204 | struct SetLargeCostTraits : public Traits { |
---|
| 205 | typedef T LargeCost; |
---|
[765] | 206 | typedef lemon::Tolerance<T> Tolerance; |
---|
| 207 | }; |
---|
| 208 | |
---|
| 209 | /// \brief \ref named-templ-param "Named parameter" for setting |
---|
[864] | 210 | /// \c LargeCost type. |
---|
[765] | 211 | /// |
---|
[864] | 212 | /// \ref named-templ-param "Named parameter" for setting \c LargeCost |
---|
[765] | 213 | /// type. It is used for internal computations in the algorithm. |
---|
| 214 | template <typename T> |
---|
[864] | 215 | struct SetLargeCost |
---|
| 216 | : public KarpMmc<GR, CM, SetLargeCostTraits<T> > { |
---|
| 217 | typedef KarpMmc<GR, CM, SetLargeCostTraits<T> > Create; |
---|
[765] | 218 | }; |
---|
| 219 | |
---|
| 220 | template <typename T> |
---|
| 221 | struct SetPathTraits : public Traits { |
---|
| 222 | typedef T Path; |
---|
| 223 | }; |
---|
| 224 | |
---|
| 225 | /// \brief \ref named-templ-param "Named parameter" for setting |
---|
| 226 | /// \c %Path type. |
---|
| 227 | /// |
---|
| 228 | /// \ref named-templ-param "Named parameter" for setting the \c %Path |
---|
| 229 | /// type of the found cycles. |
---|
| 230 | /// It must conform to the \ref lemon::concepts::Path "Path" concept |
---|
| 231 | /// and it must have an \c addFront() function. |
---|
| 232 | template <typename T> |
---|
| 233 | struct SetPath |
---|
[864] | 234 | : public KarpMmc<GR, CM, SetPathTraits<T> > { |
---|
| 235 | typedef KarpMmc<GR, CM, SetPathTraits<T> > Create; |
---|
[765] | 236 | }; |
---|
| 237 | |
---|
| 238 | /// @} |
---|
| 239 | |
---|
[863] | 240 | protected: |
---|
| 241 | |
---|
[864] | 242 | KarpMmc() {} |
---|
[863] | 243 | |
---|
[765] | 244 | public: |
---|
| 245 | |
---|
| 246 | /// \brief Constructor. |
---|
| 247 | /// |
---|
| 248 | /// The constructor of the class. |
---|
| 249 | /// |
---|
| 250 | /// \param digraph The digraph the algorithm runs on. |
---|
[864] | 251 | /// \param cost The costs of the arcs. |
---|
| 252 | KarpMmc( const Digraph &digraph, |
---|
| 253 | const CostMap &cost ) : |
---|
| 254 | _gr(digraph), _cost(cost), _comp(digraph), _out_arcs(digraph), |
---|
| 255 | _cycle_cost(0), _cycle_size(1), _cycle_node(INVALID), |
---|
[767] | 256 | _cycle_path(NULL), _local_path(false), _data(digraph), |
---|
[864] | 257 | INF(std::numeric_limits<LargeCost>::has_infinity ? |
---|
| 258 | std::numeric_limits<LargeCost>::infinity() : |
---|
| 259 | std::numeric_limits<LargeCost>::max()) |
---|
[765] | 260 | {} |
---|
| 261 | |
---|
| 262 | /// Destructor. |
---|
[864] | 263 | ~KarpMmc() { |
---|
[765] | 264 | if (_local_path) delete _cycle_path; |
---|
| 265 | } |
---|
| 266 | |
---|
| 267 | /// \brief Set the path structure for storing the found cycle. |
---|
| 268 | /// |
---|
| 269 | /// This function sets an external path structure for storing the |
---|
| 270 | /// found cycle. |
---|
| 271 | /// |
---|
| 272 | /// If you don't call this function before calling \ref run() or |
---|
[864] | 273 | /// \ref findCycleMean(), it will allocate a local \ref Path "path" |
---|
[765] | 274 | /// structure. The destuctor deallocates this automatically |
---|
| 275 | /// allocated object, of course. |
---|
| 276 | /// |
---|
| 277 | /// \note The algorithm calls only the \ref lemon::Path::addFront() |
---|
| 278 | /// "addFront()" function of the given path structure. |
---|
| 279 | /// |
---|
| 280 | /// \return <tt>(*this)</tt> |
---|
[864] | 281 | KarpMmc& cycle(Path &path) { |
---|
[765] | 282 | if (_local_path) { |
---|
| 283 | delete _cycle_path; |
---|
| 284 | _local_path = false; |
---|
| 285 | } |
---|
| 286 | _cycle_path = &path; |
---|
| 287 | return *this; |
---|
| 288 | } |
---|
| 289 | |
---|
[769] | 290 | /// \brief Set the tolerance used by the algorithm. |
---|
| 291 | /// |
---|
| 292 | /// This function sets the tolerance object used by the algorithm. |
---|
| 293 | /// |
---|
| 294 | /// \return <tt>(*this)</tt> |
---|
[864] | 295 | KarpMmc& tolerance(const Tolerance& tolerance) { |
---|
[769] | 296 | _tolerance = tolerance; |
---|
| 297 | return *this; |
---|
| 298 | } |
---|
| 299 | |
---|
| 300 | /// \brief Return a const reference to the tolerance. |
---|
| 301 | /// |
---|
| 302 | /// This function returns a const reference to the tolerance object |
---|
| 303 | /// used by the algorithm. |
---|
| 304 | const Tolerance& tolerance() const { |
---|
| 305 | return _tolerance; |
---|
| 306 | } |
---|
| 307 | |
---|
[765] | 308 | /// \name Execution control |
---|
| 309 | /// The simplest way to execute the algorithm is to call the \ref run() |
---|
| 310 | /// function.\n |
---|
[864] | 311 | /// If you only need the minimum mean cost, you may call |
---|
| 312 | /// \ref findCycleMean(). |
---|
[765] | 313 | |
---|
| 314 | /// @{ |
---|
| 315 | |
---|
| 316 | /// \brief Run the algorithm. |
---|
| 317 | /// |
---|
| 318 | /// This function runs the algorithm. |
---|
| 319 | /// It can be called more than once (e.g. if the underlying digraph |
---|
[864] | 320 | /// and/or the arc costs have been modified). |
---|
[765] | 321 | /// |
---|
| 322 | /// \return \c true if a directed cycle exists in the digraph. |
---|
| 323 | /// |
---|
| 324 | /// \note <tt>mmc.run()</tt> is just a shortcut of the following code. |
---|
| 325 | /// \code |
---|
[864] | 326 | /// return mmc.findCycleMean() && mmc.findCycle(); |
---|
[765] | 327 | /// \endcode |
---|
| 328 | bool run() { |
---|
[864] | 329 | return findCycleMean() && findCycle(); |
---|
[765] | 330 | } |
---|
| 331 | |
---|
| 332 | /// \brief Find the minimum cycle mean. |
---|
| 333 | /// |
---|
[864] | 334 | /// This function finds the minimum mean cost of the directed |
---|
[765] | 335 | /// cycles in the digraph. |
---|
| 336 | /// |
---|
| 337 | /// \return \c true if a directed cycle exists in the digraph. |
---|
[864] | 338 | bool findCycleMean() { |
---|
[765] | 339 | // Initialization and find strongly connected components |
---|
| 340 | init(); |
---|
| 341 | findComponents(); |
---|
[877] | 342 | |
---|
[765] | 343 | // Find the minimum cycle mean in the components |
---|
| 344 | for (int comp = 0; comp < _comp_num; ++comp) { |
---|
| 345 | if (!initComponent(comp)) continue; |
---|
| 346 | processRounds(); |
---|
| 347 | updateMinMean(); |
---|
| 348 | } |
---|
| 349 | return (_cycle_node != INVALID); |
---|
| 350 | } |
---|
| 351 | |
---|
| 352 | /// \brief Find a minimum mean directed cycle. |
---|
| 353 | /// |
---|
[864] | 354 | /// This function finds a directed cycle of minimum mean cost |
---|
| 355 | /// in the digraph using the data computed by findCycleMean(). |
---|
[765] | 356 | /// |
---|
| 357 | /// \return \c true if a directed cycle exists in the digraph. |
---|
| 358 | /// |
---|
[864] | 359 | /// \pre \ref findCycleMean() must be called before using this function. |
---|
[765] | 360 | bool findCycle() { |
---|
| 361 | if (_cycle_node == INVALID) return false; |
---|
| 362 | IntNodeMap reached(_gr, -1); |
---|
| 363 | int r = _data[_cycle_node].size(); |
---|
| 364 | Node u = _cycle_node; |
---|
| 365 | while (reached[u] < 0) { |
---|
| 366 | reached[u] = --r; |
---|
| 367 | u = _gr.source(_data[u][r].pred); |
---|
| 368 | } |
---|
| 369 | r = reached[u]; |
---|
| 370 | Arc e = _data[u][r].pred; |
---|
| 371 | _cycle_path->addFront(e); |
---|
[864] | 372 | _cycle_cost = _cost[e]; |
---|
[765] | 373 | _cycle_size = 1; |
---|
| 374 | Node v; |
---|
| 375 | while ((v = _gr.source(e)) != u) { |
---|
| 376 | e = _data[v][--r].pred; |
---|
| 377 | _cycle_path->addFront(e); |
---|
[864] | 378 | _cycle_cost += _cost[e]; |
---|
[765] | 379 | ++_cycle_size; |
---|
| 380 | } |
---|
| 381 | return true; |
---|
| 382 | } |
---|
| 383 | |
---|
| 384 | /// @} |
---|
| 385 | |
---|
| 386 | /// \name Query Functions |
---|
| 387 | /// The results of the algorithm can be obtained using these |
---|
| 388 | /// functions.\n |
---|
| 389 | /// The algorithm should be executed before using them. |
---|
| 390 | |
---|
| 391 | /// @{ |
---|
| 392 | |
---|
[864] | 393 | /// \brief Return the total cost of the found cycle. |
---|
[765] | 394 | /// |
---|
[864] | 395 | /// This function returns the total cost of the found cycle. |
---|
[765] | 396 | /// |
---|
[864] | 397 | /// \pre \ref run() or \ref findCycleMean() must be called before |
---|
[765] | 398 | /// using this function. |
---|
[864] | 399 | Cost cycleCost() const { |
---|
| 400 | return static_cast<Cost>(_cycle_cost); |
---|
[765] | 401 | } |
---|
| 402 | |
---|
| 403 | /// \brief Return the number of arcs on the found cycle. |
---|
| 404 | /// |
---|
| 405 | /// This function returns the number of arcs on the found cycle. |
---|
| 406 | /// |
---|
[864] | 407 | /// \pre \ref run() or \ref findCycleMean() must be called before |
---|
[765] | 408 | /// using this function. |
---|
[864] | 409 | int cycleSize() const { |
---|
[765] | 410 | return _cycle_size; |
---|
| 411 | } |
---|
| 412 | |
---|
[864] | 413 | /// \brief Return the mean cost of the found cycle. |
---|
[765] | 414 | /// |
---|
[864] | 415 | /// This function returns the mean cost of the found cycle. |
---|
[765] | 416 | /// |
---|
| 417 | /// \note <tt>alg.cycleMean()</tt> is just a shortcut of the |
---|
| 418 | /// following code. |
---|
| 419 | /// \code |
---|
[864] | 420 | /// return static_cast<double>(alg.cycleCost()) / alg.cycleSize(); |
---|
[765] | 421 | /// \endcode |
---|
| 422 | /// |
---|
[864] | 423 | /// \pre \ref run() or \ref findCycleMean() must be called before |
---|
[765] | 424 | /// using this function. |
---|
| 425 | double cycleMean() const { |
---|
[864] | 426 | return static_cast<double>(_cycle_cost) / _cycle_size; |
---|
[765] | 427 | } |
---|
| 428 | |
---|
| 429 | /// \brief Return the found cycle. |
---|
| 430 | /// |
---|
| 431 | /// This function returns a const reference to the path structure |
---|
| 432 | /// storing the found cycle. |
---|
| 433 | /// |
---|
| 434 | /// \pre \ref run() or \ref findCycle() must be called before using |
---|
| 435 | /// this function. |
---|
| 436 | const Path& cycle() const { |
---|
| 437 | return *_cycle_path; |
---|
| 438 | } |
---|
| 439 | |
---|
| 440 | ///@} |
---|
| 441 | |
---|
| 442 | private: |
---|
| 443 | |
---|
| 444 | // Initialization |
---|
| 445 | void init() { |
---|
| 446 | if (!_cycle_path) { |
---|
| 447 | _local_path = true; |
---|
| 448 | _cycle_path = new Path; |
---|
| 449 | } |
---|
| 450 | _cycle_path->clear(); |
---|
[864] | 451 | _cycle_cost = 0; |
---|
[765] | 452 | _cycle_size = 1; |
---|
| 453 | _cycle_node = INVALID; |
---|
| 454 | for (NodeIt u(_gr); u != INVALID; ++u) |
---|
| 455 | _data[u].clear(); |
---|
| 456 | } |
---|
| 457 | |
---|
| 458 | // Find strongly connected components and initialize _comp_nodes |
---|
| 459 | // and _out_arcs |
---|
| 460 | void findComponents() { |
---|
| 461 | _comp_num = stronglyConnectedComponents(_gr, _comp); |
---|
| 462 | _comp_nodes.resize(_comp_num); |
---|
| 463 | if (_comp_num == 1) { |
---|
| 464 | _comp_nodes[0].clear(); |
---|
| 465 | for (NodeIt n(_gr); n != INVALID; ++n) { |
---|
| 466 | _comp_nodes[0].push_back(n); |
---|
| 467 | _out_arcs[n].clear(); |
---|
| 468 | for (OutArcIt a(_gr, n); a != INVALID; ++a) { |
---|
| 469 | _out_arcs[n].push_back(a); |
---|
| 470 | } |
---|
| 471 | } |
---|
| 472 | } else { |
---|
| 473 | for (int i = 0; i < _comp_num; ++i) |
---|
| 474 | _comp_nodes[i].clear(); |
---|
| 475 | for (NodeIt n(_gr); n != INVALID; ++n) { |
---|
| 476 | int k = _comp[n]; |
---|
| 477 | _comp_nodes[k].push_back(n); |
---|
| 478 | _out_arcs[n].clear(); |
---|
| 479 | for (OutArcIt a(_gr, n); a != INVALID; ++a) { |
---|
| 480 | if (_comp[_gr.target(a)] == k) _out_arcs[n].push_back(a); |
---|
| 481 | } |
---|
| 482 | } |
---|
| 483 | } |
---|
| 484 | } |
---|
| 485 | |
---|
| 486 | // Initialize path data for the current component |
---|
| 487 | bool initComponent(int comp) { |
---|
| 488 | _nodes = &(_comp_nodes[comp]); |
---|
| 489 | int n = _nodes->size(); |
---|
| 490 | if (n < 1 || (n == 1 && _out_arcs[(*_nodes)[0]].size() == 0)) { |
---|
| 491 | return false; |
---|
[877] | 492 | } |
---|
[765] | 493 | for (int i = 0; i < n; ++i) { |
---|
[767] | 494 | _data[(*_nodes)[i]].resize(n + 1, PathData(INF)); |
---|
[765] | 495 | } |
---|
| 496 | return true; |
---|
| 497 | } |
---|
| 498 | |
---|
| 499 | // Process all rounds of computing path data for the current component. |
---|
[864] | 500 | // _data[v][k] is the cost of a shortest directed walk from the root |
---|
[765] | 501 | // node to node v containing exactly k arcs. |
---|
| 502 | void processRounds() { |
---|
| 503 | Node start = (*_nodes)[0]; |
---|
[767] | 504 | _data[start][0] = PathData(0); |
---|
[765] | 505 | _process.clear(); |
---|
| 506 | _process.push_back(start); |
---|
| 507 | |
---|
| 508 | int k, n = _nodes->size(); |
---|
| 509 | for (k = 1; k <= n && int(_process.size()) < n; ++k) { |
---|
| 510 | processNextBuildRound(k); |
---|
| 511 | } |
---|
| 512 | for ( ; k <= n; ++k) { |
---|
| 513 | processNextFullRound(k); |
---|
| 514 | } |
---|
| 515 | } |
---|
| 516 | |
---|
| 517 | // Process one round and rebuild _process |
---|
| 518 | void processNextBuildRound(int k) { |
---|
| 519 | std::vector<Node> next; |
---|
| 520 | Node u, v; |
---|
| 521 | Arc e; |
---|
[864] | 522 | LargeCost d; |
---|
[765] | 523 | for (int i = 0; i < int(_process.size()); ++i) { |
---|
| 524 | u = _process[i]; |
---|
| 525 | for (int j = 0; j < int(_out_arcs[u].size()); ++j) { |
---|
| 526 | e = _out_arcs[u][j]; |
---|
| 527 | v = _gr.target(e); |
---|
[864] | 528 | d = _data[u][k-1].dist + _cost[e]; |
---|
[767] | 529 | if (_tolerance.less(d, _data[v][k].dist)) { |
---|
| 530 | if (_data[v][k].dist == INF) next.push_back(v); |
---|
| 531 | _data[v][k] = PathData(d, e); |
---|
[765] | 532 | } |
---|
| 533 | } |
---|
| 534 | } |
---|
| 535 | _process.swap(next); |
---|
| 536 | } |
---|
| 537 | |
---|
| 538 | // Process one round using _nodes instead of _process |
---|
| 539 | void processNextFullRound(int k) { |
---|
| 540 | Node u, v; |
---|
| 541 | Arc e; |
---|
[864] | 542 | LargeCost d; |
---|
[765] | 543 | for (int i = 0; i < int(_nodes->size()); ++i) { |
---|
| 544 | u = (*_nodes)[i]; |
---|
| 545 | for (int j = 0; j < int(_out_arcs[u].size()); ++j) { |
---|
| 546 | e = _out_arcs[u][j]; |
---|
| 547 | v = _gr.target(e); |
---|
[864] | 548 | d = _data[u][k-1].dist + _cost[e]; |
---|
[767] | 549 | if (_tolerance.less(d, _data[v][k].dist)) { |
---|
| 550 | _data[v][k] = PathData(d, e); |
---|
[765] | 551 | } |
---|
| 552 | } |
---|
| 553 | } |
---|
| 554 | } |
---|
| 555 | |
---|
| 556 | // Update the minimum cycle mean |
---|
| 557 | void updateMinMean() { |
---|
| 558 | int n = _nodes->size(); |
---|
| 559 | for (int i = 0; i < n; ++i) { |
---|
| 560 | Node u = (*_nodes)[i]; |
---|
[767] | 561 | if (_data[u][n].dist == INF) continue; |
---|
[864] | 562 | LargeCost cost, max_cost = 0; |
---|
[765] | 563 | int size, max_size = 1; |
---|
| 564 | bool found_curr = false; |
---|
| 565 | for (int k = 0; k < n; ++k) { |
---|
[767] | 566 | if (_data[u][k].dist == INF) continue; |
---|
[864] | 567 | cost = _data[u][n].dist - _data[u][k].dist; |
---|
[765] | 568 | size = n - k; |
---|
[864] | 569 | if (!found_curr || cost * max_size > max_cost * size) { |
---|
[765] | 570 | found_curr = true; |
---|
[864] | 571 | max_cost = cost; |
---|
[765] | 572 | max_size = size; |
---|
| 573 | } |
---|
| 574 | } |
---|
| 575 | if ( found_curr && (_cycle_node == INVALID || |
---|
[864] | 576 | max_cost * _cycle_size < _cycle_cost * max_size) ) { |
---|
| 577 | _cycle_cost = max_cost; |
---|
[765] | 578 | _cycle_size = max_size; |
---|
| 579 | _cycle_node = u; |
---|
| 580 | } |
---|
| 581 | } |
---|
| 582 | } |
---|
| 583 | |
---|
[864] | 584 | }; //class KarpMmc |
---|
[765] | 585 | |
---|
| 586 | ///@} |
---|
| 587 | |
---|
| 588 | } //namespace lemon |
---|
| 589 | |
---|
[864] | 590 | #endif //LEMON_KARP_MMC_H |
---|