0
4
0
| ... | ... |
@@ -298,192 +298,226 @@ |
| 298 | 298 |
*/ |
| 299 | 299 |
|
| 300 | 300 |
/** |
| 301 | 301 |
@defgroup max_flow Maximum Flow Algorithms |
| 302 | 302 |
@ingroup algs |
| 303 | 303 |
\brief Algorithms for finding maximum flows. |
| 304 | 304 |
|
| 305 | 305 |
This group contains the algorithms for finding maximum flows and |
| 306 | 306 |
feasible circulations. |
| 307 | 307 |
|
| 308 | 308 |
The \e maximum \e flow \e problem is to find a flow of maximum value between |
| 309 | 309 |
a single source and a single target. Formally, there is a \f$G=(V,A)\f$ |
| 310 | 310 |
digraph, a \f$cap: A\rightarrow\mathbf{R}^+_0\f$ capacity function and
|
| 311 | 311 |
\f$s, t \in V\f$ source and target nodes. |
| 312 | 312 |
A maximum flow is an \f$f: A\rightarrow\mathbf{R}^+_0\f$ solution of the
|
| 313 | 313 |
following optimization problem. |
| 314 | 314 |
|
| 315 | 315 |
\f[ \max\sum_{sv\in A} f(sv) - \sum_{vs\in A} f(vs) \f]
|
| 316 | 316 |
\f[ \sum_{uv\in A} f(uv) = \sum_{vu\in A} f(vu)
|
| 317 | 317 |
\quad \forall u\in V\setminus\{s,t\} \f]
|
| 318 | 318 |
\f[ 0 \leq f(uv) \leq cap(uv) \quad \forall uv\in A \f] |
| 319 | 319 |
|
| 320 | 320 |
LEMON contains several algorithms for solving maximum flow problems: |
| 321 | 321 |
- \ref EdmondsKarp Edmonds-Karp algorithm. |
| 322 | 322 |
- \ref Preflow Goldberg-Tarjan's preflow push-relabel algorithm. |
| 323 | 323 |
- \ref DinitzSleatorTarjan Dinitz's blocking flow algorithm with dynamic trees. |
| 324 | 324 |
- \ref GoldbergTarjan Preflow push-relabel algorithm with dynamic trees. |
| 325 | 325 |
|
| 326 | 326 |
In most cases the \ref Preflow "Preflow" algorithm provides the |
| 327 | 327 |
fastest method for computing a maximum flow. All implementations |
| 328 | 328 |
also provide functions to query the minimum cut, which is the dual |
| 329 | 329 |
problem of maximum flow. |
| 330 | 330 |
|
| 331 | 331 |
\ref Circulation is a preflow push-relabel algorithm implemented directly |
| 332 | 332 |
for finding feasible circulations, which is a somewhat different problem, |
| 333 | 333 |
but it is strongly related to maximum flow. |
| 334 | 334 |
For more information, see \ref Circulation. |
| 335 | 335 |
*/ |
| 336 | 336 |
|
| 337 | 337 |
/** |
| 338 | 338 |
@defgroup min_cost_flow_algs Minimum Cost Flow Algorithms |
| 339 | 339 |
@ingroup algs |
| 340 | 340 |
|
| 341 | 341 |
\brief Algorithms for finding minimum cost flows and circulations. |
| 342 | 342 |
|
| 343 | 343 |
This group contains the algorithms for finding minimum cost flows and |
| 344 | 344 |
circulations. For more information about this problem and its dual |
| 345 | 345 |
solution see \ref min_cost_flow "Minimum Cost Flow Problem". |
| 346 | 346 |
|
| 347 | 347 |
LEMON contains several algorithms for this problem. |
| 348 | 348 |
- \ref NetworkSimplex Primal Network Simplex algorithm with various |
| 349 | 349 |
pivot strategies. |
| 350 | 350 |
- \ref CostScaling Push-Relabel and Augment-Relabel algorithms based on |
| 351 | 351 |
cost scaling. |
| 352 | 352 |
- \ref CapacityScaling Successive Shortest %Path algorithm with optional |
| 353 | 353 |
capacity scaling. |
| 354 | 354 |
- \ref CancelAndTighten The Cancel and Tighten algorithm. |
| 355 | 355 |
- \ref CycleCanceling Cycle-Canceling algorithms. |
| 356 | 356 |
|
| 357 | 357 |
In general NetworkSimplex is the most efficient implementation, |
| 358 | 358 |
but in special cases other algorithms could be faster. |
| 359 | 359 |
For example, if the total supply and/or capacities are rather small, |
| 360 | 360 |
CapacityScaling is usually the fastest algorithm (without effective scaling). |
| 361 | 361 |
*/ |
| 362 | 362 |
|
| 363 | 363 |
/** |
| 364 | 364 |
@defgroup min_cut Minimum Cut Algorithms |
| 365 | 365 |
@ingroup algs |
| 366 | 366 |
|
| 367 | 367 |
\brief Algorithms for finding minimum cut in graphs. |
| 368 | 368 |
|
| 369 | 369 |
This group contains the algorithms for finding minimum cut in graphs. |
| 370 | 370 |
|
| 371 | 371 |
The \e minimum \e cut \e problem is to find a non-empty and non-complete |
| 372 | 372 |
\f$X\f$ subset of the nodes with minimum overall capacity on |
| 373 | 373 |
outgoing arcs. Formally, there is a \f$G=(V,A)\f$ digraph, a |
| 374 | 374 |
\f$cap: A\rightarrow\mathbf{R}^+_0\f$ capacity function. The minimum
|
| 375 | 375 |
cut is the \f$X\f$ solution of the next optimization problem: |
| 376 | 376 |
|
| 377 | 377 |
\f[ \min_{X \subset V, X\not\in \{\emptyset, V\}}
|
| 378 | 378 |
\sum_{uv\in A, u\in X, v\not\in X}cap(uv) \f]
|
| 379 | 379 |
|
| 380 | 380 |
LEMON contains several algorithms related to minimum cut problems: |
| 381 | 381 |
|
| 382 | 382 |
- \ref HaoOrlin "Hao-Orlin algorithm" for calculating minimum cut |
| 383 | 383 |
in directed graphs. |
| 384 | 384 |
- \ref NagamochiIbaraki "Nagamochi-Ibaraki algorithm" for |
| 385 | 385 |
calculating minimum cut in undirected graphs. |
| 386 | 386 |
- \ref GomoryHu "Gomory-Hu tree computation" for calculating |
| 387 | 387 |
all-pairs minimum cut in undirected graphs. |
| 388 | 388 |
|
| 389 | 389 |
If you want to find minimum cut just between two distinict nodes, |
| 390 | 390 |
see the \ref max_flow "maximum flow problem". |
| 391 | 391 |
*/ |
| 392 | 392 |
|
| 393 | 393 |
/** |
| 394 |
@defgroup min_mean_cycle Minimum Mean Cycle Algorithms |
|
| 395 |
@ingroup algs |
|
| 396 |
\brief Algorithms for finding minimum mean cycles. |
|
| 397 |
|
|
| 398 |
This group contains the algorithms for finding minimum mean cycles. |
|
| 399 |
|
|
| 400 |
The \e minimum \e mean \e cycle \e problem is to find a directed cycle |
|
| 401 |
of minimum mean length (cost) in a digraph. |
|
| 402 |
The mean length of a cycle is the average length of its arcs, i.e. the |
|
| 403 |
ratio between the total length of the cycle and the number of arcs on it. |
|
| 404 |
|
|
| 405 |
This problem has an important connection to \e conservative \e length |
|
| 406 |
\e functions, too. A length function on the arcs of a digraph is called |
|
| 407 |
conservative if and only if there is no directed cycle of negative total |
|
| 408 |
length. For an arbitrary length function, the negative of the minimum |
|
| 409 |
cycle mean is the smallest \f$\epsilon\f$ value so that increasing the |
|
| 410 |
arc lengths uniformly by \f$\epsilon\f$ results in a conservative length |
|
| 411 |
function. |
|
| 412 |
|
|
| 413 |
LEMON contains three algorithms for solving the minimum mean cycle problem: |
|
| 414 |
- \ref Karp "Karp"'s original algorithm. |
|
| 415 |
- \ref HartmannOrlin "Hartmann-Orlin"'s algorithm, which is an improved |
|
| 416 |
version of Karp's algorithm. |
|
| 417 |
- \ref Howard "Howard"'s policy iteration algorithm. |
|
| 418 |
|
|
| 419 |
In practice, the Howard algorithm proved to be by far the most efficient |
|
| 420 |
one, though the best known theoretical bound on its running time is |
|
| 421 |
exponential. |
|
| 422 |
Both Karp and HartmannOrlin algorithms run in time O(ne) and use space |
|
| 423 |
O(n<sup>2</sup>+e), but the latter one is typically faster due to the |
|
| 424 |
applied early termination scheme. |
|
| 425 |
*/ |
|
| 426 |
|
|
| 427 |
/** |
|
| 394 | 428 |
@defgroup graph_properties Connectivity and Other Graph Properties |
| 395 | 429 |
@ingroup algs |
| 396 | 430 |
\brief Algorithms for discovering the graph properties |
| 397 | 431 |
|
| 398 | 432 |
This group contains the algorithms for discovering the graph properties |
| 399 | 433 |
like connectivity, bipartiteness, euler property, simplicity etc. |
| 400 | 434 |
|
| 401 | 435 |
\image html edge_biconnected_components.png |
| 402 | 436 |
\image latex edge_biconnected_components.eps "bi-edge-connected components" width=\textwidth |
| 403 | 437 |
*/ |
| 404 | 438 |
|
| 405 | 439 |
/** |
| 406 | 440 |
@defgroup planar Planarity Embedding and Drawing |
| 407 | 441 |
@ingroup algs |
| 408 | 442 |
\brief Algorithms for planarity checking, embedding and drawing |
| 409 | 443 |
|
| 410 | 444 |
This group contains the algorithms for planarity checking, |
| 411 | 445 |
embedding and drawing. |
| 412 | 446 |
|
| 413 | 447 |
\image html planar.png |
| 414 | 448 |
\image latex planar.eps "Plane graph" width=\textwidth |
| 415 | 449 |
*/ |
| 416 | 450 |
|
| 417 | 451 |
/** |
| 418 | 452 |
@defgroup matching Matching Algorithms |
| 419 | 453 |
@ingroup algs |
| 420 | 454 |
\brief Algorithms for finding matchings in graphs and bipartite graphs. |
| 421 | 455 |
|
| 422 | 456 |
This group contains the algorithms for calculating |
| 423 | 457 |
matchings in graphs and bipartite graphs. The general matching problem is |
| 424 | 458 |
finding a subset of the edges for which each node has at most one incident |
| 425 | 459 |
edge. |
| 426 | 460 |
|
| 427 | 461 |
There are several different algorithms for calculate matchings in |
| 428 | 462 |
graphs. The matching problems in bipartite graphs are generally |
| 429 | 463 |
easier than in general graphs. The goal of the matching optimization |
| 430 | 464 |
can be finding maximum cardinality, maximum weight or minimum cost |
| 431 | 465 |
matching. The search can be constrained to find perfect or |
| 432 | 466 |
maximum cardinality matching. |
| 433 | 467 |
|
| 434 | 468 |
The matching algorithms implemented in LEMON: |
| 435 | 469 |
- \ref MaxBipartiteMatching Hopcroft-Karp augmenting path algorithm |
| 436 | 470 |
for calculating maximum cardinality matching in bipartite graphs. |
| 437 | 471 |
- \ref PrBipartiteMatching Push-relabel algorithm |
| 438 | 472 |
for calculating maximum cardinality matching in bipartite graphs. |
| 439 | 473 |
- \ref MaxWeightedBipartiteMatching |
| 440 | 474 |
Successive shortest path algorithm for calculating maximum weighted |
| 441 | 475 |
matching and maximum weighted bipartite matching in bipartite graphs. |
| 442 | 476 |
- \ref MinCostMaxBipartiteMatching |
| 443 | 477 |
Successive shortest path algorithm for calculating minimum cost maximum |
| 444 | 478 |
matching in bipartite graphs. |
| 445 | 479 |
- \ref MaxMatching Edmond's blossom shrinking algorithm for calculating |
| 446 | 480 |
maximum cardinality matching in general graphs. |
| 447 | 481 |
- \ref MaxWeightedMatching Edmond's blossom shrinking algorithm for calculating |
| 448 | 482 |
maximum weighted matching in general graphs. |
| 449 | 483 |
- \ref MaxWeightedPerfectMatching |
| 450 | 484 |
Edmond's blossom shrinking algorithm for calculating maximum weighted |
| 451 | 485 |
perfect matching in general graphs. |
| 452 | 486 |
|
| 453 | 487 |
\image html bipartite_matching.png |
| 454 | 488 |
\image latex bipartite_matching.eps "Bipartite Matching" width=\textwidth |
| 455 | 489 |
*/ |
| 456 | 490 |
|
| 457 | 491 |
/** |
| 458 | 492 |
@defgroup spantree Minimum Spanning Tree Algorithms |
| 459 | 493 |
@ingroup algs |
| 460 | 494 |
\brief Algorithms for finding minimum cost spanning trees and arborescences. |
| 461 | 495 |
|
| 462 | 496 |
This group contains the algorithms for finding minimum cost spanning |
| 463 | 497 |
trees and arborescences. |
| 464 | 498 |
*/ |
| 465 | 499 |
|
| 466 | 500 |
/** |
| 467 | 501 |
@defgroup auxalg Auxiliary Algorithms |
| 468 | 502 |
@ingroup algs |
| 469 | 503 |
\brief Auxiliary algorithms implemented in LEMON. |
| 470 | 504 |
|
| 471 | 505 |
This group contains some algorithms implemented in LEMON |
| 472 | 506 |
in order to make it easier to implement complex algorithms. |
| 473 | 507 |
*/ |
| 474 | 508 |
|
| 475 | 509 |
/** |
| 476 | 510 |
@defgroup approx Approximation Algorithms |
| 477 | 511 |
@ingroup algs |
| 478 | 512 |
\brief Approximation algorithms. |
| 479 | 513 |
|
| 480 | 514 |
This group contains the approximation and heuristic algorithms |
| 481 | 515 |
implemented in LEMON. |
| 482 | 516 |
*/ |
| 483 | 517 |
|
| 484 | 518 |
/** |
| 485 | 519 |
@defgroup gen_opt_group General Optimization Tools |
| 486 | 520 |
\brief This group contains some general optimization frameworks |
| 487 | 521 |
implemented in LEMON. |
| 488 | 522 |
|
| 489 | 523 |
This group contains some general optimization frameworks |
| 1 | 1 |
/* -*- C++ -*- |
| 2 | 2 |
* |
| 3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library |
| 4 | 4 |
* |
| 5 | 5 |
* Copyright (C) 2003-2008 |
| 6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
| 7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
| 8 | 8 |
* |
| 9 | 9 |
* Permission to use, modify and distribute this software is granted |
| 10 | 10 |
* provided that this copyright notice appears in all copies. For |
| 11 | 11 |
* precise terms see the accompanying LICENSE file. |
| 12 | 12 |
* |
| 13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
| 14 | 14 |
* express or implied, and with no claim as to its suitability for any |
| 15 | 15 |
* purpose. |
| 16 | 16 |
* |
| 17 | 17 |
*/ |
| 18 | 18 |
|
| 19 | 19 |
#ifndef LEMON_HARTMANN_ORLIN_H |
| 20 | 20 |
#define LEMON_HARTMANN_ORLIN_H |
| 21 | 21 |
|
| 22 |
/// \ingroup |
|
| 22 |
/// \ingroup min_mean_cycle |
|
| 23 | 23 |
/// |
| 24 | 24 |
/// \file |
| 25 | 25 |
/// \brief Hartmann-Orlin's algorithm for finding a minimum mean cycle. |
| 26 | 26 |
|
| 27 | 27 |
#include <vector> |
| 28 | 28 |
#include <limits> |
| 29 | 29 |
#include <lemon/core.h> |
| 30 | 30 |
#include <lemon/path.h> |
| 31 | 31 |
#include <lemon/tolerance.h> |
| 32 | 32 |
#include <lemon/connectivity.h> |
| 33 | 33 |
|
| 34 | 34 |
namespace lemon {
|
| 35 | 35 |
|
| 36 | 36 |
/// \brief Default traits class of HartmannOrlin algorithm. |
| 37 | 37 |
/// |
| 38 | 38 |
/// Default traits class of HartmannOrlin algorithm. |
| 39 | 39 |
/// \tparam GR The type of the digraph. |
| 40 | 40 |
/// \tparam LEN The type of the length map. |
| 41 | 41 |
/// It must conform to the \ref concepts::Rea_data "Rea_data" concept. |
| 42 | 42 |
#ifdef DOXYGEN |
| 43 | 43 |
template <typename GR, typename LEN> |
| 44 | 44 |
#else |
| 45 | 45 |
template <typename GR, typename LEN, |
| 46 | 46 |
bool integer = std::numeric_limits<typename LEN::Value>::is_integer> |
| 47 | 47 |
#endif |
| 48 | 48 |
struct HartmannOrlinDefaultTraits |
| 49 | 49 |
{
|
| 50 | 50 |
/// The type of the digraph |
| 51 | 51 |
typedef GR Digraph; |
| 52 | 52 |
/// The type of the length map |
| 53 | 53 |
typedef LEN LengthMap; |
| 54 | 54 |
/// The type of the arc lengths |
| 55 | 55 |
typedef typename LengthMap::Value Value; |
| 56 | 56 |
|
| 57 | 57 |
/// \brief The large value type used for internal computations |
| 58 | 58 |
/// |
| 59 | 59 |
/// The large value type used for internal computations. |
| 60 | 60 |
/// It is \c long \c long if the \c Value type is integer, |
| 61 | 61 |
/// otherwise it is \c double. |
| 62 | 62 |
/// \c Value must be convertible to \c LargeValue. |
| 63 | 63 |
typedef double LargeValue; |
| 64 | 64 |
|
| 65 | 65 |
/// The tolerance type used for internal computations |
| 66 | 66 |
typedef lemon::Tolerance<LargeValue> Tolerance; |
| 67 | 67 |
|
| 68 | 68 |
/// \brief The path type of the found cycles |
| 69 | 69 |
/// |
| 70 | 70 |
/// The path type of the found cycles. |
| 71 | 71 |
/// It must conform to the \ref lemon::concepts::Path "Path" concept |
| 72 | 72 |
/// and it must have an \c addBack() function. |
| 73 | 73 |
typedef lemon::Path<Digraph> Path; |
| 74 | 74 |
}; |
| 75 | 75 |
|
| 76 | 76 |
// Default traits class for integer value types |
| 77 | 77 |
template <typename GR, typename LEN> |
| 78 | 78 |
struct HartmannOrlinDefaultTraits<GR, LEN, true> |
| 79 | 79 |
{
|
| 80 | 80 |
typedef GR Digraph; |
| 81 | 81 |
typedef LEN LengthMap; |
| 82 | 82 |
typedef typename LengthMap::Value Value; |
| 83 | 83 |
#ifdef LEMON_HAVE_LONG_LONG |
| 84 | 84 |
typedef long long LargeValue; |
| 85 | 85 |
#else |
| 86 | 86 |
typedef long LargeValue; |
| 87 | 87 |
#endif |
| 88 | 88 |
typedef lemon::Tolerance<LargeValue> Tolerance; |
| 89 | 89 |
typedef lemon::Path<Digraph> Path; |
| 90 | 90 |
}; |
| 91 | 91 |
|
| 92 | 92 |
|
| 93 |
/// \addtogroup |
|
| 93 |
/// \addtogroup min_mean_cycle |
|
| 94 | 94 |
/// @{
|
| 95 | 95 |
|
| 96 | 96 |
/// \brief Implementation of the Hartmann-Orlin algorithm for finding |
| 97 | 97 |
/// a minimum mean cycle. |
| 98 | 98 |
/// |
| 99 | 99 |
/// This class implements the Hartmann-Orlin algorithm for finding |
| 100 | 100 |
/// a directed cycle of minimum mean length (cost) in a digraph. |
| 101 |
/// It is an improved version of \ref Karp "Karp's original algorithm |
|
| 101 |
/// It is an improved version of \ref Karp "Karp"'s original algorithm, |
|
| 102 | 102 |
/// it applies an efficient early termination scheme. |
| 103 |
/// It runs in time O(ne) and uses space O(n<sup>2</sup>+e). |
|
| 103 | 104 |
/// |
| 104 | 105 |
/// \tparam GR The type of the digraph the algorithm runs on. |
| 105 | 106 |
/// \tparam LEN The type of the length map. The default |
| 106 | 107 |
/// map type is \ref concepts::Digraph::ArcMap "GR::ArcMap<int>". |
| 107 | 108 |
#ifdef DOXYGEN |
| 108 | 109 |
template <typename GR, typename LEN, typename TR> |
| 109 | 110 |
#else |
| 110 | 111 |
template < typename GR, |
| 111 | 112 |
typename LEN = typename GR::template ArcMap<int>, |
| 112 | 113 |
typename TR = HartmannOrlinDefaultTraits<GR, LEN> > |
| 113 | 114 |
#endif |
| 114 | 115 |
class HartmannOrlin |
| 115 | 116 |
{
|
| 116 | 117 |
public: |
| 117 | 118 |
|
| 118 | 119 |
/// The type of the digraph |
| 119 | 120 |
typedef typename TR::Digraph Digraph; |
| 120 | 121 |
/// The type of the length map |
| 121 | 122 |
typedef typename TR::LengthMap LengthMap; |
| 122 | 123 |
/// The type of the arc lengths |
| 123 | 124 |
typedef typename TR::Value Value; |
| 124 | 125 |
|
| 125 | 126 |
/// \brief The large value type |
| 126 | 127 |
/// |
| 127 | 128 |
/// The large value type used for internal computations. |
| 128 | 129 |
/// Using the \ref HartmannOrlinDefaultTraits "default traits class", |
| 129 | 130 |
/// it is \c long \c long if the \c Value type is integer, |
| 130 | 131 |
/// otherwise it is \c double. |
| 131 | 132 |
typedef typename TR::LargeValue LargeValue; |
| 132 | 133 |
|
| 133 | 134 |
/// The tolerance type |
| 134 | 135 |
typedef typename TR::Tolerance Tolerance; |
| 135 | 136 |
|
| 136 | 137 |
/// \brief The path type of the found cycles |
| 137 | 138 |
/// |
| 138 | 139 |
/// The path type of the found cycles. |
| 139 | 140 |
/// Using the \ref HartmannOrlinDefaultTraits "default traits class", |
| 140 | 141 |
/// it is \ref lemon::Path "Path<Digraph>". |
| 141 | 142 |
typedef typename TR::Path Path; |
| 142 | 143 |
|
| 143 | 144 |
/// The \ref HartmannOrlinDefaultTraits "traits class" of the algorithm |
| 144 | 145 |
typedef TR Traits; |
| 145 | 146 |
|
| 146 | 147 |
private: |
| 147 | 148 |
|
| 148 | 149 |
TEMPLATE_DIGRAPH_TYPEDEFS(Digraph); |
| 149 | 150 |
|
| 150 | 151 |
// Data sturcture for path data |
| 151 | 152 |
struct PathData |
| 152 | 153 |
{
|
| 153 | 154 |
LargeValue dist; |
| 154 | 155 |
Arc pred; |
| 155 | 156 |
PathData(LargeValue d, Arc p = INVALID) : |
| 156 | 157 |
dist(d), pred(p) {}
|
| 157 | 158 |
}; |
| 158 | 159 |
|
| 159 | 160 |
typedef typename Digraph::template NodeMap<std::vector<PathData> > |
| 160 | 161 |
PathDataNodeMap; |
| 161 | 162 |
|
| 162 | 163 |
private: |
| 163 | 164 |
|
| 164 | 165 |
// The digraph the algorithm runs on |
| 165 | 166 |
const Digraph &_gr; |
| 166 | 167 |
// The length of the arcs |
| 167 | 168 |
const LengthMap &_length; |
| 168 | 169 |
|
| 169 | 170 |
// Data for storing the strongly connected components |
| 170 | 171 |
int _comp_num; |
| 171 | 172 |
typename Digraph::template NodeMap<int> _comp; |
| 172 | 173 |
std::vector<std::vector<Node> > _comp_nodes; |
| 173 | 174 |
std::vector<Node>* _nodes; |
| 174 | 175 |
typename Digraph::template NodeMap<std::vector<Arc> > _out_arcs; |
| 175 | 176 |
|
| 176 | 177 |
// Data for the found cycles |
| 177 | 178 |
bool _curr_found, _best_found; |
| 178 | 179 |
LargeValue _curr_length, _best_length; |
| 179 | 180 |
int _curr_size, _best_size; |
| 180 | 181 |
Node _curr_node, _best_node; |
| 181 | 182 |
int _curr_level, _best_level; |
| 182 | 183 |
|
| 183 | 184 |
Path *_cycle_path; |
| 184 | 185 |
bool _local_path; |
| 185 | 186 |
|
| 186 | 187 |
// Node map for storing path data |
| 187 | 188 |
PathDataNodeMap _data; |
| 188 | 189 |
// The processed nodes in the last round |
| 189 | 190 |
std::vector<Node> _process; |
| 190 | 191 |
|
| 191 | 192 |
Tolerance _tolerance; |
| 192 | 193 |
|
| 193 | 194 |
// Infinite constant |
| 194 | 195 |
const LargeValue INF; |
| 195 | 196 |
|
| 196 | 197 |
public: |
| 197 | 198 |
|
| 198 | 199 |
/// \name Named Template Parameters |
| 1 | 1 |
/* -*- C++ -*- |
| 2 | 2 |
* |
| 3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library |
| 4 | 4 |
* |
| 5 | 5 |
* Copyright (C) 2003-2008 |
| 6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
| 7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
| 8 | 8 |
* |
| 9 | 9 |
* Permission to use, modify and distribute this software is granted |
| 10 | 10 |
* provided that this copyright notice appears in all copies. For |
| 11 | 11 |
* precise terms see the accompanying LICENSE file. |
| 12 | 12 |
* |
| 13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
| 14 | 14 |
* express or implied, and with no claim as to its suitability for any |
| 15 | 15 |
* purpose. |
| 16 | 16 |
* |
| 17 | 17 |
*/ |
| 18 | 18 |
|
| 19 | 19 |
#ifndef LEMON_HOWARD_H |
| 20 | 20 |
#define LEMON_HOWARD_H |
| 21 | 21 |
|
| 22 |
/// \ingroup |
|
| 22 |
/// \ingroup min_mean_cycle |
|
| 23 | 23 |
/// |
| 24 | 24 |
/// \file |
| 25 | 25 |
/// \brief Howard's algorithm for finding a minimum mean cycle. |
| 26 | 26 |
|
| 27 | 27 |
#include <vector> |
| 28 | 28 |
#include <limits> |
| 29 | 29 |
#include <lemon/core.h> |
| 30 | 30 |
#include <lemon/path.h> |
| 31 | 31 |
#include <lemon/tolerance.h> |
| 32 | 32 |
#include <lemon/connectivity.h> |
| 33 | 33 |
|
| 34 | 34 |
namespace lemon {
|
| 35 | 35 |
|
| 36 | 36 |
/// \brief Default traits class of Howard class. |
| 37 | 37 |
/// |
| 38 | 38 |
/// Default traits class of Howard class. |
| 39 | 39 |
/// \tparam GR The type of the digraph. |
| 40 | 40 |
/// \tparam LEN The type of the length map. |
| 41 | 41 |
/// It must conform to the \ref concepts::ReadMap "ReadMap" concept. |
| 42 | 42 |
#ifdef DOXYGEN |
| 43 | 43 |
template <typename GR, typename LEN> |
| 44 | 44 |
#else |
| 45 | 45 |
template <typename GR, typename LEN, |
| 46 | 46 |
bool integer = std::numeric_limits<typename LEN::Value>::is_integer> |
| 47 | 47 |
#endif |
| 48 | 48 |
struct HowardDefaultTraits |
| 49 | 49 |
{
|
| 50 | 50 |
/// The type of the digraph |
| 51 | 51 |
typedef GR Digraph; |
| 52 | 52 |
/// The type of the length map |
| 53 | 53 |
typedef LEN LengthMap; |
| 54 | 54 |
/// The type of the arc lengths |
| 55 | 55 |
typedef typename LengthMap::Value Value; |
| 56 | 56 |
|
| 57 | 57 |
/// \brief The large value type used for internal computations |
| 58 | 58 |
/// |
| 59 | 59 |
/// The large value type used for internal computations. |
| 60 | 60 |
/// It is \c long \c long if the \c Value type is integer, |
| 61 | 61 |
/// otherwise it is \c double. |
| 62 | 62 |
/// \c Value must be convertible to \c LargeValue. |
| 63 | 63 |
typedef double LargeValue; |
| 64 | 64 |
|
| 65 | 65 |
/// The tolerance type used for internal computations |
| 66 | 66 |
typedef lemon::Tolerance<LargeValue> Tolerance; |
| 67 | 67 |
|
| 68 | 68 |
/// \brief The path type of the found cycles |
| 69 | 69 |
/// |
| 70 | 70 |
/// The path type of the found cycles. |
| 71 | 71 |
/// It must conform to the \ref lemon::concepts::Path "Path" concept |
| 72 | 72 |
/// and it must have an \c addBack() function. |
| 73 | 73 |
typedef lemon::Path<Digraph> Path; |
| 74 | 74 |
}; |
| 75 | 75 |
|
| 76 | 76 |
// Default traits class for integer value types |
| 77 | 77 |
template <typename GR, typename LEN> |
| 78 | 78 |
struct HowardDefaultTraits<GR, LEN, true> |
| 79 | 79 |
{
|
| 80 | 80 |
typedef GR Digraph; |
| 81 | 81 |
typedef LEN LengthMap; |
| 82 | 82 |
typedef typename LengthMap::Value Value; |
| 83 | 83 |
#ifdef LEMON_HAVE_LONG_LONG |
| 84 | 84 |
typedef long long LargeValue; |
| 85 | 85 |
#else |
| 86 | 86 |
typedef long LargeValue; |
| 87 | 87 |
#endif |
| 88 | 88 |
typedef lemon::Tolerance<LargeValue> Tolerance; |
| 89 | 89 |
typedef lemon::Path<Digraph> Path; |
| 90 | 90 |
}; |
| 91 | 91 |
|
| 92 | 92 |
|
| 93 |
/// \addtogroup |
|
| 93 |
/// \addtogroup min_mean_cycle |
|
| 94 | 94 |
/// @{
|
| 95 | 95 |
|
| 96 | 96 |
/// \brief Implementation of Howard's algorithm for finding a minimum |
| 97 | 97 |
/// mean cycle. |
| 98 | 98 |
/// |
| 99 | 99 |
/// This class implements Howard's policy iteration algorithm for finding |
| 100 | 100 |
/// a directed cycle of minimum mean length (cost) in a digraph. |
| 101 |
/// This class provides the most efficient algorithm for the |
|
| 102 |
/// minimum mean cycle problem, though the best known theoretical |
|
| 103 |
/// bound on its running time is exponential. |
|
| 101 | 104 |
/// |
| 102 | 105 |
/// \tparam GR The type of the digraph the algorithm runs on. |
| 103 | 106 |
/// \tparam LEN The type of the length map. The default |
| 104 | 107 |
/// map type is \ref concepts::Digraph::ArcMap "GR::ArcMap<int>". |
| 105 | 108 |
#ifdef DOXYGEN |
| 106 | 109 |
template <typename GR, typename LEN, typename TR> |
| 107 | 110 |
#else |
| 108 | 111 |
template < typename GR, |
| 109 | 112 |
typename LEN = typename GR::template ArcMap<int>, |
| 110 | 113 |
typename TR = HowardDefaultTraits<GR, LEN> > |
| 111 | 114 |
#endif |
| 112 | 115 |
class Howard |
| 113 | 116 |
{
|
| 114 | 117 |
public: |
| 115 | 118 |
|
| 116 | 119 |
/// The type of the digraph |
| 117 | 120 |
typedef typename TR::Digraph Digraph; |
| 118 | 121 |
/// The type of the length map |
| 119 | 122 |
typedef typename TR::LengthMap LengthMap; |
| 120 | 123 |
/// The type of the arc lengths |
| 121 | 124 |
typedef typename TR::Value Value; |
| 122 | 125 |
|
| 123 | 126 |
/// \brief The large value type |
| 124 | 127 |
/// |
| 125 | 128 |
/// The large value type used for internal computations. |
| 126 | 129 |
/// Using the \ref HowardDefaultTraits "default traits class", |
| 127 | 130 |
/// it is \c long \c long if the \c Value type is integer, |
| 128 | 131 |
/// otherwise it is \c double. |
| 129 | 132 |
typedef typename TR::LargeValue LargeValue; |
| 130 | 133 |
|
| 131 | 134 |
/// The tolerance type |
| 132 | 135 |
typedef typename TR::Tolerance Tolerance; |
| 133 | 136 |
|
| 134 | 137 |
/// \brief The path type of the found cycles |
| 135 | 138 |
/// |
| 136 | 139 |
/// The path type of the found cycles. |
| 137 | 140 |
/// Using the \ref HowardDefaultTraits "default traits class", |
| 138 | 141 |
/// it is \ref lemon::Path "Path<Digraph>". |
| 139 | 142 |
typedef typename TR::Path Path; |
| 140 | 143 |
|
| 141 | 144 |
/// The \ref HowardDefaultTraits "traits class" of the algorithm |
| 142 | 145 |
typedef TR Traits; |
| 143 | 146 |
|
| 144 | 147 |
private: |
| 145 | 148 |
|
| 146 | 149 |
TEMPLATE_DIGRAPH_TYPEDEFS(Digraph); |
| 147 | 150 |
|
| 148 | 151 |
// The digraph the algorithm runs on |
| 149 | 152 |
const Digraph &_gr; |
| 150 | 153 |
// The length of the arcs |
| 151 | 154 |
const LengthMap &_length; |
| 152 | 155 |
|
| 153 | 156 |
// Data for the found cycles |
| 154 | 157 |
bool _curr_found, _best_found; |
| 155 | 158 |
LargeValue _curr_length, _best_length; |
| 156 | 159 |
int _curr_size, _best_size; |
| 157 | 160 |
Node _curr_node, _best_node; |
| 158 | 161 |
|
| 159 | 162 |
Path *_cycle_path; |
| 160 | 163 |
bool _local_path; |
| 161 | 164 |
|
| 162 | 165 |
// Internal data used by the algorithm |
| 163 | 166 |
typename Digraph::template NodeMap<Arc> _policy; |
| 164 | 167 |
typename Digraph::template NodeMap<bool> _reached; |
| 165 | 168 |
typename Digraph::template NodeMap<int> _level; |
| 166 | 169 |
typename Digraph::template NodeMap<LargeValue> _dist; |
| 167 | 170 |
|
| 168 | 171 |
// Data for storing the strongly connected components |
| 169 | 172 |
int _comp_num; |
| 170 | 173 |
typename Digraph::template NodeMap<int> _comp; |
| 171 | 174 |
std::vector<std::vector<Node> > _comp_nodes; |
| 172 | 175 |
std::vector<Node>* _nodes; |
| 173 | 176 |
typename Digraph::template NodeMap<std::vector<Arc> > _in_arcs; |
| 174 | 177 |
|
| 175 | 178 |
// Queue used for BFS search |
| 176 | 179 |
std::vector<Node> _queue; |
| 177 | 180 |
int _qfront, _qback; |
| 178 | 181 |
|
| 179 | 182 |
Tolerance _tolerance; |
| 180 | 183 |
|
| 181 | 184 |
// Infinite constant |
| 182 | 185 |
const LargeValue INF; |
| 183 | 186 |
|
| 184 | 187 |
public: |
| 185 | 188 |
|
| 186 | 189 |
/// \name Named Template Parameters |
| 187 | 190 |
/// @{
|
| 188 | 191 |
|
| 189 | 192 |
template <typename T> |
| 190 | 193 |
struct SetLargeValueTraits : public Traits {
|
| 191 | 194 |
typedef T LargeValue; |
| 192 | 195 |
typedef lemon::Tolerance<T> Tolerance; |
| 193 | 196 |
}; |
| 194 | 197 |
|
| 195 | 198 |
/// \brief \ref named-templ-param "Named parameter" for setting |
| 196 | 199 |
/// \c LargeValue type. |
| 1 | 1 |
/* -*- C++ -*- |
| 2 | 2 |
* |
| 3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library |
| 4 | 4 |
* |
| 5 | 5 |
* Copyright (C) 2003-2008 |
| 6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
| 7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
| 8 | 8 |
* |
| 9 | 9 |
* Permission to use, modify and distribute this software is granted |
| 10 | 10 |
* provided that this copyright notice appears in all copies. For |
| 11 | 11 |
* precise terms see the accompanying LICENSE file. |
| 12 | 12 |
* |
| 13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
| 14 | 14 |
* express or implied, and with no claim as to its suitability for any |
| 15 | 15 |
* purpose. |
| 16 | 16 |
* |
| 17 | 17 |
*/ |
| 18 | 18 |
|
| 19 | 19 |
#ifndef LEMON_KARP_H |
| 20 | 20 |
#define LEMON_KARP_H |
| 21 | 21 |
|
| 22 |
/// \ingroup |
|
| 22 |
/// \ingroup min_mean_cycle |
|
| 23 | 23 |
/// |
| 24 | 24 |
/// \file |
| 25 | 25 |
/// \brief Karp's algorithm for finding a minimum mean cycle. |
| 26 | 26 |
|
| 27 | 27 |
#include <vector> |
| 28 | 28 |
#include <limits> |
| 29 | 29 |
#include <lemon/core.h> |
| 30 | 30 |
#include <lemon/path.h> |
| 31 | 31 |
#include <lemon/tolerance.h> |
| 32 | 32 |
#include <lemon/connectivity.h> |
| 33 | 33 |
|
| 34 | 34 |
namespace lemon {
|
| 35 | 35 |
|
| 36 | 36 |
/// \brief Default traits class of Karp algorithm. |
| 37 | 37 |
/// |
| 38 | 38 |
/// Default traits class of Karp algorithm. |
| 39 | 39 |
/// \tparam GR The type of the digraph. |
| 40 | 40 |
/// \tparam LEN The type of the length map. |
| 41 | 41 |
/// It must conform to the \ref concepts::ReadMap "ReadMap" concept. |
| 42 | 42 |
#ifdef DOXYGEN |
| 43 | 43 |
template <typename GR, typename LEN> |
| 44 | 44 |
#else |
| 45 | 45 |
template <typename GR, typename LEN, |
| 46 | 46 |
bool integer = std::numeric_limits<typename LEN::Value>::is_integer> |
| 47 | 47 |
#endif |
| 48 | 48 |
struct KarpDefaultTraits |
| 49 | 49 |
{
|
| 50 | 50 |
/// The type of the digraph |
| 51 | 51 |
typedef GR Digraph; |
| 52 | 52 |
/// The type of the length map |
| 53 | 53 |
typedef LEN LengthMap; |
| 54 | 54 |
/// The type of the arc lengths |
| 55 | 55 |
typedef typename LengthMap::Value Value; |
| 56 | 56 |
|
| 57 | 57 |
/// \brief The large value type used for internal computations |
| 58 | 58 |
/// |
| 59 | 59 |
/// The large value type used for internal computations. |
| 60 | 60 |
/// It is \c long \c long if the \c Value type is integer, |
| 61 | 61 |
/// otherwise it is \c double. |
| 62 | 62 |
/// \c Value must be convertible to \c LargeValue. |
| 63 | 63 |
typedef double LargeValue; |
| 64 | 64 |
|
| 65 | 65 |
/// The tolerance type used for internal computations |
| 66 | 66 |
typedef lemon::Tolerance<LargeValue> Tolerance; |
| 67 | 67 |
|
| 68 | 68 |
/// \brief The path type of the found cycles |
| 69 | 69 |
/// |
| 70 | 70 |
/// The path type of the found cycles. |
| 71 | 71 |
/// It must conform to the \ref lemon::concepts::Path "Path" concept |
| 72 | 72 |
/// and it must have an \c addBack() function. |
| 73 | 73 |
typedef lemon::Path<Digraph> Path; |
| 74 | 74 |
}; |
| 75 | 75 |
|
| 76 | 76 |
// Default traits class for integer value types |
| 77 | 77 |
template <typename GR, typename LEN> |
| 78 | 78 |
struct KarpDefaultTraits<GR, LEN, true> |
| 79 | 79 |
{
|
| 80 | 80 |
typedef GR Digraph; |
| 81 | 81 |
typedef LEN LengthMap; |
| 82 | 82 |
typedef typename LengthMap::Value Value; |
| 83 | 83 |
#ifdef LEMON_HAVE_LONG_LONG |
| 84 | 84 |
typedef long long LargeValue; |
| 85 | 85 |
#else |
| 86 | 86 |
typedef long LargeValue; |
| 87 | 87 |
#endif |
| 88 | 88 |
typedef lemon::Tolerance<LargeValue> Tolerance; |
| 89 | 89 |
typedef lemon::Path<Digraph> Path; |
| 90 | 90 |
}; |
| 91 | 91 |
|
| 92 | 92 |
|
| 93 |
/// \addtogroup |
|
| 93 |
/// \addtogroup min_mean_cycle |
|
| 94 | 94 |
/// @{
|
| 95 | 95 |
|
| 96 | 96 |
/// \brief Implementation of Karp's algorithm for finding a minimum |
| 97 | 97 |
/// mean cycle. |
| 98 | 98 |
/// |
| 99 | 99 |
/// This class implements Karp's algorithm for finding a directed |
| 100 | 100 |
/// cycle of minimum mean length (cost) in a digraph. |
| 101 |
/// It runs in time O(ne) and uses space O(n<sup>2</sup>+e). |
|
| 101 | 102 |
/// |
| 102 | 103 |
/// \tparam GR The type of the digraph the algorithm runs on. |
| 103 | 104 |
/// \tparam LEN The type of the length map. The default |
| 104 | 105 |
/// map type is \ref concepts::Digraph::ArcMap "GR::ArcMap<int>". |
| 105 | 106 |
#ifdef DOXYGEN |
| 106 | 107 |
template <typename GR, typename LEN, typename TR> |
| 107 | 108 |
#else |
| 108 | 109 |
template < typename GR, |
| 109 | 110 |
typename LEN = typename GR::template ArcMap<int>, |
| 110 | 111 |
typename TR = KarpDefaultTraits<GR, LEN> > |
| 111 | 112 |
#endif |
| 112 | 113 |
class Karp |
| 113 | 114 |
{
|
| 114 | 115 |
public: |
| 115 | 116 |
|
| 116 | 117 |
/// The type of the digraph |
| 117 | 118 |
typedef typename TR::Digraph Digraph; |
| 118 | 119 |
/// The type of the length map |
| 119 | 120 |
typedef typename TR::LengthMap LengthMap; |
| 120 | 121 |
/// The type of the arc lengths |
| 121 | 122 |
typedef typename TR::Value Value; |
| 122 | 123 |
|
| 123 | 124 |
/// \brief The large value type |
| 124 | 125 |
/// |
| 125 | 126 |
/// The large value type used for internal computations. |
| 126 | 127 |
/// Using the \ref KarpDefaultTraits "default traits class", |
| 127 | 128 |
/// it is \c long \c long if the \c Value type is integer, |
| 128 | 129 |
/// otherwise it is \c double. |
| 129 | 130 |
typedef typename TR::LargeValue LargeValue; |
| 130 | 131 |
|
| 131 | 132 |
/// The tolerance type |
| 132 | 133 |
typedef typename TR::Tolerance Tolerance; |
| 133 | 134 |
|
| 134 | 135 |
/// \brief The path type of the found cycles |
| 135 | 136 |
/// |
| 136 | 137 |
/// The path type of the found cycles. |
| 137 | 138 |
/// Using the \ref KarpDefaultTraits "default traits class", |
| 138 | 139 |
/// it is \ref lemon::Path "Path<Digraph>". |
| 139 | 140 |
typedef typename TR::Path Path; |
| 140 | 141 |
|
| 141 | 142 |
/// The \ref KarpDefaultTraits "traits class" of the algorithm |
| 142 | 143 |
typedef TR Traits; |
| 143 | 144 |
|
| 144 | 145 |
private: |
| 145 | 146 |
|
| 146 | 147 |
TEMPLATE_DIGRAPH_TYPEDEFS(Digraph); |
| 147 | 148 |
|
| 148 | 149 |
// Data sturcture for path data |
| 149 | 150 |
struct PathData |
| 150 | 151 |
{
|
| 151 | 152 |
LargeValue dist; |
| 152 | 153 |
Arc pred; |
| 153 | 154 |
PathData(LargeValue d, Arc p = INVALID) : |
| 154 | 155 |
dist(d), pred(p) {}
|
| 155 | 156 |
}; |
| 156 | 157 |
|
| 157 | 158 |
typedef typename Digraph::template NodeMap<std::vector<PathData> > |
| 158 | 159 |
PathDataNodeMap; |
| 159 | 160 |
|
| 160 | 161 |
private: |
| 161 | 162 |
|
| 162 | 163 |
// The digraph the algorithm runs on |
| 163 | 164 |
const Digraph &_gr; |
| 164 | 165 |
// The length of the arcs |
| 165 | 166 |
const LengthMap &_length; |
| 166 | 167 |
|
| 167 | 168 |
// Data for storing the strongly connected components |
| 168 | 169 |
int _comp_num; |
| 169 | 170 |
typename Digraph::template NodeMap<int> _comp; |
| 170 | 171 |
std::vector<std::vector<Node> > _comp_nodes; |
| 171 | 172 |
std::vector<Node>* _nodes; |
| 172 | 173 |
typename Digraph::template NodeMap<std::vector<Arc> > _out_arcs; |
| 173 | 174 |
|
| 174 | 175 |
// Data for the found cycle |
| 175 | 176 |
LargeValue _cycle_length; |
| 176 | 177 |
int _cycle_size; |
| 177 | 178 |
Node _cycle_node; |
| 178 | 179 |
|
| 179 | 180 |
Path *_cycle_path; |
| 180 | 181 |
bool _local_path; |
| 181 | 182 |
|
| 182 | 183 |
// Node map for storing path data |
| 183 | 184 |
PathDataNodeMap _data; |
| 184 | 185 |
// The processed nodes in the last round |
| 185 | 186 |
std::vector<Node> _process; |
| 186 | 187 |
|
| 187 | 188 |
Tolerance _tolerance; |
| 188 | 189 |
|
| 189 | 190 |
// Infinite constant |
| 190 | 191 |
const LargeValue INF; |
| 191 | 192 |
|
| 192 | 193 |
public: |
| 193 | 194 |
|
| 194 | 195 |
/// \name Named Template Parameters |
| 195 | 196 |
/// @{
|
| 196 | 197 |
|
0 comments (0 inline)