0
8
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 |
... | ... |
@@ -43,98 +43,98 @@ |
43 | 43 |
struct CapacityScalingDefaultTraits |
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 number types must be signed and all input data must |
90 | 90 |
/// be integer. |
91 |
/// \warning This algorithm does not support negative costs for such |
|
92 |
/// arcs that have infinite upper bound. |
|
91 |
/// \warning This algorithm does not support negative costs for |
|
92 |
/// arcs having infinite upper bound. |
|
93 | 93 |
#ifdef DOXYGEN |
94 | 94 |
template <typename GR, typename V, typename C, typename TR> |
95 | 95 |
#else |
96 | 96 |
template < typename GR, typename V = int, typename C = V, |
97 | 97 |
typename TR = CapacityScalingDefaultTraits<GR, V, C> > |
98 | 98 |
#endif |
99 | 99 |
class CapacityScaling |
100 | 100 |
{ |
101 | 101 |
public: |
102 | 102 |
|
103 | 103 |
/// The type of the digraph |
104 | 104 |
typedef typename TR::Digraph Digraph; |
105 | 105 |
/// The type of the flow amounts, capacity bounds and supply values |
106 | 106 |
typedef typename TR::Value Value; |
107 | 107 |
/// The type of the arc costs |
108 | 108 |
typedef typename TR::Cost Cost; |
109 | 109 |
|
110 | 110 |
/// The type of the heap used for internal Dijkstra computations |
111 | 111 |
typedef typename TR::Heap Heap; |
112 | 112 |
|
113 | 113 |
/// The \ref CapacityScalingDefaultTraits "traits class" of the algorithm |
114 | 114 |
typedef TR Traits; |
115 | 115 |
|
116 | 116 |
public: |
117 | 117 |
|
118 | 118 |
/// \brief Problem type constants for the \c run() function. |
119 | 119 |
/// |
120 | 120 |
/// Enum type containing the problem type constants that can be |
121 | 121 |
/// returned by the \ref run() function of the algorithm. |
122 | 122 |
enum ProblemType { |
123 | 123 |
/// The problem has no feasible solution (flow). |
124 | 124 |
INFEASIBLE, |
125 | 125 |
/// The problem has optimal solution (i.e. it is feasible and |
126 | 126 |
/// bounded), and the algorithm has found optimal flow and node |
127 | 127 |
/// potentials (primal and dual solutions). |
128 | 128 |
OPTIMAL, |
129 | 129 |
/// The digraph contains an arc of negative cost and infinite |
130 | 130 |
/// upper bound. It means that the objective function is unbounded |
131 | 131 |
/// on that arc, however, note that it could actually be bounded |
132 | 132 |
/// over the feasible flows, but this algroithm cannot handle |
133 | 133 |
/// these cases. |
134 | 134 |
UNBOUNDED |
135 | 135 |
}; |
136 | 136 |
|
137 | 137 |
private: |
138 | 138 |
|
139 | 139 |
TEMPLATE_DIGRAPH_TYPEDEFS(GR); |
140 | 140 |
|
... | ... |
@@ -377,97 +377,97 @@ |
377 | 377 |
|
378 | 378 |
/// \brief Set the costs of the arcs. |
379 | 379 |
/// |
380 | 380 |
/// This function sets the costs of the arcs. |
381 | 381 |
/// If it is not used before calling \ref run(), the costs |
382 | 382 |
/// will be set to \c 1 on all arcs. |
383 | 383 |
/// |
384 | 384 |
/// \param map An arc map storing the costs. |
385 | 385 |
/// Its \c Value type must be convertible to the \c Cost type |
386 | 386 |
/// of the algorithm. |
387 | 387 |
/// |
388 | 388 |
/// \return <tt>(*this)</tt> |
389 | 389 |
template<typename CostMap> |
390 | 390 |
CapacityScaling& costMap(const CostMap& map) { |
391 | 391 |
for (ArcIt a(_graph); a != INVALID; ++a) { |
392 | 392 |
_cost[_arc_idf[a]] = map[a]; |
393 | 393 |
_cost[_arc_idb[a]] = -map[a]; |
394 | 394 |
} |
395 | 395 |
return *this; |
396 | 396 |
} |
397 | 397 |
|
398 | 398 |
/// \brief Set the supply values of the nodes. |
399 | 399 |
/// |
400 | 400 |
/// This function sets the supply values of the nodes. |
401 | 401 |
/// If neither this function nor \ref stSupply() is used before |
402 | 402 |
/// calling \ref run(), the supply of each node will be set to zero. |
403 | 403 |
/// |
404 | 404 |
/// \param map A node map storing the supply values. |
405 | 405 |
/// Its \c Value type must be convertible to the \c Value type |
406 | 406 |
/// of the algorithm. |
407 | 407 |
/// |
408 | 408 |
/// \return <tt>(*this)</tt> |
409 | 409 |
template<typename SupplyMap> |
410 | 410 |
CapacityScaling& supplyMap(const SupplyMap& map) { |
411 | 411 |
for (NodeIt n(_graph); n != INVALID; ++n) { |
412 | 412 |
_supply[_node_id[n]] = map[n]; |
413 | 413 |
} |
414 | 414 |
return *this; |
415 | 415 |
} |
416 | 416 |
|
417 | 417 |
/// \brief Set single source and target nodes and a supply value. |
418 | 418 |
/// |
419 | 419 |
/// This function sets a single source node and a single target node |
420 | 420 |
/// and the required flow value. |
421 | 421 |
/// If neither this function nor \ref supplyMap() is used before |
422 | 422 |
/// calling \ref run(), the supply of each node will be set to zero. |
423 | 423 |
/// |
424 | 424 |
/// Using this function has the same effect as using \ref supplyMap() |
425 |
/// with |
|
425 |
/// with a map in which \c k is assigned to \c s, \c -k is |
|
426 | 426 |
/// assigned to \c t and all other nodes have zero supply value. |
427 | 427 |
/// |
428 | 428 |
/// \param s The source node. |
429 | 429 |
/// \param t The target node. |
430 | 430 |
/// \param k The required amount of flow from node \c s to node \c t |
431 | 431 |
/// (i.e. the supply of \c s and the demand of \c t). |
432 | 432 |
/// |
433 | 433 |
/// \return <tt>(*this)</tt> |
434 | 434 |
CapacityScaling& stSupply(const Node& s, const Node& t, Value k) { |
435 | 435 |
for (int i = 0; i != _node_num; ++i) { |
436 | 436 |
_supply[i] = 0; |
437 | 437 |
} |
438 | 438 |
_supply[_node_id[s]] = k; |
439 | 439 |
_supply[_node_id[t]] = -k; |
440 | 440 |
return *this; |
441 | 441 |
} |
442 | 442 |
|
443 | 443 |
/// @} |
444 | 444 |
|
445 | 445 |
/// \name Execution control |
446 | 446 |
/// The algorithm can be executed using \ref run(). |
447 | 447 |
|
448 | 448 |
/// @{ |
449 | 449 |
|
450 | 450 |
/// \brief Run the algorithm. |
451 | 451 |
/// |
452 | 452 |
/// This function runs the algorithm. |
453 | 453 |
/// The paramters can be specified using functions \ref lowerMap(), |
454 | 454 |
/// \ref upperMap(), \ref costMap(), \ref supplyMap(), \ref stSupply(). |
455 | 455 |
/// For example, |
456 | 456 |
/// \code |
457 | 457 |
/// CapacityScaling<ListDigraph> cs(graph); |
458 | 458 |
/// cs.lowerMap(lower).upperMap(upper).costMap(cost) |
459 | 459 |
/// .supplyMap(sup).run(); |
460 | 460 |
/// \endcode |
461 | 461 |
/// |
462 | 462 |
/// This function can be called more than once. All the given parameters |
463 | 463 |
/// are kept for the next call, unless \ref resetParams() or \ref reset() |
464 | 464 |
/// is used, thus only the modified parameters have to be set again. |
465 | 465 |
/// If the underlying digraph was also modified after the construction |
466 | 466 |
/// of the class (or the last \ref reset() call), then the \ref reset() |
467 | 467 |
/// function must be called. |
468 | 468 |
/// |
469 | 469 |
/// \param factor The capacity scaling factor. It must be larger than |
470 | 470 |
/// one to use scaling. If it is less or equal to one, then scaling |
471 | 471 |
/// will be disabled. |
472 | 472 |
/// |
473 | 473 |
/// \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,178 +52,181 @@ |
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 number types must be signed and all input data must |
117 | 120 |
/// be integer. |
118 |
/// \warning This algorithm does not support negative costs for such |
|
119 |
/// arcs that have infinite upper bound. |
|
121 |
/// \warning This algorithm does not support negative costs for |
|
122 |
/// arcs having infinite upper bound. |
|
120 | 123 |
/// |
121 | 124 |
/// \note %CostScaling provides three different internal methods, |
122 | 125 |
/// from which the most efficient one is used by default. |
123 | 126 |
/// For more information, see \ref Method. |
124 | 127 |
#ifdef DOXYGEN |
125 | 128 |
template <typename GR, typename V, typename C, typename TR> |
126 | 129 |
#else |
127 | 130 |
template < typename GR, typename V = int, typename C = V, |
128 | 131 |
typename TR = CostScalingDefaultTraits<GR, V, C> > |
129 | 132 |
#endif |
130 | 133 |
class CostScaling |
131 | 134 |
{ |
132 | 135 |
public: |
133 | 136 |
|
134 | 137 |
/// The type of the digraph |
135 | 138 |
typedef typename TR::Digraph Digraph; |
136 | 139 |
/// The type of the flow amounts, capacity bounds and supply values |
137 | 140 |
typedef typename TR::Value Value; |
138 | 141 |
/// The type of the arc costs |
139 | 142 |
typedef typename TR::Cost Cost; |
140 | 143 |
|
141 | 144 |
/// \brief The large cost type |
142 | 145 |
/// |
143 | 146 |
/// The large cost type used for internal computations. |
144 | 147 |
/// By default, it is \c long \c long if the \c Cost type is integer, |
145 | 148 |
/// otherwise it is \c double. |
146 | 149 |
typedef typename TR::LargeCost LargeCost; |
147 | 150 |
|
148 | 151 |
/// The \ref CostScalingDefaultTraits "traits class" of the algorithm |
149 | 152 |
typedef TR Traits; |
150 | 153 |
|
151 | 154 |
public: |
152 | 155 |
|
153 | 156 |
/// \brief Problem type constants for the \c run() function. |
154 | 157 |
/// |
155 | 158 |
/// Enum type containing the problem type constants that can be |
156 | 159 |
/// returned by the \ref run() function of the algorithm. |
157 | 160 |
enum ProblemType { |
158 | 161 |
/// The problem has no feasible solution (flow). |
159 | 162 |
INFEASIBLE, |
160 | 163 |
/// The problem has optimal solution (i.e. it is feasible and |
161 | 164 |
/// bounded), and the algorithm has found optimal flow and node |
162 | 165 |
/// potentials (primal and dual solutions). |
163 | 166 |
OPTIMAL, |
164 | 167 |
/// The digraph contains an arc of negative cost and infinite |
165 | 168 |
/// upper bound. It means that the objective function is unbounded |
166 | 169 |
/// on that arc, however, note that it could actually be bounded |
167 | 170 |
/// over the feasible flows, but this algroithm cannot handle |
168 | 171 |
/// these cases. |
169 | 172 |
UNBOUNDED |
170 | 173 |
}; |
171 | 174 |
|
172 | 175 |
/// \brief Constants for selecting the internal method. |
173 | 176 |
/// |
174 | 177 |
/// Enum type containing constants for selecting the internal method |
175 | 178 |
/// for the \ref run() function. |
176 | 179 |
/// |
177 | 180 |
/// \ref CostScaling provides three internal methods that differ mainly |
178 | 181 |
/// in their base operations, which are used in conjunction with the |
179 | 182 |
/// relabel operation. |
180 | 183 |
/// By default, the so called \ref PARTIAL_AUGMENT |
181 |
/// "Partial Augment-Relabel" method is used, which |
|
184 |
/// "Partial Augment-Relabel" method is used, which turned out to be |
|
182 | 185 |
/// the most efficient and the most robust on various test inputs. |
183 | 186 |
/// However, the other methods can be selected using the \ref run() |
184 | 187 |
/// function with the proper parameter. |
185 | 188 |
enum Method { |
186 | 189 |
/// Local push operations are used, i.e. flow is moved only on one |
187 | 190 |
/// admissible arc at once. |
188 | 191 |
PUSH, |
189 | 192 |
/// Augment operations are used, i.e. flow is moved on admissible |
190 | 193 |
/// paths from a node with excess to a node with deficit. |
191 | 194 |
AUGMENT, |
192 | 195 |
/// Partial augment operations are used, i.e. flow is moved on |
193 | 196 |
/// admissible paths started from a node with excess, but the |
194 | 197 |
/// lengths of these paths are limited. This method can be viewed |
195 | 198 |
/// as a combined version of the previous two operations. |
196 | 199 |
PARTIAL_AUGMENT |
197 | 200 |
}; |
198 | 201 |
|
199 | 202 |
private: |
200 | 203 |
|
201 | 204 |
TEMPLATE_DIGRAPH_TYPEDEFS(GR); |
202 | 205 |
|
203 | 206 |
typedef std::vector<int> IntVector; |
204 | 207 |
typedef std::vector<Value> ValueVector; |
205 | 208 |
typedef std::vector<Cost> CostVector; |
206 | 209 |
typedef std::vector<LargeCost> LargeCostVector; |
207 | 210 |
typedef std::vector<char> BoolVector; |
208 | 211 |
// Note: vector<char> is used instead of vector<bool> for efficiency reasons |
209 | 212 |
|
210 | 213 |
private: |
211 | 214 |
|
212 | 215 |
template <typename KT, typename VT> |
213 | 216 |
class StaticVectorMap { |
214 | 217 |
public: |
215 | 218 |
typedef KT Key; |
216 | 219 |
typedef VT Value; |
217 | 220 |
|
218 | 221 |
StaticVectorMap(std::vector<Value>& v) : _v(v) {} |
219 | 222 |
|
220 | 223 |
const Value& operator[](const Key& key) const { |
221 | 224 |
return _v[StaticDigraph::id(key)]; |
222 | 225 |
} |
223 | 226 |
|
224 | 227 |
Value& operator[](const Key& key) { |
225 | 228 |
return _v[StaticDigraph::id(key)]; |
226 | 229 |
} |
227 | 230 |
|
228 | 231 |
void set(const Key& key, const Value& val) { |
229 | 232 |
_v[StaticDigraph::id(key)] = val; |
... | ... |
@@ -402,97 +405,97 @@ |
402 | 405 |
|
403 | 406 |
/// \brief Set the costs of the arcs. |
404 | 407 |
/// |
405 | 408 |
/// This function sets the costs of the arcs. |
406 | 409 |
/// If it is not used before calling \ref run(), the costs |
407 | 410 |
/// will be set to \c 1 on all arcs. |
408 | 411 |
/// |
409 | 412 |
/// \param map An arc map storing the costs. |
410 | 413 |
/// Its \c Value type must be convertible to the \c Cost type |
411 | 414 |
/// of the algorithm. |
412 | 415 |
/// |
413 | 416 |
/// \return <tt>(*this)</tt> |
414 | 417 |
template<typename CostMap> |
415 | 418 |
CostScaling& costMap(const CostMap& map) { |
416 | 419 |
for (ArcIt a(_graph); a != INVALID; ++a) { |
417 | 420 |
_scost[_arc_idf[a]] = map[a]; |
418 | 421 |
_scost[_arc_idb[a]] = -map[a]; |
419 | 422 |
} |
420 | 423 |
return *this; |
421 | 424 |
} |
422 | 425 |
|
423 | 426 |
/// \brief Set the supply values of the nodes. |
424 | 427 |
/// |
425 | 428 |
/// This function sets the supply values of the nodes. |
426 | 429 |
/// If neither this function nor \ref stSupply() is used before |
427 | 430 |
/// calling \ref run(), the supply of each node will be set to zero. |
428 | 431 |
/// |
429 | 432 |
/// \param map A node map storing the supply values. |
430 | 433 |
/// Its \c Value type must be convertible to the \c Value type |
431 | 434 |
/// of the algorithm. |
432 | 435 |
/// |
433 | 436 |
/// \return <tt>(*this)</tt> |
434 | 437 |
template<typename SupplyMap> |
435 | 438 |
CostScaling& supplyMap(const SupplyMap& map) { |
436 | 439 |
for (NodeIt n(_graph); n != INVALID; ++n) { |
437 | 440 |
_supply[_node_id[n]] = map[n]; |
438 | 441 |
} |
439 | 442 |
return *this; |
440 | 443 |
} |
441 | 444 |
|
442 | 445 |
/// \brief Set single source and target nodes and a supply value. |
443 | 446 |
/// |
444 | 447 |
/// This function sets a single source node and a single target node |
445 | 448 |
/// and the required flow value. |
446 | 449 |
/// If neither this function nor \ref supplyMap() is used before |
447 | 450 |
/// calling \ref run(), the supply of each node will be set to zero. |
448 | 451 |
/// |
449 | 452 |
/// Using this function has the same effect as using \ref supplyMap() |
450 |
/// with |
|
453 |
/// with a map in which \c k is assigned to \c s, \c -k is |
|
451 | 454 |
/// assigned to \c t and all other nodes have zero supply value. |
452 | 455 |
/// |
453 | 456 |
/// \param s The source node. |
454 | 457 |
/// \param t The target node. |
455 | 458 |
/// \param k The required amount of flow from node \c s to node \c t |
456 | 459 |
/// (i.e. the supply of \c s and the demand of \c t). |
457 | 460 |
/// |
458 | 461 |
/// \return <tt>(*this)</tt> |
459 | 462 |
CostScaling& stSupply(const Node& s, const Node& t, Value k) { |
460 | 463 |
for (int i = 0; i != _res_node_num; ++i) { |
461 | 464 |
_supply[i] = 0; |
462 | 465 |
} |
463 | 466 |
_supply[_node_id[s]] = k; |
464 | 467 |
_supply[_node_id[t]] = -k; |
465 | 468 |
return *this; |
466 | 469 |
} |
467 | 470 |
|
468 | 471 |
/// @} |
469 | 472 |
|
470 | 473 |
/// \name Execution control |
471 | 474 |
/// The algorithm can be executed using \ref run(). |
472 | 475 |
|
473 | 476 |
/// @{ |
474 | 477 |
|
475 | 478 |
/// \brief Run the algorithm. |
476 | 479 |
/// |
477 | 480 |
/// This function runs the algorithm. |
478 | 481 |
/// The paramters can be specified using functions \ref lowerMap(), |
479 | 482 |
/// \ref upperMap(), \ref costMap(), \ref supplyMap(), \ref stSupply(). |
480 | 483 |
/// For example, |
481 | 484 |
/// \code |
482 | 485 |
/// CostScaling<ListDigraph> cs(graph); |
483 | 486 |
/// cs.lowerMap(lower).upperMap(upper).costMap(cost) |
484 | 487 |
/// .supplyMap(sup).run(); |
485 | 488 |
/// \endcode |
486 | 489 |
/// |
487 | 490 |
/// This function can be called more than once. All the given parameters |
488 | 491 |
/// are kept for the next call, unless \ref resetParams() or \ref reset() |
489 | 492 |
/// is used, thus only the modified parameters have to be set again. |
490 | 493 |
/// If the underlying digraph was also modified after the construction |
491 | 494 |
/// of the class (or the last \ref reset() call), then the \ref reset() |
492 | 495 |
/// function must be called. |
493 | 496 |
/// |
494 | 497 |
/// \param method The internal method that will be used in the |
495 | 498 |
/// algorithm. For more information, see \ref Method. |
496 | 499 |
/// \param factor The cost scaling factor. It must be larger than one. |
497 | 500 |
/// |
498 | 501 |
/// \return \c INFEASIBLE if no feasible flow exists, |
... | ... |
@@ -22,147 +22,146 @@ |
22 | 22 |
/// \ingroup min_cost_flow_algs |
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 number types must be signed and all input data must |
69 | 69 |
/// be integer. |
70 |
/// \warning This algorithm does not support negative costs for such |
|
71 |
/// arcs that have infinite upper bound. |
|
70 |
/// \warning This algorithm does not support negative costs for |
|
71 |
/// arcs having infinite upper bound. |
|
72 | 72 |
/// |
73 | 73 |
/// \note For more information about the three available methods, |
74 | 74 |
/// see \ref Method. |
75 | 75 |
#ifdef DOXYGEN |
76 | 76 |
template <typename GR, typename V, typename C> |
77 | 77 |
#else |
78 | 78 |
template <typename GR, typename V = int, typename C = V> |
79 | 79 |
#endif |
80 | 80 |
class CycleCanceling |
81 | 81 |
{ |
82 | 82 |
public: |
83 | 83 |
|
84 | 84 |
/// The type of the digraph |
85 | 85 |
typedef GR Digraph; |
86 | 86 |
/// The type of the flow amounts, capacity bounds and supply values |
87 | 87 |
typedef V Value; |
88 | 88 |
/// The type of the arc costs |
89 | 89 |
typedef C Cost; |
90 | 90 |
|
91 | 91 |
public: |
92 | 92 |
|
93 | 93 |
/// \brief Problem type constants for the \c run() function. |
94 | 94 |
/// |
95 | 95 |
/// Enum type containing the problem type constants that can be |
96 | 96 |
/// returned by the \ref run() function of the algorithm. |
97 | 97 |
enum ProblemType { |
98 | 98 |
/// The problem has no feasible solution (flow). |
99 | 99 |
INFEASIBLE, |
100 | 100 |
/// The problem has optimal solution (i.e. it is feasible and |
101 | 101 |
/// bounded), and the algorithm has found optimal flow and node |
102 | 102 |
/// potentials (primal and dual solutions). |
103 | 103 |
OPTIMAL, |
104 | 104 |
/// The digraph contains an arc of negative cost and infinite |
105 | 105 |
/// upper bound. It means that the objective function is unbounded |
106 | 106 |
/// on that arc, however, note that it could actually be bounded |
107 | 107 |
/// over the feasible flows, but this algroithm cannot handle |
108 | 108 |
/// these cases. |
109 | 109 |
UNBOUNDED |
110 | 110 |
}; |
111 | 111 |
|
112 | 112 |
/// \brief Constants for selecting the used method. |
113 | 113 |
/// |
114 | 114 |
/// Enum type containing constants for selecting the used method |
115 | 115 |
/// for the \ref run() function. |
116 | 116 |
/// |
117 | 117 |
/// \ref CycleCanceling provides three different cycle-canceling |
118 | 118 |
/// methods. By default, \ref CANCEL_AND_TIGHTEN "Cancel and Tighten" |
119 |
/// is used, which proved to be the most efficient and the most robust |
|
120 |
/// on various test inputs. |
|
119 |
/// is used, which is by far the most efficient and the most robust. |
|
121 | 120 |
/// However, the other methods can be selected using the \ref run() |
122 | 121 |
/// function with the proper parameter. |
123 | 122 |
enum Method { |
124 | 123 |
/// A simple cycle-canceling method, which uses the |
125 | 124 |
/// \ref BellmanFord "Bellman-Ford" algorithm with limited iteration |
126 | 125 |
/// number for detecting negative cycles in the residual network. |
127 | 126 |
SIMPLE_CYCLE_CANCELING, |
128 | 127 |
/// The "Minimum Mean Cycle-Canceling" algorithm, which is a |
129 | 128 |
/// well-known strongly polynomial method |
130 | 129 |
/// \ref goldberg89cyclecanceling. It improves along a |
131 | 130 |
/// \ref min_mean_cycle "minimum mean cycle" in each iteration. |
132 | 131 |
/// Its running time complexity is O(n<sup>2</sup>m<sup>3</sup>log(n)). |
133 | 132 |
MINIMUM_MEAN_CYCLE_CANCELING, |
134 | 133 |
/// The "Cancel And Tighten" algorithm, which can be viewed as an |
135 | 134 |
/// improved version of the previous method |
136 | 135 |
/// \ref goldberg89cyclecanceling. |
137 | 136 |
/// It is faster both in theory and in practice, its running time |
138 | 137 |
/// complexity is O(n<sup>2</sup>m<sup>2</sup>log(n)). |
139 | 138 |
CANCEL_AND_TIGHTEN |
140 | 139 |
}; |
141 | 140 |
|
142 | 141 |
private: |
143 | 142 |
|
144 | 143 |
TEMPLATE_DIGRAPH_TYPEDEFS(GR); |
145 | 144 |
|
146 | 145 |
typedef std::vector<int> IntVector; |
147 | 146 |
typedef std::vector<double> DoubleVector; |
148 | 147 |
typedef std::vector<Value> ValueVector; |
149 | 148 |
typedef std::vector<Cost> CostVector; |
150 | 149 |
typedef std::vector<char> BoolVector; |
151 | 150 |
// Note: vector<char> is used instead of vector<bool> for efficiency reasons |
152 | 151 |
|
153 | 152 |
private: |
154 | 153 |
|
155 | 154 |
template <typename KT, typename VT> |
156 | 155 |
class StaticVectorMap { |
157 | 156 |
public: |
158 | 157 |
typedef KT Key; |
159 | 158 |
typedef VT Value; |
160 | 159 |
|
161 | 160 |
StaticVectorMap(std::vector<Value>& v) : _v(v) {} |
162 | 161 |
|
163 | 162 |
const Value& operator[](const Key& key) const { |
164 | 163 |
return _v[StaticDigraph::id(key)]; |
165 | 164 |
} |
166 | 165 |
|
167 | 166 |
Value& operator[](const Key& key) { |
168 | 167 |
return _v[StaticDigraph::id(key)]; |
... | ... |
@@ -304,97 +303,97 @@ |
304 | 303 |
|
305 | 304 |
/// \brief Set the costs of the arcs. |
306 | 305 |
/// |
307 | 306 |
/// This function sets the costs of the arcs. |
308 | 307 |
/// If it is not used before calling \ref run(), the costs |
309 | 308 |
/// will be set to \c 1 on all arcs. |
310 | 309 |
/// |
311 | 310 |
/// \param map An arc map storing the costs. |
312 | 311 |
/// Its \c Value type must be convertible to the \c Cost type |
313 | 312 |
/// of the algorithm. |
314 | 313 |
/// |
315 | 314 |
/// \return <tt>(*this)</tt> |
316 | 315 |
template<typename CostMap> |
317 | 316 |
CycleCanceling& costMap(const CostMap& map) { |
318 | 317 |
for (ArcIt a(_graph); a != INVALID; ++a) { |
319 | 318 |
_cost[_arc_idf[a]] = map[a]; |
320 | 319 |
_cost[_arc_idb[a]] = -map[a]; |
321 | 320 |
} |
322 | 321 |
return *this; |
323 | 322 |
} |
324 | 323 |
|
325 | 324 |
/// \brief Set the supply values of the nodes. |
326 | 325 |
/// |
327 | 326 |
/// This function sets the supply values of the nodes. |
328 | 327 |
/// If neither this function nor \ref stSupply() is used before |
329 | 328 |
/// calling \ref run(), the supply of each node will be set to zero. |
330 | 329 |
/// |
331 | 330 |
/// \param map A node map storing the supply values. |
332 | 331 |
/// Its \c Value type must be convertible to the \c Value type |
333 | 332 |
/// of the algorithm. |
334 | 333 |
/// |
335 | 334 |
/// \return <tt>(*this)</tt> |
336 | 335 |
template<typename SupplyMap> |
337 | 336 |
CycleCanceling& supplyMap(const SupplyMap& map) { |
338 | 337 |
for (NodeIt n(_graph); n != INVALID; ++n) { |
339 | 338 |
_supply[_node_id[n]] = map[n]; |
340 | 339 |
} |
341 | 340 |
return *this; |
342 | 341 |
} |
343 | 342 |
|
344 | 343 |
/// \brief Set single source and target nodes and a supply value. |
345 | 344 |
/// |
346 | 345 |
/// This function sets a single source node and a single target node |
347 | 346 |
/// and the required flow value. |
348 | 347 |
/// If neither this function nor \ref supplyMap() is used before |
349 | 348 |
/// calling \ref run(), the supply of each node will be set to zero. |
350 | 349 |
/// |
351 | 350 |
/// Using this function has the same effect as using \ref supplyMap() |
352 |
/// with |
|
351 |
/// with a map in which \c k is assigned to \c s, \c -k is |
|
353 | 352 |
/// assigned to \c t and all other nodes have zero supply value. |
354 | 353 |
/// |
355 | 354 |
/// \param s The source node. |
356 | 355 |
/// \param t The target node. |
357 | 356 |
/// \param k The required amount of flow from node \c s to node \c t |
358 | 357 |
/// (i.e. the supply of \c s and the demand of \c t). |
359 | 358 |
/// |
360 | 359 |
/// \return <tt>(*this)</tt> |
361 | 360 |
CycleCanceling& stSupply(const Node& s, const Node& t, Value k) { |
362 | 361 |
for (int i = 0; i != _res_node_num; ++i) { |
363 | 362 |
_supply[i] = 0; |
364 | 363 |
} |
365 | 364 |
_supply[_node_id[s]] = k; |
366 | 365 |
_supply[_node_id[t]] = -k; |
367 | 366 |
return *this; |
368 | 367 |
} |
369 | 368 |
|
370 | 369 |
/// @} |
371 | 370 |
|
372 | 371 |
/// \name Execution control |
373 | 372 |
/// The algorithm can be executed using \ref run(). |
374 | 373 |
|
375 | 374 |
/// @{ |
376 | 375 |
|
377 | 376 |
/// \brief Run the algorithm. |
378 | 377 |
/// |
379 | 378 |
/// This function runs the algorithm. |
380 | 379 |
/// The paramters can be specified using functions \ref lowerMap(), |
381 | 380 |
/// \ref upperMap(), \ref costMap(), \ref supplyMap(), \ref stSupply(). |
382 | 381 |
/// For example, |
383 | 382 |
/// \code |
384 | 383 |
/// CycleCanceling<ListDigraph> cc(graph); |
385 | 384 |
/// cc.lowerMap(lower).upperMap(upper).costMap(cost) |
386 | 385 |
/// .supplyMap(sup).run(); |
387 | 386 |
/// \endcode |
388 | 387 |
/// |
389 | 388 |
/// This function can be called more than once. All the given parameters |
390 | 389 |
/// are kept for the next call, unless \ref resetParams() or \ref reset() |
391 | 390 |
/// is used, thus only the modified parameters have to be set again. |
392 | 391 |
/// If the underlying digraph was also modified after the construction |
393 | 392 |
/// of the class (or the last \ref reset() call), then the \ref reset() |
394 | 393 |
/// function must be called. |
395 | 394 |
/// |
396 | 395 |
/// \param method The cycle-canceling method that will be used. |
397 | 396 |
/// For more information, see \ref Method. |
398 | 397 |
/// |
399 | 398 |
/// \return \c INFEASIBLE if no feasible flow exists, |
400 | 399 |
/// \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) { |
... | ... |
@@ -2,217 +2,217 @@ |
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 number types must be signed and all input data must |
67 | 67 |
/// be integer. |
68 | 68 |
/// |
69 | 69 |
/// \note %NetworkSimplex provides five different pivot rule |
70 | 70 |
/// implementations, from which the most efficient one is used |
71 | 71 |
/// by default. For more information, see \ref PivotRule. |
72 | 72 |
template <typename GR, typename V = int, typename C = V> |
73 | 73 |
class NetworkSimplex |
74 | 74 |
{ |
75 | 75 |
public: |
76 | 76 |
|
77 | 77 |
/// The type of the flow amounts, capacity bounds and supply values |
78 | 78 |
typedef V Value; |
79 | 79 |
/// The type of the arc costs |
80 | 80 |
typedef C Cost; |
81 | 81 |
|
82 | 82 |
public: |
83 | 83 |
|
84 | 84 |
/// \brief Problem type constants for the \c run() function. |
85 | 85 |
/// |
86 | 86 |
/// Enum type containing the problem type constants that can be |
87 | 87 |
/// returned by the \ref run() function of the algorithm. |
88 | 88 |
enum ProblemType { |
89 | 89 |
/// The problem has no feasible solution (flow). |
90 | 90 |
INFEASIBLE, |
91 | 91 |
/// The problem has optimal solution (i.e. it is feasible and |
92 | 92 |
/// bounded), and the algorithm has found optimal flow and node |
93 | 93 |
/// potentials (primal and dual solutions). |
94 | 94 |
OPTIMAL, |
95 | 95 |
/// The objective function of the problem is unbounded, i.e. |
96 | 96 |
/// there is a directed cycle having negative total cost and |
97 | 97 |
/// infinite upper bound. |
98 | 98 |
UNBOUNDED |
99 | 99 |
}; |
100 | 100 |
|
101 | 101 |
/// \brief Constants for selecting the type of the supply constraints. |
102 | 102 |
/// |
103 | 103 |
/// Enum type containing constants for selecting the supply type, |
104 | 104 |
/// i.e. the direction of the inequalities in the supply/demand |
105 | 105 |
/// constraints of the \ref min_cost_flow "minimum cost flow problem". |
106 | 106 |
/// |
107 | 107 |
/// The default supply type is \c GEQ, the \c LEQ type can be |
108 | 108 |
/// selected using \ref supplyType(). |
109 | 109 |
/// The equality form is a special case of both supply types. |
110 | 110 |
enum SupplyType { |
111 | 111 |
/// This option means that there are <em>"greater or equal"</em> |
112 | 112 |
/// supply/demand constraints in the definition of the problem. |
113 | 113 |
GEQ, |
114 | 114 |
/// This option means that there are <em>"less or equal"</em> |
115 | 115 |
/// supply/demand constraints in the definition of the problem. |
116 | 116 |
LEQ |
117 | 117 |
}; |
118 | 118 |
|
119 | 119 |
/// \brief Constants for selecting the pivot rule. |
120 | 120 |
/// |
121 | 121 |
/// Enum type containing constants for selecting the pivot rule for |
122 | 122 |
/// the \ref run() function. |
123 | 123 |
/// |
124 | 124 |
/// \ref NetworkSimplex provides five different pivot rule |
125 | 125 |
/// implementations that significantly affect the running time |
126 | 126 |
/// of the algorithm. |
127 | 127 |
/// By default, \ref BLOCK_SEARCH "Block Search" is used, which |
128 |
/// |
|
128 |
/// turend out to be the most efficient and the most robust on various |
|
129 | 129 |
/// test inputs. |
130 | 130 |
/// However, another pivot rule can be selected using the \ref run() |
131 | 131 |
/// function with the proper parameter. |
132 | 132 |
enum PivotRule { |
133 | 133 |
|
134 | 134 |
/// The \e First \e Eligible pivot rule. |
135 | 135 |
/// The next eligible arc is selected in a wraparound fashion |
136 | 136 |
/// in every iteration. |
137 | 137 |
FIRST_ELIGIBLE, |
138 | 138 |
|
139 | 139 |
/// The \e Best \e Eligible pivot rule. |
140 | 140 |
/// The best eligible arc is selected in every iteration. |
141 | 141 |
BEST_ELIGIBLE, |
142 | 142 |
|
143 | 143 |
/// The \e Block \e Search pivot rule. |
144 | 144 |
/// A specified number of arcs are examined in every iteration |
145 | 145 |
/// in a wraparound fashion and the best eligible arc is selected |
146 | 146 |
/// from this block. |
147 | 147 |
BLOCK_SEARCH, |
148 | 148 |
|
149 | 149 |
/// The \e Candidate \e List pivot rule. |
150 | 150 |
/// In a major iteration a candidate list is built from eligible arcs |
151 | 151 |
/// in a wraparound fashion and in the following minor iterations |
152 | 152 |
/// the best eligible arc is selected from this list. |
153 | 153 |
CANDIDATE_LIST, |
154 | 154 |
|
155 | 155 |
/// The \e Altering \e Candidate \e List pivot rule. |
156 | 156 |
/// It is a modified version of the Candidate List method. |
157 | 157 |
/// It keeps only the several best eligible arcs from the former |
158 | 158 |
/// candidate list and extends this list in every iteration. |
159 | 159 |
ALTERING_LIST |
160 | 160 |
}; |
161 | 161 |
|
162 | 162 |
private: |
163 | 163 |
|
164 | 164 |
TEMPLATE_DIGRAPH_TYPEDEFS(GR); |
165 | 165 |
|
166 | 166 |
typedef std::vector<int> IntVector; |
167 | 167 |
typedef std::vector<Value> ValueVector; |
168 | 168 |
typedef std::vector<Cost> CostVector; |
169 | 169 |
typedef std::vector<signed char> CharVector; |
170 |
// Note: vector<signed char> is used instead of vector<ArcState> and |
|
170 |
// Note: vector<signed char> is used instead of vector<ArcState> and |
|
171 | 171 |
// vector<ArcDirection> for efficiency reasons |
172 | 172 |
|
173 | 173 |
// State constants for arcs |
174 | 174 |
enum ArcState { |
175 | 175 |
STATE_UPPER = -1, |
176 | 176 |
STATE_TREE = 0, |
177 | 177 |
STATE_LOWER = 1 |
178 | 178 |
}; |
179 | 179 |
|
180 | 180 |
// Direction constants for tree arcs |
181 | 181 |
enum ArcDirection { |
182 | 182 |
DIR_DOWN = -1, |
183 | 183 |
DIR_UP = 1 |
184 | 184 |
}; |
185 | 185 |
|
186 | 186 |
private: |
187 | 187 |
|
188 | 188 |
// Data related to the underlying digraph |
189 | 189 |
const GR &_graph; |
190 | 190 |
int _node_num; |
191 | 191 |
int _arc_num; |
192 | 192 |
int _all_arc_num; |
193 | 193 |
int _search_arc_num; |
194 | 194 |
|
195 | 195 |
// Parameters of the problem |
196 | 196 |
bool _have_lower; |
197 | 197 |
SupplyType _stype; |
198 | 198 |
Value _sum_supply; |
199 | 199 |
|
200 | 200 |
// Data structures for storing the digraph |
201 | 201 |
IntNodeMap _node_id; |
202 | 202 |
IntArcMap _arc_id; |
203 | 203 |
IntVector _source; |
204 | 204 |
IntVector _target; |
205 | 205 |
bool _arc_mixing; |
206 | 206 |
|
207 | 207 |
// Node and arc data |
208 | 208 |
ValueVector _lower; |
209 | 209 |
ValueVector _upper; |
210 | 210 |
ValueVector _cap; |
211 | 211 |
CostVector _cost; |
212 | 212 |
ValueVector _supply; |
213 | 213 |
ValueVector _flow; |
214 | 214 |
CostVector _pi; |
215 | 215 |
|
216 | 216 |
// Data for storing the spanning tree structure |
217 | 217 |
IntVector _parent; |
218 | 218 |
IntVector _pred; |
... | ... |
@@ -689,113 +689,115 @@ |
689 | 689 |
/// This function sets the upper bounds (capacities) on the arcs. |
690 | 690 |
/// If it is not used before calling \ref run(), the upper bounds |
691 | 691 |
/// will be set to \ref INF on all arcs (i.e. the flow value will be |
692 | 692 |
/// unbounded from above). |
693 | 693 |
/// |
694 | 694 |
/// \param map An arc map storing the upper bounds. |
695 | 695 |
/// Its \c Value type must be convertible to the \c Value type |
696 | 696 |
/// of the algorithm. |
697 | 697 |
/// |
698 | 698 |
/// \return <tt>(*this)</tt> |
699 | 699 |
template<typename UpperMap> |
700 | 700 |
NetworkSimplex& upperMap(const UpperMap& map) { |
701 | 701 |
for (ArcIt a(_graph); a != INVALID; ++a) { |
702 | 702 |
_upper[_arc_id[a]] = map[a]; |
703 | 703 |
} |
704 | 704 |
return *this; |
705 | 705 |
} |
706 | 706 |
|
707 | 707 |
/// \brief Set the costs of the arcs. |
708 | 708 |
/// |
709 | 709 |
/// This function sets the costs of the arcs. |
710 | 710 |
/// If it is not used before calling \ref run(), the costs |
711 | 711 |
/// will be set to \c 1 on all arcs. |
712 | 712 |
/// |
713 | 713 |
/// \param map An arc map storing the costs. |
714 | 714 |
/// Its \c Value type must be convertible to the \c Cost type |
715 | 715 |
/// of the algorithm. |
716 | 716 |
/// |
717 | 717 |
/// \return <tt>(*this)</tt> |
718 | 718 |
template<typename CostMap> |
719 | 719 |
NetworkSimplex& costMap(const CostMap& map) { |
720 | 720 |
for (ArcIt a(_graph); a != INVALID; ++a) { |
721 | 721 |
_cost[_arc_id[a]] = map[a]; |
722 | 722 |
} |
723 | 723 |
return *this; |
724 | 724 |
} |
725 | 725 |
|
726 | 726 |
/// \brief Set the supply values of the nodes. |
727 | 727 |
/// |
728 | 728 |
/// This function sets the supply values of the nodes. |
729 | 729 |
/// If neither this function nor \ref stSupply() is used before |
730 | 730 |
/// calling \ref run(), the supply of each node will be set to zero. |
731 | 731 |
/// |
732 | 732 |
/// \param map A node map storing the supply values. |
733 | 733 |
/// Its \c Value type must be convertible to the \c Value type |
734 | 734 |
/// of the algorithm. |
735 | 735 |
/// |
736 | 736 |
/// \return <tt>(*this)</tt> |
737 |
/// |
|
738 |
/// \sa supplyType() |
|
737 | 739 |
template<typename SupplyMap> |
738 | 740 |
NetworkSimplex& supplyMap(const SupplyMap& map) { |
739 | 741 |
for (NodeIt n(_graph); n != INVALID; ++n) { |
740 | 742 |
_supply[_node_id[n]] = map[n]; |
741 | 743 |
} |
742 | 744 |
return *this; |
743 | 745 |
} |
744 | 746 |
|
745 | 747 |
/// \brief Set single source and target nodes and a supply value. |
746 | 748 |
/// |
747 | 749 |
/// This function sets a single source node and a single target node |
748 | 750 |
/// and the required flow value. |
749 | 751 |
/// If neither this function nor \ref supplyMap() is used before |
750 | 752 |
/// calling \ref run(), the supply of each node will be set to zero. |
751 | 753 |
/// |
752 | 754 |
/// Using this function has the same effect as using \ref supplyMap() |
753 |
/// with |
|
755 |
/// with a map in which \c k is assigned to \c s, \c -k is |
|
754 | 756 |
/// assigned to \c t and all other nodes have zero supply value. |
755 | 757 |
/// |
756 | 758 |
/// \param s The source node. |
757 | 759 |
/// \param t The target node. |
758 | 760 |
/// \param k The required amount of flow from node \c s to node \c t |
759 | 761 |
/// (i.e. the supply of \c s and the demand of \c t). |
760 | 762 |
/// |
761 | 763 |
/// \return <tt>(*this)</tt> |
762 | 764 |
NetworkSimplex& stSupply(const Node& s, const Node& t, Value k) { |
763 | 765 |
for (int i = 0; i != _node_num; ++i) { |
764 | 766 |
_supply[i] = 0; |
765 | 767 |
} |
766 | 768 |
_supply[_node_id[s]] = k; |
767 | 769 |
_supply[_node_id[t]] = -k; |
768 | 770 |
return *this; |
769 | 771 |
} |
770 | 772 |
|
771 | 773 |
/// \brief Set the type of the supply constraints. |
772 | 774 |
/// |
773 | 775 |
/// This function sets the type of the supply/demand constraints. |
774 | 776 |
/// If it is not used before calling \ref run(), the \ref GEQ supply |
775 | 777 |
/// type will be used. |
776 | 778 |
/// |
777 | 779 |
/// For more information, see \ref SupplyType. |
778 | 780 |
/// |
779 | 781 |
/// \return <tt>(*this)</tt> |
780 | 782 |
NetworkSimplex& supplyType(SupplyType supply_type) { |
781 | 783 |
_stype = supply_type; |
782 | 784 |
return *this; |
783 | 785 |
} |
784 | 786 |
|
785 | 787 |
/// @} |
786 | 788 |
|
787 | 789 |
/// \name Execution Control |
788 | 790 |
/// The algorithm can be executed using \ref run(). |
789 | 791 |
|
790 | 792 |
/// @{ |
791 | 793 |
|
792 | 794 |
/// \brief Run the algorithm. |
793 | 795 |
/// |
794 | 796 |
/// This function runs the algorithm. |
795 | 797 |
/// The paramters can be specified using functions \ref lowerMap(), |
796 | 798 |
/// \ref upperMap(), \ref costMap(), \ref supplyMap(), \ref stSupply(), |
797 | 799 |
/// \ref supplyType(). |
798 | 800 |
/// For example, |
799 | 801 |
/// \code |
800 | 802 |
/// NetworkSimplex<ListDigraph> ns(graph); |
801 | 803 |
/// ns.lowerMap(lower).upperMap(upper).costMap(cost) |
0 comments (0 inline)