0
11
0
| ... | ... |
@@ -53,75 +53,75 @@ |
| 53 | 53 |
Note that all standard LEMON headers are located in the \c lemon subdirectory, |
| 54 | 54 |
so you should include them from C++ source like this: |
| 55 | 55 |
|
| 56 | 56 |
\code |
| 57 | 57 |
#include <lemon/header_file.h> |
| 58 | 58 |
\endcode |
| 59 | 59 |
|
| 60 | 60 |
The source code files use the same style and they have '.cc' extension. |
| 61 | 61 |
|
| 62 | 62 |
\code |
| 63 | 63 |
source_code.cc |
| 64 | 64 |
\endcode |
| 65 | 65 |
|
| 66 | 66 |
\subsection cs-class Classes and other types |
| 67 | 67 |
|
| 68 | 68 |
The name of a class or any type should look like the following. |
| 69 | 69 |
|
| 70 | 70 |
\code |
| 71 | 71 |
AllWordsCapitalizedWithoutUnderscores |
| 72 | 72 |
\endcode |
| 73 | 73 |
|
| 74 | 74 |
\subsection cs-func Methods and other functions |
| 75 | 75 |
|
| 76 | 76 |
The name of a function should look like the following. |
| 77 | 77 |
|
| 78 | 78 |
\code |
| 79 | 79 |
firstWordLowerCaseRestCapitalizedWithoutUnderscores |
| 80 | 80 |
\endcode |
| 81 | 81 |
|
| 82 | 82 |
\subsection cs-funcs Constants, Macros |
| 83 | 83 |
|
| 84 | 84 |
The names of constants and macros should look like the following. |
| 85 | 85 |
|
| 86 | 86 |
\code |
| 87 | 87 |
ALL_UPPER_CASE_WITH_UNDERSCORES |
| 88 | 88 |
\endcode |
| 89 | 89 |
|
| 90 | 90 |
\subsection cs-loc-var Class and instance member variables, auto variables |
| 91 | 91 |
|
| 92 | 92 |
The names of class and instance member variables and auto variables |
| 93 | 93 |
(=variables used locally in methods) should look like the following. |
| 94 | 94 |
|
| 95 | 95 |
\code |
| 96 | 96 |
all_lower_case_with_underscores |
| 97 | 97 |
\endcode |
| 98 | 98 |
|
| 99 | 99 |
\subsection pri-loc-var Private member variables |
| 100 | 100 |
|
| 101 |
Private member variables should start with underscore |
|
| 101 |
Private member variables should start with underscore. |
|
| 102 | 102 |
|
| 103 | 103 |
\code |
| 104 |
|
|
| 104 |
_start_with_underscore |
|
| 105 | 105 |
\endcode |
| 106 | 106 |
|
| 107 | 107 |
\subsection cs-excep Exceptions |
| 108 | 108 |
|
| 109 | 109 |
When writing exceptions please comply the following naming conventions. |
| 110 | 110 |
|
| 111 | 111 |
\code |
| 112 | 112 |
ClassNameEndsWithException |
| 113 | 113 |
\endcode |
| 114 | 114 |
|
| 115 | 115 |
or |
| 116 | 116 |
|
| 117 | 117 |
\code |
| 118 | 118 |
ClassNameEndsWithError |
| 119 | 119 |
\endcode |
| 120 | 120 |
|
| 121 | 121 |
\section header-template Template Header File |
| 122 | 122 |
|
| 123 | 123 |
Each LEMON header file should look like this: |
| 124 | 124 |
|
| 125 | 125 |
\include template.h |
| 126 | 126 |
|
| 127 | 127 |
*/ |
| ... | ... |
@@ -361,230 +361,230 @@ |
| 361 | 361 |
\f[ \max\sum_{sv\in A} f(sv) - \sum_{vs\in A} f(vs) \f]
|
| 362 | 362 |
\f[ \sum_{uv\in A} f(uv) = \sum_{vu\in A} f(vu)
|
| 363 | 363 |
\quad \forall u\in V\setminus\{s,t\} \f]
|
| 364 | 364 |
\f[ 0 \leq f(uv) \leq cap(uv) \quad \forall uv\in A \f] |
| 365 | 365 |
|
| 366 | 366 |
LEMON contains several algorithms for solving maximum flow problems: |
| 367 | 367 |
- \ref EdmondsKarp Edmonds-Karp algorithm |
| 368 | 368 |
\ref edmondskarp72theoretical. |
| 369 | 369 |
- \ref Preflow Goldberg-Tarjan's preflow push-relabel algorithm |
| 370 | 370 |
\ref goldberg88newapproach. |
| 371 | 371 |
- \ref DinitzSleatorTarjan Dinitz's blocking flow algorithm with dynamic trees |
| 372 | 372 |
\ref dinic70algorithm, \ref sleator83dynamic. |
| 373 | 373 |
- \ref GoldbergTarjan !Preflow push-relabel algorithm with dynamic trees |
| 374 | 374 |
\ref goldberg88newapproach, \ref sleator83dynamic. |
| 375 | 375 |
|
| 376 | 376 |
In most cases the \ref Preflow algorithm provides the |
| 377 | 377 |
fastest method for computing a maximum flow. All implementations |
| 378 | 378 |
also provide functions to query the minimum cut, which is the dual |
| 379 | 379 |
problem of maximum flow. |
| 380 | 380 |
|
| 381 | 381 |
\ref Circulation is a preflow push-relabel algorithm implemented directly |
| 382 | 382 |
for finding feasible circulations, which is a somewhat different problem, |
| 383 | 383 |
but it is strongly related to maximum flow. |
| 384 | 384 |
For more information, see \ref Circulation. |
| 385 | 385 |
*/ |
| 386 | 386 |
|
| 387 | 387 |
/** |
| 388 | 388 |
@defgroup min_cost_flow_algs Minimum Cost Flow Algorithms |
| 389 | 389 |
@ingroup algs |
| 390 | 390 |
|
| 391 | 391 |
\brief Algorithms for finding minimum cost flows and circulations. |
| 392 | 392 |
|
| 393 | 393 |
This group contains the algorithms for finding minimum cost flows and |
| 394 | 394 |
circulations \ref amo93networkflows. For more information about this |
| 395 | 395 |
problem and its dual solution, see \ref min_cost_flow |
| 396 | 396 |
"Minimum Cost Flow Problem". |
| 397 | 397 |
|
| 398 | 398 |
LEMON contains several algorithms for this problem. |
| 399 | 399 |
- \ref NetworkSimplex Primal Network Simplex algorithm with various |
| 400 | 400 |
pivot strategies \ref dantzig63linearprog, \ref kellyoneill91netsimplex. |
| 401 | 401 |
- \ref CostScaling Cost Scaling algorithm based on push/augment and |
| 402 | 402 |
relabel operations \ref goldberg90approximation, \ref goldberg97efficient, |
| 403 | 403 |
\ref bunnagel98efficient. |
| 404 | 404 |
- \ref CapacityScaling Capacity Scaling algorithm based on the successive |
| 405 | 405 |
shortest path method \ref edmondskarp72theoretical. |
| 406 | 406 |
- \ref CycleCanceling Cycle-Canceling algorithms, two of which are |
| 407 | 407 |
strongly polynomial \ref klein67primal, \ref goldberg89cyclecanceling. |
| 408 | 408 |
|
| 409 |
In general NetworkSimplex is the most efficient implementation, |
|
| 410 |
but in special cases other algorithms could be faster. |
|
| 409 |
In general, \ref NetworkSimplex and \ref CostScaling are the most efficient |
|
| 410 |
implementations, but the other two algorithms could be faster in special cases. |
|
| 411 | 411 |
For example, if the total supply and/or capacities are rather small, |
| 412 |
CapacityScaling is usually the fastest algorithm (without effective scaling). |
|
| 412 |
\ref CapacityScaling is usually the fastest algorithm (without effective scaling). |
|
| 413 | 413 |
*/ |
| 414 | 414 |
|
| 415 | 415 |
/** |
| 416 | 416 |
@defgroup min_cut Minimum Cut Algorithms |
| 417 | 417 |
@ingroup algs |
| 418 | 418 |
|
| 419 | 419 |
\brief Algorithms for finding minimum cut in graphs. |
| 420 | 420 |
|
| 421 | 421 |
This group contains the algorithms for finding minimum cut in graphs. |
| 422 | 422 |
|
| 423 | 423 |
The \e minimum \e cut \e problem is to find a non-empty and non-complete |
| 424 | 424 |
\f$X\f$ subset of the nodes with minimum overall capacity on |
| 425 | 425 |
outgoing arcs. Formally, there is a \f$G=(V,A)\f$ digraph, a |
| 426 | 426 |
\f$cap: A\rightarrow\mathbf{R}^+_0\f$ capacity function. The minimum
|
| 427 | 427 |
cut is the \f$X\f$ solution of the next optimization problem: |
| 428 | 428 |
|
| 429 | 429 |
\f[ \min_{X \subset V, X\not\in \{\emptyset, V\}}
|
| 430 | 430 |
\sum_{uv\in A: u\in X, v\not\in X}cap(uv) \f]
|
| 431 | 431 |
|
| 432 | 432 |
LEMON contains several algorithms related to minimum cut problems: |
| 433 | 433 |
|
| 434 | 434 |
- \ref HaoOrlin "Hao-Orlin algorithm" for calculating minimum cut |
| 435 | 435 |
in directed graphs. |
| 436 | 436 |
- \ref NagamochiIbaraki "Nagamochi-Ibaraki algorithm" for |
| 437 | 437 |
calculating minimum cut in undirected graphs. |
| 438 | 438 |
- \ref GomoryHu "Gomory-Hu tree computation" for calculating |
| 439 | 439 |
all-pairs minimum cut in undirected graphs. |
| 440 | 440 |
|
| 441 | 441 |
If you want to find minimum cut just between two distinict nodes, |
| 442 | 442 |
see the \ref max_flow "maximum flow problem". |
| 443 | 443 |
*/ |
| 444 | 444 |
|
| 445 | 445 |
/** |
| 446 | 446 |
@defgroup min_mean_cycle Minimum Mean Cycle Algorithms |
| 447 | 447 |
@ingroup algs |
| 448 | 448 |
\brief Algorithms for finding minimum mean cycles. |
| 449 | 449 |
|
| 450 | 450 |
This group contains the algorithms for finding minimum mean cycles |
| 451 | 451 |
\ref clrs01algorithms, \ref amo93networkflows. |
| 452 | 452 |
|
| 453 | 453 |
The \e minimum \e mean \e cycle \e problem is to find a directed cycle |
| 454 | 454 |
of minimum mean length (cost) in a digraph. |
| 455 | 455 |
The mean length of a cycle is the average length of its arcs, i.e. the |
| 456 | 456 |
ratio between the total length of the cycle and the number of arcs on it. |
| 457 | 457 |
|
| 458 | 458 |
This problem has an important connection to \e conservative \e length |
| 459 | 459 |
\e functions, too. A length function on the arcs of a digraph is called |
| 460 | 460 |
conservative if and only if there is no directed cycle of negative total |
| 461 | 461 |
length. For an arbitrary length function, the negative of the minimum |
| 462 | 462 |
cycle mean is the smallest \f$\epsilon\f$ value so that increasing the |
| 463 | 463 |
arc lengths uniformly by \f$\epsilon\f$ results in a conservative length |
| 464 | 464 |
function. |
| 465 | 465 |
|
| 466 | 466 |
LEMON contains three algorithms for solving the minimum mean cycle problem: |
| 467 | 467 |
- \ref KarpMmc Karp's original algorithm \ref amo93networkflows, |
| 468 | 468 |
\ref dasdan98minmeancycle. |
| 469 | 469 |
- \ref HartmannOrlinMmc Hartmann-Orlin's algorithm, which is an improved |
| 470 | 470 |
version of Karp's algorithm \ref dasdan98minmeancycle. |
| 471 | 471 |
- \ref HowardMmc Howard's policy iteration algorithm |
| 472 | 472 |
\ref dasdan98minmeancycle. |
| 473 | 473 |
|
| 474 |
In practice, the \ref HowardMmc "Howard" algorithm |
|
| 474 |
In practice, the \ref HowardMmc "Howard" algorithm turned out to be by far the |
|
| 475 | 475 |
most efficient one, though the best known theoretical bound on its running |
| 476 | 476 |
time is exponential. |
| 477 | 477 |
Both \ref KarpMmc "Karp" and \ref HartmannOrlinMmc "Hartmann-Orlin" algorithms |
| 478 | 478 |
run in time O(ne) and use space O(n<sup>2</sup>+e), but the latter one is |
| 479 | 479 |
typically faster due to the applied early termination scheme. |
| 480 | 480 |
*/ |
| 481 | 481 |
|
| 482 | 482 |
/** |
| 483 | 483 |
@defgroup matching Matching Algorithms |
| 484 | 484 |
@ingroup algs |
| 485 | 485 |
\brief Algorithms for finding matchings in graphs and bipartite graphs. |
| 486 | 486 |
|
| 487 | 487 |
This group contains the algorithms for calculating |
| 488 | 488 |
matchings in graphs and bipartite graphs. The general matching problem is |
| 489 | 489 |
finding a subset of the edges for which each node has at most one incident |
| 490 | 490 |
edge. |
| 491 | 491 |
|
| 492 | 492 |
There are several different algorithms for calculate matchings in |
| 493 | 493 |
graphs. The matching problems in bipartite graphs are generally |
| 494 | 494 |
easier than in general graphs. The goal of the matching optimization |
| 495 | 495 |
can be finding maximum cardinality, maximum weight or minimum cost |
| 496 | 496 |
matching. The search can be constrained to find perfect or |
| 497 | 497 |
maximum cardinality matching. |
| 498 | 498 |
|
| 499 | 499 |
The matching algorithms implemented in LEMON: |
| 500 | 500 |
- \ref MaxBipartiteMatching Hopcroft-Karp augmenting path algorithm |
| 501 | 501 |
for calculating maximum cardinality matching in bipartite graphs. |
| 502 | 502 |
- \ref PrBipartiteMatching Push-relabel algorithm |
| 503 | 503 |
for calculating maximum cardinality matching in bipartite graphs. |
| 504 | 504 |
- \ref MaxWeightedBipartiteMatching |
| 505 | 505 |
Successive shortest path algorithm for calculating maximum weighted |
| 506 | 506 |
matching and maximum weighted bipartite matching in bipartite graphs. |
| 507 | 507 |
- \ref MinCostMaxBipartiteMatching |
| 508 | 508 |
Successive shortest path algorithm for calculating minimum cost maximum |
| 509 | 509 |
matching in bipartite graphs. |
| 510 | 510 |
- \ref MaxMatching Edmond's blossom shrinking algorithm for calculating |
| 511 | 511 |
maximum cardinality matching in general graphs. |
| 512 | 512 |
- \ref MaxWeightedMatching Edmond's blossom shrinking algorithm for calculating |
| 513 | 513 |
maximum weighted matching in general graphs. |
| 514 | 514 |
- \ref MaxWeightedPerfectMatching |
| 515 | 515 |
Edmond's blossom shrinking algorithm for calculating maximum weighted |
| 516 | 516 |
perfect matching in general graphs. |
| 517 | 517 |
- \ref MaxFractionalMatching Push-relabel algorithm for calculating |
| 518 | 518 |
maximum cardinality fractional matching in general graphs. |
| 519 | 519 |
- \ref MaxWeightedFractionalMatching Augmenting path algorithm for calculating |
| 520 | 520 |
maximum weighted fractional matching in general graphs. |
| 521 | 521 |
- \ref MaxWeightedPerfectFractionalMatching |
| 522 | 522 |
Augmenting path algorithm for calculating maximum weighted |
| 523 | 523 |
perfect fractional matching in general graphs. |
| 524 | 524 |
|
| 525 | 525 |
\image html matching.png |
| 526 | 526 |
\image latex matching.eps "Min Cost Perfect Matching" width=\textwidth |
| 527 | 527 |
*/ |
| 528 | 528 |
|
| 529 | 529 |
/** |
| 530 | 530 |
@defgroup graph_properties Connectivity and Other Graph Properties |
| 531 | 531 |
@ingroup algs |
| 532 | 532 |
\brief Algorithms for discovering the graph properties |
| 533 | 533 |
|
| 534 | 534 |
This group contains the algorithms for discovering the graph properties |
| 535 | 535 |
like connectivity, bipartiteness, euler property, simplicity etc. |
| 536 | 536 |
|
| 537 | 537 |
\image html connected_components.png |
| 538 | 538 |
\image latex connected_components.eps "Connected components" width=\textwidth |
| 539 | 539 |
*/ |
| 540 | 540 |
|
| 541 | 541 |
/** |
| 542 |
@defgroup planar |
|
| 542 |
@defgroup planar Planar Embedding and Drawing |
|
| 543 | 543 |
@ingroup algs |
| 544 | 544 |
\brief Algorithms for planarity checking, embedding and drawing |
| 545 | 545 |
|
| 546 | 546 |
This group contains the algorithms for planarity checking, |
| 547 | 547 |
embedding and drawing. |
| 548 | 548 |
|
| 549 | 549 |
\image html planar.png |
| 550 | 550 |
\image latex planar.eps "Plane graph" width=\textwidth |
| 551 | 551 |
*/ |
| 552 | 552 |
|
| 553 | 553 |
/** |
| 554 | 554 |
@defgroup approx_algs Approximation Algorithms |
| 555 | 555 |
@ingroup algs |
| 556 | 556 |
\brief Approximation algorithms. |
| 557 | 557 |
|
| 558 | 558 |
This group contains the approximation and heuristic algorithms |
| 559 | 559 |
implemented in LEMON. |
| 560 | 560 |
|
| 561 | 561 |
<b>Maximum Clique Problem</b> |
| 562 | 562 |
- \ref GrossoLocatelliPullanMc An efficient heuristic algorithm of |
| 563 | 563 |
Grosso, Locatelli, and Pullan. |
| 564 | 564 |
*/ |
| 565 | 565 |
|
| 566 | 566 |
/** |
| 567 | 567 |
@defgroup auxalg Auxiliary Algorithms |
| 568 | 568 |
@ingroup algs |
| 569 | 569 |
\brief Auxiliary algorithms implemented in LEMON. |
| 570 | 570 |
|
| 571 | 571 |
This group contains some algorithms implemented in LEMON |
| 572 | 572 |
in order to make it easier to implement complex algorithms. |
| 573 | 573 |
*/ |
| 574 | 574 |
|
| 575 | 575 |
/** |
| 576 | 576 |
@defgroup gen_opt_group General Optimization Tools |
| 577 | 577 |
\brief This group contains some general optimization frameworks |
| 578 | 578 |
implemented in LEMON. |
| 579 | 579 |
|
| 580 | 580 |
This group contains some general optimization frameworks |
| 581 | 581 |
implemented in LEMON. |
| 582 | 582 |
*/ |
| 583 | 583 |
|
| 584 | 584 |
/** |
| 585 | 585 |
@defgroup lp_group LP and MIP Solvers |
| 586 | 586 |
@ingroup gen_opt_group |
| 587 | 587 |
\brief LP and MIP solver interfaces for LEMON. |
| 588 | 588 |
|
| 589 | 589 |
This group contains LP and MIP solver interfaces for LEMON. |
| 590 | 590 |
Various LP solvers could be used in the same manner with this |
| ... | ... |
@@ -44,98 +44,98 @@ |
| 44 | 44 |
{
|
| 45 | 45 |
/// The type of the digraph |
| 46 | 46 |
typedef GR Digraph; |
| 47 | 47 |
/// The type of the flow amounts, capacity bounds and supply values |
| 48 | 48 |
typedef V Value; |
| 49 | 49 |
/// The type of the arc costs |
| 50 | 50 |
typedef C Cost; |
| 51 | 51 |
|
| 52 | 52 |
/// \brief The type of the heap used for internal Dijkstra computations. |
| 53 | 53 |
/// |
| 54 | 54 |
/// The type of the heap used for internal Dijkstra computations. |
| 55 | 55 |
/// It must conform to the \ref lemon::concepts::Heap "Heap" concept, |
| 56 | 56 |
/// its priority type must be \c Cost and its cross reference type |
| 57 | 57 |
/// must be \ref RangeMap "RangeMap<int>". |
| 58 | 58 |
typedef BinHeap<Cost, RangeMap<int> > Heap; |
| 59 | 59 |
}; |
| 60 | 60 |
|
| 61 | 61 |
/// \addtogroup min_cost_flow_algs |
| 62 | 62 |
/// @{
|
| 63 | 63 |
|
| 64 | 64 |
/// \brief Implementation of the Capacity Scaling algorithm for |
| 65 | 65 |
/// finding a \ref min_cost_flow "minimum cost flow". |
| 66 | 66 |
/// |
| 67 | 67 |
/// \ref CapacityScaling implements the capacity scaling version |
| 68 | 68 |
/// of the successive shortest path algorithm for finding a |
| 69 | 69 |
/// \ref min_cost_flow "minimum cost flow" \ref amo93networkflows, |
| 70 | 70 |
/// \ref edmondskarp72theoretical. It is an efficient dual |
| 71 | 71 |
/// solution method. |
| 72 | 72 |
/// |
| 73 | 73 |
/// Most of the parameters of the problem (except for the digraph) |
| 74 | 74 |
/// can be given using separate functions, and the algorithm can be |
| 75 | 75 |
/// executed using the \ref run() function. If some parameters are not |
| 76 | 76 |
/// specified, then default values will be used. |
| 77 | 77 |
/// |
| 78 | 78 |
/// \tparam GR The digraph type the algorithm runs on. |
| 79 | 79 |
/// \tparam V The number type used for flow amounts, capacity bounds |
| 80 | 80 |
/// and supply values in the algorithm. By default, it is \c int. |
| 81 | 81 |
/// \tparam C The number type used for costs and potentials in the |
| 82 | 82 |
/// algorithm. By default, it is the same as \c V. |
| 83 | 83 |
/// \tparam TR The traits class that defines various types used by the |
| 84 | 84 |
/// algorithm. By default, it is \ref CapacityScalingDefaultTraits |
| 85 | 85 |
/// "CapacityScalingDefaultTraits<GR, V, C>". |
| 86 | 86 |
/// In most cases, this parameter should not be set directly, |
| 87 | 87 |
/// consider to use the named template parameters instead. |
| 88 | 88 |
/// |
| 89 | 89 |
/// \warning Both \c V and \c C must be signed number types. |
| 90 | 90 |
/// \warning All input data (capacities, supply values, and costs) must |
| 91 | 91 |
/// be integer. |
| 92 |
/// \warning This algorithm does not support negative costs for such |
|
| 93 |
/// arcs that have infinite upper bound. |
|
| 92 |
/// \warning This algorithm does not support negative costs for |
|
| 93 |
/// arcs having infinite upper bound. |
|
| 94 | 94 |
#ifdef DOXYGEN |
| 95 | 95 |
template <typename GR, typename V, typename C, typename TR> |
| 96 | 96 |
#else |
| 97 | 97 |
template < typename GR, typename V = int, typename C = V, |
| 98 | 98 |
typename TR = CapacityScalingDefaultTraits<GR, V, C> > |
| 99 | 99 |
#endif |
| 100 | 100 |
class CapacityScaling |
| 101 | 101 |
{
|
| 102 | 102 |
public: |
| 103 | 103 |
|
| 104 | 104 |
/// The type of the digraph |
| 105 | 105 |
typedef typename TR::Digraph Digraph; |
| 106 | 106 |
/// The type of the flow amounts, capacity bounds and supply values |
| 107 | 107 |
typedef typename TR::Value Value; |
| 108 | 108 |
/// The type of the arc costs |
| 109 | 109 |
typedef typename TR::Cost Cost; |
| 110 | 110 |
|
| 111 | 111 |
/// The type of the heap used for internal Dijkstra computations |
| 112 | 112 |
typedef typename TR::Heap Heap; |
| 113 | 113 |
|
| 114 | 114 |
/// The \ref CapacityScalingDefaultTraits "traits class" of the algorithm |
| 115 | 115 |
typedef TR Traits; |
| 116 | 116 |
|
| 117 | 117 |
public: |
| 118 | 118 |
|
| 119 | 119 |
/// \brief Problem type constants for the \c run() function. |
| 120 | 120 |
/// |
| 121 | 121 |
/// Enum type containing the problem type constants that can be |
| 122 | 122 |
/// returned by the \ref run() function of the algorithm. |
| 123 | 123 |
enum ProblemType {
|
| 124 | 124 |
/// The problem has no feasible solution (flow). |
| 125 | 125 |
INFEASIBLE, |
| 126 | 126 |
/// The problem has optimal solution (i.e. it is feasible and |
| 127 | 127 |
/// bounded), and the algorithm has found optimal flow and node |
| 128 | 128 |
/// potentials (primal and dual solutions). |
| 129 | 129 |
OPTIMAL, |
| 130 | 130 |
/// The digraph contains an arc of negative cost and infinite |
| 131 | 131 |
/// upper bound. It means that the objective function is unbounded |
| 132 | 132 |
/// on that arc, however, note that it could actually be bounded |
| 133 | 133 |
/// over the feasible flows, but this algroithm cannot handle |
| 134 | 134 |
/// these cases. |
| 135 | 135 |
UNBOUNDED |
| 136 | 136 |
}; |
| 137 | 137 |
|
| 138 | 138 |
private: |
| 139 | 139 |
|
| 140 | 140 |
TEMPLATE_DIGRAPH_TYPEDEFS(GR); |
| 141 | 141 |
|
| ... | ... |
@@ -378,97 +378,97 @@ |
| 378 | 378 |
|
| 379 | 379 |
/// \brief Set the costs of the arcs. |
| 380 | 380 |
/// |
| 381 | 381 |
/// This function sets the costs of the arcs. |
| 382 | 382 |
/// If it is not used before calling \ref run(), the costs |
| 383 | 383 |
/// will be set to \c 1 on all arcs. |
| 384 | 384 |
/// |
| 385 | 385 |
/// \param map An arc map storing the costs. |
| 386 | 386 |
/// Its \c Value type must be convertible to the \c Cost type |
| 387 | 387 |
/// of the algorithm. |
| 388 | 388 |
/// |
| 389 | 389 |
/// \return <tt>(*this)</tt> |
| 390 | 390 |
template<typename CostMap> |
| 391 | 391 |
CapacityScaling& costMap(const CostMap& map) {
|
| 392 | 392 |
for (ArcIt a(_graph); a != INVALID; ++a) {
|
| 393 | 393 |
_cost[_arc_idf[a]] = map[a]; |
| 394 | 394 |
_cost[_arc_idb[a]] = -map[a]; |
| 395 | 395 |
} |
| 396 | 396 |
return *this; |
| 397 | 397 |
} |
| 398 | 398 |
|
| 399 | 399 |
/// \brief Set the supply values of the nodes. |
| 400 | 400 |
/// |
| 401 | 401 |
/// This function sets the supply values of the nodes. |
| 402 | 402 |
/// If neither this function nor \ref stSupply() is used before |
| 403 | 403 |
/// calling \ref run(), the supply of each node will be set to zero. |
| 404 | 404 |
/// |
| 405 | 405 |
/// \param map A node map storing the supply values. |
| 406 | 406 |
/// Its \c Value type must be convertible to the \c Value type |
| 407 | 407 |
/// of the algorithm. |
| 408 | 408 |
/// |
| 409 | 409 |
/// \return <tt>(*this)</tt> |
| 410 | 410 |
template<typename SupplyMap> |
| 411 | 411 |
CapacityScaling& supplyMap(const SupplyMap& map) {
|
| 412 | 412 |
for (NodeIt n(_graph); n != INVALID; ++n) {
|
| 413 | 413 |
_supply[_node_id[n]] = map[n]; |
| 414 | 414 |
} |
| 415 | 415 |
return *this; |
| 416 | 416 |
} |
| 417 | 417 |
|
| 418 | 418 |
/// \brief Set single source and target nodes and a supply value. |
| 419 | 419 |
/// |
| 420 | 420 |
/// This function sets a single source node and a single target node |
| 421 | 421 |
/// and the required flow value. |
| 422 | 422 |
/// If neither this function nor \ref supplyMap() is used before |
| 423 | 423 |
/// calling \ref run(), the supply of each node will be set to zero. |
| 424 | 424 |
/// |
| 425 | 425 |
/// Using this function has the same effect as using \ref supplyMap() |
| 426 |
/// with |
|
| 426 |
/// with a map in which \c k is assigned to \c s, \c -k is |
|
| 427 | 427 |
/// assigned to \c t and all other nodes have zero supply value. |
| 428 | 428 |
/// |
| 429 | 429 |
/// \param s The source node. |
| 430 | 430 |
/// \param t The target node. |
| 431 | 431 |
/// \param k The required amount of flow from node \c s to node \c t |
| 432 | 432 |
/// (i.e. the supply of \c s and the demand of \c t). |
| 433 | 433 |
/// |
| 434 | 434 |
/// \return <tt>(*this)</tt> |
| 435 | 435 |
CapacityScaling& stSupply(const Node& s, const Node& t, Value k) {
|
| 436 | 436 |
for (int i = 0; i != _node_num; ++i) {
|
| 437 | 437 |
_supply[i] = 0; |
| 438 | 438 |
} |
| 439 | 439 |
_supply[_node_id[s]] = k; |
| 440 | 440 |
_supply[_node_id[t]] = -k; |
| 441 | 441 |
return *this; |
| 442 | 442 |
} |
| 443 | 443 |
|
| 444 | 444 |
/// @} |
| 445 | 445 |
|
| 446 | 446 |
/// \name Execution control |
| 447 | 447 |
/// The algorithm can be executed using \ref run(). |
| 448 | 448 |
|
| 449 | 449 |
/// @{
|
| 450 | 450 |
|
| 451 | 451 |
/// \brief Run the algorithm. |
| 452 | 452 |
/// |
| 453 | 453 |
/// This function runs the algorithm. |
| 454 | 454 |
/// The paramters can be specified using functions \ref lowerMap(), |
| 455 | 455 |
/// \ref upperMap(), \ref costMap(), \ref supplyMap(), \ref stSupply(). |
| 456 | 456 |
/// For example, |
| 457 | 457 |
/// \code |
| 458 | 458 |
/// CapacityScaling<ListDigraph> cs(graph); |
| 459 | 459 |
/// cs.lowerMap(lower).upperMap(upper).costMap(cost) |
| 460 | 460 |
/// .supplyMap(sup).run(); |
| 461 | 461 |
/// \endcode |
| 462 | 462 |
/// |
| 463 | 463 |
/// This function can be called more than once. All the given parameters |
| 464 | 464 |
/// are kept for the next call, unless \ref resetParams() or \ref reset() |
| 465 | 465 |
/// is used, thus only the modified parameters have to be set again. |
| 466 | 466 |
/// If the underlying digraph was also modified after the construction |
| 467 | 467 |
/// of the class (or the last \ref reset() call), then the \ref reset() |
| 468 | 468 |
/// function must be called. |
| 469 | 469 |
/// |
| 470 | 470 |
/// \param factor The capacity scaling factor. It must be larger than |
| 471 | 471 |
/// one to use scaling. If it is less or equal to one, then scaling |
| 472 | 472 |
/// will be disabled. |
| 473 | 473 |
/// |
| 474 | 474 |
/// \return \c INFEASIBLE if no feasible flow exists, |
| ... | ... |
@@ -402,97 +402,97 @@ |
| 402 | 402 |
arcRefMap[it] = to.addArc(nodeRefMap[from.source(it)], |
| 403 | 403 |
nodeRefMap[from.target(it)]); |
| 404 | 404 |
} |
| 405 | 405 |
} |
| 406 | 406 |
}; |
| 407 | 407 |
|
| 408 | 408 |
template <typename Digraph> |
| 409 | 409 |
struct DigraphCopySelector< |
| 410 | 410 |
Digraph, |
| 411 | 411 |
typename enable_if<typename Digraph::BuildTag, void>::type> |
| 412 | 412 |
{
|
| 413 | 413 |
template <typename From, typename NodeRefMap, typename ArcRefMap> |
| 414 | 414 |
static void copy(const From& from, Digraph &to, |
| 415 | 415 |
NodeRefMap& nodeRefMap, ArcRefMap& arcRefMap) {
|
| 416 | 416 |
to.build(from, nodeRefMap, arcRefMap); |
| 417 | 417 |
} |
| 418 | 418 |
}; |
| 419 | 419 |
|
| 420 | 420 |
template <typename Graph, typename Enable = void> |
| 421 | 421 |
struct GraphCopySelector {
|
| 422 | 422 |
template <typename From, typename NodeRefMap, typename EdgeRefMap> |
| 423 | 423 |
static void copy(const From& from, Graph &to, |
| 424 | 424 |
NodeRefMap& nodeRefMap, EdgeRefMap& edgeRefMap) {
|
| 425 | 425 |
to.clear(); |
| 426 | 426 |
for (typename From::NodeIt it(from); it != INVALID; ++it) {
|
| 427 | 427 |
nodeRefMap[it] = to.addNode(); |
| 428 | 428 |
} |
| 429 | 429 |
for (typename From::EdgeIt it(from); it != INVALID; ++it) {
|
| 430 | 430 |
edgeRefMap[it] = to.addEdge(nodeRefMap[from.u(it)], |
| 431 | 431 |
nodeRefMap[from.v(it)]); |
| 432 | 432 |
} |
| 433 | 433 |
} |
| 434 | 434 |
}; |
| 435 | 435 |
|
| 436 | 436 |
template <typename Graph> |
| 437 | 437 |
struct GraphCopySelector< |
| 438 | 438 |
Graph, |
| 439 | 439 |
typename enable_if<typename Graph::BuildTag, void>::type> |
| 440 | 440 |
{
|
| 441 | 441 |
template <typename From, typename NodeRefMap, typename EdgeRefMap> |
| 442 | 442 |
static void copy(const From& from, Graph &to, |
| 443 | 443 |
NodeRefMap& nodeRefMap, EdgeRefMap& edgeRefMap) {
|
| 444 | 444 |
to.build(from, nodeRefMap, edgeRefMap); |
| 445 | 445 |
} |
| 446 | 446 |
}; |
| 447 | 447 |
|
| 448 | 448 |
} |
| 449 | 449 |
|
| 450 |
/// Check whether a graph is undirected. |
|
| 450 |
/// \brief Check whether a graph is undirected. |
|
| 451 | 451 |
/// |
| 452 | 452 |
/// This function returns \c true if the given graph is undirected. |
| 453 | 453 |
#ifdef DOXYGEN |
| 454 | 454 |
template <typename GR> |
| 455 | 455 |
bool undirected(const GR& g) { return false; }
|
| 456 | 456 |
#else |
| 457 | 457 |
template <typename GR> |
| 458 | 458 |
typename enable_if<UndirectedTagIndicator<GR>, bool>::type |
| 459 | 459 |
undirected(const GR&) {
|
| 460 | 460 |
return true; |
| 461 | 461 |
} |
| 462 | 462 |
template <typename GR> |
| 463 | 463 |
typename disable_if<UndirectedTagIndicator<GR>, bool>::type |
| 464 | 464 |
undirected(const GR&) {
|
| 465 | 465 |
return false; |
| 466 | 466 |
} |
| 467 | 467 |
#endif |
| 468 | 468 |
|
| 469 | 469 |
/// \brief Class to copy a digraph. |
| 470 | 470 |
/// |
| 471 | 471 |
/// Class to copy a digraph to another digraph (duplicate a digraph). The |
| 472 | 472 |
/// simplest way of using it is through the \c digraphCopy() function. |
| 473 | 473 |
/// |
| 474 | 474 |
/// This class not only make a copy of a digraph, but it can create |
| 475 | 475 |
/// references and cross references between the nodes and arcs of |
| 476 | 476 |
/// the two digraphs, and it can copy maps to use with the newly created |
| 477 | 477 |
/// digraph. |
| 478 | 478 |
/// |
| 479 | 479 |
/// To make a copy from a digraph, first an instance of DigraphCopy |
| 480 | 480 |
/// should be created, then the data belongs to the digraph should |
| 481 | 481 |
/// assigned to copy. In the end, the \c run() member should be |
| 482 | 482 |
/// called. |
| 483 | 483 |
/// |
| 484 | 484 |
/// The next code copies a digraph with several data: |
| 485 | 485 |
///\code |
| 486 | 486 |
/// DigraphCopy<OrigGraph, NewGraph> cg(orig_graph, new_graph); |
| 487 | 487 |
/// // Create references for the nodes |
| 488 | 488 |
/// OrigGraph::NodeMap<NewGraph::Node> nr(orig_graph); |
| 489 | 489 |
/// cg.nodeRef(nr); |
| 490 | 490 |
/// // Create cross references (inverse) for the arcs |
| 491 | 491 |
/// NewGraph::ArcMap<OrigGraph::Arc> acr(new_graph); |
| 492 | 492 |
/// cg.arcCrossRef(acr); |
| 493 | 493 |
/// // Copy an arc map |
| 494 | 494 |
/// OrigGraph::ArcMap<double> oamap(orig_graph); |
| 495 | 495 |
/// NewGraph::ArcMap<double> namap(new_graph); |
| 496 | 496 |
/// cg.arcMap(oamap, namap); |
| 497 | 497 |
/// // Copy a node |
| 498 | 498 |
/// OrigGraph::Node on; |
| ... | ... |
@@ -52,179 +52,182 @@ |
| 52 | 52 |
#endif |
| 53 | 53 |
struct CostScalingDefaultTraits |
| 54 | 54 |
{
|
| 55 | 55 |
/// The type of the digraph |
| 56 | 56 |
typedef GR Digraph; |
| 57 | 57 |
/// The type of the flow amounts, capacity bounds and supply values |
| 58 | 58 |
typedef V Value; |
| 59 | 59 |
/// The type of the arc costs |
| 60 | 60 |
typedef C Cost; |
| 61 | 61 |
|
| 62 | 62 |
/// \brief The large cost type used for internal computations |
| 63 | 63 |
/// |
| 64 | 64 |
/// The large cost type used for internal computations. |
| 65 | 65 |
/// It is \c long \c long if the \c Cost type is integer, |
| 66 | 66 |
/// otherwise it is \c double. |
| 67 | 67 |
/// \c Cost must be convertible to \c LargeCost. |
| 68 | 68 |
typedef double LargeCost; |
| 69 | 69 |
}; |
| 70 | 70 |
|
| 71 | 71 |
// Default traits class for integer cost types |
| 72 | 72 |
template <typename GR, typename V, typename C> |
| 73 | 73 |
struct CostScalingDefaultTraits<GR, V, C, true> |
| 74 | 74 |
{
|
| 75 | 75 |
typedef GR Digraph; |
| 76 | 76 |
typedef V Value; |
| 77 | 77 |
typedef C Cost; |
| 78 | 78 |
#ifdef LEMON_HAVE_LONG_LONG |
| 79 | 79 |
typedef long long LargeCost; |
| 80 | 80 |
#else |
| 81 | 81 |
typedef long LargeCost; |
| 82 | 82 |
#endif |
| 83 | 83 |
}; |
| 84 | 84 |
|
| 85 | 85 |
|
| 86 | 86 |
/// \addtogroup min_cost_flow_algs |
| 87 | 87 |
/// @{
|
| 88 | 88 |
|
| 89 | 89 |
/// \brief Implementation of the Cost Scaling algorithm for |
| 90 | 90 |
/// finding a \ref min_cost_flow "minimum cost flow". |
| 91 | 91 |
/// |
| 92 | 92 |
/// \ref CostScaling implements a cost scaling algorithm that performs |
| 93 | 93 |
/// push/augment and relabel operations for finding a \ref min_cost_flow |
| 94 | 94 |
/// "minimum cost flow" \ref amo93networkflows, \ref goldberg90approximation, |
| 95 | 95 |
/// \ref goldberg97efficient, \ref bunnagel98efficient. |
| 96 | 96 |
/// It is a highly efficient primal-dual solution method, which |
| 97 | 97 |
/// can be viewed as the generalization of the \ref Preflow |
| 98 | 98 |
/// "preflow push-relabel" algorithm for the maximum flow problem. |
| 99 | 99 |
/// |
| 100 |
/// In general, \ref NetworkSimplex and \ref CostScaling are the fastest |
|
| 101 |
/// implementations available in LEMON for this problem. |
|
| 102 |
/// |
|
| 100 | 103 |
/// Most of the parameters of the problem (except for the digraph) |
| 101 | 104 |
/// can be given using separate functions, and the algorithm can be |
| 102 | 105 |
/// executed using the \ref run() function. If some parameters are not |
| 103 | 106 |
/// specified, then default values will be used. |
| 104 | 107 |
/// |
| 105 | 108 |
/// \tparam GR The digraph type the algorithm runs on. |
| 106 | 109 |
/// \tparam V The number type used for flow amounts, capacity bounds |
| 107 | 110 |
/// and supply values in the algorithm. By default, it is \c int. |
| 108 | 111 |
/// \tparam C The number type used for costs and potentials in the |
| 109 | 112 |
/// algorithm. By default, it is the same as \c V. |
| 110 | 113 |
/// \tparam TR The traits class that defines various types used by the |
| 111 | 114 |
/// algorithm. By default, it is \ref CostScalingDefaultTraits |
| 112 | 115 |
/// "CostScalingDefaultTraits<GR, V, C>". |
| 113 | 116 |
/// In most cases, this parameter should not be set directly, |
| 114 | 117 |
/// consider to use the named template parameters instead. |
| 115 | 118 |
/// |
| 116 | 119 |
/// \warning Both \c V and \c C must be signed number types. |
| 117 | 120 |
/// \warning All input data (capacities, supply values, and costs) must |
| 118 | 121 |
/// be integer. |
| 119 |
/// \warning This algorithm does not support negative costs for such |
|
| 120 |
/// arcs that have infinite upper bound. |
|
| 122 |
/// \warning This algorithm does not support negative costs for |
|
| 123 |
/// arcs having infinite upper bound. |
|
| 121 | 124 |
/// |
| 122 | 125 |
/// \note %CostScaling provides three different internal methods, |
| 123 | 126 |
/// from which the most efficient one is used by default. |
| 124 | 127 |
/// For more information, see \ref Method. |
| 125 | 128 |
#ifdef DOXYGEN |
| 126 | 129 |
template <typename GR, typename V, typename C, typename TR> |
| 127 | 130 |
#else |
| 128 | 131 |
template < typename GR, typename V = int, typename C = V, |
| 129 | 132 |
typename TR = CostScalingDefaultTraits<GR, V, C> > |
| 130 | 133 |
#endif |
| 131 | 134 |
class CostScaling |
| 132 | 135 |
{
|
| 133 | 136 |
public: |
| 134 | 137 |
|
| 135 | 138 |
/// The type of the digraph |
| 136 | 139 |
typedef typename TR::Digraph Digraph; |
| 137 | 140 |
/// The type of the flow amounts, capacity bounds and supply values |
| 138 | 141 |
typedef typename TR::Value Value; |
| 139 | 142 |
/// The type of the arc costs |
| 140 | 143 |
typedef typename TR::Cost Cost; |
| 141 | 144 |
|
| 142 | 145 |
/// \brief The large cost type |
| 143 | 146 |
/// |
| 144 | 147 |
/// The large cost type used for internal computations. |
| 145 | 148 |
/// By default, it is \c long \c long if the \c Cost type is integer, |
| 146 | 149 |
/// otherwise it is \c double. |
| 147 | 150 |
typedef typename TR::LargeCost LargeCost; |
| 148 | 151 |
|
| 149 | 152 |
/// The \ref CostScalingDefaultTraits "traits class" of the algorithm |
| 150 | 153 |
typedef TR Traits; |
| 151 | 154 |
|
| 152 | 155 |
public: |
| 153 | 156 |
|
| 154 | 157 |
/// \brief Problem type constants for the \c run() function. |
| 155 | 158 |
/// |
| 156 | 159 |
/// Enum type containing the problem type constants that can be |
| 157 | 160 |
/// returned by the \ref run() function of the algorithm. |
| 158 | 161 |
enum ProblemType {
|
| 159 | 162 |
/// The problem has no feasible solution (flow). |
| 160 | 163 |
INFEASIBLE, |
| 161 | 164 |
/// The problem has optimal solution (i.e. it is feasible and |
| 162 | 165 |
/// bounded), and the algorithm has found optimal flow and node |
| 163 | 166 |
/// potentials (primal and dual solutions). |
| 164 | 167 |
OPTIMAL, |
| 165 | 168 |
/// The digraph contains an arc of negative cost and infinite |
| 166 | 169 |
/// upper bound. It means that the objective function is unbounded |
| 167 | 170 |
/// on that arc, however, note that it could actually be bounded |
| 168 | 171 |
/// over the feasible flows, but this algroithm cannot handle |
| 169 | 172 |
/// these cases. |
| 170 | 173 |
UNBOUNDED |
| 171 | 174 |
}; |
| 172 | 175 |
|
| 173 | 176 |
/// \brief Constants for selecting the internal method. |
| 174 | 177 |
/// |
| 175 | 178 |
/// Enum type containing constants for selecting the internal method |
| 176 | 179 |
/// for the \ref run() function. |
| 177 | 180 |
/// |
| 178 | 181 |
/// \ref CostScaling provides three internal methods that differ mainly |
| 179 | 182 |
/// in their base operations, which are used in conjunction with the |
| 180 | 183 |
/// relabel operation. |
| 181 | 184 |
/// By default, the so called \ref PARTIAL_AUGMENT |
| 182 |
/// "Partial Augment-Relabel" method is used, which |
|
| 185 |
/// "Partial Augment-Relabel" method is used, which turned out to be |
|
| 183 | 186 |
/// the most efficient and the most robust on various test inputs. |
| 184 | 187 |
/// However, the other methods can be selected using the \ref run() |
| 185 | 188 |
/// function with the proper parameter. |
| 186 | 189 |
enum Method {
|
| 187 | 190 |
/// Local push operations are used, i.e. flow is moved only on one |
| 188 | 191 |
/// admissible arc at once. |
| 189 | 192 |
PUSH, |
| 190 | 193 |
/// Augment operations are used, i.e. flow is moved on admissible |
| 191 | 194 |
/// paths from a node with excess to a node with deficit. |
| 192 | 195 |
AUGMENT, |
| 193 | 196 |
/// Partial augment operations are used, i.e. flow is moved on |
| 194 | 197 |
/// admissible paths started from a node with excess, but the |
| 195 | 198 |
/// lengths of these paths are limited. This method can be viewed |
| 196 | 199 |
/// as a combined version of the previous two operations. |
| 197 | 200 |
PARTIAL_AUGMENT |
| 198 | 201 |
}; |
| 199 | 202 |
|
| 200 | 203 |
private: |
| 201 | 204 |
|
| 202 | 205 |
TEMPLATE_DIGRAPH_TYPEDEFS(GR); |
| 203 | 206 |
|
| 204 | 207 |
typedef std::vector<int> IntVector; |
| 205 | 208 |
typedef std::vector<Value> ValueVector; |
| 206 | 209 |
typedef std::vector<Cost> CostVector; |
| 207 | 210 |
typedef std::vector<LargeCost> LargeCostVector; |
| 208 | 211 |
typedef std::vector<char> BoolVector; |
| 209 | 212 |
// Note: vector<char> is used instead of vector<bool> for efficiency reasons |
| 210 | 213 |
|
| 211 | 214 |
private: |
| 212 | 215 |
|
| 213 | 216 |
template <typename KT, typename VT> |
| 214 | 217 |
class StaticVectorMap {
|
| 215 | 218 |
public: |
| 216 | 219 |
typedef KT Key; |
| 217 | 220 |
typedef VT Value; |
| 218 | 221 |
|
| 219 | 222 |
StaticVectorMap(std::vector<Value>& v) : _v(v) {}
|
| 220 | 223 |
|
| 221 | 224 |
const Value& operator[](const Key& key) const {
|
| 222 | 225 |
return _v[StaticDigraph::id(key)]; |
| 223 | 226 |
} |
| 224 | 227 |
|
| 225 | 228 |
Value& operator[](const Key& key) {
|
| 226 | 229 |
return _v[StaticDigraph::id(key)]; |
| 227 | 230 |
} |
| 228 | 231 |
|
| 229 | 232 |
void set(const Key& key, const Value& val) {
|
| 230 | 233 |
_v[StaticDigraph::id(key)] = val; |
| ... | ... |
@@ -403,97 +406,97 @@ |
| 403 | 406 |
|
| 404 | 407 |
/// \brief Set the costs of the arcs. |
| 405 | 408 |
/// |
| 406 | 409 |
/// This function sets the costs of the arcs. |
| 407 | 410 |
/// If it is not used before calling \ref run(), the costs |
| 408 | 411 |
/// will be set to \c 1 on all arcs. |
| 409 | 412 |
/// |
| 410 | 413 |
/// \param map An arc map storing the costs. |
| 411 | 414 |
/// Its \c Value type must be convertible to the \c Cost type |
| 412 | 415 |
/// of the algorithm. |
| 413 | 416 |
/// |
| 414 | 417 |
/// \return <tt>(*this)</tt> |
| 415 | 418 |
template<typename CostMap> |
| 416 | 419 |
CostScaling& costMap(const CostMap& map) {
|
| 417 | 420 |
for (ArcIt a(_graph); a != INVALID; ++a) {
|
| 418 | 421 |
_scost[_arc_idf[a]] = map[a]; |
| 419 | 422 |
_scost[_arc_idb[a]] = -map[a]; |
| 420 | 423 |
} |
| 421 | 424 |
return *this; |
| 422 | 425 |
} |
| 423 | 426 |
|
| 424 | 427 |
/// \brief Set the supply values of the nodes. |
| 425 | 428 |
/// |
| 426 | 429 |
/// This function sets the supply values of the nodes. |
| 427 | 430 |
/// If neither this function nor \ref stSupply() is used before |
| 428 | 431 |
/// calling \ref run(), the supply of each node will be set to zero. |
| 429 | 432 |
/// |
| 430 | 433 |
/// \param map A node map storing the supply values. |
| 431 | 434 |
/// Its \c Value type must be convertible to the \c Value type |
| 432 | 435 |
/// of the algorithm. |
| 433 | 436 |
/// |
| 434 | 437 |
/// \return <tt>(*this)</tt> |
| 435 | 438 |
template<typename SupplyMap> |
| 436 | 439 |
CostScaling& supplyMap(const SupplyMap& map) {
|
| 437 | 440 |
for (NodeIt n(_graph); n != INVALID; ++n) {
|
| 438 | 441 |
_supply[_node_id[n]] = map[n]; |
| 439 | 442 |
} |
| 440 | 443 |
return *this; |
| 441 | 444 |
} |
| 442 | 445 |
|
| 443 | 446 |
/// \brief Set single source and target nodes and a supply value. |
| 444 | 447 |
/// |
| 445 | 448 |
/// This function sets a single source node and a single target node |
| 446 | 449 |
/// and the required flow value. |
| 447 | 450 |
/// If neither this function nor \ref supplyMap() is used before |
| 448 | 451 |
/// calling \ref run(), the supply of each node will be set to zero. |
| 449 | 452 |
/// |
| 450 | 453 |
/// Using this function has the same effect as using \ref supplyMap() |
| 451 |
/// with |
|
| 454 |
/// with a map in which \c k is assigned to \c s, \c -k is |
|
| 452 | 455 |
/// assigned to \c t and all other nodes have zero supply value. |
| 453 | 456 |
/// |
| 454 | 457 |
/// \param s The source node. |
| 455 | 458 |
/// \param t The target node. |
| 456 | 459 |
/// \param k The required amount of flow from node \c s to node \c t |
| 457 | 460 |
/// (i.e. the supply of \c s and the demand of \c t). |
| 458 | 461 |
/// |
| 459 | 462 |
/// \return <tt>(*this)</tt> |
| 460 | 463 |
CostScaling& stSupply(const Node& s, const Node& t, Value k) {
|
| 461 | 464 |
for (int i = 0; i != _res_node_num; ++i) {
|
| 462 | 465 |
_supply[i] = 0; |
| 463 | 466 |
} |
| 464 | 467 |
_supply[_node_id[s]] = k; |
| 465 | 468 |
_supply[_node_id[t]] = -k; |
| 466 | 469 |
return *this; |
| 467 | 470 |
} |
| 468 | 471 |
|
| 469 | 472 |
/// @} |
| 470 | 473 |
|
| 471 | 474 |
/// \name Execution control |
| 472 | 475 |
/// The algorithm can be executed using \ref run(). |
| 473 | 476 |
|
| 474 | 477 |
/// @{
|
| 475 | 478 |
|
| 476 | 479 |
/// \brief Run the algorithm. |
| 477 | 480 |
/// |
| 478 | 481 |
/// This function runs the algorithm. |
| 479 | 482 |
/// The paramters can be specified using functions \ref lowerMap(), |
| 480 | 483 |
/// \ref upperMap(), \ref costMap(), \ref supplyMap(), \ref stSupply(). |
| 481 | 484 |
/// For example, |
| 482 | 485 |
/// \code |
| 483 | 486 |
/// CostScaling<ListDigraph> cs(graph); |
| 484 | 487 |
/// cs.lowerMap(lower).upperMap(upper).costMap(cost) |
| 485 | 488 |
/// .supplyMap(sup).run(); |
| 486 | 489 |
/// \endcode |
| 487 | 490 |
/// |
| 488 | 491 |
/// This function can be called more than once. All the given parameters |
| 489 | 492 |
/// are kept for the next call, unless \ref resetParams() or \ref reset() |
| 490 | 493 |
/// is used, thus only the modified parameters have to be set again. |
| 491 | 494 |
/// If the underlying digraph was also modified after the construction |
| 492 | 495 |
/// of the class (or the last \ref reset() call), then the \ref reset() |
| 493 | 496 |
/// function must be called. |
| 494 | 497 |
/// |
| 495 | 498 |
/// \param method The internal method that will be used in the |
| 496 | 499 |
/// algorithm. For more information, see \ref Method. |
| 497 | 500 |
/// \param factor The cost scaling factor. It must be larger than one. |
| 498 | 501 |
/// |
| 499 | 502 |
/// \return \c INFEASIBLE if no feasible flow exists, |
| ... | ... |
@@ -23,147 +23,146 @@ |
| 23 | 23 |
/// \file |
| 24 | 24 |
/// \brief Cycle-canceling algorithms for finding a minimum cost flow. |
| 25 | 25 |
|
| 26 | 26 |
#include <vector> |
| 27 | 27 |
#include <limits> |
| 28 | 28 |
|
| 29 | 29 |
#include <lemon/core.h> |
| 30 | 30 |
#include <lemon/maps.h> |
| 31 | 31 |
#include <lemon/path.h> |
| 32 | 32 |
#include <lemon/math.h> |
| 33 | 33 |
#include <lemon/static_graph.h> |
| 34 | 34 |
#include <lemon/adaptors.h> |
| 35 | 35 |
#include <lemon/circulation.h> |
| 36 | 36 |
#include <lemon/bellman_ford.h> |
| 37 | 37 |
#include <lemon/howard_mmc.h> |
| 38 | 38 |
|
| 39 | 39 |
namespace lemon {
|
| 40 | 40 |
|
| 41 | 41 |
/// \addtogroup min_cost_flow_algs |
| 42 | 42 |
/// @{
|
| 43 | 43 |
|
| 44 | 44 |
/// \brief Implementation of cycle-canceling algorithms for |
| 45 | 45 |
/// finding a \ref min_cost_flow "minimum cost flow". |
| 46 | 46 |
/// |
| 47 | 47 |
/// \ref CycleCanceling implements three different cycle-canceling |
| 48 | 48 |
/// algorithms for finding a \ref min_cost_flow "minimum cost flow" |
| 49 | 49 |
/// \ref amo93networkflows, \ref klein67primal, |
| 50 | 50 |
/// \ref goldberg89cyclecanceling. |
| 51 | 51 |
/// The most efficent one (both theoretically and practically) |
| 52 | 52 |
/// is the \ref CANCEL_AND_TIGHTEN "Cancel and Tighten" algorithm, |
| 53 | 53 |
/// thus it is the default method. |
| 54 | 54 |
/// It is strongly polynomial, but in practice, it is typically much |
| 55 | 55 |
/// slower than the scaling algorithms and NetworkSimplex. |
| 56 | 56 |
/// |
| 57 | 57 |
/// Most of the parameters of the problem (except for the digraph) |
| 58 | 58 |
/// can be given using separate functions, and the algorithm can be |
| 59 | 59 |
/// executed using the \ref run() function. If some parameters are not |
| 60 | 60 |
/// specified, then default values will be used. |
| 61 | 61 |
/// |
| 62 | 62 |
/// \tparam GR The digraph type the algorithm runs on. |
| 63 | 63 |
/// \tparam V The number type used for flow amounts, capacity bounds |
| 64 | 64 |
/// and supply values in the algorithm. By default, it is \c int. |
| 65 | 65 |
/// \tparam C The number type used for costs and potentials in the |
| 66 | 66 |
/// algorithm. By default, it is the same as \c V. |
| 67 | 67 |
/// |
| 68 | 68 |
/// \warning Both \c V and \c C must be signed number types. |
| 69 | 69 |
/// \warning All input data (capacities, supply values, and costs) must |
| 70 | 70 |
/// be integer. |
| 71 |
/// \warning This algorithm does not support negative costs for such |
|
| 72 |
/// arcs that have infinite upper bound. |
|
| 71 |
/// \warning This algorithm does not support negative costs for |
|
| 72 |
/// arcs having infinite upper bound. |
|
| 73 | 73 |
/// |
| 74 | 74 |
/// \note For more information about the three available methods, |
| 75 | 75 |
/// see \ref Method. |
| 76 | 76 |
#ifdef DOXYGEN |
| 77 | 77 |
template <typename GR, typename V, typename C> |
| 78 | 78 |
#else |
| 79 | 79 |
template <typename GR, typename V = int, typename C = V> |
| 80 | 80 |
#endif |
| 81 | 81 |
class CycleCanceling |
| 82 | 82 |
{
|
| 83 | 83 |
public: |
| 84 | 84 |
|
| 85 | 85 |
/// The type of the digraph |
| 86 | 86 |
typedef GR Digraph; |
| 87 | 87 |
/// The type of the flow amounts, capacity bounds and supply values |
| 88 | 88 |
typedef V Value; |
| 89 | 89 |
/// The type of the arc costs |
| 90 | 90 |
typedef C Cost; |
| 91 | 91 |
|
| 92 | 92 |
public: |
| 93 | 93 |
|
| 94 | 94 |
/// \brief Problem type constants for the \c run() function. |
| 95 | 95 |
/// |
| 96 | 96 |
/// Enum type containing the problem type constants that can be |
| 97 | 97 |
/// returned by the \ref run() function of the algorithm. |
| 98 | 98 |
enum ProblemType {
|
| 99 | 99 |
/// The problem has no feasible solution (flow). |
| 100 | 100 |
INFEASIBLE, |
| 101 | 101 |
/// The problem has optimal solution (i.e. it is feasible and |
| 102 | 102 |
/// bounded), and the algorithm has found optimal flow and node |
| 103 | 103 |
/// potentials (primal and dual solutions). |
| 104 | 104 |
OPTIMAL, |
| 105 | 105 |
/// The digraph contains an arc of negative cost and infinite |
| 106 | 106 |
/// upper bound. It means that the objective function is unbounded |
| 107 | 107 |
/// on that arc, however, note that it could actually be bounded |
| 108 | 108 |
/// over the feasible flows, but this algroithm cannot handle |
| 109 | 109 |
/// these cases. |
| 110 | 110 |
UNBOUNDED |
| 111 | 111 |
}; |
| 112 | 112 |
|
| 113 | 113 |
/// \brief Constants for selecting the used method. |
| 114 | 114 |
/// |
| 115 | 115 |
/// Enum type containing constants for selecting the used method |
| 116 | 116 |
/// for the \ref run() function. |
| 117 | 117 |
/// |
| 118 | 118 |
/// \ref CycleCanceling provides three different cycle-canceling |
| 119 | 119 |
/// methods. By default, \ref CANCEL_AND_TIGHTEN "Cancel and Tighten" |
| 120 |
/// is used, which proved to be the most efficient and the most robust |
|
| 121 |
/// on various test inputs. |
|
| 120 |
/// is used, which is by far the most efficient and the most robust. |
|
| 122 | 121 |
/// However, the other methods can be selected using the \ref run() |
| 123 | 122 |
/// function with the proper parameter. |
| 124 | 123 |
enum Method {
|
| 125 | 124 |
/// A simple cycle-canceling method, which uses the |
| 126 | 125 |
/// \ref BellmanFord "Bellman-Ford" algorithm with limited iteration |
| 127 | 126 |
/// number for detecting negative cycles in the residual network. |
| 128 | 127 |
SIMPLE_CYCLE_CANCELING, |
| 129 | 128 |
/// The "Minimum Mean Cycle-Canceling" algorithm, which is a |
| 130 | 129 |
/// well-known strongly polynomial method |
| 131 | 130 |
/// \ref goldberg89cyclecanceling. It improves along a |
| 132 | 131 |
/// \ref min_mean_cycle "minimum mean cycle" in each iteration. |
| 133 | 132 |
/// Its running time complexity is O(n<sup>2</sup>m<sup>3</sup>log(n)). |
| 134 | 133 |
MINIMUM_MEAN_CYCLE_CANCELING, |
| 135 | 134 |
/// The "Cancel And Tighten" algorithm, which can be viewed as an |
| 136 | 135 |
/// improved version of the previous method |
| 137 | 136 |
/// \ref goldberg89cyclecanceling. |
| 138 | 137 |
/// It is faster both in theory and in practice, its running time |
| 139 | 138 |
/// complexity is O(n<sup>2</sup>m<sup>2</sup>log(n)). |
| 140 | 139 |
CANCEL_AND_TIGHTEN |
| 141 | 140 |
}; |
| 142 | 141 |
|
| 143 | 142 |
private: |
| 144 | 143 |
|
| 145 | 144 |
TEMPLATE_DIGRAPH_TYPEDEFS(GR); |
| 146 | 145 |
|
| 147 | 146 |
typedef std::vector<int> IntVector; |
| 148 | 147 |
typedef std::vector<double> DoubleVector; |
| 149 | 148 |
typedef std::vector<Value> ValueVector; |
| 150 | 149 |
typedef std::vector<Cost> CostVector; |
| 151 | 150 |
typedef std::vector<char> BoolVector; |
| 152 | 151 |
// Note: vector<char> is used instead of vector<bool> for efficiency reasons |
| 153 | 152 |
|
| 154 | 153 |
private: |
| 155 | 154 |
|
| 156 | 155 |
template <typename KT, typename VT> |
| 157 | 156 |
class StaticVectorMap {
|
| 158 | 157 |
public: |
| 159 | 158 |
typedef KT Key; |
| 160 | 159 |
typedef VT Value; |
| 161 | 160 |
|
| 162 | 161 |
StaticVectorMap(std::vector<Value>& v) : _v(v) {}
|
| 163 | 162 |
|
| 164 | 163 |
const Value& operator[](const Key& key) const {
|
| 165 | 164 |
return _v[StaticDigraph::id(key)]; |
| 166 | 165 |
} |
| 167 | 166 |
|
| 168 | 167 |
Value& operator[](const Key& key) {
|
| 169 | 168 |
return _v[StaticDigraph::id(key)]; |
| ... | ... |
@@ -305,97 +304,97 @@ |
| 305 | 304 |
|
| 306 | 305 |
/// \brief Set the costs of the arcs. |
| 307 | 306 |
/// |
| 308 | 307 |
/// This function sets the costs of the arcs. |
| 309 | 308 |
/// If it is not used before calling \ref run(), the costs |
| 310 | 309 |
/// will be set to \c 1 on all arcs. |
| 311 | 310 |
/// |
| 312 | 311 |
/// \param map An arc map storing the costs. |
| 313 | 312 |
/// Its \c Value type must be convertible to the \c Cost type |
| 314 | 313 |
/// of the algorithm. |
| 315 | 314 |
/// |
| 316 | 315 |
/// \return <tt>(*this)</tt> |
| 317 | 316 |
template<typename CostMap> |
| 318 | 317 |
CycleCanceling& costMap(const CostMap& map) {
|
| 319 | 318 |
for (ArcIt a(_graph); a != INVALID; ++a) {
|
| 320 | 319 |
_cost[_arc_idf[a]] = map[a]; |
| 321 | 320 |
_cost[_arc_idb[a]] = -map[a]; |
| 322 | 321 |
} |
| 323 | 322 |
return *this; |
| 324 | 323 |
} |
| 325 | 324 |
|
| 326 | 325 |
/// \brief Set the supply values of the nodes. |
| 327 | 326 |
/// |
| 328 | 327 |
/// This function sets the supply values of the nodes. |
| 329 | 328 |
/// If neither this function nor \ref stSupply() is used before |
| 330 | 329 |
/// calling \ref run(), the supply of each node will be set to zero. |
| 331 | 330 |
/// |
| 332 | 331 |
/// \param map A node map storing the supply values. |
| 333 | 332 |
/// Its \c Value type must be convertible to the \c Value type |
| 334 | 333 |
/// of the algorithm. |
| 335 | 334 |
/// |
| 336 | 335 |
/// \return <tt>(*this)</tt> |
| 337 | 336 |
template<typename SupplyMap> |
| 338 | 337 |
CycleCanceling& supplyMap(const SupplyMap& map) {
|
| 339 | 338 |
for (NodeIt n(_graph); n != INVALID; ++n) {
|
| 340 | 339 |
_supply[_node_id[n]] = map[n]; |
| 341 | 340 |
} |
| 342 | 341 |
return *this; |
| 343 | 342 |
} |
| 344 | 343 |
|
| 345 | 344 |
/// \brief Set single source and target nodes and a supply value. |
| 346 | 345 |
/// |
| 347 | 346 |
/// This function sets a single source node and a single target node |
| 348 | 347 |
/// and the required flow value. |
| 349 | 348 |
/// If neither this function nor \ref supplyMap() is used before |
| 350 | 349 |
/// calling \ref run(), the supply of each node will be set to zero. |
| 351 | 350 |
/// |
| 352 | 351 |
/// Using this function has the same effect as using \ref supplyMap() |
| 353 |
/// with |
|
| 352 |
/// with a map in which \c k is assigned to \c s, \c -k is |
|
| 354 | 353 |
/// assigned to \c t and all other nodes have zero supply value. |
| 355 | 354 |
/// |
| 356 | 355 |
/// \param s The source node. |
| 357 | 356 |
/// \param t The target node. |
| 358 | 357 |
/// \param k The required amount of flow from node \c s to node \c t |
| 359 | 358 |
/// (i.e. the supply of \c s and the demand of \c t). |
| 360 | 359 |
/// |
| 361 | 360 |
/// \return <tt>(*this)</tt> |
| 362 | 361 |
CycleCanceling& stSupply(const Node& s, const Node& t, Value k) {
|
| 363 | 362 |
for (int i = 0; i != _res_node_num; ++i) {
|
| 364 | 363 |
_supply[i] = 0; |
| 365 | 364 |
} |
| 366 | 365 |
_supply[_node_id[s]] = k; |
| 367 | 366 |
_supply[_node_id[t]] = -k; |
| 368 | 367 |
return *this; |
| 369 | 368 |
} |
| 370 | 369 |
|
| 371 | 370 |
/// @} |
| 372 | 371 |
|
| 373 | 372 |
/// \name Execution control |
| 374 | 373 |
/// The algorithm can be executed using \ref run(). |
| 375 | 374 |
|
| 376 | 375 |
/// @{
|
| 377 | 376 |
|
| 378 | 377 |
/// \brief Run the algorithm. |
| 379 | 378 |
/// |
| 380 | 379 |
/// This function runs the algorithm. |
| 381 | 380 |
/// The paramters can be specified using functions \ref lowerMap(), |
| 382 | 381 |
/// \ref upperMap(), \ref costMap(), \ref supplyMap(), \ref stSupply(). |
| 383 | 382 |
/// For example, |
| 384 | 383 |
/// \code |
| 385 | 384 |
/// CycleCanceling<ListDigraph> cc(graph); |
| 386 | 385 |
/// cc.lowerMap(lower).upperMap(upper).costMap(cost) |
| 387 | 386 |
/// .supplyMap(sup).run(); |
| 388 | 387 |
/// \endcode |
| 389 | 388 |
/// |
| 390 | 389 |
/// This function can be called more than once. All the given parameters |
| 391 | 390 |
/// are kept for the next call, unless \ref resetParams() or \ref reset() |
| 392 | 391 |
/// is used, thus only the modified parameters have to be set again. |
| 393 | 392 |
/// If the underlying digraph was also modified after the construction |
| 394 | 393 |
/// of the class (or the last \ref reset() call), then the \ref reset() |
| 395 | 394 |
/// function must be called. |
| 396 | 395 |
/// |
| 397 | 396 |
/// \param method The cycle-canceling method that will be used. |
| 398 | 397 |
/// For more information, see \ref Method. |
| 399 | 398 |
/// |
| 400 | 399 |
/// \return \c INFEASIBLE if no feasible flow exists, |
| 401 | 400 |
/// \n \c OPTIMAL if the problem has optimal solution |
| 1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
| 2 | 2 |
* |
| 3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
| 4 | 4 |
* |
| 5 | 5 |
* Copyright (C) 2003-2010 |
| 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_EULER_H |
| 20 | 20 |
#define LEMON_EULER_H |
| 21 | 21 |
|
| 22 | 22 |
#include<lemon/core.h> |
| 23 | 23 |
#include<lemon/adaptors.h> |
| 24 | 24 |
#include<lemon/connectivity.h> |
| 25 | 25 |
#include <list> |
| 26 | 26 |
|
| 27 | 27 |
/// \ingroup graph_properties |
| 28 | 28 |
/// \file |
| 29 | 29 |
/// \brief Euler tour iterators and a function for checking the \e Eulerian |
| 30 | 30 |
/// property. |
| 31 | 31 |
/// |
| 32 | 32 |
///This file provides Euler tour iterators and a function to check |
| 33 | 33 |
///if a (di)graph is \e Eulerian. |
| 34 | 34 |
|
| 35 | 35 |
namespace lemon {
|
| 36 | 36 |
|
| 37 | 37 |
///Euler tour iterator for digraphs. |
| 38 | 38 |
|
| 39 |
/// \ingroup |
|
| 39 |
/// \ingroup graph_properties |
|
| 40 | 40 |
///This iterator provides an Euler tour (Eulerian circuit) of a \e directed |
| 41 | 41 |
///graph (if there exists) and it converts to the \c Arc type of the digraph. |
| 42 | 42 |
/// |
| 43 | 43 |
///For example, if the given digraph has an Euler tour (i.e it has only one |
| 44 | 44 |
///non-trivial component and the in-degree is equal to the out-degree |
| 45 | 45 |
///for all nodes), then the following code will put the arcs of \c g |
| 46 | 46 |
///to the vector \c et according to an Euler tour of \c g. |
| 47 | 47 |
///\code |
| 48 | 48 |
/// std::vector<ListDigraph::Arc> et; |
| 49 | 49 |
/// for(DiEulerIt<ListDigraph> e(g); e!=INVALID; ++e) |
| 50 | 50 |
/// et.push_back(e); |
| 51 | 51 |
///\endcode |
| 52 | 52 |
///If \c g has no Euler tour, then the resulted walk will not be closed |
| 53 | 53 |
///or not contain all arcs. |
| 54 | 54 |
///\sa EulerIt |
| 55 | 55 |
template<typename GR> |
| 56 | 56 |
class DiEulerIt |
| 57 | 57 |
{
|
| 58 | 58 |
typedef typename GR::Node Node; |
| 59 | 59 |
typedef typename GR::NodeIt NodeIt; |
| 60 | 60 |
typedef typename GR::Arc Arc; |
| 61 | 61 |
typedef typename GR::ArcIt ArcIt; |
| 62 | 62 |
typedef typename GR::OutArcIt OutArcIt; |
| 63 | 63 |
typedef typename GR::InArcIt InArcIt; |
| 64 | 64 |
|
| 65 | 65 |
const GR &g; |
| 66 | 66 |
typename GR::template NodeMap<OutArcIt> narc; |
| 67 | 67 |
std::list<Arc> euler; |
| 68 | 68 |
|
| 69 | 69 |
public: |
| 70 | 70 |
|
| 71 | 71 |
///Constructor |
| 72 | 72 |
|
| 73 | 73 |
///Constructor. |
| 74 | 74 |
///\param gr A digraph. |
| 75 | 75 |
///\param start The starting point of the tour. If it is not given, |
| 76 | 76 |
///the tour will start from the first node that has an outgoing arc. |
| 77 | 77 |
DiEulerIt(const GR &gr, typename GR::Node start = INVALID) |
| 78 | 78 |
: g(gr), narc(g) |
| 79 | 79 |
{
|
| 80 | 80 |
if (start==INVALID) {
|
| 81 | 81 |
NodeIt n(g); |
| 82 | 82 |
while (n!=INVALID && OutArcIt(g,n)==INVALID) ++n; |
| 83 | 83 |
start=n; |
| 84 | 84 |
} |
| 85 | 85 |
if (start!=INVALID) {
|
| 86 | 86 |
for (NodeIt n(g); n!=INVALID; ++n) narc[n]=OutArcIt(g,n); |
| 87 | 87 |
while (narc[start]!=INVALID) {
|
| 1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
| 2 | 2 |
* |
| 3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
| 4 | 4 |
* |
| 5 | 5 |
* Copyright (C) 2003-2010 |
| 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_GROSSO_LOCATELLI_PULLAN_MC_H |
| 20 | 20 |
#define LEMON_GROSSO_LOCATELLI_PULLAN_MC_H |
| 21 | 21 |
|
| 22 | 22 |
/// \ingroup approx_algs |
| 23 | 23 |
/// |
| 24 | 24 |
/// \file |
| 25 | 25 |
/// \brief The iterated local search algorithm of Grosso, Locatelli, and Pullan |
| 26 | 26 |
/// for the maximum clique problem |
| 27 | 27 |
|
| 28 | 28 |
#include <vector> |
| 29 | 29 |
#include <limits> |
| 30 | 30 |
#include <lemon/core.h> |
| 31 | 31 |
#include <lemon/random.h> |
| 32 | 32 |
|
| 33 | 33 |
namespace lemon {
|
| 34 | 34 |
|
| 35 | 35 |
/// \addtogroup approx_algs |
| 36 | 36 |
/// @{
|
| 37 | 37 |
|
| 38 | 38 |
/// \brief Implementation of the iterated local search algorithm of Grosso, |
| 39 | 39 |
/// Locatelli, and Pullan for the maximum clique problem |
| 40 | 40 |
/// |
| 41 | 41 |
/// \ref GrossoLocatelliPullanMc implements the iterated local search |
| 42 | 42 |
/// algorithm of Grosso, Locatelli, and Pullan for solving the \e maximum |
| 43 | 43 |
/// \e clique \e problem \ref grosso08maxclique. |
| 44 | 44 |
/// It is to find the largest complete subgraph (\e clique) in an |
| 45 | 45 |
/// undirected graph, i.e., the largest set of nodes where each |
| 46 | 46 |
/// pair of nodes is connected. |
| 47 | 47 |
/// |
| 48 | 48 |
/// This class provides a simple but highly efficient and robust heuristic |
| 49 |
/// method that quickly finds a large clique, but not necessarily the |
|
| 49 |
/// method that quickly finds a quite large clique, but not necessarily the |
|
| 50 | 50 |
/// largest one. |
| 51 |
/// The algorithm performs a certain number of iterations to find several |
|
| 52 |
/// cliques and selects the largest one among them. Various limits can be |
|
| 53 |
/// specified to control the running time and the effectiveness of the |
|
| 54 |
/// search process. |
|
| 51 | 55 |
/// |
| 52 | 56 |
/// \tparam GR The undirected graph type the algorithm runs on. |
| 53 | 57 |
/// |
| 54 | 58 |
/// \note %GrossoLocatelliPullanMc provides three different node selection |
| 55 | 59 |
/// rules, from which the most powerful one is used by default. |
| 56 | 60 |
/// For more information, see \ref SelectionRule. |
| 57 | 61 |
template <typename GR> |
| 58 | 62 |
class GrossoLocatelliPullanMc |
| 59 | 63 |
{
|
| 60 | 64 |
public: |
| 61 | 65 |
|
| 62 | 66 |
/// \brief Constants for specifying the node selection rule. |
| 63 | 67 |
/// |
| 64 | 68 |
/// Enum type containing constants for specifying the node selection rule |
| 65 | 69 |
/// for the \ref run() function. |
| 66 | 70 |
/// |
| 67 | 71 |
/// During the algorithm, nodes are selected for addition to the current |
| 68 | 72 |
/// clique according to the applied rule. |
| 69 | 73 |
/// In general, the PENALTY_BASED rule turned out to be the most powerful |
| 70 | 74 |
/// and the most robust, thus it is the default option. |
| 71 | 75 |
/// However, another selection rule can be specified using the \ref run() |
| 72 | 76 |
/// function with the proper parameter. |
| 73 | 77 |
enum SelectionRule {
|
| 74 | 78 |
|
| 75 | 79 |
/// A node is selected randomly without any evaluation at each step. |
| 76 | 80 |
RANDOM, |
| 77 | 81 |
|
| 78 | 82 |
/// A node of maximum degree is selected randomly at each step. |
| 79 | 83 |
DEGREE_BASED, |
| 80 | 84 |
|
| 81 | 85 |
/// A node of minimum penalty is selected randomly at each step. |
| 82 | 86 |
/// The node penalties are updated adaptively after each stage of the |
| 83 | 87 |
/// search process. |
| 84 | 88 |
PENALTY_BASED |
| 85 | 89 |
}; |
| 86 | 90 |
|
| 91 |
/// \brief Constants for the causes of search termination. |
|
| 92 |
/// |
|
| 93 |
/// Enum type containing constants for the different causes of search |
|
| 94 |
/// termination. The \ref run() function returns one of these values. |
|
| 95 |
enum TerminationCause {
|
|
| 96 |
|
|
| 97 |
/// The iteration count limit is reached. |
|
| 98 |
ITERATION_LIMIT, |
|
| 99 |
|
|
| 100 |
/// The step count limit is reached. |
|
| 101 |
STEP_LIMIT, |
|
| 102 |
|
|
| 103 |
/// The clique size limit is reached. |
|
| 104 |
SIZE_LIMIT |
|
| 105 |
}; |
|
| 106 |
|
|
| 87 | 107 |
private: |
| 88 | 108 |
|
| 89 | 109 |
TEMPLATE_GRAPH_TYPEDEFS(GR); |
| 90 | 110 |
|
| 91 | 111 |
typedef std::vector<int> IntVector; |
| 92 | 112 |
typedef std::vector<char> BoolVector; |
| 93 | 113 |
typedef std::vector<BoolVector> BoolMatrix; |
| 94 | 114 |
// Note: vector<char> is used instead of vector<bool> for efficiency reasons |
| 95 | 115 |
|
| 116 |
// The underlying graph |
|
| 96 | 117 |
const GR &_graph; |
| 97 | 118 |
IntNodeMap _id; |
| 98 | 119 |
|
| 99 | 120 |
// Internal matrix representation of the graph |
| 100 | 121 |
BoolMatrix _gr; |
| 101 | 122 |
int _n; |
| 102 | 123 |
|
| 124 |
// Search options |
|
| 125 |
bool _delta_based_restart; |
|
| 126 |
int _restart_delta_limit; |
|
| 127 |
|
|
| 128 |
// Search limits |
|
| 129 |
int _iteration_limit; |
|
| 130 |
int _step_limit; |
|
| 131 |
int _size_limit; |
|
| 132 |
|
|
| 103 | 133 |
// The current clique |
| 104 | 134 |
BoolVector _clique; |
| 105 | 135 |
int _size; |
| 106 | 136 |
|
| 107 | 137 |
// The best clique found so far |
| 108 | 138 |
BoolVector _best_clique; |
| 109 | 139 |
int _best_size; |
| 110 | 140 |
|
| 111 | 141 |
// The "distances" of the nodes from the current clique. |
| 112 | 142 |
// _delta[u] is the number of nodes in the clique that are |
| 113 | 143 |
// not connected with u. |
| 114 | 144 |
IntVector _delta; |
| 115 | 145 |
|
| 116 | 146 |
// The current tabu set |
| 117 | 147 |
BoolVector _tabu; |
| 118 | 148 |
|
| 119 | 149 |
// Random number generator |
| 120 | 150 |
Random _rnd; |
| 121 | 151 |
|
| 122 | 152 |
private: |
| 123 | 153 |
|
| 124 | 154 |
// Implementation of the RANDOM node selection rule. |
| 125 | 155 |
class RandomSelectionRule |
| 126 | 156 |
{
|
| 127 | 157 |
private: |
| 128 | 158 |
|
| 129 | 159 |
// References to the algorithm instance |
| 130 | 160 |
const BoolVector &_clique; |
| 131 | 161 |
const IntVector &_delta; |
| 132 | 162 |
const BoolVector &_tabu; |
| 133 | 163 |
Random &_rnd; |
| 134 | 164 |
|
| 135 | 165 |
// Pivot rule data |
| 136 | 166 |
int _n; |
| 137 | 167 |
|
| 138 | 168 |
public: |
| 139 | 169 |
|
| 140 | 170 |
// Constructor |
| 141 | 171 |
RandomSelectionRule(GrossoLocatelliPullanMc &mc) : |
| 142 | 172 |
_clique(mc._clique), _delta(mc._delta), _tabu(mc._tabu), |
| 143 | 173 |
_rnd(mc._rnd), _n(mc._n) |
| 144 | 174 |
{}
|
| 145 | 175 |
|
| 146 | 176 |
// Return a node index for a feasible add move or -1 if no one exists |
| 147 | 177 |
int nextFeasibleAddNode() const {
|
| 148 | 178 |
int start_node = _rnd[_n]; |
| 149 | 179 |
for (int i = start_node; i != _n; i++) {
|
| 150 | 180 |
if (_delta[i] == 0 && !_tabu[i]) return i; |
| ... | ... |
@@ -335,346 +365,476 @@ |
| 335 | 365 |
min_p = _penalty[i]; |
| 336 | 366 |
} |
| 337 | 367 |
} |
| 338 | 368 |
for (int i = 0; i != start_node; i++) {
|
| 339 | 369 |
if (!_clique[i] && _delta[i] == 1 && !_tabu[i] && |
| 340 | 370 |
_penalty[i] < min_p) {
|
| 341 | 371 |
node = i; |
| 342 | 372 |
min_p = _penalty[i]; |
| 343 | 373 |
} |
| 344 | 374 |
} |
| 345 | 375 |
return node; |
| 346 | 376 |
} |
| 347 | 377 |
|
| 348 | 378 |
// Return a node index for an add move or -1 if no one exists |
| 349 | 379 |
int nextAddNode() const {
|
| 350 | 380 |
int start_node = _rnd[_n]; |
| 351 | 381 |
int node = -1, min_p = std::numeric_limits<int>::max(); |
| 352 | 382 |
for (int i = start_node; i != _n; i++) {
|
| 353 | 383 |
if (_delta[i] == 0 && _penalty[i] < min_p) {
|
| 354 | 384 |
node = i; |
| 355 | 385 |
min_p = _penalty[i]; |
| 356 | 386 |
} |
| 357 | 387 |
} |
| 358 | 388 |
for (int i = 0; i != start_node; i++) {
|
| 359 | 389 |
if (_delta[i] == 0 && _penalty[i] < min_p) {
|
| 360 | 390 |
node = i; |
| 361 | 391 |
min_p = _penalty[i]; |
| 362 | 392 |
} |
| 363 | 393 |
} |
| 364 | 394 |
return node; |
| 365 | 395 |
} |
| 366 | 396 |
|
| 367 | 397 |
// Update internal data structures between stages (if necessary) |
| 368 | 398 |
void update() {}
|
| 369 | 399 |
|
| 370 | 400 |
}; //class PenaltyBasedSelectionRule |
| 371 | 401 |
|
| 372 | 402 |
public: |
| 373 | 403 |
|
| 374 | 404 |
/// \brief Constructor. |
| 375 | 405 |
/// |
| 376 | 406 |
/// Constructor. |
| 377 | 407 |
/// The global \ref rnd "random number generator instance" is used |
| 378 | 408 |
/// during the algorithm. |
| 379 | 409 |
/// |
| 380 | 410 |
/// \param graph The undirected graph the algorithm runs on. |
| 381 | 411 |
GrossoLocatelliPullanMc(const GR& graph) : |
| 382 | 412 |
_graph(graph), _id(_graph), _rnd(rnd) |
| 383 |
{
|
|
| 413 |
{
|
|
| 414 |
initOptions(); |
|
| 415 |
} |
|
| 384 | 416 |
|
| 385 | 417 |
/// \brief Constructor with random seed. |
| 386 | 418 |
/// |
| 387 | 419 |
/// Constructor with random seed. |
| 388 | 420 |
/// |
| 389 | 421 |
/// \param graph The undirected graph the algorithm runs on. |
| 390 | 422 |
/// \param seed Seed value for the internal random number generator |
| 391 | 423 |
/// that is used during the algorithm. |
| 392 | 424 |
GrossoLocatelliPullanMc(const GR& graph, int seed) : |
| 393 | 425 |
_graph(graph), _id(_graph), _rnd(seed) |
| 394 |
{
|
|
| 426 |
{
|
|
| 427 |
initOptions(); |
|
| 428 |
} |
|
| 395 | 429 |
|
| 396 | 430 |
/// \brief Constructor with random number generator. |
| 397 | 431 |
/// |
| 398 | 432 |
/// Constructor with random number generator. |
| 399 | 433 |
/// |
| 400 | 434 |
/// \param graph The undirected graph the algorithm runs on. |
| 401 | 435 |
/// \param random A random number generator that is used during the |
| 402 | 436 |
/// algorithm. |
| 403 | 437 |
GrossoLocatelliPullanMc(const GR& graph, const Random& random) : |
| 404 | 438 |
_graph(graph), _id(_graph), _rnd(random) |
| 405 |
{
|
|
| 439 |
{
|
|
| 440 |
initOptions(); |
|
| 441 |
} |
|
| 406 | 442 |
|
| 407 | 443 |
/// \name Execution Control |
| 444 |
/// The \ref run() function can be used to execute the algorithm.\n |
|
| 445 |
/// The functions \ref iterationLimit(int), \ref stepLimit(int), and |
|
| 446 |
/// \ref sizeLimit(int) can be used to specify various limits for the |
|
| 447 |
/// search process. |
|
| 448 |
|
|
| 408 | 449 |
/// @{
|
| 409 | 450 |
|
| 451 |
/// \brief Sets the maximum number of iterations. |
|
| 452 |
/// |
|
| 453 |
/// This function sets the maximum number of iterations. |
|
| 454 |
/// Each iteration of the algorithm finds a maximal clique (but not |
|
| 455 |
/// necessarily the largest one) by performing several search steps |
|
| 456 |
/// (node selections). |
|
| 457 |
/// |
|
| 458 |
/// This limit controls the running time and the success of the |
|
| 459 |
/// algorithm. For larger values, the algorithm runs slower, but it more |
|
| 460 |
/// likely finds larger cliques. For smaller values, the algorithm is |
|
| 461 |
/// faster but probably gives worse results. |
|
| 462 |
/// |
|
| 463 |
/// The default value is \c 1000. |
|
| 464 |
/// \c -1 means that number of iterations is not limited. |
|
| 465 |
/// |
|
| 466 |
/// \warning You should specify a reasonable limit for the number of |
|
| 467 |
/// iterations and/or the number of search steps. |
|
| 468 |
/// |
|
| 469 |
/// \return <tt>(*this)</tt> |
|
| 470 |
/// |
|
| 471 |
/// \sa stepLimit(int) |
|
| 472 |
/// \sa sizeLimit(int) |
|
| 473 |
GrossoLocatelliPullanMc& iterationLimit(int limit) {
|
|
| 474 |
_iteration_limit = limit; |
|
| 475 |
return *this; |
|
| 476 |
} |
|
| 477 |
|
|
| 478 |
/// \brief Sets the maximum number of search steps. |
|
| 479 |
/// |
|
| 480 |
/// This function sets the maximum number of elementary search steps. |
|
| 481 |
/// Each iteration of the algorithm finds a maximal clique (but not |
|
| 482 |
/// necessarily the largest one) by performing several search steps |
|
| 483 |
/// (node selections). |
|
| 484 |
/// |
|
| 485 |
/// This limit controls the running time and the success of the |
|
| 486 |
/// algorithm. For larger values, the algorithm runs slower, but it more |
|
| 487 |
/// likely finds larger cliques. For smaller values, the algorithm is |
|
| 488 |
/// faster but probably gives worse results. |
|
| 489 |
/// |
|
| 490 |
/// The default value is \c -1, which means that number of steps |
|
| 491 |
/// is not limited explicitly. However, the number of iterations is |
|
| 492 |
/// limited and each iteration performs a finite number of search steps. |
|
| 493 |
/// |
|
| 494 |
/// \warning You should specify a reasonable limit for the number of |
|
| 495 |
/// iterations and/or the number of search steps. |
|
| 496 |
/// |
|
| 497 |
/// \return <tt>(*this)</tt> |
|
| 498 |
/// |
|
| 499 |
/// \sa iterationLimit(int) |
|
| 500 |
/// \sa sizeLimit(int) |
|
| 501 |
GrossoLocatelliPullanMc& stepLimit(int limit) {
|
|
| 502 |
_step_limit = limit; |
|
| 503 |
return *this; |
|
| 504 |
} |
|
| 505 |
|
|
| 506 |
/// \brief Sets the desired clique size. |
|
| 507 |
/// |
|
| 508 |
/// This function sets the desired clique size that serves as a search |
|
| 509 |
/// limit. If a clique of this size (or a larger one) is found, then the |
|
| 510 |
/// algorithm terminates. |
|
| 511 |
/// |
|
| 512 |
/// This function is especially useful if you know an exact upper bound |
|
| 513 |
/// for the size of the cliques in the graph or if any clique above |
|
| 514 |
/// a certain size limit is sufficient for your application. |
|
| 515 |
/// |
|
| 516 |
/// The default value is \c -1, which means that the size limit is set to |
|
| 517 |
/// the number of nodes in the graph. |
|
| 518 |
/// |
|
| 519 |
/// \return <tt>(*this)</tt> |
|
| 520 |
/// |
|
| 521 |
/// \sa iterationLimit(int) |
|
| 522 |
/// \sa stepLimit(int) |
|
| 523 |
GrossoLocatelliPullanMc& sizeLimit(int limit) {
|
|
| 524 |
_size_limit = limit; |
|
| 525 |
return *this; |
|
| 526 |
} |
|
| 527 |
|
|
| 528 |
/// \brief The maximum number of iterations. |
|
| 529 |
/// |
|
| 530 |
/// This function gives back the maximum number of iterations. |
|
| 531 |
/// \c -1 means that no limit is specified. |
|
| 532 |
/// |
|
| 533 |
/// \sa iterationLimit(int) |
|
| 534 |
int iterationLimit() const {
|
|
| 535 |
return _iteration_limit; |
|
| 536 |
} |
|
| 537 |
|
|
| 538 |
/// \brief The maximum number of search steps. |
|
| 539 |
/// |
|
| 540 |
/// This function gives back the maximum number of search steps. |
|
| 541 |
/// \c -1 means that no limit is specified. |
|
| 542 |
/// |
|
| 543 |
/// \sa stepLimit(int) |
|
| 544 |
int stepLimit() const {
|
|
| 545 |
return _step_limit; |
|
| 546 |
} |
|
| 547 |
|
|
| 548 |
/// \brief The desired clique size. |
|
| 549 |
/// |
|
| 550 |
/// This function gives back the desired clique size that serves as a |
|
| 551 |
/// search limit. \c -1 means that this limit is set to the number of |
|
| 552 |
/// nodes in the graph. |
|
| 553 |
/// |
|
| 554 |
/// \sa sizeLimit(int) |
|
| 555 |
int sizeLimit() const {
|
|
| 556 |
return _size_limit; |
|
| 557 |
} |
|
| 558 |
|
|
| 410 | 559 |
/// \brief Runs the algorithm. |
| 411 | 560 |
/// |
| 412 |
/// This function runs the algorithm. |
|
| 561 |
/// This function runs the algorithm. If one of the specified limits |
|
| 562 |
/// is reached, the search process terminates. |
|
| 413 | 563 |
/// |
| 414 |
/// \param step_num The maximum number of node selections (steps) |
|
| 415 |
/// during the search process. |
|
| 416 |
/// This parameter controls the running time and the success of the |
|
| 417 |
/// algorithm. For larger values, the algorithm runs slower but it more |
|
| 418 |
/// likely finds larger cliques. For smaller values, the algorithm is |
|
| 419 |
/// faster but probably gives worse results. |
|
| 420 | 564 |
/// \param rule The node selection rule. For more information, see |
| 421 | 565 |
/// \ref SelectionRule. |
| 422 | 566 |
/// |
| 423 |
/// \return The size of the found clique. |
|
| 424 |
int run(int step_num = 100000, |
|
| 425 |
|
|
| 567 |
/// \return The termination cause of the search. For more information, |
|
| 568 |
/// see \ref TerminationCause. |
|
| 569 |
TerminationCause run(SelectionRule rule = PENALTY_BASED) |
|
| 426 | 570 |
{
|
| 427 | 571 |
init(); |
| 428 | 572 |
switch (rule) {
|
| 429 | 573 |
case RANDOM: |
| 430 |
return start<RandomSelectionRule>( |
|
| 574 |
return start<RandomSelectionRule>(); |
|
| 431 | 575 |
case DEGREE_BASED: |
| 432 |
return start<DegreeBasedSelectionRule>(step_num); |
|
| 433 |
case PENALTY_BASED: |
|
| 434 |
return start< |
|
| 576 |
return start<DegreeBasedSelectionRule>(); |
|
| 577 |
default: |
|
| 578 |
return start<PenaltyBasedSelectionRule>(); |
|
| 435 | 579 |
} |
| 436 |
return 0; // avoid warning |
|
| 437 | 580 |
} |
| 438 | 581 |
|
| 439 | 582 |
/// @} |
| 440 | 583 |
|
| 441 | 584 |
/// \name Query Functions |
| 585 |
/// The results of the algorithm can be obtained using these functions.\n |
|
| 586 |
/// The run() function must be called before using them. |
|
| 587 |
|
|
| 442 | 588 |
/// @{
|
| 443 | 589 |
|
| 444 | 590 |
/// \brief The size of the found clique |
| 445 | 591 |
/// |
| 446 | 592 |
/// This function returns the size of the found clique. |
| 447 | 593 |
/// |
| 448 | 594 |
/// \pre run() must be called before using this function. |
| 449 | 595 |
int cliqueSize() const {
|
| 450 | 596 |
return _best_size; |
| 451 | 597 |
} |
| 452 | 598 |
|
| 453 | 599 |
/// \brief Gives back the found clique in a \c bool node map |
| 454 | 600 |
/// |
| 455 | 601 |
/// This function gives back the characteristic vector of the found |
| 456 | 602 |
/// clique in the given node map. |
| 457 | 603 |
/// It must be a \ref concepts::WriteMap "writable" node map with |
| 458 | 604 |
/// \c bool (or convertible) value type. |
| 459 | 605 |
/// |
| 460 | 606 |
/// \pre run() must be called before using this function. |
| 461 | 607 |
template <typename CliqueMap> |
| 462 | 608 |
void cliqueMap(CliqueMap &map) const {
|
| 463 | 609 |
for (NodeIt n(_graph); n != INVALID; ++n) {
|
| 464 | 610 |
map[n] = static_cast<bool>(_best_clique[_id[n]]); |
| 465 | 611 |
} |
| 466 | 612 |
} |
| 467 | 613 |
|
| 468 | 614 |
/// \brief Iterator to list the nodes of the found clique |
| 469 | 615 |
/// |
| 470 | 616 |
/// This iterator class lists the nodes of the found clique. |
| 471 | 617 |
/// Before using it, you must allocate a GrossoLocatelliPullanMc instance |
| 472 | 618 |
/// and call its \ref GrossoLocatelliPullanMc::run() "run()" method. |
| 473 | 619 |
/// |
| 474 | 620 |
/// The following example prints out the IDs of the nodes in the found |
| 475 | 621 |
/// clique. |
| 476 | 622 |
/// \code |
| 477 | 623 |
/// GrossoLocatelliPullanMc<Graph> mc(g); |
| 478 | 624 |
/// mc.run(); |
| 479 | 625 |
/// for (GrossoLocatelliPullanMc<Graph>::CliqueNodeIt n(mc); |
| 480 | 626 |
/// n != INVALID; ++n) |
| 481 | 627 |
/// {
|
| 482 | 628 |
/// std::cout << g.id(n) << std::endl; |
| 483 | 629 |
/// } |
| 484 | 630 |
/// \endcode |
| 485 | 631 |
class CliqueNodeIt |
| 486 | 632 |
{
|
| 487 | 633 |
private: |
| 488 | 634 |
NodeIt _it; |
| 489 | 635 |
BoolNodeMap _map; |
| 490 | 636 |
|
| 491 | 637 |
public: |
| 492 | 638 |
|
| 493 | 639 |
/// Constructor |
| 494 | 640 |
|
| 495 | 641 |
/// Constructor. |
| 496 | 642 |
/// \param mc The algorithm instance. |
| 497 | 643 |
CliqueNodeIt(const GrossoLocatelliPullanMc &mc) |
| 498 | 644 |
: _map(mc._graph) |
| 499 | 645 |
{
|
| 500 | 646 |
mc.cliqueMap(_map); |
| 501 | 647 |
for (_it = NodeIt(mc._graph); _it != INVALID && !_map[_it]; ++_it) ; |
| 502 | 648 |
} |
| 503 | 649 |
|
| 504 | 650 |
/// Conversion to \c Node |
| 505 | 651 |
operator Node() const { return _it; }
|
| 506 | 652 |
|
| 507 | 653 |
bool operator==(Invalid) const { return _it == INVALID; }
|
| 508 | 654 |
bool operator!=(Invalid) const { return _it != INVALID; }
|
| 509 | 655 |
|
| 510 | 656 |
/// Next node |
| 511 | 657 |
CliqueNodeIt &operator++() {
|
| 512 | 658 |
for (++_it; _it != INVALID && !_map[_it]; ++_it) ; |
| 513 | 659 |
return *this; |
| 514 | 660 |
} |
| 515 | 661 |
|
| 516 | 662 |
/// Postfix incrementation |
| 517 | 663 |
|
| 518 | 664 |
/// Postfix incrementation. |
| 519 | 665 |
/// |
| 520 | 666 |
/// \warning This incrementation returns a \c Node, not a |
| 521 | 667 |
/// \c CliqueNodeIt as one may expect. |
| 522 | 668 |
typename GR::Node operator++(int) {
|
| 523 | 669 |
Node n=*this; |
| 524 | 670 |
++(*this); |
| 525 | 671 |
return n; |
| 526 | 672 |
} |
| 527 | 673 |
|
| 528 | 674 |
}; |
| 529 | 675 |
|
| 530 | 676 |
/// @} |
| 531 | 677 |
|
| 532 | 678 |
private: |
| 533 | 679 |
|
| 680 |
// Initialize search options and limits |
|
| 681 |
void initOptions() {
|
|
| 682 |
// Search options |
|
| 683 |
_delta_based_restart = true; |
|
| 684 |
_restart_delta_limit = 4; |
|
| 685 |
|
|
| 686 |
// Search limits |
|
| 687 |
_iteration_limit = 1000; |
|
| 688 |
_step_limit = -1; // this is disabled by default |
|
| 689 |
_size_limit = -1; // this is disabled by default |
|
| 690 |
} |
|
| 691 |
|
|
| 534 | 692 |
// Adds a node to the current clique |
| 535 | 693 |
void addCliqueNode(int u) {
|
| 536 | 694 |
if (_clique[u]) return; |
| 537 | 695 |
_clique[u] = true; |
| 538 | 696 |
_size++; |
| 539 | 697 |
BoolVector &row = _gr[u]; |
| 540 | 698 |
for (int i = 0; i != _n; i++) {
|
| 541 | 699 |
if (!row[i]) _delta[i]++; |
| 542 | 700 |
} |
| 543 | 701 |
} |
| 544 | 702 |
|
| 545 | 703 |
// Removes a node from the current clique |
| 546 | 704 |
void delCliqueNode(int u) {
|
| 547 | 705 |
if (!_clique[u]) return; |
| 548 | 706 |
_clique[u] = false; |
| 549 | 707 |
_size--; |
| 550 | 708 |
BoolVector &row = _gr[u]; |
| 551 | 709 |
for (int i = 0; i != _n; i++) {
|
| 552 | 710 |
if (!row[i]) _delta[i]--; |
| 553 | 711 |
} |
| 554 | 712 |
} |
| 555 | 713 |
|
| 556 | 714 |
// Initialize data structures |
| 557 | 715 |
void init() {
|
| 558 | 716 |
_n = countNodes(_graph); |
| 559 | 717 |
int ui = 0; |
| 560 | 718 |
for (NodeIt u(_graph); u != INVALID; ++u) {
|
| 561 | 719 |
_id[u] = ui++; |
| 562 | 720 |
} |
| 563 | 721 |
_gr.clear(); |
| 564 | 722 |
_gr.resize(_n, BoolVector(_n, false)); |
| 565 | 723 |
ui = 0; |
| 566 | 724 |
for (NodeIt u(_graph); u != INVALID; ++u) {
|
| 567 | 725 |
for (IncEdgeIt e(_graph, u); e != INVALID; ++e) {
|
| 568 | 726 |
int vi = _id[_graph.runningNode(e)]; |
| 569 | 727 |
_gr[ui][vi] = true; |
| 570 | 728 |
_gr[vi][ui] = true; |
| 571 | 729 |
} |
| 572 | 730 |
++ui; |
| 573 | 731 |
} |
| 574 | 732 |
|
| 575 | 733 |
_clique.clear(); |
| 576 | 734 |
_clique.resize(_n, false); |
| 577 | 735 |
_size = 0; |
| 578 | 736 |
_best_clique.clear(); |
| 579 | 737 |
_best_clique.resize(_n, false); |
| 580 | 738 |
_best_size = 0; |
| 581 | 739 |
_delta.clear(); |
| 582 | 740 |
_delta.resize(_n, 0); |
| 583 | 741 |
_tabu.clear(); |
| 584 | 742 |
_tabu.resize(_n, false); |
| 585 | 743 |
} |
| 586 | 744 |
|
| 587 | 745 |
// Executes the algorithm |
| 588 | 746 |
template <typename SelectionRuleImpl> |
| 589 |
int start(int max_select) {
|
|
| 590 |
// Options for the restart rule |
|
| 591 |
const bool delta_based_restart = true; |
|
| 592 |
const int restart_delta_limit = 4; |
|
| 593 |
|
|
| 594 |
if (_n == 0) return 0; |
|
| 747 |
TerminationCause start() {
|
|
| 748 |
if (_n == 0) return SIZE_LIMIT; |
|
| 595 | 749 |
if (_n == 1) {
|
| 596 | 750 |
_best_clique[0] = true; |
| 597 | 751 |
_best_size = 1; |
| 598 |
return |
|
| 752 |
return SIZE_LIMIT; |
|
| 599 | 753 |
} |
| 600 | 754 |
|
| 601 |
// Iterated local search |
|
| 755 |
// Iterated local search algorithm |
|
| 756 |
const int max_size = _size_limit >= 0 ? _size_limit : _n; |
|
| 757 |
const int max_restart = _iteration_limit >= 0 ? |
|
| 758 |
_iteration_limit : std::numeric_limits<int>::max(); |
|
| 759 |
const int max_select = _step_limit >= 0 ? |
|
| 760 |
_step_limit : std::numeric_limits<int>::max(); |
|
| 761 |
|
|
| 602 | 762 |
SelectionRuleImpl sel_method(*this); |
| 603 |
int select = 0; |
|
| 763 |
int select = 0, restart = 0; |
|
| 604 | 764 |
IntVector restart_nodes; |
| 605 |
|
|
| 606 |
while (select < max_select) {
|
|
| 765 |
while (select < max_select && restart < max_restart) {
|
|
| 607 | 766 |
|
| 608 | 767 |
// Perturbation/restart |
| 609 |
|
|
| 768 |
restart++; |
|
| 769 |
if (_delta_based_restart) {
|
|
| 610 | 770 |
restart_nodes.clear(); |
| 611 | 771 |
for (int i = 0; i != _n; i++) {
|
| 612 |
if (_delta[i] >= |
|
| 772 |
if (_delta[i] >= _restart_delta_limit) |
|
| 613 | 773 |
restart_nodes.push_back(i); |
| 614 | 774 |
} |
| 615 | 775 |
} |
| 616 | 776 |
int rs_node = -1; |
| 617 | 777 |
if (restart_nodes.size() > 0) {
|
| 618 | 778 |
rs_node = restart_nodes[_rnd[restart_nodes.size()]]; |
| 619 | 779 |
} else {
|
| 620 | 780 |
rs_node = _rnd[_n]; |
| 621 | 781 |
} |
| 622 | 782 |
BoolVector &row = _gr[rs_node]; |
| 623 | 783 |
for (int i = 0; i != _n; i++) {
|
| 624 | 784 |
if (_clique[i] && !row[i]) delCliqueNode(i); |
| 625 | 785 |
} |
| 626 | 786 |
addCliqueNode(rs_node); |
| 627 | 787 |
|
| 628 | 788 |
// Local search |
| 629 | 789 |
_tabu.clear(); |
| 630 | 790 |
_tabu.resize(_n, false); |
| 631 | 791 |
bool tabu_empty = true; |
| 632 | 792 |
int max_swap = _size; |
| 633 | 793 |
while (select < max_select) {
|
| 634 | 794 |
select++; |
| 635 | 795 |
int u; |
| 636 | 796 |
if ((u = sel_method.nextFeasibleAddNode()) != -1) {
|
| 637 | 797 |
// Feasible add move |
| 638 | 798 |
addCliqueNode(u); |
| 639 | 799 |
if (tabu_empty) max_swap = _size; |
| 640 | 800 |
} |
| 641 | 801 |
else if ((u = sel_method.nextFeasibleSwapNode()) != -1) {
|
| 642 | 802 |
// Feasible swap move |
| 643 | 803 |
int v = -1; |
| 644 | 804 |
BoolVector &row = _gr[u]; |
| 645 | 805 |
for (int i = 0; i != _n; i++) {
|
| 646 | 806 |
if (_clique[i] && !row[i]) {
|
| 647 | 807 |
v = i; |
| 648 | 808 |
break; |
| 649 | 809 |
} |
| 650 | 810 |
} |
| 651 | 811 |
addCliqueNode(u); |
| 652 | 812 |
delCliqueNode(v); |
| 653 | 813 |
_tabu[v] = true; |
| 654 | 814 |
tabu_empty = false; |
| 655 | 815 |
if (--max_swap <= 0) break; |
| 656 | 816 |
} |
| 657 | 817 |
else if ((u = sel_method.nextAddNode()) != -1) {
|
| 658 | 818 |
// Non-feasible add move |
| 659 | 819 |
addCliqueNode(u); |
| 660 | 820 |
} |
| 661 | 821 |
else break; |
| 662 | 822 |
} |
| 663 | 823 |
if (_size > _best_size) {
|
| 664 | 824 |
_best_clique = _clique; |
| 665 | 825 |
_best_size = _size; |
| 666 |
if (_best_size |
|
| 826 |
if (_best_size >= max_size) return SIZE_LIMIT; |
|
| 667 | 827 |
} |
| 668 | 828 |
sel_method.update(); |
| 669 | 829 |
} |
| 670 | 830 |
|
| 671 |
return |
|
| 831 |
return (restart >= max_restart ? ITERATION_LIMIT : STEP_LIMIT); |
|
| 672 | 832 |
} |
| 673 | 833 |
|
| 674 | 834 |
}; //class GrossoLocatelliPullanMc |
| 675 | 835 |
|
| 676 | 836 |
///@} |
| 677 | 837 |
|
| 678 | 838 |
} //namespace lemon |
| 679 | 839 |
|
| 680 | 840 |
#endif //LEMON_GROSSO_LOCATELLI_PULLAN_MC_H |
| ... | ... |
@@ -2,176 +2,176 @@ |
| 2 | 2 |
* |
| 3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
| 4 | 4 |
* |
| 5 | 5 |
* Copyright (C) 2003-2010 |
| 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_NETWORK_SIMPLEX_H |
| 20 | 20 |
#define LEMON_NETWORK_SIMPLEX_H |
| 21 | 21 |
|
| 22 | 22 |
/// \ingroup min_cost_flow_algs |
| 23 | 23 |
/// |
| 24 | 24 |
/// \file |
| 25 | 25 |
/// \brief Network Simplex algorithm for finding a minimum cost flow. |
| 26 | 26 |
|
| 27 | 27 |
#include <vector> |
| 28 | 28 |
#include <limits> |
| 29 | 29 |
#include <algorithm> |
| 30 | 30 |
|
| 31 | 31 |
#include <lemon/core.h> |
| 32 | 32 |
#include <lemon/math.h> |
| 33 | 33 |
|
| 34 | 34 |
namespace lemon {
|
| 35 | 35 |
|
| 36 | 36 |
/// \addtogroup min_cost_flow_algs |
| 37 | 37 |
/// @{
|
| 38 | 38 |
|
| 39 | 39 |
/// \brief Implementation of the primal Network Simplex algorithm |
| 40 | 40 |
/// for finding a \ref min_cost_flow "minimum cost flow". |
| 41 | 41 |
/// |
| 42 | 42 |
/// \ref NetworkSimplex implements the primal Network Simplex algorithm |
| 43 | 43 |
/// for finding a \ref min_cost_flow "minimum cost flow" |
| 44 | 44 |
/// \ref amo93networkflows, \ref dantzig63linearprog, |
| 45 | 45 |
/// \ref kellyoneill91netsimplex. |
| 46 | 46 |
/// This algorithm is a highly efficient specialized version of the |
| 47 | 47 |
/// linear programming simplex method directly for the minimum cost |
| 48 | 48 |
/// flow problem. |
| 49 | 49 |
/// |
| 50 |
/// In general, %NetworkSimplex is the fastest implementation available |
|
| 51 |
/// in LEMON for this problem. |
|
| 52 |
/// Moreover, it supports both directions of the supply/demand inequality |
|
| 53 |
/// constraints. For more information, see \ref SupplyType. |
|
| 50 |
/// In general, \ref NetworkSimplex and \ref CostScaling are the fastest |
|
| 51 |
/// implementations available in LEMON for this problem. |
|
| 52 |
/// Furthermore, this class supports both directions of the supply/demand |
|
| 53 |
/// inequality constraints. For more information, see \ref SupplyType. |
|
| 54 | 54 |
/// |
| 55 | 55 |
/// Most of the parameters of the problem (except for the digraph) |
| 56 | 56 |
/// can be given using separate functions, and the algorithm can be |
| 57 | 57 |
/// executed using the \ref run() function. If some parameters are not |
| 58 | 58 |
/// specified, then default values will be used. |
| 59 | 59 |
/// |
| 60 | 60 |
/// \tparam GR The digraph type the algorithm runs on. |
| 61 | 61 |
/// \tparam V The number type used for flow amounts, capacity bounds |
| 62 | 62 |
/// and supply values in the algorithm. By default, it is \c int. |
| 63 | 63 |
/// \tparam C The number type used for costs and potentials in the |
| 64 | 64 |
/// algorithm. By default, it is the same as \c V. |
| 65 | 65 |
/// |
| 66 | 66 |
/// \warning Both \c V and \c C must be signed number types. |
| 67 | 67 |
/// \warning All input data (capacities, supply values, and costs) must |
| 68 | 68 |
/// be integer. |
| 69 | 69 |
/// |
| 70 | 70 |
/// \note %NetworkSimplex provides five different pivot rule |
| 71 | 71 |
/// implementations, from which the most efficient one is used |
| 72 | 72 |
/// by default. For more information, see \ref PivotRule. |
| 73 | 73 |
template <typename GR, typename V = int, typename C = V> |
| 74 | 74 |
class NetworkSimplex |
| 75 | 75 |
{
|
| 76 | 76 |
public: |
| 77 | 77 |
|
| 78 | 78 |
/// The type of the flow amounts, capacity bounds and supply values |
| 79 | 79 |
typedef V Value; |
| 80 | 80 |
/// The type of the arc costs |
| 81 | 81 |
typedef C Cost; |
| 82 | 82 |
|
| 83 | 83 |
public: |
| 84 | 84 |
|
| 85 | 85 |
/// \brief Problem type constants for the \c run() function. |
| 86 | 86 |
/// |
| 87 | 87 |
/// Enum type containing the problem type constants that can be |
| 88 | 88 |
/// returned by the \ref run() function of the algorithm. |
| 89 | 89 |
enum ProblemType {
|
| 90 | 90 |
/// The problem has no feasible solution (flow). |
| 91 | 91 |
INFEASIBLE, |
| 92 | 92 |
/// The problem has optimal solution (i.e. it is feasible and |
| 93 | 93 |
/// bounded), and the algorithm has found optimal flow and node |
| 94 | 94 |
/// potentials (primal and dual solutions). |
| 95 | 95 |
OPTIMAL, |
| 96 | 96 |
/// The objective function of the problem is unbounded, i.e. |
| 97 | 97 |
/// there is a directed cycle having negative total cost and |
| 98 | 98 |
/// infinite upper bound. |
| 99 | 99 |
UNBOUNDED |
| 100 | 100 |
}; |
| 101 | 101 |
|
| 102 | 102 |
/// \brief Constants for selecting the type of the supply constraints. |
| 103 | 103 |
/// |
| 104 | 104 |
/// Enum type containing constants for selecting the supply type, |
| 105 | 105 |
/// i.e. the direction of the inequalities in the supply/demand |
| 106 | 106 |
/// constraints of the \ref min_cost_flow "minimum cost flow problem". |
| 107 | 107 |
/// |
| 108 | 108 |
/// The default supply type is \c GEQ, the \c LEQ type can be |
| 109 | 109 |
/// selected using \ref supplyType(). |
| 110 | 110 |
/// The equality form is a special case of both supply types. |
| 111 | 111 |
enum SupplyType {
|
| 112 | 112 |
/// This option means that there are <em>"greater or equal"</em> |
| 113 | 113 |
/// supply/demand constraints in the definition of the problem. |
| 114 | 114 |
GEQ, |
| 115 | 115 |
/// This option means that there are <em>"less or equal"</em> |
| 116 | 116 |
/// supply/demand constraints in the definition of the problem. |
| 117 | 117 |
LEQ |
| 118 | 118 |
}; |
| 119 | 119 |
|
| 120 | 120 |
/// \brief Constants for selecting the pivot rule. |
| 121 | 121 |
/// |
| 122 | 122 |
/// Enum type containing constants for selecting the pivot rule for |
| 123 | 123 |
/// the \ref run() function. |
| 124 | 124 |
/// |
| 125 | 125 |
/// \ref NetworkSimplex provides five different pivot rule |
| 126 | 126 |
/// implementations that significantly affect the running time |
| 127 | 127 |
/// of the algorithm. |
| 128 | 128 |
/// By default, \ref BLOCK_SEARCH "Block Search" is used, which |
| 129 |
/// |
|
| 129 |
/// turend out to be the most efficient and the most robust on various |
|
| 130 | 130 |
/// test inputs. |
| 131 | 131 |
/// However, another pivot rule can be selected using the \ref run() |
| 132 | 132 |
/// function with the proper parameter. |
| 133 | 133 |
enum PivotRule {
|
| 134 | 134 |
|
| 135 | 135 |
/// The \e First \e Eligible pivot rule. |
| 136 | 136 |
/// The next eligible arc is selected in a wraparound fashion |
| 137 | 137 |
/// in every iteration. |
| 138 | 138 |
FIRST_ELIGIBLE, |
| 139 | 139 |
|
| 140 | 140 |
/// The \e Best \e Eligible pivot rule. |
| 141 | 141 |
/// The best eligible arc is selected in every iteration. |
| 142 | 142 |
BEST_ELIGIBLE, |
| 143 | 143 |
|
| 144 | 144 |
/// The \e Block \e Search pivot rule. |
| 145 | 145 |
/// A specified number of arcs are examined in every iteration |
| 146 | 146 |
/// in a wraparound fashion and the best eligible arc is selected |
| 147 | 147 |
/// from this block. |
| 148 | 148 |
BLOCK_SEARCH, |
| 149 | 149 |
|
| 150 | 150 |
/// The \e Candidate \e List pivot rule. |
| 151 | 151 |
/// In a major iteration a candidate list is built from eligible arcs |
| 152 | 152 |
/// in a wraparound fashion and in the following minor iterations |
| 153 | 153 |
/// the best eligible arc is selected from this list. |
| 154 | 154 |
CANDIDATE_LIST, |
| 155 | 155 |
|
| 156 | 156 |
/// The \e Altering \e Candidate \e List pivot rule. |
| 157 | 157 |
/// It is a modified version of the Candidate List method. |
| 158 | 158 |
/// It keeps only the several best eligible arcs from the former |
| 159 | 159 |
/// candidate list and extends this list in every iteration. |
| 160 | 160 |
ALTERING_LIST |
| 161 | 161 |
}; |
| 162 | 162 |
|
| 163 | 163 |
private: |
| 164 | 164 |
|
| 165 | 165 |
TEMPLATE_DIGRAPH_TYPEDEFS(GR); |
| 166 | 166 |
|
| 167 | 167 |
typedef std::vector<int> IntVector; |
| 168 | 168 |
typedef std::vector<Value> ValueVector; |
| 169 | 169 |
typedef std::vector<Cost> CostVector; |
| 170 | 170 |
typedef std::vector<signed char> CharVector; |
| 171 | 171 |
// Note: vector<signed char> is used instead of vector<ArcState> and |
| 172 | 172 |
// vector<ArcDirection> for efficiency reasons |
| 173 | 173 |
|
| 174 | 174 |
// State constants for arcs |
| 175 | 175 |
enum ArcState {
|
| 176 | 176 |
STATE_UPPER = -1, |
| 177 | 177 |
STATE_TREE = 0, |
| ... | ... |
@@ -690,113 +690,115 @@ |
| 690 | 690 |
/// This function sets the upper bounds (capacities) on the arcs. |
| 691 | 691 |
/// If it is not used before calling \ref run(), the upper bounds |
| 692 | 692 |
/// will be set to \ref INF on all arcs (i.e. the flow value will be |
| 693 | 693 |
/// unbounded from above). |
| 694 | 694 |
/// |
| 695 | 695 |
/// \param map An arc map storing the upper bounds. |
| 696 | 696 |
/// Its \c Value type must be convertible to the \c Value type |
| 697 | 697 |
/// of the algorithm. |
| 698 | 698 |
/// |
| 699 | 699 |
/// \return <tt>(*this)</tt> |
| 700 | 700 |
template<typename UpperMap> |
| 701 | 701 |
NetworkSimplex& upperMap(const UpperMap& map) {
|
| 702 | 702 |
for (ArcIt a(_graph); a != INVALID; ++a) {
|
| 703 | 703 |
_upper[_arc_id[a]] = map[a]; |
| 704 | 704 |
} |
| 705 | 705 |
return *this; |
| 706 | 706 |
} |
| 707 | 707 |
|
| 708 | 708 |
/// \brief Set the costs of the arcs. |
| 709 | 709 |
/// |
| 710 | 710 |
/// This function sets the costs of the arcs. |
| 711 | 711 |
/// If it is not used before calling \ref run(), the costs |
| 712 | 712 |
/// will be set to \c 1 on all arcs. |
| 713 | 713 |
/// |
| 714 | 714 |
/// \param map An arc map storing the costs. |
| 715 | 715 |
/// Its \c Value type must be convertible to the \c Cost type |
| 716 | 716 |
/// of the algorithm. |
| 717 | 717 |
/// |
| 718 | 718 |
/// \return <tt>(*this)</tt> |
| 719 | 719 |
template<typename CostMap> |
| 720 | 720 |
NetworkSimplex& costMap(const CostMap& map) {
|
| 721 | 721 |
for (ArcIt a(_graph); a != INVALID; ++a) {
|
| 722 | 722 |
_cost[_arc_id[a]] = map[a]; |
| 723 | 723 |
} |
| 724 | 724 |
return *this; |
| 725 | 725 |
} |
| 726 | 726 |
|
| 727 | 727 |
/// \brief Set the supply values of the nodes. |
| 728 | 728 |
/// |
| 729 | 729 |
/// This function sets the supply values of the nodes. |
| 730 | 730 |
/// If neither this function nor \ref stSupply() is used before |
| 731 | 731 |
/// calling \ref run(), the supply of each node will be set to zero. |
| 732 | 732 |
/// |
| 733 | 733 |
/// \param map A node map storing the supply values. |
| 734 | 734 |
/// Its \c Value type must be convertible to the \c Value type |
| 735 | 735 |
/// of the algorithm. |
| 736 | 736 |
/// |
| 737 | 737 |
/// \return <tt>(*this)</tt> |
| 738 |
/// |
|
| 739 |
/// \sa supplyType() |
|
| 738 | 740 |
template<typename SupplyMap> |
| 739 | 741 |
NetworkSimplex& supplyMap(const SupplyMap& map) {
|
| 740 | 742 |
for (NodeIt n(_graph); n != INVALID; ++n) {
|
| 741 | 743 |
_supply[_node_id[n]] = map[n]; |
| 742 | 744 |
} |
| 743 | 745 |
return *this; |
| 744 | 746 |
} |
| 745 | 747 |
|
| 746 | 748 |
/// \brief Set single source and target nodes and a supply value. |
| 747 | 749 |
/// |
| 748 | 750 |
/// This function sets a single source node and a single target node |
| 749 | 751 |
/// and the required flow value. |
| 750 | 752 |
/// If neither this function nor \ref supplyMap() is used before |
| 751 | 753 |
/// calling \ref run(), the supply of each node will be set to zero. |
| 752 | 754 |
/// |
| 753 | 755 |
/// Using this function has the same effect as using \ref supplyMap() |
| 754 |
/// with |
|
| 756 |
/// with a map in which \c k is assigned to \c s, \c -k is |
|
| 755 | 757 |
/// assigned to \c t and all other nodes have zero supply value. |
| 756 | 758 |
/// |
| 757 | 759 |
/// \param s The source node. |
| 758 | 760 |
/// \param t The target node. |
| 759 | 761 |
/// \param k The required amount of flow from node \c s to node \c t |
| 760 | 762 |
/// (i.e. the supply of \c s and the demand of \c t). |
| 761 | 763 |
/// |
| 762 | 764 |
/// \return <tt>(*this)</tt> |
| 763 | 765 |
NetworkSimplex& stSupply(const Node& s, const Node& t, Value k) {
|
| 764 | 766 |
for (int i = 0; i != _node_num; ++i) {
|
| 765 | 767 |
_supply[i] = 0; |
| 766 | 768 |
} |
| 767 | 769 |
_supply[_node_id[s]] = k; |
| 768 | 770 |
_supply[_node_id[t]] = -k; |
| 769 | 771 |
return *this; |
| 770 | 772 |
} |
| 771 | 773 |
|
| 772 | 774 |
/// \brief Set the type of the supply constraints. |
| 773 | 775 |
/// |
| 774 | 776 |
/// This function sets the type of the supply/demand constraints. |
| 775 | 777 |
/// If it is not used before calling \ref run(), the \ref GEQ supply |
| 776 | 778 |
/// type will be used. |
| 777 | 779 |
/// |
| 778 | 780 |
/// For more information, see \ref SupplyType. |
| 779 | 781 |
/// |
| 780 | 782 |
/// \return <tt>(*this)</tt> |
| 781 | 783 |
NetworkSimplex& supplyType(SupplyType supply_type) {
|
| 782 | 784 |
_stype = supply_type; |
| 783 | 785 |
return *this; |
| 784 | 786 |
} |
| 785 | 787 |
|
| 786 | 788 |
/// @} |
| 787 | 789 |
|
| 788 | 790 |
/// \name Execution Control |
| 789 | 791 |
/// The algorithm can be executed using \ref run(). |
| 790 | 792 |
|
| 791 | 793 |
/// @{
|
| 792 | 794 |
|
| 793 | 795 |
/// \brief Run the algorithm. |
| 794 | 796 |
/// |
| 795 | 797 |
/// This function runs the algorithm. |
| 796 | 798 |
/// The paramters can be specified using functions \ref lowerMap(), |
| 797 | 799 |
/// \ref upperMap(), \ref costMap(), \ref supplyMap(), \ref stSupply(), |
| 798 | 800 |
/// \ref supplyType(). |
| 799 | 801 |
/// For example, |
| 800 | 802 |
/// \code |
| 801 | 803 |
/// NetworkSimplex<ListDigraph> ns(graph); |
| 802 | 804 |
/// ns.lowerMap(lower).upperMap(upper).costMap(cost) |
| 1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
| 2 | 2 |
* |
| 3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
| 4 | 4 |
* |
| 5 | 5 |
* Copyright (C) 2003-2010 |
| 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 |
///\ingroup paths |
| 20 | 20 |
///\file |
| 21 | 21 |
///\brief Classes for representing paths in digraphs. |
| 22 | 22 |
/// |
| 23 | 23 |
|
| 24 | 24 |
#ifndef LEMON_PATH_H |
| 25 | 25 |
#define LEMON_PATH_H |
| 26 | 26 |
|
| 27 | 27 |
#include <vector> |
| 28 | 28 |
#include <algorithm> |
| 29 | 29 |
|
| 30 | 30 |
#include <lemon/error.h> |
| 31 | 31 |
#include <lemon/core.h> |
| 32 | 32 |
#include <lemon/concepts/path.h> |
| 33 | 33 |
|
| 34 | 34 |
namespace lemon {
|
| 35 | 35 |
|
| 36 | 36 |
/// \addtogroup paths |
| 37 | 37 |
/// @{
|
| 38 | 38 |
|
| 39 | 39 |
|
| 40 | 40 |
/// \brief A structure for representing directed paths in a digraph. |
| 41 | 41 |
/// |
| 42 | 42 |
/// A structure for representing directed path in a digraph. |
| 43 | 43 |
/// \tparam GR The digraph type in which the path is. |
| 44 | 44 |
/// |
| 45 | 45 |
/// In a sense, the path can be treated as a list of arcs. The |
| 46 |
/// |
|
| 46 |
/// LEMON path type stores just this list. As a consequence, it |
|
| 47 | 47 |
/// cannot enumerate the nodes of the path and the source node of |
| 48 | 48 |
/// a zero length path is undefined. |
| 49 | 49 |
/// |
| 50 | 50 |
/// This implementation is a back and front insertable and erasable |
| 51 | 51 |
/// path type. It can be indexed in O(1) time. The front and back |
| 52 | 52 |
/// insertion and erase is done in O(1) (amortized) time. The |
| 53 | 53 |
/// implementation uses two vectors for storing the front and back |
| 54 | 54 |
/// insertions. |
| 55 | 55 |
template <typename GR> |
| 56 | 56 |
class Path {
|
| 57 | 57 |
public: |
| 58 | 58 |
|
| 59 | 59 |
typedef GR Digraph; |
| 60 | 60 |
typedef typename Digraph::Arc Arc; |
| 61 | 61 |
|
| 62 | 62 |
/// \brief Default constructor |
| 63 | 63 |
/// |
| 64 | 64 |
/// Default constructor |
| 65 | 65 |
Path() {}
|
| 66 | 66 |
|
| 67 | 67 |
/// \brief Template copy constructor |
| 68 | 68 |
/// |
| 69 | 69 |
/// This constuctor initializes the path from any other path type. |
| 70 | 70 |
/// It simply makes a copy of the given path. |
| 71 | 71 |
template <typename CPath> |
| 72 | 72 |
Path(const CPath& cpath) {
|
| 73 | 73 |
pathCopy(cpath, *this); |
| 74 | 74 |
} |
| 75 | 75 |
|
| 76 | 76 |
/// \brief Template copy assignment |
| 77 | 77 |
/// |
| 78 | 78 |
/// This operator makes a copy of a path of any other type. |
| 79 | 79 |
template <typename CPath> |
| 80 | 80 |
Path& operator=(const CPath& cpath) {
|
| 81 | 81 |
pathCopy(cpath, *this); |
| 82 | 82 |
return *this; |
| 83 | 83 |
} |
| 84 | 84 |
|
| 85 | 85 |
/// \brief LEMON style iterator for path arcs |
| 86 | 86 |
/// |
| 87 | 87 |
/// This class is used to iterate on the arcs of the paths. |
| 88 | 88 |
class ArcIt {
|
| 89 | 89 |
friend class Path; |
| 90 | 90 |
public: |
| 91 | 91 |
/// \brief Default constructor |
| 92 | 92 |
ArcIt() {}
|
| 93 | 93 |
/// \brief Invalid constructor |
| 94 | 94 |
ArcIt(Invalid) : path(0), idx(-1) {}
|
| 95 | 95 |
/// \brief Initializate the iterator to the first arc of path |
| 96 | 96 |
ArcIt(const Path &_path) |
| 97 | 97 |
: path(&_path), idx(_path.empty() ? -1 : 0) {}
|
| 98 | 98 |
|
| 99 | 99 |
private: |
| 100 | 100 |
|
| 101 | 101 |
ArcIt(const Path &_path, int _idx) |
| 102 | 102 |
: path(&_path), idx(_idx) {}
|
| 103 | 103 |
|
| 104 | 104 |
public: |
| 105 | 105 |
|
| 106 | 106 |
/// \brief Conversion to Arc |
| 107 | 107 |
operator const Arc&() const {
|
| 108 | 108 |
return path->nth(idx); |
| 109 | 109 |
} |
| 110 | 110 |
|
| 111 | 111 |
/// \brief Next arc |
| 112 | 112 |
ArcIt& operator++() {
|
| 113 | 113 |
++idx; |
| 114 | 114 |
if (idx >= path->length()) idx = -1; |
| 115 | 115 |
return *this; |
| 116 | 116 |
} |
| 117 | 117 |
|
| 118 | 118 |
/// \brief Comparison operator |
| 119 | 119 |
bool operator==(const ArcIt& e) const { return idx==e.idx; }
|
| 120 | 120 |
/// \brief Comparison operator |
| 121 | 121 |
bool operator!=(const ArcIt& e) const { return idx!=e.idx; }
|
| 122 | 122 |
/// \brief Comparison operator |
| 123 | 123 |
bool operator<(const ArcIt& e) const { return idx<e.idx; }
|
| 124 | 124 |
|
| 125 | 125 |
private: |
| 126 | 126 |
const Path *path; |
| 127 | 127 |
int idx; |
| 128 | 128 |
}; |
| 129 | 129 |
|
| 130 | 130 |
/// \brief Length of the path. |
| 131 | 131 |
int length() const { return head.size() + tail.size(); }
|
| 132 | 132 |
/// \brief Return whether the path is empty. |
| 133 | 133 |
bool empty() const { return head.empty() && tail.empty(); }
|
| 134 | 134 |
|
| 135 | 135 |
/// \brief Reset the path to an empty one. |
| 136 | 136 |
void clear() { head.clear(); tail.clear(); }
|
| 137 | 137 |
|
| 138 |
/// \brief The |
|
| 138 |
/// \brief The n-th arc. |
|
| 139 | 139 |
/// |
| 140 | 140 |
/// \pre \c n is in the <tt>[0..length() - 1]</tt> range. |
| 141 | 141 |
const Arc& nth(int n) const {
|
| 142 | 142 |
return n < int(head.size()) ? *(head.rbegin() + n) : |
| 143 | 143 |
*(tail.begin() + (n - head.size())); |
| 144 | 144 |
} |
| 145 | 145 |
|
| 146 |
/// \brief Initialize arc iterator to point to the |
|
| 146 |
/// \brief Initialize arc iterator to point to the n-th arc |
|
| 147 | 147 |
/// |
| 148 | 148 |
/// \pre \c n is in the <tt>[0..length() - 1]</tt> range. |
| 149 | 149 |
ArcIt nthIt(int n) const {
|
| 150 | 150 |
return ArcIt(*this, n); |
| 151 | 151 |
} |
| 152 | 152 |
|
| 153 | 153 |
/// \brief The first arc of the path |
| 154 | 154 |
const Arc& front() const {
|
| 155 | 155 |
return head.empty() ? tail.front() : head.back(); |
| 156 | 156 |
} |
| 157 | 157 |
|
| 158 | 158 |
/// \brief Add a new arc before the current path |
| 159 | 159 |
void addFront(const Arc& arc) {
|
| 160 | 160 |
head.push_back(arc); |
| 161 | 161 |
} |
| 162 | 162 |
|
| 163 | 163 |
/// \brief Erase the first arc of the path |
| 164 | 164 |
void eraseFront() {
|
| 165 | 165 |
if (!head.empty()) {
|
| 166 | 166 |
head.pop_back(); |
| 167 | 167 |
} else {
|
| 168 | 168 |
head.clear(); |
| 169 | 169 |
int halfsize = tail.size() / 2; |
| 170 | 170 |
head.resize(halfsize); |
| 171 | 171 |
std::copy(tail.begin() + 1, tail.begin() + halfsize + 1, |
| 172 | 172 |
head.rbegin()); |
| 173 | 173 |
std::copy(tail.begin() + halfsize + 1, tail.end(), tail.begin()); |
| 174 | 174 |
tail.resize(tail.size() - halfsize - 1); |
| 175 | 175 |
} |
| 176 | 176 |
} |
| 177 | 177 |
|
| 178 | 178 |
/// \brief The last arc of the path |
| 179 | 179 |
const Arc& back() const {
|
| 180 | 180 |
return tail.empty() ? head.front() : tail.back(); |
| 181 | 181 |
} |
| 182 | 182 |
|
| 183 | 183 |
/// \brief Add a new arc behind the current path |
| 184 | 184 |
void addBack(const Arc& arc) {
|
| 185 | 185 |
tail.push_back(arc); |
| 186 | 186 |
} |
| 187 | 187 |
|
| 188 | 188 |
/// \brief Erase the last arc of the path |
| 189 | 189 |
void eraseBack() {
|
| 190 | 190 |
if (!tail.empty()) {
|
| 191 | 191 |
tail.pop_back(); |
| 192 | 192 |
} else {
|
| 193 | 193 |
int halfsize = head.size() / 2; |
| 194 | 194 |
tail.resize(halfsize); |
| 195 | 195 |
std::copy(head.begin() + 1, head.begin() + halfsize + 1, |
| 196 | 196 |
tail.rbegin()); |
| 197 | 197 |
std::copy(head.begin() + halfsize + 1, head.end(), head.begin()); |
| 198 | 198 |
head.resize(head.size() - halfsize - 1); |
| 199 | 199 |
} |
| 200 | 200 |
} |
| 201 | 201 |
|
| 202 | 202 |
typedef True BuildTag; |
| 203 | 203 |
|
| 204 | 204 |
template <typename CPath> |
| 205 | 205 |
void build(const CPath& path) {
|
| 206 | 206 |
int len = path.length(); |
| 207 | 207 |
tail.reserve(len); |
| 208 | 208 |
for (typename CPath::ArcIt it(path); it != INVALID; ++it) {
|
| 209 | 209 |
tail.push_back(it); |
| 210 | 210 |
} |
| 211 | 211 |
} |
| 212 | 212 |
|
| 213 | 213 |
template <typename CPath> |
| 214 | 214 |
void buildRev(const CPath& path) {
|
| 215 | 215 |
int len = path.length(); |
| 216 | 216 |
head.reserve(len); |
| 217 | 217 |
for (typename CPath::RevArcIt it(path); it != INVALID; ++it) {
|
| 218 | 218 |
head.push_back(it); |
| 219 | 219 |
} |
| 220 | 220 |
} |
| 221 | 221 |
|
| 222 | 222 |
protected: |
| 223 | 223 |
typedef std::vector<Arc> Container; |
| 224 | 224 |
Container head, tail; |
| 225 | 225 |
|
| 226 | 226 |
}; |
| 227 | 227 |
|
| 228 | 228 |
/// \brief A structure for representing directed paths in a digraph. |
| 229 | 229 |
/// |
| 230 | 230 |
/// A structure for representing directed path in a digraph. |
| 231 | 231 |
/// \tparam GR The digraph type in which the path is. |
| 232 | 232 |
/// |
| 233 | 233 |
/// In a sense, the path can be treated as a list of arcs. The |
| 234 |
/// |
|
| 234 |
/// LEMON path type stores just this list. As a consequence it |
|
| 235 | 235 |
/// cannot enumerate the nodes in the path and the zero length paths |
| 236 | 236 |
/// cannot store the source. |
| 237 | 237 |
/// |
| 238 | 238 |
/// This implementation is a just back insertable and erasable path |
| 239 | 239 |
/// type. It can be indexed in O(1) time. The back insertion and |
| 240 | 240 |
/// erasure is amortized O(1) time. This implementation is faster |
| 241 | 241 |
/// then the \c Path type because it use just one vector for the |
| 242 | 242 |
/// arcs. |
| 243 | 243 |
template <typename GR> |
| 244 | 244 |
class SimplePath {
|
| 245 | 245 |
public: |
| 246 | 246 |
|
| 247 | 247 |
typedef GR Digraph; |
| 248 | 248 |
typedef typename Digraph::Arc Arc; |
| 249 | 249 |
|
| 250 | 250 |
/// \brief Default constructor |
| 251 | 251 |
/// |
| 252 | 252 |
/// Default constructor |
| 253 | 253 |
SimplePath() {}
|
| 254 | 254 |
|
| 255 | 255 |
/// \brief Template copy constructor |
| 256 | 256 |
/// |
| 257 | 257 |
/// This path can be initialized with any other path type. It just |
| 258 | 258 |
/// makes a copy of the given path. |
| 259 | 259 |
template <typename CPath> |
| 260 | 260 |
SimplePath(const CPath& cpath) {
|
| 261 | 261 |
pathCopy(cpath, *this); |
| 262 | 262 |
} |
| 263 | 263 |
|
| 264 | 264 |
/// \brief Template copy assignment |
| 265 | 265 |
/// |
| 266 | 266 |
/// This path can be initialized with any other path type. It just |
| 267 | 267 |
/// makes a copy of the given path. |
| 268 | 268 |
template <typename CPath> |
| 269 | 269 |
SimplePath& operator=(const CPath& cpath) {
|
| 270 | 270 |
pathCopy(cpath, *this); |
| 271 | 271 |
return *this; |
| 272 | 272 |
} |
| 273 | 273 |
|
| 274 | 274 |
/// \brief Iterator class to iterate on the arcs of the paths |
| 275 | 275 |
/// |
| 276 | 276 |
/// This class is used to iterate on the arcs of the paths |
| 277 | 277 |
/// |
| 278 | 278 |
/// Of course it converts to Digraph::Arc |
| 279 | 279 |
class ArcIt {
|
| 280 | 280 |
friend class SimplePath; |
| 281 | 281 |
public: |
| 282 | 282 |
/// Default constructor |
| 283 | 283 |
ArcIt() {}
|
| 284 | 284 |
/// Invalid constructor |
| 285 | 285 |
ArcIt(Invalid) : path(0), idx(-1) {}
|
| 286 | 286 |
/// \brief Initializate the constructor to the first arc of path |
| 287 | 287 |
ArcIt(const SimplePath &_path) |
| 288 | 288 |
: path(&_path), idx(_path.empty() ? -1 : 0) {}
|
| 289 | 289 |
|
| 290 | 290 |
private: |
| 291 | 291 |
|
| 292 | 292 |
/// Constructor with starting point |
| 293 | 293 |
ArcIt(const SimplePath &_path, int _idx) |
| 294 | 294 |
: idx(_idx), path(&_path) {}
|
| 295 | 295 |
|
| 296 | 296 |
public: |
| 297 | 297 |
|
| 298 | 298 |
///Conversion to Digraph::Arc |
| 299 | 299 |
operator const Arc&() const {
|
| 300 | 300 |
return path->nth(idx); |
| 301 | 301 |
} |
| 302 | 302 |
|
| 303 | 303 |
/// Next arc |
| 304 | 304 |
ArcIt& operator++() {
|
| 305 | 305 |
++idx; |
| 306 | 306 |
if (idx >= path->length()) idx = -1; |
| 307 | 307 |
return *this; |
| 308 | 308 |
} |
| 309 | 309 |
|
| 310 | 310 |
/// Comparison operator |
| 311 | 311 |
bool operator==(const ArcIt& e) const { return idx==e.idx; }
|
| 312 | 312 |
/// Comparison operator |
| 313 | 313 |
bool operator!=(const ArcIt& e) const { return idx!=e.idx; }
|
| 314 | 314 |
/// Comparison operator |
| 315 | 315 |
bool operator<(const ArcIt& e) const { return idx<e.idx; }
|
| 316 | 316 |
|
| 317 | 317 |
private: |
| 318 | 318 |
const SimplePath *path; |
| 319 | 319 |
int idx; |
| 320 | 320 |
}; |
| 321 | 321 |
|
| 322 | 322 |
/// \brief Length of the path. |
| 323 | 323 |
int length() const { return data.size(); }
|
| 324 | 324 |
/// \brief Return true if the path is empty. |
| 325 | 325 |
bool empty() const { return data.empty(); }
|
| 326 | 326 |
|
| 327 | 327 |
/// \brief Reset the path to an empty one. |
| 328 | 328 |
void clear() { data.clear(); }
|
| 329 | 329 |
|
| 330 |
/// \brief The |
|
| 330 |
/// \brief The n-th arc. |
|
| 331 | 331 |
/// |
| 332 | 332 |
/// \pre \c n is in the <tt>[0..length() - 1]</tt> range. |
| 333 | 333 |
const Arc& nth(int n) const {
|
| 334 | 334 |
return data[n]; |
| 335 | 335 |
} |
| 336 | 336 |
|
| 337 |
/// \brief Initializes arc iterator to point to the |
|
| 337 |
/// \brief Initializes arc iterator to point to the n-th arc. |
|
| 338 | 338 |
ArcIt nthIt(int n) const {
|
| 339 | 339 |
return ArcIt(*this, n); |
| 340 | 340 |
} |
| 341 | 341 |
|
| 342 | 342 |
/// \brief The first arc of the path. |
| 343 | 343 |
const Arc& front() const {
|
| 344 | 344 |
return data.front(); |
| 345 | 345 |
} |
| 346 | 346 |
|
| 347 | 347 |
/// \brief The last arc of the path. |
| 348 | 348 |
const Arc& back() const {
|
| 349 | 349 |
return data.back(); |
| 350 | 350 |
} |
| 351 | 351 |
|
| 352 | 352 |
/// \brief Add a new arc behind the current path. |
| 353 | 353 |
void addBack(const Arc& arc) {
|
| 354 | 354 |
data.push_back(arc); |
| 355 | 355 |
} |
| 356 | 356 |
|
| 357 | 357 |
/// \brief Erase the last arc of the path |
| 358 | 358 |
void eraseBack() {
|
| 359 | 359 |
data.pop_back(); |
| 360 | 360 |
} |
| 361 | 361 |
|
| 362 | 362 |
typedef True BuildTag; |
| 363 | 363 |
|
| 364 | 364 |
template <typename CPath> |
| 365 | 365 |
void build(const CPath& path) {
|
| 366 | 366 |
int len = path.length(); |
| 367 | 367 |
data.resize(len); |
| 368 | 368 |
int index = 0; |
| 369 | 369 |
for (typename CPath::ArcIt it(path); it != INVALID; ++it) {
|
| 370 | 370 |
data[index] = it;; |
| 371 | 371 |
++index; |
| 372 | 372 |
} |
| 373 | 373 |
} |
| 374 | 374 |
|
| 375 | 375 |
template <typename CPath> |
| 376 | 376 |
void buildRev(const CPath& path) {
|
| 377 | 377 |
int len = path.length(); |
| 378 | 378 |
data.resize(len); |
| 379 | 379 |
int index = len; |
| 380 | 380 |
for (typename CPath::RevArcIt it(path); it != INVALID; ++it) {
|
| 381 | 381 |
--index; |
| 382 | 382 |
data[index] = it;; |
| 383 | 383 |
} |
| 384 | 384 |
} |
| 385 | 385 |
|
| 386 | 386 |
protected: |
| 387 | 387 |
typedef std::vector<Arc> Container; |
| 388 | 388 |
Container data; |
| 389 | 389 |
|
| 390 | 390 |
}; |
| 391 | 391 |
|
| 392 | 392 |
/// \brief A structure for representing directed paths in a digraph. |
| 393 | 393 |
/// |
| 394 | 394 |
/// A structure for representing directed path in a digraph. |
| 395 | 395 |
/// \tparam GR The digraph type in which the path is. |
| 396 | 396 |
/// |
| 397 | 397 |
/// In a sense, the path can be treated as a list of arcs. The |
| 398 |
/// |
|
| 398 |
/// LEMON path type stores just this list. As a consequence it |
|
| 399 | 399 |
/// cannot enumerate the nodes in the path and the zero length paths |
| 400 | 400 |
/// cannot store the source. |
| 401 | 401 |
/// |
| 402 | 402 |
/// This implementation is a back and front insertable and erasable |
| 403 | 403 |
/// path type. It can be indexed in O(k) time, where k is the rank |
| 404 | 404 |
/// of the arc in the path. The length can be computed in O(n) |
| 405 | 405 |
/// time. The front and back insertion and erasure is O(1) time |
| 406 | 406 |
/// and it can be splited and spliced in O(1) time. |
| 407 | 407 |
template <typename GR> |
| 408 | 408 |
class ListPath {
|
| 409 | 409 |
public: |
| 410 | 410 |
|
| 411 | 411 |
typedef GR Digraph; |
| 412 | 412 |
typedef typename Digraph::Arc Arc; |
| 413 | 413 |
|
| 414 | 414 |
protected: |
| 415 | 415 |
|
| 416 | 416 |
// the std::list<> is incompatible |
| 417 | 417 |
// hard to create invalid iterator |
| 418 | 418 |
struct Node {
|
| 419 | 419 |
Arc arc; |
| 420 | 420 |
Node *next, *prev; |
| 421 | 421 |
}; |
| 422 | 422 |
|
| 423 | 423 |
Node *first, *last; |
| 424 | 424 |
|
| 425 | 425 |
std::allocator<Node> alloc; |
| 426 | 426 |
|
| 427 | 427 |
public: |
| 428 | 428 |
|
| 429 | 429 |
/// \brief Default constructor |
| 430 | 430 |
/// |
| 431 | 431 |
/// Default constructor |
| 432 | 432 |
ListPath() : first(0), last(0) {}
|
| 433 | 433 |
|
| 434 | 434 |
/// \brief Template copy constructor |
| 435 | 435 |
/// |
| 436 | 436 |
/// This path can be initialized with any other path type. It just |
| 437 | 437 |
/// makes a copy of the given path. |
| 438 | 438 |
template <typename CPath> |
| 439 | 439 |
ListPath(const CPath& cpath) : first(0), last(0) {
|
| 440 | 440 |
pathCopy(cpath, *this); |
| 441 | 441 |
} |
| 442 | 442 |
|
| 443 | 443 |
/// \brief Destructor of the path |
| 444 | 444 |
/// |
| 445 | 445 |
/// Destructor of the path |
| 446 | 446 |
~ListPath() {
|
| ... | ... |
@@ -459,109 +459,109 @@ |
| 459 | 459 |
|
| 460 | 460 |
/// \brief Iterator class to iterate on the arcs of the paths |
| 461 | 461 |
/// |
| 462 | 462 |
/// This class is used to iterate on the arcs of the paths |
| 463 | 463 |
/// |
| 464 | 464 |
/// Of course it converts to Digraph::Arc |
| 465 | 465 |
class ArcIt {
|
| 466 | 466 |
friend class ListPath; |
| 467 | 467 |
public: |
| 468 | 468 |
/// Default constructor |
| 469 | 469 |
ArcIt() {}
|
| 470 | 470 |
/// Invalid constructor |
| 471 | 471 |
ArcIt(Invalid) : path(0), node(0) {}
|
| 472 | 472 |
/// \brief Initializate the constructor to the first arc of path |
| 473 | 473 |
ArcIt(const ListPath &_path) |
| 474 | 474 |
: path(&_path), node(_path.first) {}
|
| 475 | 475 |
|
| 476 | 476 |
protected: |
| 477 | 477 |
|
| 478 | 478 |
ArcIt(const ListPath &_path, Node *_node) |
| 479 | 479 |
: path(&_path), node(_node) {}
|
| 480 | 480 |
|
| 481 | 481 |
|
| 482 | 482 |
public: |
| 483 | 483 |
|
| 484 | 484 |
///Conversion to Digraph::Arc |
| 485 | 485 |
operator const Arc&() const {
|
| 486 | 486 |
return node->arc; |
| 487 | 487 |
} |
| 488 | 488 |
|
| 489 | 489 |
/// Next arc |
| 490 | 490 |
ArcIt& operator++() {
|
| 491 | 491 |
node = node->next; |
| 492 | 492 |
return *this; |
| 493 | 493 |
} |
| 494 | 494 |
|
| 495 | 495 |
/// Comparison operator |
| 496 | 496 |
bool operator==(const ArcIt& e) const { return node==e.node; }
|
| 497 | 497 |
/// Comparison operator |
| 498 | 498 |
bool operator!=(const ArcIt& e) const { return node!=e.node; }
|
| 499 | 499 |
/// Comparison operator |
| 500 | 500 |
bool operator<(const ArcIt& e) const { return node<e.node; }
|
| 501 | 501 |
|
| 502 | 502 |
private: |
| 503 | 503 |
const ListPath *path; |
| 504 | 504 |
Node *node; |
| 505 | 505 |
}; |
| 506 | 506 |
|
| 507 |
/// \brief The |
|
| 507 |
/// \brief The n-th arc. |
|
| 508 | 508 |
/// |
| 509 |
/// This function looks for the |
|
| 509 |
/// This function looks for the n-th arc in O(n) time. |
|
| 510 | 510 |
/// \pre \c n is in the <tt>[0..length() - 1]</tt> range. |
| 511 | 511 |
const Arc& nth(int n) const {
|
| 512 | 512 |
Node *node = first; |
| 513 | 513 |
for (int i = 0; i < n; ++i) {
|
| 514 | 514 |
node = node->next; |
| 515 | 515 |
} |
| 516 | 516 |
return node->arc; |
| 517 | 517 |
} |
| 518 | 518 |
|
| 519 |
/// \brief Initializes arc iterator to point to the |
|
| 519 |
/// \brief Initializes arc iterator to point to the n-th arc. |
|
| 520 | 520 |
ArcIt nthIt(int n) const {
|
| 521 | 521 |
Node *node = first; |
| 522 | 522 |
for (int i = 0; i < n; ++i) {
|
| 523 | 523 |
node = node->next; |
| 524 | 524 |
} |
| 525 | 525 |
return ArcIt(*this, node); |
| 526 | 526 |
} |
| 527 | 527 |
|
| 528 | 528 |
/// \brief Length of the path. |
| 529 | 529 |
int length() const {
|
| 530 | 530 |
int len = 0; |
| 531 | 531 |
Node *node = first; |
| 532 | 532 |
while (node != 0) {
|
| 533 | 533 |
node = node->next; |
| 534 | 534 |
++len; |
| 535 | 535 |
} |
| 536 | 536 |
return len; |
| 537 | 537 |
} |
| 538 | 538 |
|
| 539 | 539 |
/// \brief Return true if the path is empty. |
| 540 | 540 |
bool empty() const { return first == 0; }
|
| 541 | 541 |
|
| 542 | 542 |
/// \brief Reset the path to an empty one. |
| 543 | 543 |
void clear() {
|
| 544 | 544 |
while (first != 0) {
|
| 545 | 545 |
last = first->next; |
| 546 | 546 |
alloc.destroy(first); |
| 547 | 547 |
alloc.deallocate(first, 1); |
| 548 | 548 |
first = last; |
| 549 | 549 |
} |
| 550 | 550 |
} |
| 551 | 551 |
|
| 552 | 552 |
/// \brief The first arc of the path |
| 553 | 553 |
const Arc& front() const {
|
| 554 | 554 |
return first->arc; |
| 555 | 555 |
} |
| 556 | 556 |
|
| 557 | 557 |
/// \brief Add a new arc before the current path |
| 558 | 558 |
void addFront(const Arc& arc) {
|
| 559 | 559 |
Node *node = alloc.allocate(1); |
| 560 | 560 |
alloc.construct(node, Node()); |
| 561 | 561 |
node->prev = 0; |
| 562 | 562 |
node->next = first; |
| 563 | 563 |
node->arc = arc; |
| 564 | 564 |
if (first) {
|
| 565 | 565 |
first->prev = node; |
| 566 | 566 |
first = node; |
| 567 | 567 |
} else {
|
| ... | ... |
@@ -690,200 +690,200 @@ |
| 690 | 690 |
/// |
| 691 | 691 |
/// It splits the current path into two parts. The part before |
| 692 | 692 |
/// the iterator \c it will remain in the current path and the part |
| 693 | 693 |
/// starting with |
| 694 | 694 |
/// \c it will put into \c tpath. If \c tpath have arcs |
| 695 | 695 |
/// before the operation they are removed first. The time |
| 696 | 696 |
/// complexity of this function is O(1) plus the the time of emtying |
| 697 | 697 |
/// \c tpath. If \c it is \c INVALID then it just clears \c tpath |
| 698 | 698 |
void split(ArcIt it, ListPath& tpath) {
|
| 699 | 699 |
tpath.clear(); |
| 700 | 700 |
if (it.node) {
|
| 701 | 701 |
tpath.first = it.node; |
| 702 | 702 |
tpath.last = last; |
| 703 | 703 |
if (it.node->prev) {
|
| 704 | 704 |
last = it.node->prev; |
| 705 | 705 |
last->next = 0; |
| 706 | 706 |
} else {
|
| 707 | 707 |
first = last = 0; |
| 708 | 708 |
} |
| 709 | 709 |
it.node->prev = 0; |
| 710 | 710 |
} |
| 711 | 711 |
} |
| 712 | 712 |
|
| 713 | 713 |
|
| 714 | 714 |
typedef True BuildTag; |
| 715 | 715 |
|
| 716 | 716 |
template <typename CPath> |
| 717 | 717 |
void build(const CPath& path) {
|
| 718 | 718 |
for (typename CPath::ArcIt it(path); it != INVALID; ++it) {
|
| 719 | 719 |
addBack(it); |
| 720 | 720 |
} |
| 721 | 721 |
} |
| 722 | 722 |
|
| 723 | 723 |
template <typename CPath> |
| 724 | 724 |
void buildRev(const CPath& path) {
|
| 725 | 725 |
for (typename CPath::RevArcIt it(path); it != INVALID; ++it) {
|
| 726 | 726 |
addFront(it); |
| 727 | 727 |
} |
| 728 | 728 |
} |
| 729 | 729 |
|
| 730 | 730 |
}; |
| 731 | 731 |
|
| 732 | 732 |
/// \brief A structure for representing directed paths in a digraph. |
| 733 | 733 |
/// |
| 734 | 734 |
/// A structure for representing directed path in a digraph. |
| 735 | 735 |
/// \tparam GR The digraph type in which the path is. |
| 736 | 736 |
/// |
| 737 | 737 |
/// In a sense, the path can be treated as a list of arcs. The |
| 738 |
/// |
|
| 738 |
/// LEMON path type stores just this list. As a consequence it |
|
| 739 | 739 |
/// cannot enumerate the nodes in the path and the source node of |
| 740 | 740 |
/// a zero length path is undefined. |
| 741 | 741 |
/// |
| 742 | 742 |
/// This implementation is completly static, i.e. it can be copy constucted |
| 743 | 743 |
/// or copy assigned from another path, but otherwise it cannot be |
| 744 | 744 |
/// modified. |
| 745 | 745 |
/// |
| 746 | 746 |
/// Being the the most memory efficient path type in LEMON, |
| 747 | 747 |
/// it is intented to be |
| 748 | 748 |
/// used when you want to store a large number of paths. |
| 749 | 749 |
template <typename GR> |
| 750 | 750 |
class StaticPath {
|
| 751 | 751 |
public: |
| 752 | 752 |
|
| 753 | 753 |
typedef GR Digraph; |
| 754 | 754 |
typedef typename Digraph::Arc Arc; |
| 755 | 755 |
|
| 756 | 756 |
/// \brief Default constructor |
| 757 | 757 |
/// |
| 758 | 758 |
/// Default constructor |
| 759 | 759 |
StaticPath() : len(0), arcs(0) {}
|
| 760 | 760 |
|
| 761 | 761 |
/// \brief Template copy constructor |
| 762 | 762 |
/// |
| 763 | 763 |
/// This path can be initialized from any other path type. |
| 764 | 764 |
template <typename CPath> |
| 765 | 765 |
StaticPath(const CPath& cpath) : arcs(0) {
|
| 766 | 766 |
pathCopy(cpath, *this); |
| 767 | 767 |
} |
| 768 | 768 |
|
| 769 | 769 |
/// \brief Destructor of the path |
| 770 | 770 |
/// |
| 771 | 771 |
/// Destructor of the path |
| 772 | 772 |
~StaticPath() {
|
| 773 | 773 |
if (arcs) delete[] arcs; |
| 774 | 774 |
} |
| 775 | 775 |
|
| 776 | 776 |
/// \brief Template copy assignment |
| 777 | 777 |
/// |
| 778 | 778 |
/// This path can be made equal to any other path type. It simply |
| 779 | 779 |
/// makes a copy of the given path. |
| 780 | 780 |
template <typename CPath> |
| 781 | 781 |
StaticPath& operator=(const CPath& cpath) {
|
| 782 | 782 |
pathCopy(cpath, *this); |
| 783 | 783 |
return *this; |
| 784 | 784 |
} |
| 785 | 785 |
|
| 786 | 786 |
/// \brief Iterator class to iterate on the arcs of the paths |
| 787 | 787 |
/// |
| 788 | 788 |
/// This class is used to iterate on the arcs of the paths |
| 789 | 789 |
/// |
| 790 | 790 |
/// Of course it converts to Digraph::Arc |
| 791 | 791 |
class ArcIt {
|
| 792 | 792 |
friend class StaticPath; |
| 793 | 793 |
public: |
| 794 | 794 |
/// Default constructor |
| 795 | 795 |
ArcIt() {}
|
| 796 | 796 |
/// Invalid constructor |
| 797 | 797 |
ArcIt(Invalid) : path(0), idx(-1) {}
|
| 798 | 798 |
/// Initializate the constructor to the first arc of path |
| 799 | 799 |
ArcIt(const StaticPath &_path) |
| 800 | 800 |
: path(&_path), idx(_path.empty() ? -1 : 0) {}
|
| 801 | 801 |
|
| 802 | 802 |
private: |
| 803 | 803 |
|
| 804 | 804 |
/// Constructor with starting point |
| 805 | 805 |
ArcIt(const StaticPath &_path, int _idx) |
| 806 | 806 |
: idx(_idx), path(&_path) {}
|
| 807 | 807 |
|
| 808 | 808 |
public: |
| 809 | 809 |
|
| 810 | 810 |
///Conversion to Digraph::Arc |
| 811 | 811 |
operator const Arc&() const {
|
| 812 | 812 |
return path->nth(idx); |
| 813 | 813 |
} |
| 814 | 814 |
|
| 815 | 815 |
/// Next arc |
| 816 | 816 |
ArcIt& operator++() {
|
| 817 | 817 |
++idx; |
| 818 | 818 |
if (idx >= path->length()) idx = -1; |
| 819 | 819 |
return *this; |
| 820 | 820 |
} |
| 821 | 821 |
|
| 822 | 822 |
/// Comparison operator |
| 823 | 823 |
bool operator==(const ArcIt& e) const { return idx==e.idx; }
|
| 824 | 824 |
/// Comparison operator |
| 825 | 825 |
bool operator!=(const ArcIt& e) const { return idx!=e.idx; }
|
| 826 | 826 |
/// Comparison operator |
| 827 | 827 |
bool operator<(const ArcIt& e) const { return idx<e.idx; }
|
| 828 | 828 |
|
| 829 | 829 |
private: |
| 830 | 830 |
const StaticPath *path; |
| 831 | 831 |
int idx; |
| 832 | 832 |
}; |
| 833 | 833 |
|
| 834 |
/// \brief The |
|
| 834 |
/// \brief The n-th arc. |
|
| 835 | 835 |
/// |
| 836 | 836 |
/// \pre \c n is in the <tt>[0..length() - 1]</tt> range. |
| 837 | 837 |
const Arc& nth(int n) const {
|
| 838 | 838 |
return arcs[n]; |
| 839 | 839 |
} |
| 840 | 840 |
|
| 841 |
/// \brief The arc iterator pointing to the |
|
| 841 |
/// \brief The arc iterator pointing to the n-th arc. |
|
| 842 | 842 |
ArcIt nthIt(int n) const {
|
| 843 | 843 |
return ArcIt(*this, n); |
| 844 | 844 |
} |
| 845 | 845 |
|
| 846 | 846 |
/// \brief The length of the path. |
| 847 | 847 |
int length() const { return len; }
|
| 848 | 848 |
|
| 849 | 849 |
/// \brief Return true when the path is empty. |
| 850 | 850 |
int empty() const { return len == 0; }
|
| 851 | 851 |
|
| 852 | 852 |
/// \brief Erase all arcs in the digraph. |
| 853 | 853 |
void clear() {
|
| 854 | 854 |
len = 0; |
| 855 | 855 |
if (arcs) delete[] arcs; |
| 856 | 856 |
arcs = 0; |
| 857 | 857 |
} |
| 858 | 858 |
|
| 859 | 859 |
/// \brief The first arc of the path. |
| 860 | 860 |
const Arc& front() const {
|
| 861 | 861 |
return arcs[0]; |
| 862 | 862 |
} |
| 863 | 863 |
|
| 864 | 864 |
/// \brief The last arc of the path. |
| 865 | 865 |
const Arc& back() const {
|
| 866 | 866 |
return arcs[len - 1]; |
| 867 | 867 |
} |
| 868 | 868 |
|
| 869 | 869 |
|
| 870 | 870 |
typedef True BuildTag; |
| 871 | 871 |
|
| 872 | 872 |
template <typename CPath> |
| 873 | 873 |
void build(const CPath& path) {
|
| 874 | 874 |
len = path.length(); |
| 875 | 875 |
arcs = new Arc[len]; |
| 876 | 876 |
int index = 0; |
| 877 | 877 |
for (typename CPath::ArcIt it(path); it != INVALID; ++it) {
|
| 878 | 878 |
arcs[index] = it; |
| 879 | 879 |
++index; |
| 880 | 880 |
} |
| 881 | 881 |
} |
| 882 | 882 |
|
| 883 | 883 |
template <typename CPath> |
| 884 | 884 |
void buildRev(const CPath& path) {
|
| 885 | 885 |
len = path.length(); |
| 886 | 886 |
arcs = new Arc[len]; |
| 887 | 887 |
int index = len; |
| 888 | 888 |
for (typename CPath::RevArcIt it(path); it != INVALID; ++it) {
|
| 889 | 889 |
--index; |
| ... | ... |
@@ -997,97 +997,97 @@ |
| 997 | 997 |
/// \brief Deprecated version of \ref pathCopy(). |
| 998 | 998 |
/// |
| 999 | 999 |
/// Deprecated version of \ref pathCopy() (only for reverse compatibility). |
| 1000 | 1000 |
template <typename To, typename From> |
| 1001 | 1001 |
void copyPath(To& to, const From& from) {
|
| 1002 | 1002 |
pathCopy(from, to); |
| 1003 | 1003 |
} |
| 1004 | 1004 |
|
| 1005 | 1005 |
/// \brief Check the consistency of a path. |
| 1006 | 1006 |
/// |
| 1007 | 1007 |
/// This function checks that the target of each arc is the same |
| 1008 | 1008 |
/// as the source of the next one. |
| 1009 | 1009 |
/// |
| 1010 | 1010 |
template <typename Digraph, typename Path> |
| 1011 | 1011 |
bool checkPath(const Digraph& digraph, const Path& path) {
|
| 1012 | 1012 |
typename Path::ArcIt it(path); |
| 1013 | 1013 |
if (it == INVALID) return true; |
| 1014 | 1014 |
typename Digraph::Node node = digraph.target(it); |
| 1015 | 1015 |
++it; |
| 1016 | 1016 |
while (it != INVALID) {
|
| 1017 | 1017 |
if (digraph.source(it) != node) return false; |
| 1018 | 1018 |
node = digraph.target(it); |
| 1019 | 1019 |
++it; |
| 1020 | 1020 |
} |
| 1021 | 1021 |
return true; |
| 1022 | 1022 |
} |
| 1023 | 1023 |
|
| 1024 | 1024 |
/// \brief The source of a path |
| 1025 | 1025 |
/// |
| 1026 | 1026 |
/// This function returns the source node of the given path. |
| 1027 | 1027 |
/// If the path is empty, then it returns \c INVALID. |
| 1028 | 1028 |
template <typename Digraph, typename Path> |
| 1029 | 1029 |
typename Digraph::Node pathSource(const Digraph& digraph, const Path& path) {
|
| 1030 | 1030 |
return path.empty() ? INVALID : digraph.source(path.front()); |
| 1031 | 1031 |
} |
| 1032 | 1032 |
|
| 1033 | 1033 |
/// \brief The target of a path |
| 1034 | 1034 |
/// |
| 1035 | 1035 |
/// This function returns the target node of the given path. |
| 1036 | 1036 |
/// If the path is empty, then it returns \c INVALID. |
| 1037 | 1037 |
template <typename Digraph, typename Path> |
| 1038 | 1038 |
typename Digraph::Node pathTarget(const Digraph& digraph, const Path& path) {
|
| 1039 | 1039 |
return path.empty() ? INVALID : digraph.target(path.back()); |
| 1040 | 1040 |
} |
| 1041 | 1041 |
|
| 1042 | 1042 |
/// \brief Class which helps to iterate through the nodes of a path |
| 1043 | 1043 |
/// |
| 1044 | 1044 |
/// In a sense, the path can be treated as a list of arcs. The |
| 1045 |
/// |
|
| 1045 |
/// LEMON path type stores only this list. As a consequence, it |
|
| 1046 | 1046 |
/// cannot enumerate the nodes in the path and the zero length paths |
| 1047 | 1047 |
/// cannot have a source node. |
| 1048 | 1048 |
/// |
| 1049 | 1049 |
/// This class implements the node iterator of a path structure. To |
| 1050 | 1050 |
/// provide this feature, the underlying digraph should be passed to |
| 1051 | 1051 |
/// the constructor of the iterator. |
| 1052 | 1052 |
template <typename Path> |
| 1053 | 1053 |
class PathNodeIt {
|
| 1054 | 1054 |
private: |
| 1055 | 1055 |
const typename Path::Digraph *_digraph; |
| 1056 | 1056 |
typename Path::ArcIt _it; |
| 1057 | 1057 |
typename Path::Digraph::Node _nd; |
| 1058 | 1058 |
|
| 1059 | 1059 |
public: |
| 1060 | 1060 |
|
| 1061 | 1061 |
typedef typename Path::Digraph Digraph; |
| 1062 | 1062 |
typedef typename Digraph::Node Node; |
| 1063 | 1063 |
|
| 1064 | 1064 |
/// Default constructor |
| 1065 | 1065 |
PathNodeIt() {}
|
| 1066 | 1066 |
/// Invalid constructor |
| 1067 | 1067 |
PathNodeIt(Invalid) |
| 1068 | 1068 |
: _digraph(0), _it(INVALID), _nd(INVALID) {}
|
| 1069 | 1069 |
/// Constructor |
| 1070 | 1070 |
PathNodeIt(const Digraph& digraph, const Path& path) |
| 1071 | 1071 |
: _digraph(&digraph), _it(path) {
|
| 1072 | 1072 |
_nd = (_it != INVALID ? _digraph->source(_it) : INVALID); |
| 1073 | 1073 |
} |
| 1074 | 1074 |
/// Constructor |
| 1075 | 1075 |
PathNodeIt(const Digraph& digraph, const Path& path, const Node& src) |
| 1076 | 1076 |
: _digraph(&digraph), _it(path), _nd(src) {}
|
| 1077 | 1077 |
|
| 1078 | 1078 |
///Conversion to Digraph::Node |
| 1079 | 1079 |
operator Node() const {
|
| 1080 | 1080 |
return _nd; |
| 1081 | 1081 |
} |
| 1082 | 1082 |
|
| 1083 | 1083 |
/// Next node |
| 1084 | 1084 |
PathNodeIt& operator++() {
|
| 1085 | 1085 |
if (_it == INVALID) _nd = INVALID; |
| 1086 | 1086 |
else {
|
| 1087 | 1087 |
_nd = _digraph->target(_it); |
| 1088 | 1088 |
++_it; |
| 1089 | 1089 |
} |
| 1090 | 1090 |
return *this; |
| 1091 | 1091 |
} |
| 1092 | 1092 |
|
| 1093 | 1093 |
/// Comparison operator |
| ... | ... |
@@ -13,164 +13,176 @@ |
| 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 |
#include <sstream> |
| 20 | 20 |
#include <lemon/list_graph.h> |
| 21 | 21 |
#include <lemon/full_graph.h> |
| 22 | 22 |
#include <lemon/grid_graph.h> |
| 23 | 23 |
#include <lemon/lgf_reader.h> |
| 24 | 24 |
#include <lemon/grosso_locatelli_pullan_mc.h> |
| 25 | 25 |
|
| 26 | 26 |
#include "test_tools.h" |
| 27 | 27 |
|
| 28 | 28 |
using namespace lemon; |
| 29 | 29 |
|
| 30 | 30 |
char test_lgf[] = |
| 31 | 31 |
"@nodes\n" |
| 32 | 32 |
"label max_clique\n" |
| 33 | 33 |
"1 0\n" |
| 34 | 34 |
"2 0\n" |
| 35 | 35 |
"3 0\n" |
| 36 | 36 |
"4 1\n" |
| 37 | 37 |
"5 1\n" |
| 38 | 38 |
"6 1\n" |
| 39 | 39 |
"7 1\n" |
| 40 | 40 |
"@edges\n" |
| 41 | 41 |
" label\n" |
| 42 | 42 |
"1 2 1\n" |
| 43 | 43 |
"1 3 2\n" |
| 44 | 44 |
"1 4 3\n" |
| 45 | 45 |
"1 6 4\n" |
| 46 | 46 |
"2 3 5\n" |
| 47 | 47 |
"2 5 6\n" |
| 48 | 48 |
"2 7 7\n" |
| 49 | 49 |
"3 4 8\n" |
| 50 | 50 |
"3 5 9\n" |
| 51 | 51 |
"4 5 10\n" |
| 52 | 52 |
"4 6 11\n" |
| 53 | 53 |
"4 7 12\n" |
| 54 | 54 |
"5 6 13\n" |
| 55 | 55 |
"5 7 14\n" |
| 56 | 56 |
"6 7 15\n"; |
| 57 | 57 |
|
| 58 | 58 |
|
| 59 | 59 |
// Check with general graphs |
| 60 | 60 |
template <typename Param> |
| 61 |
void checkMaxCliqueGeneral( |
|
| 61 |
void checkMaxCliqueGeneral(Param rule) {
|
|
| 62 | 62 |
typedef ListGraph GR; |
| 63 | 63 |
typedef GrossoLocatelliPullanMc<GR> McAlg; |
| 64 | 64 |
typedef McAlg::CliqueNodeIt CliqueIt; |
| 65 | 65 |
|
| 66 | 66 |
// Basic tests |
| 67 | 67 |
{
|
| 68 | 68 |
GR g; |
| 69 | 69 |
GR::NodeMap<bool> map(g); |
| 70 | 70 |
McAlg mc(g); |
| 71 |
|
|
| 71 |
mc.iterationLimit(50); |
|
| 72 |
check(mc.run(rule) == McAlg::SIZE_LIMIT, "Wrong termination cause"); |
|
| 72 | 73 |
check(mc.cliqueSize() == 0, "Wrong clique size"); |
| 73 | 74 |
check(CliqueIt(mc) == INVALID, "Wrong CliqueNodeIt"); |
| 74 | 75 |
|
| 75 | 76 |
GR::Node u = g.addNode(); |
| 76 |
check(mc.run( |
|
| 77 |
check(mc.run(rule) == McAlg::SIZE_LIMIT, "Wrong termination cause"); |
|
| 77 | 78 |
check(mc.cliqueSize() == 1, "Wrong clique size"); |
| 78 | 79 |
mc.cliqueMap(map); |
| 79 | 80 |
check(map[u], "Wrong clique map"); |
| 80 | 81 |
CliqueIt it1(mc); |
| 81 | 82 |
check(static_cast<GR::Node>(it1) == u && ++it1 == INVALID, |
| 82 | 83 |
"Wrong CliqueNodeIt"); |
| 83 | 84 |
|
| 84 | 85 |
GR::Node v = g.addNode(); |
| 85 |
check(mc.run( |
|
| 86 |
check(mc.run(rule) == McAlg::ITERATION_LIMIT, "Wrong termination cause"); |
|
| 86 | 87 |
check(mc.cliqueSize() == 1, "Wrong clique size"); |
| 87 | 88 |
mc.cliqueMap(map); |
| 88 | 89 |
check((map[u] && !map[v]) || (map[v] && !map[u]), "Wrong clique map"); |
| 89 | 90 |
CliqueIt it2(mc); |
| 90 | 91 |
check(it2 != INVALID && ++it2 == INVALID, "Wrong CliqueNodeIt"); |
| 91 | 92 |
|
| 92 | 93 |
g.addEdge(u, v); |
| 93 |
check(mc.run( |
|
| 94 |
check(mc.run(rule) == McAlg::SIZE_LIMIT, "Wrong termination cause"); |
|
| 94 | 95 |
check(mc.cliqueSize() == 2, "Wrong clique size"); |
| 95 | 96 |
mc.cliqueMap(map); |
| 96 | 97 |
check(map[u] && map[v], "Wrong clique map"); |
| 97 | 98 |
CliqueIt it3(mc); |
| 98 | 99 |
check(it3 != INVALID && ++it3 != INVALID && ++it3 == INVALID, |
| 99 | 100 |
"Wrong CliqueNodeIt"); |
| 100 | 101 |
} |
| 101 | 102 |
|
| 102 | 103 |
// Test graph |
| 103 | 104 |
{
|
| 104 | 105 |
GR g; |
| 105 | 106 |
GR::NodeMap<bool> max_clique(g); |
| 106 | 107 |
GR::NodeMap<bool> map(g); |
| 107 | 108 |
std::istringstream input(test_lgf); |
| 108 | 109 |
graphReader(g, input) |
| 109 | 110 |
.nodeMap("max_clique", max_clique)
|
| 110 | 111 |
.run(); |
| 111 | 112 |
|
| 112 | 113 |
McAlg mc(g); |
| 113 |
|
|
| 114 |
mc.iterationLimit(50); |
|
| 115 |
check(mc.run(rule) == McAlg::ITERATION_LIMIT, "Wrong termination cause"); |
|
| 114 | 116 |
check(mc.cliqueSize() == 4, "Wrong clique size"); |
| 115 | 117 |
mc.cliqueMap(map); |
| 116 | 118 |
for (GR::NodeIt n(g); n != INVALID; ++n) {
|
| 117 | 119 |
check(map[n] == max_clique[n], "Wrong clique map"); |
| 118 | 120 |
} |
| 119 | 121 |
int cnt = 0; |
| 120 | 122 |
for (CliqueIt n(mc); n != INVALID; ++n) {
|
| 121 | 123 |
cnt++; |
| 122 | 124 |
check(map[n] && max_clique[n], "Wrong CliqueNodeIt"); |
| 123 | 125 |
} |
| 124 | 126 |
check(cnt == 4, "Wrong CliqueNodeIt"); |
| 125 | 127 |
} |
| 126 | 128 |
} |
| 127 | 129 |
|
| 128 | 130 |
// Check with full graphs |
| 129 | 131 |
template <typename Param> |
| 130 |
void checkMaxCliqueFullGraph( |
|
| 132 |
void checkMaxCliqueFullGraph(Param rule) {
|
|
| 131 | 133 |
typedef FullGraph GR; |
| 132 | 134 |
typedef GrossoLocatelliPullanMc<FullGraph> McAlg; |
| 133 | 135 |
typedef McAlg::CliqueNodeIt CliqueIt; |
| 134 | 136 |
|
| 135 | 137 |
for (int size = 0; size <= 40; size = size * 3 + 1) {
|
| 136 | 138 |
GR g(size); |
| 137 | 139 |
GR::NodeMap<bool> map(g); |
| 138 | 140 |
McAlg mc(g); |
| 139 |
check(mc.run( |
|
| 141 |
check(mc.run(rule) == McAlg::SIZE_LIMIT, "Wrong termination cause"); |
|
| 140 | 142 |
check(mc.cliqueSize() == size, "Wrong clique size"); |
| 141 | 143 |
mc.cliqueMap(map); |
| 142 | 144 |
for (GR::NodeIt n(g); n != INVALID; ++n) {
|
| 143 | 145 |
check(map[n], "Wrong clique map"); |
| 144 | 146 |
} |
| 145 | 147 |
int cnt = 0; |
| 146 | 148 |
for (CliqueIt n(mc); n != INVALID; ++n) cnt++; |
| 147 | 149 |
check(cnt == size, "Wrong CliqueNodeIt"); |
| 148 | 150 |
} |
| 149 | 151 |
} |
| 150 | 152 |
|
| 151 | 153 |
// Check with grid graphs |
| 152 | 154 |
template <typename Param> |
| 153 |
void checkMaxCliqueGridGraph( |
|
| 155 |
void checkMaxCliqueGridGraph(Param rule) {
|
|
| 154 | 156 |
GridGraph g(5, 7); |
| 155 | 157 |
GridGraph::NodeMap<char> map(g); |
| 156 | 158 |
GrossoLocatelliPullanMc<GridGraph> mc(g); |
| 157 |
|
|
| 159 |
|
|
| 160 |
mc.iterationLimit(100); |
|
| 161 |
check(mc.run(rule) == mc.ITERATION_LIMIT, "Wrong termination cause"); |
|
| 162 |
check(mc.cliqueSize() == 2, "Wrong clique size"); |
|
| 163 |
|
|
| 164 |
mc.stepLimit(100); |
|
| 165 |
check(mc.run(rule) == mc.STEP_LIMIT, "Wrong termination cause"); |
|
| 166 |
check(mc.cliqueSize() == 2, "Wrong clique size"); |
|
| 167 |
|
|
| 168 |
mc.sizeLimit(2); |
|
| 169 |
check(mc.run(rule) == mc.SIZE_LIMIT, "Wrong termination cause"); |
|
| 158 | 170 |
check(mc.cliqueSize() == 2, "Wrong clique size"); |
| 159 | 171 |
} |
| 160 | 172 |
|
| 161 | 173 |
|
| 162 | 174 |
int main() {
|
| 163 |
checkMaxCliqueGeneral(50, GrossoLocatelliPullanMc<ListGraph>::RANDOM); |
|
| 164 |
checkMaxCliqueGeneral(50, GrossoLocatelliPullanMc<ListGraph>::DEGREE_BASED); |
|
| 165 |
checkMaxCliqueGeneral( |
|
| 175 |
checkMaxCliqueGeneral(GrossoLocatelliPullanMc<ListGraph>::RANDOM); |
|
| 176 |
checkMaxCliqueGeneral(GrossoLocatelliPullanMc<ListGraph>::DEGREE_BASED); |
|
| 177 |
checkMaxCliqueGeneral(GrossoLocatelliPullanMc<ListGraph>::PENALTY_BASED); |
|
| 166 | 178 |
|
| 167 |
checkMaxCliqueFullGraph(50, GrossoLocatelliPullanMc<FullGraph>::RANDOM); |
|
| 168 |
checkMaxCliqueFullGraph(50, GrossoLocatelliPullanMc<FullGraph>::DEGREE_BASED); |
|
| 169 |
checkMaxCliqueFullGraph( |
|
| 179 |
checkMaxCliqueFullGraph(GrossoLocatelliPullanMc<FullGraph>::RANDOM); |
|
| 180 |
checkMaxCliqueFullGraph(GrossoLocatelliPullanMc<FullGraph>::DEGREE_BASED); |
|
| 181 |
checkMaxCliqueFullGraph(GrossoLocatelliPullanMc<FullGraph>::PENALTY_BASED); |
|
| 170 | 182 |
|
| 171 |
checkMaxCliqueGridGraph(50, GrossoLocatelliPullanMc<GridGraph>::RANDOM); |
|
| 172 |
checkMaxCliqueGridGraph(50, GrossoLocatelliPullanMc<GridGraph>::DEGREE_BASED); |
|
| 173 |
checkMaxCliqueGridGraph( |
|
| 183 |
checkMaxCliqueGridGraph(GrossoLocatelliPullanMc<GridGraph>::RANDOM); |
|
| 184 |
checkMaxCliqueGridGraph(GrossoLocatelliPullanMc<GridGraph>::DEGREE_BASED); |
|
| 185 |
checkMaxCliqueGridGraph(GrossoLocatelliPullanMc<GridGraph>::PENALTY_BASED); |
|
| 174 | 186 |
|
| 175 | 187 |
return 0; |
| 176 | 188 |
} |
0 comments (0 inline)