0
4
0
... | ... |
@@ -364,212 +364,215 @@ |
364 | 364 |
digraph, a \f$cap: A\rightarrow\mathbf{R}^+_0\f$ capacity function and |
365 | 365 |
\f$s, t \in V\f$ source and target nodes. |
366 | 366 |
A maximum flow is an \f$f: A\rightarrow\mathbf{R}^+_0\f$ solution of the |
367 | 367 |
following optimization problem. |
368 | 368 |
|
369 | 369 |
\f[ \max\sum_{sv\in A} f(sv) - \sum_{vs\in A} f(vs) \f] |
370 | 370 |
\f[ \sum_{uv\in A} f(uv) = \sum_{vu\in A} f(vu) |
371 | 371 |
\quad \forall u\in V\setminus\{s,t\} \f] |
372 | 372 |
\f[ 0 \leq f(uv) \leq cap(uv) \quad \forall uv\in A \f] |
373 | 373 |
|
374 | 374 |
LEMON contains several algorithms for solving maximum flow problems: |
375 | 375 |
- \ref EdmondsKarp Edmonds-Karp algorithm |
376 | 376 |
\ref edmondskarp72theoretical. |
377 | 377 |
- \ref Preflow Goldberg-Tarjan's preflow push-relabel algorithm |
378 | 378 |
\ref goldberg88newapproach. |
379 | 379 |
- \ref DinitzSleatorTarjan Dinitz's blocking flow algorithm with dynamic trees |
380 | 380 |
\ref dinic70algorithm, \ref sleator83dynamic. |
381 | 381 |
- \ref GoldbergTarjan !Preflow push-relabel algorithm with dynamic trees |
382 | 382 |
\ref goldberg88newapproach, \ref sleator83dynamic. |
383 | 383 |
|
384 | 384 |
In most cases the \ref Preflow algorithm provides the |
385 | 385 |
fastest method for computing a maximum flow. All implementations |
386 | 386 |
also provide functions to query the minimum cut, which is the dual |
387 | 387 |
problem of maximum flow. |
388 | 388 |
|
389 | 389 |
\ref Circulation is a preflow push-relabel algorithm implemented directly |
390 | 390 |
for finding feasible circulations, which is a somewhat different problem, |
391 | 391 |
but it is strongly related to maximum flow. |
392 | 392 |
For more information, see \ref Circulation. |
393 | 393 |
*/ |
394 | 394 |
|
395 | 395 |
/** |
396 | 396 |
@defgroup min_cost_flow_algs Minimum Cost Flow Algorithms |
397 | 397 |
@ingroup algs |
398 | 398 |
|
399 | 399 |
\brief Algorithms for finding minimum cost flows and circulations. |
400 | 400 |
|
401 | 401 |
This group contains the algorithms for finding minimum cost flows and |
402 | 402 |
circulations \ref amo93networkflows. For more information about this |
403 | 403 |
problem and its dual solution, see \ref min_cost_flow |
404 | 404 |
"Minimum Cost Flow Problem". |
405 | 405 |
|
406 | 406 |
LEMON contains several algorithms for this problem. |
407 | 407 |
- \ref NetworkSimplex Primal Network Simplex algorithm with various |
408 | 408 |
pivot strategies \ref dantzig63linearprog, \ref kellyoneill91netsimplex. |
409 | 409 |
- \ref CostScaling Push-Relabel and Augment-Relabel algorithms based on |
410 | 410 |
cost scaling \ref goldberg90approximation, \ref goldberg97efficient, |
411 | 411 |
\ref bunnagel98efficient. |
412 | 412 |
- \ref CapacityScaling Successive Shortest %Path algorithm with optional |
413 | 413 |
capacity scaling \ref edmondskarp72theoretical. |
414 | 414 |
- \ref CancelAndTighten The Cancel and Tighten algorithm |
415 | 415 |
\ref goldberg89cyclecanceling. |
416 | 416 |
- \ref CycleCanceling Cycle-Canceling algorithms |
417 | 417 |
\ref klein67primal, \ref goldberg89cyclecanceling. |
418 | 418 |
|
419 | 419 |
In general NetworkSimplex is the most efficient implementation, |
420 | 420 |
but in special cases other algorithms could be faster. |
421 | 421 |
For example, if the total supply and/or capacities are rather small, |
422 | 422 |
CapacityScaling is usually the fastest algorithm (without effective scaling). |
423 | 423 |
*/ |
424 | 424 |
|
425 | 425 |
/** |
426 | 426 |
@defgroup min_cut Minimum Cut Algorithms |
427 | 427 |
@ingroup algs |
428 | 428 |
|
429 | 429 |
\brief Algorithms for finding minimum cut in graphs. |
430 | 430 |
|
431 | 431 |
This group contains the algorithms for finding minimum cut in graphs. |
432 | 432 |
|
433 | 433 |
The \e minimum \e cut \e problem is to find a non-empty and non-complete |
434 | 434 |
\f$X\f$ subset of the nodes with minimum overall capacity on |
435 | 435 |
outgoing arcs. Formally, there is a \f$G=(V,A)\f$ digraph, a |
436 | 436 |
\f$cap: A\rightarrow\mathbf{R}^+_0\f$ capacity function. The minimum |
437 | 437 |
cut is the \f$X\f$ solution of the next optimization problem: |
438 | 438 |
|
439 | 439 |
\f[ \min_{X \subset V, X\not\in \{\emptyset, V\}} |
440 | 440 |
\sum_{uv\in A: u\in X, v\not\in X}cap(uv) \f] |
441 | 441 |
|
442 | 442 |
LEMON contains several algorithms related to minimum cut problems: |
443 | 443 |
|
444 | 444 |
- \ref HaoOrlin "Hao-Orlin algorithm" for calculating minimum cut |
445 | 445 |
in directed graphs. |
446 | 446 |
- \ref NagamochiIbaraki "Nagamochi-Ibaraki algorithm" for |
447 | 447 |
calculating minimum cut in undirected graphs. |
448 | 448 |
- \ref GomoryHu "Gomory-Hu tree computation" for calculating |
449 | 449 |
all-pairs minimum cut in undirected graphs. |
450 | 450 |
|
451 | 451 |
If you want to find minimum cut just between two distinict nodes, |
452 | 452 |
see the \ref max_flow "maximum flow problem". |
453 | 453 |
*/ |
454 | 454 |
|
455 | 455 |
/** |
456 | 456 |
@defgroup min_mean_cycle Minimum Mean Cycle Algorithms |
457 | 457 |
@ingroup algs |
458 | 458 |
\brief Algorithms for finding minimum mean cycles. |
459 | 459 |
|
460 |
This group contains the algorithms for finding minimum mean cycles |
|
460 |
This group contains the algorithms for finding minimum mean cycles |
|
461 |
\ref clrs01algorithms, \ref amo93networkflows. |
|
461 | 462 |
|
462 | 463 |
The \e minimum \e mean \e cycle \e problem is to find a directed cycle |
463 | 464 |
of minimum mean length (cost) in a digraph. |
464 | 465 |
The mean length of a cycle is the average length of its arcs, i.e. the |
465 | 466 |
ratio between the total length of the cycle and the number of arcs on it. |
466 | 467 |
|
467 | 468 |
This problem has an important connection to \e conservative \e length |
468 | 469 |
\e functions, too. A length function on the arcs of a digraph is called |
469 | 470 |
conservative if and only if there is no directed cycle of negative total |
470 | 471 |
length. For an arbitrary length function, the negative of the minimum |
471 | 472 |
cycle mean is the smallest \f$\epsilon\f$ value so that increasing the |
472 | 473 |
arc lengths uniformly by \f$\epsilon\f$ results in a conservative length |
473 | 474 |
function. |
474 | 475 |
|
475 | 476 |
LEMON contains three algorithms for solving the minimum mean cycle problem: |
476 |
- \ref Karp "Karp"'s original algorithm |
|
477 |
- \ref Karp "Karp"'s original algorithm \ref amo93networkflows, |
|
478 |
\ref dasdan98minmeancycle. |
|
477 | 479 |
- \ref HartmannOrlin "Hartmann-Orlin"'s algorithm, which is an improved |
478 |
version of Karp's algorithm. |
|
479 |
- \ref Howard "Howard"'s policy iteration algorithm. |
|
480 |
version of Karp's algorithm \ref dasdan98minmeancycle. |
|
481 |
- \ref Howard "Howard"'s policy iteration algorithm |
|
482 |
\ref dasdan98minmeancycle. |
|
480 | 483 |
|
481 | 484 |
In practice, the Howard algorithm proved to be by far the most efficient |
482 | 485 |
one, though the best known theoretical bound on its running time is |
483 | 486 |
exponential. |
484 | 487 |
Both Karp and HartmannOrlin algorithms run in time O(ne) and use space |
485 | 488 |
O(n<sup>2</sup>+e), but the latter one is typically faster due to the |
486 | 489 |
applied early termination scheme. |
487 | 490 |
*/ |
488 | 491 |
|
489 | 492 |
/** |
490 | 493 |
@defgroup matching Matching Algorithms |
491 | 494 |
@ingroup algs |
492 | 495 |
\brief Algorithms for finding matchings in graphs and bipartite graphs. |
493 | 496 |
|
494 | 497 |
This group contains the algorithms for calculating |
495 | 498 |
matchings in graphs and bipartite graphs. The general matching problem is |
496 | 499 |
finding a subset of the edges for which each node has at most one incident |
497 | 500 |
edge. |
498 | 501 |
|
499 | 502 |
There are several different algorithms for calculate matchings in |
500 | 503 |
graphs. The matching problems in bipartite graphs are generally |
501 | 504 |
easier than in general graphs. The goal of the matching optimization |
502 | 505 |
can be finding maximum cardinality, maximum weight or minimum cost |
503 | 506 |
matching. The search can be constrained to find perfect or |
504 | 507 |
maximum cardinality matching. |
505 | 508 |
|
506 | 509 |
The matching algorithms implemented in LEMON: |
507 | 510 |
- \ref MaxBipartiteMatching Hopcroft-Karp augmenting path algorithm |
508 | 511 |
for calculating maximum cardinality matching in bipartite graphs. |
509 | 512 |
- \ref PrBipartiteMatching Push-relabel algorithm |
510 | 513 |
for calculating maximum cardinality matching in bipartite graphs. |
511 | 514 |
- \ref MaxWeightedBipartiteMatching |
512 | 515 |
Successive shortest path algorithm for calculating maximum weighted |
513 | 516 |
matching and maximum weighted bipartite matching in bipartite graphs. |
514 | 517 |
- \ref MinCostMaxBipartiteMatching |
515 | 518 |
Successive shortest path algorithm for calculating minimum cost maximum |
516 | 519 |
matching in bipartite graphs. |
517 | 520 |
- \ref MaxMatching Edmond's blossom shrinking algorithm for calculating |
518 | 521 |
maximum cardinality matching in general graphs. |
519 | 522 |
- \ref MaxWeightedMatching Edmond's blossom shrinking algorithm for calculating |
520 | 523 |
maximum weighted matching in general graphs. |
521 | 524 |
- \ref MaxWeightedPerfectMatching |
522 | 525 |
Edmond's blossom shrinking algorithm for calculating maximum weighted |
523 | 526 |
perfect matching in general graphs. |
524 | 527 |
|
525 | 528 |
\image html bipartite_matching.png |
526 | 529 |
\image latex bipartite_matching.eps "Bipartite Matching" width=\textwidth |
527 | 530 |
*/ |
528 | 531 |
|
529 | 532 |
/** |
530 | 533 |
@defgroup graph_properties Connectivity and Other Graph Properties |
531 | 534 |
@ingroup algs |
532 | 535 |
\brief Algorithms for discovering the graph properties |
533 | 536 |
|
534 | 537 |
This group contains the algorithms for discovering the graph properties |
535 | 538 |
like connectivity, bipartiteness, euler property, simplicity etc. |
536 | 539 |
|
537 | 540 |
\image html connected_components.png |
538 | 541 |
\image latex connected_components.eps "Connected components" width=\textwidth |
539 | 542 |
*/ |
540 | 543 |
|
541 | 544 |
/** |
542 | 545 |
@defgroup planar Planarity Embedding and Drawing |
543 | 546 |
@ingroup algs |
544 | 547 |
\brief Algorithms for planarity checking, embedding and drawing |
545 | 548 |
|
546 | 549 |
This group contains the algorithms for planarity checking, |
547 | 550 |
embedding and drawing. |
548 | 551 |
|
549 | 552 |
\image html planar.png |
550 | 553 |
\image latex planar.eps "Plane graph" width=\textwidth |
551 | 554 |
*/ |
552 | 555 |
|
553 | 556 |
/** |
554 | 557 |
@defgroup approx Approximation Algorithms |
555 | 558 |
@ingroup algs |
556 | 559 |
\brief Approximation algorithms. |
557 | 560 |
|
558 | 561 |
This group contains the approximation and heuristic algorithms |
559 | 562 |
implemented in LEMON. |
560 | 563 |
*/ |
561 | 564 |
|
562 | 565 |
/** |
563 | 566 |
@defgroup auxalg Auxiliary Algorithms |
564 | 567 |
@ingroup algs |
565 | 568 |
\brief Auxiliary algorithms implemented in LEMON. |
566 | 569 |
|
567 | 570 |
This group contains some algorithms implemented in LEMON |
568 | 571 |
in order to make it easier to implement complex algorithms. |
569 | 572 |
*/ |
570 | 573 |
|
571 | 574 |
/** |
572 | 575 |
@defgroup gen_opt_group General Optimization Tools |
573 | 576 |
\brief This group contains some general optimization frameworks |
574 | 577 |
implemented in LEMON. |
575 | 578 |
... | ... |
@@ -4,193 +4,194 @@ |
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 | 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 | 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 |
/// a directed cycle of minimum mean length (cost) in a digraph |
|
100 |
/// a directed cycle of minimum mean length (cost) in a digraph |
|
101 |
/// \ref amo93networkflows, \ref dasdan98minmeancycle. |
|
101 | 102 |
/// It is an improved version of \ref Karp "Karp"'s original algorithm, |
102 | 103 |
/// it applies an efficient early termination scheme. |
103 | 104 |
/// It runs in time O(ne) and uses space O(n<sup>2</sup>+e). |
104 | 105 |
/// |
105 | 106 |
/// \tparam GR The type of the digraph the algorithm runs on. |
106 | 107 |
/// \tparam LEN The type of the length map. The default |
107 | 108 |
/// map type is \ref concepts::Digraph::ArcMap "GR::ArcMap<int>". |
108 | 109 |
#ifdef DOXYGEN |
109 | 110 |
template <typename GR, typename LEN, typename TR> |
110 | 111 |
#else |
111 | 112 |
template < typename GR, |
112 | 113 |
typename LEN = typename GR::template ArcMap<int>, |
113 | 114 |
typename TR = HartmannOrlinDefaultTraits<GR, LEN> > |
114 | 115 |
#endif |
115 | 116 |
class HartmannOrlin |
116 | 117 |
{ |
117 | 118 |
public: |
118 | 119 |
|
119 | 120 |
/// The type of the digraph |
120 | 121 |
typedef typename TR::Digraph Digraph; |
121 | 122 |
/// The type of the length map |
122 | 123 |
typedef typename TR::LengthMap LengthMap; |
123 | 124 |
/// The type of the arc lengths |
124 | 125 |
typedef typename TR::Value Value; |
125 | 126 |
|
126 | 127 |
/// \brief The large value type |
127 | 128 |
/// |
128 | 129 |
/// The large value type used for internal computations. |
129 | 130 |
/// Using the \ref HartmannOrlinDefaultTraits "default traits class", |
130 | 131 |
/// it is \c long \c long if the \c Value type is integer, |
131 | 132 |
/// otherwise it is \c double. |
132 | 133 |
typedef typename TR::LargeValue LargeValue; |
133 | 134 |
|
134 | 135 |
/// The tolerance type |
135 | 136 |
typedef typename TR::Tolerance Tolerance; |
136 | 137 |
|
137 | 138 |
/// \brief The path type of the found cycles |
138 | 139 |
/// |
139 | 140 |
/// The path type of the found cycles. |
140 | 141 |
/// Using the \ref HartmannOrlinDefaultTraits "default traits class", |
141 | 142 |
/// it is \ref lemon::Path "Path<Digraph>". |
142 | 143 |
typedef typename TR::Path Path; |
143 | 144 |
|
144 | 145 |
/// The \ref HartmannOrlinDefaultTraits "traits class" of the algorithm |
145 | 146 |
typedef TR Traits; |
146 | 147 |
|
147 | 148 |
private: |
148 | 149 |
|
149 | 150 |
TEMPLATE_DIGRAPH_TYPEDEFS(Digraph); |
150 | 151 |
|
151 | 152 |
// Data sturcture for path data |
152 | 153 |
struct PathData |
153 | 154 |
{ |
154 | 155 |
LargeValue dist; |
155 | 156 |
Arc pred; |
156 | 157 |
PathData(LargeValue d, Arc p = INVALID) : |
157 | 158 |
dist(d), pred(p) {} |
158 | 159 |
}; |
159 | 160 |
|
160 | 161 |
typedef typename Digraph::template NodeMap<std::vector<PathData> > |
161 | 162 |
PathDataNodeMap; |
162 | 163 |
|
163 | 164 |
private: |
164 | 165 |
|
165 | 166 |
// The digraph the algorithm runs on |
166 | 167 |
const Digraph &_gr; |
167 | 168 |
// The length of the arcs |
168 | 169 |
const LengthMap &_length; |
169 | 170 |
|
170 | 171 |
// Data for storing the strongly connected components |
171 | 172 |
int _comp_num; |
172 | 173 |
typename Digraph::template NodeMap<int> _comp; |
173 | 174 |
std::vector<std::vector<Node> > _comp_nodes; |
174 | 175 |
std::vector<Node>* _nodes; |
175 | 176 |
typename Digraph::template NodeMap<std::vector<Arc> > _out_arcs; |
176 | 177 |
|
177 | 178 |
// Data for the found cycles |
178 | 179 |
bool _curr_found, _best_found; |
179 | 180 |
LargeValue _curr_length, _best_length; |
180 | 181 |
int _curr_size, _best_size; |
181 | 182 |
Node _curr_node, _best_node; |
182 | 183 |
int _curr_level, _best_level; |
183 | 184 |
|
184 | 185 |
Path *_cycle_path; |
185 | 186 |
bool _local_path; |
186 | 187 |
|
187 | 188 |
// Node map for storing path data |
188 | 189 |
PathDataNodeMap _data; |
189 | 190 |
// The processed nodes in the last round |
190 | 191 |
std::vector<Node> _process; |
191 | 192 |
|
192 | 193 |
Tolerance _tolerance; |
193 | 194 |
|
194 | 195 |
// Infinite constant |
195 | 196 |
const LargeValue INF; |
196 | 197 |
... | ... |
@@ -4,193 +4,194 @@ |
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 | 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 | 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 |
/// a directed cycle of minimum mean length (cost) in a digraph |
|
100 |
/// a directed cycle of minimum mean length (cost) in a digraph |
|
101 |
/// \ref amo93networkflows, \ref dasdan98minmeancycle. |
|
101 | 102 |
/// This class provides the most efficient algorithm for the |
102 | 103 |
/// minimum mean cycle problem, though the best known theoretical |
103 | 104 |
/// bound on its running time is exponential. |
104 | 105 |
/// |
105 | 106 |
/// \tparam GR The type of the digraph the algorithm runs on. |
106 | 107 |
/// \tparam LEN The type of the length map. The default |
107 | 108 |
/// map type is \ref concepts::Digraph::ArcMap "GR::ArcMap<int>". |
108 | 109 |
#ifdef DOXYGEN |
109 | 110 |
template <typename GR, typename LEN, typename TR> |
110 | 111 |
#else |
111 | 112 |
template < typename GR, |
112 | 113 |
typename LEN = typename GR::template ArcMap<int>, |
113 | 114 |
typename TR = HowardDefaultTraits<GR, LEN> > |
114 | 115 |
#endif |
115 | 116 |
class Howard |
116 | 117 |
{ |
117 | 118 |
public: |
118 | 119 |
|
119 | 120 |
/// The type of the digraph |
120 | 121 |
typedef typename TR::Digraph Digraph; |
121 | 122 |
/// The type of the length map |
122 | 123 |
typedef typename TR::LengthMap LengthMap; |
123 | 124 |
/// The type of the arc lengths |
124 | 125 |
typedef typename TR::Value Value; |
125 | 126 |
|
126 | 127 |
/// \brief The large value type |
127 | 128 |
/// |
128 | 129 |
/// The large value type used for internal computations. |
129 | 130 |
/// Using the \ref HowardDefaultTraits "default traits class", |
130 | 131 |
/// it is \c long \c long if the \c Value type is integer, |
131 | 132 |
/// otherwise it is \c double. |
132 | 133 |
typedef typename TR::LargeValue LargeValue; |
133 | 134 |
|
134 | 135 |
/// The tolerance type |
135 | 136 |
typedef typename TR::Tolerance Tolerance; |
136 | 137 |
|
137 | 138 |
/// \brief The path type of the found cycles |
138 | 139 |
/// |
139 | 140 |
/// The path type of the found cycles. |
140 | 141 |
/// Using the \ref HowardDefaultTraits "default traits class", |
141 | 142 |
/// it is \ref lemon::Path "Path<Digraph>". |
142 | 143 |
typedef typename TR::Path Path; |
143 | 144 |
|
144 | 145 |
/// The \ref HowardDefaultTraits "traits class" of the algorithm |
145 | 146 |
typedef TR Traits; |
146 | 147 |
|
147 | 148 |
private: |
148 | 149 |
|
149 | 150 |
TEMPLATE_DIGRAPH_TYPEDEFS(Digraph); |
150 | 151 |
|
151 | 152 |
// The digraph the algorithm runs on |
152 | 153 |
const Digraph &_gr; |
153 | 154 |
// The length of the arcs |
154 | 155 |
const LengthMap &_length; |
155 | 156 |
|
156 | 157 |
// Data for the found cycles |
157 | 158 |
bool _curr_found, _best_found; |
158 | 159 |
LargeValue _curr_length, _best_length; |
159 | 160 |
int _curr_size, _best_size; |
160 | 161 |
Node _curr_node, _best_node; |
161 | 162 |
|
162 | 163 |
Path *_cycle_path; |
163 | 164 |
bool _local_path; |
164 | 165 |
|
165 | 166 |
// Internal data used by the algorithm |
166 | 167 |
typename Digraph::template NodeMap<Arc> _policy; |
167 | 168 |
typename Digraph::template NodeMap<bool> _reached; |
168 | 169 |
typename Digraph::template NodeMap<int> _level; |
169 | 170 |
typename Digraph::template NodeMap<LargeValue> _dist; |
170 | 171 |
|
171 | 172 |
// Data for storing the strongly connected components |
172 | 173 |
int _comp_num; |
173 | 174 |
typename Digraph::template NodeMap<int> _comp; |
174 | 175 |
std::vector<std::vector<Node> > _comp_nodes; |
175 | 176 |
std::vector<Node>* _nodes; |
176 | 177 |
typename Digraph::template NodeMap<std::vector<Arc> > _in_arcs; |
177 | 178 |
|
178 | 179 |
// Queue used for BFS search |
179 | 180 |
std::vector<Node> _queue; |
180 | 181 |
int _qfront, _qback; |
181 | 182 |
|
182 | 183 |
Tolerance _tolerance; |
183 | 184 |
|
184 | 185 |
// Infinite constant |
185 | 186 |
const LargeValue INF; |
186 | 187 |
|
187 | 188 |
public: |
188 | 189 |
|
189 | 190 |
/// \name Named Template Parameters |
190 | 191 |
/// @{ |
191 | 192 |
|
192 | 193 |
template <typename T> |
193 | 194 |
struct SetLargeValueTraits : public Traits { |
194 | 195 |
typedef T LargeValue; |
195 | 196 |
typedef lemon::Tolerance<T> Tolerance; |
196 | 197 |
}; |
... | ... |
@@ -4,193 +4,194 @@ |
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 | 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 | 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 |
/// cycle of minimum mean length (cost) in a digraph |
|
100 |
/// cycle of minimum mean length (cost) in a digraph |
|
101 |
/// \ref amo93networkflows, \ref dasdan98minmeancycle. |
|
101 | 102 |
/// It runs in time O(ne) and uses space O(n<sup>2</sup>+e). |
102 | 103 |
/// |
103 | 104 |
/// \tparam GR The type of the digraph the algorithm runs on. |
104 | 105 |
/// \tparam LEN The type of the length map. The default |
105 | 106 |
/// map type is \ref concepts::Digraph::ArcMap "GR::ArcMap<int>". |
106 | 107 |
#ifdef DOXYGEN |
107 | 108 |
template <typename GR, typename LEN, typename TR> |
108 | 109 |
#else |
109 | 110 |
template < typename GR, |
110 | 111 |
typename LEN = typename GR::template ArcMap<int>, |
111 | 112 |
typename TR = KarpDefaultTraits<GR, LEN> > |
112 | 113 |
#endif |
113 | 114 |
class Karp |
114 | 115 |
{ |
115 | 116 |
public: |
116 | 117 |
|
117 | 118 |
/// The type of the digraph |
118 | 119 |
typedef typename TR::Digraph Digraph; |
119 | 120 |
/// The type of the length map |
120 | 121 |
typedef typename TR::LengthMap LengthMap; |
121 | 122 |
/// The type of the arc lengths |
122 | 123 |
typedef typename TR::Value Value; |
123 | 124 |
|
124 | 125 |
/// \brief The large value type |
125 | 126 |
/// |
126 | 127 |
/// The large value type used for internal computations. |
127 | 128 |
/// Using the \ref KarpDefaultTraits "default traits class", |
128 | 129 |
/// it is \c long \c long if the \c Value type is integer, |
129 | 130 |
/// otherwise it is \c double. |
130 | 131 |
typedef typename TR::LargeValue LargeValue; |
131 | 132 |
|
132 | 133 |
/// The tolerance type |
133 | 134 |
typedef typename TR::Tolerance Tolerance; |
134 | 135 |
|
135 | 136 |
/// \brief The path type of the found cycles |
136 | 137 |
/// |
137 | 138 |
/// The path type of the found cycles. |
138 | 139 |
/// Using the \ref KarpDefaultTraits "default traits class", |
139 | 140 |
/// it is \ref lemon::Path "Path<Digraph>". |
140 | 141 |
typedef typename TR::Path Path; |
141 | 142 |
|
142 | 143 |
/// The \ref KarpDefaultTraits "traits class" of the algorithm |
143 | 144 |
typedef TR Traits; |
144 | 145 |
|
145 | 146 |
private: |
146 | 147 |
|
147 | 148 |
TEMPLATE_DIGRAPH_TYPEDEFS(Digraph); |
148 | 149 |
|
149 | 150 |
// Data sturcture for path data |
150 | 151 |
struct PathData |
151 | 152 |
{ |
152 | 153 |
LargeValue dist; |
153 | 154 |
Arc pred; |
154 | 155 |
PathData(LargeValue d, Arc p = INVALID) : |
155 | 156 |
dist(d), pred(p) {} |
156 | 157 |
}; |
157 | 158 |
|
158 | 159 |
typedef typename Digraph::template NodeMap<std::vector<PathData> > |
159 | 160 |
PathDataNodeMap; |
160 | 161 |
|
161 | 162 |
private: |
162 | 163 |
|
163 | 164 |
// The digraph the algorithm runs on |
164 | 165 |
const Digraph &_gr; |
165 | 166 |
// The length of the arcs |
166 | 167 |
const LengthMap &_length; |
167 | 168 |
|
168 | 169 |
// Data for storing the strongly connected components |
169 | 170 |
int _comp_num; |
170 | 171 |
typename Digraph::template NodeMap<int> _comp; |
171 | 172 |
std::vector<std::vector<Node> > _comp_nodes; |
172 | 173 |
std::vector<Node>* _nodes; |
173 | 174 |
typename Digraph::template NodeMap<std::vector<Arc> > _out_arcs; |
174 | 175 |
|
175 | 176 |
// Data for the found cycle |
176 | 177 |
LargeValue _cycle_length; |
177 | 178 |
int _cycle_size; |
178 | 179 |
Node _cycle_node; |
179 | 180 |
|
180 | 181 |
Path *_cycle_path; |
181 | 182 |
bool _local_path; |
182 | 183 |
|
183 | 184 |
// Node map for storing path data |
184 | 185 |
PathDataNodeMap _data; |
185 | 186 |
// The processed nodes in the last round |
186 | 187 |
std::vector<Node> _process; |
187 | 188 |
|
188 | 189 |
Tolerance _tolerance; |
189 | 190 |
|
190 | 191 |
// Infinite constant |
191 | 192 |
const LargeValue INF; |
192 | 193 |
|
193 | 194 |
public: |
194 | 195 |
|
195 | 196 |
/// \name Named Template Parameters |
196 | 197 |
/// @{ |
0 comments (0 inline)