gravatar
kpeter (Peter Kovacs)
kpeter@inf.elte.hu
Add citations to the scaling MCF algorithms (#180, #184) and improve the doc of their group.
0 3 0
default
3 files changed with 12 insertions and 11 deletions:
↑ Collapse diff ↑
Ignore white space 6 line context
... ...
@@ -217,393 +217,391 @@
217 217

	
218 218
  Dijkstra<Digraph, TimeMap> dijkstra(graph, time);
219 219
  dijkstra.run(source, target);
220 220
\endcode
221 221
We have a length map and a maximum speed map on the arcs of a digraph.
222 222
The minimum time to pass the arc can be calculated as the division of
223 223
the two maps which can be done implicitly with the \c DivMap template
224 224
class. We use the implicit minimum time map as the length map of the
225 225
\c Dijkstra algorithm.
226 226
*/
227 227

	
228 228
/**
229 229
@defgroup paths Path Structures
230 230
@ingroup datas
231 231
\brief %Path structures implemented in LEMON.
232 232

	
233 233
This group contains the path structures implemented in LEMON.
234 234

	
235 235
LEMON provides flexible data structures to work with paths.
236 236
All of them have similar interfaces and they can be copied easily with
237 237
assignment operators and copy constructors. This makes it easy and
238 238
efficient to have e.g. the Dijkstra algorithm to store its result in
239 239
any kind of path structure.
240 240

	
241 241
\sa \ref concepts::Path "Path concept"
242 242
*/
243 243

	
244 244
/**
245 245
@defgroup heaps Heap Structures
246 246
@ingroup datas
247 247
\brief %Heap structures implemented in LEMON.
248 248

	
249 249
This group contains the heap structures implemented in LEMON.
250 250

	
251 251
LEMON provides several heap classes. They are efficient implementations
252 252
of the abstract data type \e priority \e queue. They store items with
253 253
specified values called \e priorities in such a way that finding and
254 254
removing the item with minimum priority are efficient.
255 255
The basic operations are adding and erasing items, changing the priority
256 256
of an item, etc.
257 257

	
258 258
Heaps are crucial in several algorithms, such as Dijkstra and Prim.
259 259
The heap implementations have the same interface, thus any of them can be
260 260
used easily in such algorithms.
261 261

	
262 262
\sa \ref concepts::Heap "Heap concept"
263 263
*/
264 264

	
265 265
/**
266 266
@defgroup matrices Matrices
267 267
@ingroup datas
268 268
\brief Two dimensional data storages implemented in LEMON.
269 269

	
270 270
This group contains two dimensional data storages implemented in LEMON.
271 271
*/
272 272

	
273 273
/**
274 274
@defgroup auxdat Auxiliary Data Structures
275 275
@ingroup datas
276 276
\brief Auxiliary data structures implemented in LEMON.
277 277

	
278 278
This group contains some data structures implemented in LEMON in
279 279
order to make it easier to implement combinatorial algorithms.
280 280
*/
281 281

	
282 282
/**
283 283
@defgroup geomdat Geometric Data Structures
284 284
@ingroup auxdat
285 285
\brief Geometric data structures implemented in LEMON.
286 286

	
287 287
This group contains geometric data structures implemented in LEMON.
288 288

	
289 289
 - \ref lemon::dim2::Point "dim2::Point" implements a two dimensional
290 290
   vector with the usual operations.
291 291
 - \ref lemon::dim2::Box "dim2::Box" can be used to determine the
292 292
   rectangular bounding box of a set of \ref lemon::dim2::Point
293 293
   "dim2::Point"'s.
294 294
*/
295 295

	
296 296
/**
297 297
@defgroup matrices Matrices
298 298
@ingroup auxdat
299 299
\brief Two dimensional data storages implemented in LEMON.
300 300

	
301 301
This group contains two dimensional data storages implemented in LEMON.
302 302
*/
303 303

	
304 304
/**
305 305
@defgroup algs Algorithms
306 306
\brief This group contains the several algorithms
307 307
implemented in LEMON.
308 308

	
309 309
This group contains the several algorithms
310 310
implemented in LEMON.
311 311
*/
312 312

	
313 313
/**
314 314
@defgroup search Graph Search
315 315
@ingroup algs
316 316
\brief Common graph search algorithms.
317 317

	
318 318
This group contains the common graph search algorithms, namely
319 319
\e breadth-first \e search (BFS) and \e depth-first \e search (DFS)
320 320
\ref clrs01algorithms.
321 321
*/
322 322

	
323 323
/**
324 324
@defgroup shortest_path Shortest Path Algorithms
325 325
@ingroup algs
326 326
\brief Algorithms for finding shortest paths.
327 327

	
328 328
This group contains the algorithms for finding shortest paths in digraphs
329 329
\ref clrs01algorithms.
330 330

	
331 331
 - \ref Dijkstra algorithm for finding shortest paths from a source node
332 332
   when all arc lengths are non-negative.
333 333
 - \ref BellmanFord "Bellman-Ford" algorithm for finding shortest paths
334 334
   from a source node when arc lenghts can be either positive or negative,
335 335
   but the digraph should not contain directed cycles with negative total
336 336
   length.
337 337
 - \ref FloydWarshall "Floyd-Warshall" and \ref Johnson "Johnson" algorithms
338 338
   for solving the \e all-pairs \e shortest \e paths \e problem when arc
339 339
   lenghts can be either positive or negative, but the digraph should
340 340
   not contain directed cycles with negative total length.
341 341
 - \ref Suurballe A successive shortest path algorithm for finding
342 342
   arc-disjoint paths between two nodes having minimum total length.
343 343
*/
344 344

	
345 345
/**
346 346
@defgroup spantree Minimum Spanning Tree Algorithms
347 347
@ingroup algs
348 348
\brief Algorithms for finding minimum cost spanning trees and arborescences.
349 349

	
350 350
This group contains the algorithms for finding minimum cost spanning
351 351
trees and arborescences \ref clrs01algorithms.
352 352
*/
353 353

	
354 354
/**
355 355
@defgroup max_flow Maximum Flow Algorithms
356 356
@ingroup algs
357 357
\brief Algorithms for finding maximum flows.
358 358

	
359 359
This group contains the algorithms for finding maximum flows and
360 360
feasible circulations \ref clrs01algorithms, \ref amo93networkflows.
361 361

	
362 362
The \e maximum \e flow \e problem is to find a flow of maximum value between
363 363
a single source and a single target. Formally, there is a \f$G=(V,A)\f$
364 364
digraph, a \f$cap: A\rightarrow\mathbf{R}^+_0\f$ capacity function and
365 365
\f$s, t \in V\f$ source and target nodes.
366 366
A maximum flow is an \f$f: A\rightarrow\mathbf{R}^+_0\f$ solution of the
367 367
following optimization problem.
368 368

	
369 369
\f[ \max\sum_{sv\in A} f(sv) - \sum_{vs\in A} f(vs) \f]
370 370
\f[ \sum_{uv\in A} f(uv) = \sum_{vu\in A} f(vu)
371 371
    \quad \forall u\in V\setminus\{s,t\} \f]
372 372
\f[ 0 \leq f(uv) \leq cap(uv) \quad \forall uv\in A \f]
373 373

	
374 374
LEMON contains several algorithms for solving maximum flow problems:
375 375
- \ref EdmondsKarp Edmonds-Karp algorithm
376 376
  \ref edmondskarp72theoretical.
377 377
- \ref Preflow Goldberg-Tarjan's preflow push-relabel algorithm
378 378
  \ref goldberg88newapproach.
379 379
- \ref DinitzSleatorTarjan Dinitz's blocking flow algorithm with dynamic trees
380 380
  \ref dinic70algorithm, \ref sleator83dynamic.
381 381
- \ref GoldbergTarjan !Preflow push-relabel algorithm with dynamic trees
382 382
  \ref goldberg88newapproach, \ref sleator83dynamic.
383 383

	
384 384
In most cases the \ref Preflow algorithm provides the
385 385
fastest method for computing a maximum flow. All implementations
386 386
also provide functions to query the minimum cut, which is the dual
387 387
problem of maximum flow.
388 388

	
389 389
\ref Circulation is a preflow push-relabel algorithm implemented directly 
390 390
for finding feasible circulations, which is a somewhat different problem,
391 391
but it is strongly related to maximum flow.
392 392
For more information, see \ref Circulation.
393 393
*/
394 394

	
395 395
/**
396 396
@defgroup min_cost_flow_algs Minimum Cost Flow Algorithms
397 397
@ingroup algs
398 398

	
399 399
\brief Algorithms for finding minimum cost flows and circulations.
400 400

	
401 401
This group contains the algorithms for finding minimum cost flows and
402 402
circulations \ref amo93networkflows. For more information about this
403 403
problem and its dual solution, see \ref min_cost_flow
404 404
"Minimum Cost Flow Problem".
405 405

	
406 406
LEMON contains several algorithms for this problem.
407 407
 - \ref NetworkSimplex Primal Network Simplex algorithm with various
408 408
   pivot strategies \ref dantzig63linearprog, \ref kellyoneill91netsimplex.
409
 - \ref CostScaling Push-Relabel and Augment-Relabel algorithms based on
410
   cost scaling \ref goldberg90approximation, \ref goldberg97efficient,
409
 - \ref CostScaling Cost Scaling algorithm based on push/augment and
410
   relabel operations \ref goldberg90approximation, \ref goldberg97efficient,
411 411
   \ref bunnagel98efficient.
412
 - \ref CapacityScaling Successive Shortest %Path algorithm with optional
413
   capacity scaling \ref edmondskarp72theoretical.
414
 - \ref CancelAndTighten The Cancel and Tighten algorithm
415
   \ref goldberg89cyclecanceling.
416
 - \ref CycleCanceling Cycle-Canceling algorithms
417
   \ref klein67primal, \ref goldberg89cyclecanceling.
412
 - \ref CapacityScaling Capacity Scaling algorithm based on the successive
413
   shortest path method \ref edmondskarp72theoretical.
414
 - \ref CycleCanceling Cycle-Canceling algorithms, two of which are
415
   strongly polynomial \ref klein67primal, \ref goldberg89cyclecanceling.
418 416

	
419 417
In general NetworkSimplex is the most efficient implementation,
420 418
but in special cases other algorithms could be faster.
421 419
For example, if the total supply and/or capacities are rather small,
422 420
CapacityScaling is usually the fastest algorithm (without effective scaling).
423 421
*/
424 422

	
425 423
/**
426 424
@defgroup min_cut Minimum Cut Algorithms
427 425
@ingroup algs
428 426

	
429 427
\brief Algorithms for finding minimum cut in graphs.
430 428

	
431 429
This group contains the algorithms for finding minimum cut in graphs.
432 430

	
433 431
The \e minimum \e cut \e problem is to find a non-empty and non-complete
434 432
\f$X\f$ subset of the nodes with minimum overall capacity on
435 433
outgoing arcs. Formally, there is a \f$G=(V,A)\f$ digraph, a
436 434
\f$cap: A\rightarrow\mathbf{R}^+_0\f$ capacity function. The minimum
437 435
cut is the \f$X\f$ solution of the next optimization problem:
438 436

	
439 437
\f[ \min_{X \subset V, X\not\in \{\emptyset, V\}}
440 438
    \sum_{uv\in A: u\in X, v\not\in X}cap(uv) \f]
441 439

	
442 440
LEMON contains several algorithms related to minimum cut problems:
443 441

	
444 442
- \ref HaoOrlin "Hao-Orlin algorithm" for calculating minimum cut
445 443
  in directed graphs.
446 444
- \ref NagamochiIbaraki "Nagamochi-Ibaraki algorithm" for
447 445
  calculating minimum cut in undirected graphs.
448 446
- \ref GomoryHu "Gomory-Hu tree computation" for calculating
449 447
  all-pairs minimum cut in undirected graphs.
450 448

	
451 449
If you want to find minimum cut just between two distinict nodes,
452 450
see the \ref max_flow "maximum flow problem".
453 451
*/
454 452

	
455 453
/**
456 454
@defgroup min_mean_cycle Minimum Mean Cycle Algorithms
457 455
@ingroup algs
458 456
\brief Algorithms for finding minimum mean cycles.
459 457

	
460 458
This group contains the algorithms for finding minimum mean cycles
461 459
\ref clrs01algorithms, \ref amo93networkflows.
462 460

	
463 461
The \e minimum \e mean \e cycle \e problem is to find a directed cycle
464 462
of minimum mean length (cost) in a digraph.
465 463
The mean length of a cycle is the average length of its arcs, i.e. the
466 464
ratio between the total length of the cycle and the number of arcs on it.
467 465

	
468 466
This problem has an important connection to \e conservative \e length
469 467
\e functions, too. A length function on the arcs of a digraph is called
470 468
conservative if and only if there is no directed cycle of negative total
471 469
length. For an arbitrary length function, the negative of the minimum
472 470
cycle mean is the smallest \f$\epsilon\f$ value so that increasing the
473 471
arc lengths uniformly by \f$\epsilon\f$ results in a conservative length
474 472
function.
475 473

	
476 474
LEMON contains three algorithms for solving the minimum mean cycle problem:
477 475
- \ref Karp "Karp"'s original algorithm \ref amo93networkflows,
478 476
  \ref dasdan98minmeancycle.
479 477
- \ref HartmannOrlin "Hartmann-Orlin"'s algorithm, which is an improved
480 478
  version of Karp's algorithm \ref dasdan98minmeancycle.
481 479
- \ref Howard "Howard"'s policy iteration algorithm
482 480
  \ref dasdan98minmeancycle.
483 481

	
484 482
In practice, the Howard algorithm proved to be by far the most efficient
485 483
one, though the best known theoretical bound on its running time is
486 484
exponential.
487 485
Both Karp and HartmannOrlin algorithms run in time O(ne) and use space
488 486
O(n<sup>2</sup>+e), but the latter one is typically faster due to the
489 487
applied early termination scheme.
490 488
*/
491 489

	
492 490
/**
493 491
@defgroup matching Matching Algorithms
494 492
@ingroup algs
495 493
\brief Algorithms for finding matchings in graphs and bipartite graphs.
496 494

	
497 495
This group contains the algorithms for calculating
498 496
matchings in graphs and bipartite graphs. The general matching problem is
499 497
finding a subset of the edges for which each node has at most one incident
500 498
edge.
501 499

	
502 500
There are several different algorithms for calculate matchings in
503 501
graphs.  The matching problems in bipartite graphs are generally
504 502
easier than in general graphs. The goal of the matching optimization
505 503
can be finding maximum cardinality, maximum weight or minimum cost
506 504
matching. The search can be constrained to find perfect or
507 505
maximum cardinality matching.
508 506

	
509 507
The matching algorithms implemented in LEMON:
510 508
- \ref MaxBipartiteMatching Hopcroft-Karp augmenting path algorithm
511 509
  for calculating maximum cardinality matching in bipartite graphs.
512 510
- \ref PrBipartiteMatching Push-relabel algorithm
513 511
  for calculating maximum cardinality matching in bipartite graphs.
514 512
- \ref MaxWeightedBipartiteMatching
515 513
  Successive shortest path algorithm for calculating maximum weighted
516 514
  matching and maximum weighted bipartite matching in bipartite graphs.
517 515
- \ref MinCostMaxBipartiteMatching
518 516
  Successive shortest path algorithm for calculating minimum cost maximum
519 517
  matching in bipartite graphs.
520 518
- \ref MaxMatching Edmond's blossom shrinking algorithm for calculating
521 519
  maximum cardinality matching in general graphs.
522 520
- \ref MaxWeightedMatching Edmond's blossom shrinking algorithm for calculating
523 521
  maximum weighted matching in general graphs.
524 522
- \ref MaxWeightedPerfectMatching
525 523
  Edmond's blossom shrinking algorithm for calculating maximum weighted
526 524
  perfect matching in general graphs.
527 525

	
528 526
\image html bipartite_matching.png
529 527
\image latex bipartite_matching.eps "Bipartite Matching" width=\textwidth
530 528
*/
531 529

	
532 530
/**
533 531
@defgroup graph_properties Connectivity and Other Graph Properties
534 532
@ingroup algs
535 533
\brief Algorithms for discovering the graph properties
536 534

	
537 535
This group contains the algorithms for discovering the graph properties
538 536
like connectivity, bipartiteness, euler property, simplicity etc.
539 537

	
540 538
\image html connected_components.png
541 539
\image latex connected_components.eps "Connected components" width=\textwidth
542 540
*/
543 541

	
544 542
/**
545 543
@defgroup planar Planarity Embedding and Drawing
546 544
@ingroup algs
547 545
\brief Algorithms for planarity checking, embedding and drawing
548 546

	
549 547
This group contains the algorithms for planarity checking,
550 548
embedding and drawing.
551 549

	
552 550
\image html planar.png
553 551
\image latex planar.eps "Plane graph" width=\textwidth
554 552
*/
555 553

	
556 554
/**
557 555
@defgroup approx Approximation Algorithms
558 556
@ingroup algs
559 557
\brief Approximation algorithms.
560 558

	
561 559
This group contains the approximation and heuristic algorithms
562 560
implemented in LEMON.
563 561
*/
564 562

	
565 563
/**
566 564
@defgroup auxalg Auxiliary Algorithms
567 565
@ingroup algs
568 566
\brief Auxiliary algorithms implemented in LEMON.
569 567

	
570 568
This group contains some algorithms implemented in LEMON
571 569
in order to make it easier to implement complex algorithms.
572 570
*/
573 571

	
574 572
/**
575 573
@defgroup gen_opt_group General Optimization Tools
576 574
\brief This group contains some general optimization frameworks
577 575
implemented in LEMON.
578 576

	
579 577
This group contains some general optimization frameworks
580 578
implemented in LEMON.
581 579
*/
582 580

	
583 581
/**
584 582
@defgroup lp_group LP and MIP Solvers
585 583
@ingroup gen_opt_group
586 584
\brief LP and MIP solver interfaces for LEMON.
587 585

	
588 586
This group contains LP and MIP solver interfaces for LEMON.
589 587
Various LP solvers could be used in the same manner with this
590 588
high-level interface.
591 589

	
592 590
The currently supported solvers are \ref glpk, \ref clp, \ref cbc,
593 591
\ref cplex, \ref soplex.
594 592
*/
595 593

	
596 594
/**
597 595
@defgroup lp_utils Tools for Lp and Mip Solvers
598 596
@ingroup lp_group
599 597
\brief Helper tools to the Lp and Mip solvers.
600 598

	
601 599
This group adds some helper tools to general optimization framework
602 600
implemented in LEMON.
603 601
*/
604 602

	
605 603
/**
606 604
@defgroup metah Metaheuristics
607 605
@ingroup gen_opt_group
608 606
\brief Metaheuristics for LEMON library.
609 607

	
Ignore white space 6 line context
1 1
/* -*- C++ -*-
2 2
 *
3 3
 * This file is a part of LEMON, a generic C++ optimization library
4 4
 *
5 5
 * Copyright (C) 2003-2008
6 6
 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
7 7
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
8 8
 *
9 9
 * Permission to use, modify and distribute this software is granted
10 10
 * provided that this copyright notice appears in all copies. For
11 11
 * precise terms see the accompanying LICENSE file.
12 12
 *
13 13
 * This software is provided "AS IS" with no warranty of any kind,
14 14
 * express or implied, and with no claim as to its suitability for any
15 15
 * purpose.
16 16
 *
17 17
 */
18 18

	
19 19
#ifndef LEMON_CAPACITY_SCALING_H
20 20
#define LEMON_CAPACITY_SCALING_H
21 21

	
22 22
/// \ingroup min_cost_flow_algs
23 23
///
24 24
/// \file
25 25
/// \brief Capacity Scaling algorithm for finding a minimum cost flow.
26 26

	
27 27
#include <vector>
28 28
#include <limits>
29 29
#include <lemon/core.h>
30 30
#include <lemon/bin_heap.h>
31 31

	
32 32
namespace lemon {
33 33

	
34 34
  /// \brief Default traits class of CapacityScaling algorithm.
35 35
  ///
36 36
  /// Default traits class of CapacityScaling algorithm.
37 37
  /// \tparam GR Digraph type.
38 38
  /// \tparam V The number type used for flow amounts, capacity bounds
39 39
  /// and supply values. By default it is \c int.
40 40
  /// \tparam C The number type used for costs and potentials.
41 41
  /// By default it is the same as \c V.
42 42
  template <typename GR, typename V = int, typename C = V>
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
  /// \ref min_cost_flow "minimum cost flow". It is an efficient dual
69
  /// \ref min_cost_flow "minimum cost flow" \ref amo93networkflows,
70
  /// \ref edmondskarp72theoretical. It is an efficient dual
70 71
  /// solution method.
71 72
  ///
72 73
  /// Most of the parameters of the problem (except for the digraph)
73 74
  /// can be given using separate functions, and the algorithm can be
74 75
  /// executed using the \ref run() function. If some parameters are not
75 76
  /// specified, then default values will be used.
76 77
  ///
77 78
  /// \tparam GR The digraph type the algorithm runs on.
78 79
  /// \tparam V The number type used for flow amounts, capacity bounds
79 80
  /// and supply values in the algorithm. By default it is \c int.
80 81
  /// \tparam C The number type used for costs and potentials in the
81 82
  /// algorithm. By default it is the same as \c V.
82 83
  ///
83 84
  /// \warning Both number types must be signed and all input data must
84 85
  /// be integer.
85 86
  /// \warning This algorithm does not support negative costs for such
86 87
  /// arcs that have infinite upper bound.
87 88
#ifdef DOXYGEN
88 89
  template <typename GR, typename V, typename C, typename TR>
89 90
#else
90 91
  template < typename GR, typename V = int, typename C = V,
91 92
             typename TR = CapacityScalingDefaultTraits<GR, V, C> >
92 93
#endif
93 94
  class CapacityScaling
94 95
  {
95 96
  public:
96 97

	
97 98
    /// The type of the digraph
98 99
    typedef typename TR::Digraph Digraph;
99 100
    /// The type of the flow amounts, capacity bounds and supply values
100 101
    typedef typename TR::Value Value;
101 102
    /// The type of the arc costs
102 103
    typedef typename TR::Cost Cost;
103 104

	
104 105
    /// The type of the heap used for internal Dijkstra computations
105 106
    typedef typename TR::Heap Heap;
106 107

	
107 108
    /// The \ref CapacityScalingDefaultTraits "traits class" of the algorithm
108 109
    typedef TR Traits;
109 110

	
110 111
  public:
111 112

	
112 113
    /// \brief Problem type constants for the \c run() function.
113 114
    ///
114 115
    /// Enum type containing the problem type constants that can be
115 116
    /// returned by the \ref run() function of the algorithm.
116 117
    enum ProblemType {
117 118
      /// The problem has no feasible solution (flow).
118 119
      INFEASIBLE,
119 120
      /// The problem has optimal solution (i.e. it is feasible and
120 121
      /// bounded), and the algorithm has found optimal flow and node
121 122
      /// potentials (primal and dual solutions).
122 123
      OPTIMAL,
123 124
      /// The digraph contains an arc of negative cost and infinite
124 125
      /// upper bound. It means that the objective function is unbounded
125 126
      /// on that arc, however, note that it could actually be bounded
126 127
      /// over the feasible flows, but this algroithm cannot handle
127 128
      /// these cases.
128 129
      UNBOUNDED
129 130
    };
130 131
  
131 132
  private:
132 133

	
133 134
    TEMPLATE_DIGRAPH_TYPEDEFS(GR);
134 135

	
135 136
    typedef std::vector<int> IntVector;
136 137
    typedef std::vector<char> BoolVector;
137 138
    typedef std::vector<Value> ValueVector;
138 139
    typedef std::vector<Cost> CostVector;
139 140

	
140 141
  private:
141 142

	
142 143
    // Data related to the underlying digraph
143 144
    const GR &_graph;
144 145
    int _node_num;
145 146
    int _arc_num;
146 147
    int _res_arc_num;
147 148
    int _root;
148 149

	
149 150
    // Parameters of the problem
150 151
    bool _have_lower;
151 152
    Value _sum_supply;
152 153

	
153 154
    // Data structures for storing the digraph
154 155
    IntNodeMap _node_id;
155 156
    IntArcMap _arc_idf;
156 157
    IntArcMap _arc_idb;
157 158
    IntVector _first_out;
158 159
    BoolVector _forward;
159 160
    IntVector _source;
160 161
    IntVector _target;
161 162
    IntVector _reverse;
162 163

	
163 164
    // Node and arc data
164 165
    ValueVector _lower;
165 166
    ValueVector _upper;
166 167
    CostVector _cost;
167 168
    ValueVector _supply;
168 169

	
169 170
    ValueVector _res_cap;
170 171
    CostVector _pi;
171 172
    ValueVector _excess;
172 173
    IntVector _excess_nodes;
173 174
    IntVector _deficit_nodes;
174 175

	
175 176
    Value _delta;
176 177
    int _factor;
177 178
    IntVector _pred;
178 179

	
179 180
  public:
180 181
  
181 182
    /// \brief Constant for infinite upper bounds (capacities).
182 183
    ///
183 184
    /// Constant for infinite upper bounds (capacities).
184 185
    /// It is \c std::numeric_limits<Value>::infinity() if available,
185 186
    /// \c std::numeric_limits<Value>::max() otherwise.
186 187
    const Value INF;
187 188

	
188 189
  private:
189 190

	
190 191
    // Special implementation of the Dijkstra algorithm for finding
191 192
    // shortest paths in the residual network of the digraph with
192 193
    // respect to the reduced arc costs and modifying the node
193 194
    // potentials according to the found distance labels.
194 195
    class ResidualDijkstra
195 196
    {
196 197
    private:
197 198

	
198 199
      int _node_num;
199 200
      bool _geq;
200 201
      const IntVector &_first_out;
201 202
      const IntVector &_target;
202 203
      const CostVector &_cost;
203 204
      const ValueVector &_res_cap;
204 205
      const ValueVector &_excess;
205 206
      CostVector &_pi;
206 207
      IntVector &_pred;
207 208
      
208 209
      IntVector _proc_nodes;
209 210
      CostVector _dist;
210 211
      
211 212
    public:
212 213

	
213 214
      ResidualDijkstra(CapacityScaling& cs) :
214 215
        _node_num(cs._node_num), _geq(cs._sum_supply < 0),
215 216
        _first_out(cs._first_out), _target(cs._target), _cost(cs._cost),
216 217
        _res_cap(cs._res_cap), _excess(cs._excess), _pi(cs._pi),
217 218
        _pred(cs._pred), _dist(cs._node_num)
218 219
      {}
219 220

	
220 221
      int run(int s, Value delta = 1) {
221 222
        RangeMap<int> heap_cross_ref(_node_num, Heap::PRE_HEAP);
222 223
        Heap heap(heap_cross_ref);
223 224
        heap.push(s, 0);
224 225
        _pred[s] = -1;
225 226
        _proc_nodes.clear();
226 227

	
227 228
        // Process nodes
228 229
        while (!heap.empty() && _excess[heap.top()] > -delta) {
229 230
          int u = heap.top(), v;
230 231
          Cost d = heap.prio() + _pi[u], dn;
231 232
          _dist[u] = heap.prio();
232 233
          _proc_nodes.push_back(u);
233 234
          heap.pop();
234 235

	
235 236
          // Traverse outgoing residual arcs
236 237
          int last_out = _geq ? _first_out[u+1] : _first_out[u+1] - 1;
237 238
          for (int a = _first_out[u]; a != last_out; ++a) {
238 239
            if (_res_cap[a] < delta) continue;
239 240
            v = _target[a];
240 241
            switch (heap.state(v)) {
241 242
              case Heap::PRE_HEAP:
242 243
                heap.push(v, d + _cost[a] - _pi[v]);
243 244
                _pred[v] = a;
244 245
                break;
245 246
              case Heap::IN_HEAP:
246 247
                dn = d + _cost[a] - _pi[v];
247 248
                if (dn < heap[v]) {
248 249
                  heap.decrease(v, dn);
249 250
                  _pred[v] = a;
250 251
                }
251 252
                break;
252 253
              case Heap::POST_HEAP:
253 254
                break;
254 255
            }
255 256
          }
256 257
        }
257 258
        if (heap.empty()) return -1;
258 259

	
259 260
        // Update potentials of processed nodes
260 261
        int t = heap.top();
261 262
        Cost dt = heap.prio();
Ignore white space 384 line context
1 1
/* -*- C++ -*-
2 2
 *
3 3
 * This file is a part of LEMON, a generic C++ optimization library
4 4
 *
5 5
 * Copyright (C) 2003-2008
6 6
 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
7 7
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
8 8
 *
9 9
 * Permission to use, modify and distribute this software is granted
10 10
 * provided that this copyright notice appears in all copies. For
11 11
 * precise terms see the accompanying LICENSE file.
12 12
 *
13 13
 * This software is provided "AS IS" with no warranty of any kind,
14 14
 * express or implied, and with no claim as to its suitability for any
15 15
 * purpose.
16 16
 *
17 17
 */
18 18

	
19 19
#ifndef LEMON_COST_SCALING_H
20 20
#define LEMON_COST_SCALING_H
21 21

	
22 22
/// \ingroup min_cost_flow_algs
23 23
/// \file
24 24
/// \brief Cost scaling algorithm for finding a minimum cost flow.
25 25

	
26 26
#include <vector>
27 27
#include <deque>
28 28
#include <limits>
29 29

	
30 30
#include <lemon/core.h>
31 31
#include <lemon/maps.h>
32 32
#include <lemon/math.h>
33 33
#include <lemon/static_graph.h>
34 34
#include <lemon/circulation.h>
35 35
#include <lemon/bellman_ford.h>
36 36

	
37 37
namespace lemon {
38 38

	
39 39
  /// \brief Default traits class of CostScaling algorithm.
40 40
  ///
41 41
  /// Default traits class of CostScaling algorithm.
42 42
  /// \tparam GR Digraph type.
43 43
  /// \tparam V The number type used for flow amounts, capacity bounds
44 44
  /// and supply values. By default it is \c int.
45 45
  /// \tparam C The number type used for costs and potentials.
46 46
  /// By default it is the same as \c V.
47 47
#ifdef DOXYGEN
48 48
  template <typename GR, typename V = int, typename C = V>
49 49
#else
50 50
  template < typename GR, typename V = int, typename C = V,
51 51
             bool integer = std::numeric_limits<C>::is_integer >
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
  /// push/augment and relabel operations for finding a minimum cost
94
  /// flow. It is an efficient primal-dual solution method, which
93
  /// push/augment and relabel operations for finding a \ref min_cost_flow
94
  /// "minimum cost flow" \ref amo93networkflows, \ref goldberg90approximation,
95
  /// \ref goldberg97efficient, \ref bunnagel98efficient. 
96
  /// It is a highly efficient primal-dual solution method, which
95 97
  /// can be viewed as the generalization of the \ref Preflow
96 98
  /// "preflow push-relabel" algorithm for the maximum flow problem.
97 99
  ///
98 100
  /// Most of the parameters of the problem (except for the digraph)
99 101
  /// can be given using separate functions, and the algorithm can be
100 102
  /// executed using the \ref run() function. If some parameters are not
101 103
  /// specified, then default values will be used.
102 104
  ///
103 105
  /// \tparam GR The digraph type the algorithm runs on.
104 106
  /// \tparam V The number type used for flow amounts, capacity bounds
105 107
  /// and supply values in the algorithm. By default it is \c int.
106 108
  /// \tparam C The number type used for costs and potentials in the
107 109
  /// algorithm. By default it is the same as \c V.
108 110
  ///
109 111
  /// \warning Both number types must be signed and all input data must
110 112
  /// be integer.
111 113
  /// \warning This algorithm does not support negative costs for such
112 114
  /// arcs that have infinite upper bound.
113 115
  ///
114 116
  /// \note %CostScaling provides three different internal methods,
115 117
  /// from which the most efficient one is used by default.
116 118
  /// For more information, see \ref Method.
117 119
#ifdef DOXYGEN
118 120
  template <typename GR, typename V, typename C, typename TR>
119 121
#else
120 122
  template < typename GR, typename V = int, typename C = V,
121 123
             typename TR = CostScalingDefaultTraits<GR, V, C> >
122 124
#endif
123 125
  class CostScaling
124 126
  {
125 127
  public:
126 128

	
127 129
    /// The type of the digraph
128 130
    typedef typename TR::Digraph Digraph;
129 131
    /// The type of the flow amounts, capacity bounds and supply values
130 132
    typedef typename TR::Value Value;
131 133
    /// The type of the arc costs
132 134
    typedef typename TR::Cost Cost;
133 135

	
134 136
    /// \brief The large cost type
135 137
    ///
136 138
    /// The large cost type used for internal computations.
137 139
    /// Using the \ref CostScalingDefaultTraits "default traits class",
138 140
    /// it is \c long \c long if the \c Cost type is integer,
139 141
    /// otherwise it is \c double.
140 142
    typedef typename TR::LargeCost LargeCost;
141 143

	
142 144
    /// The \ref CostScalingDefaultTraits "traits class" of the algorithm
143 145
    typedef TR Traits;
144 146

	
145 147
  public:
146 148

	
147 149
    /// \brief Problem type constants for the \c run() function.
148 150
    ///
149 151
    /// Enum type containing the problem type constants that can be
150 152
    /// returned by the \ref run() function of the algorithm.
151 153
    enum ProblemType {
152 154
      /// The problem has no feasible solution (flow).
153 155
      INFEASIBLE,
154 156
      /// The problem has optimal solution (i.e. it is feasible and
155 157
      /// bounded), and the algorithm has found optimal flow and node
156 158
      /// potentials (primal and dual solutions).
157 159
      OPTIMAL,
158 160
      /// The digraph contains an arc of negative cost and infinite
159 161
      /// upper bound. It means that the objective function is unbounded
160 162
      /// on that arc, however, note that it could actually be bounded
161 163
      /// over the feasible flows, but this algroithm cannot handle
162 164
      /// these cases.
163 165
      UNBOUNDED
164 166
    };
165 167

	
166 168
    /// \brief Constants for selecting the internal method.
167 169
    ///
168 170
    /// Enum type containing constants for selecting the internal method
169 171
    /// for the \ref run() function.
170 172
    ///
171 173
    /// \ref CostScaling provides three internal methods that differ mainly
172 174
    /// in their base operations, which are used in conjunction with the
173 175
    /// relabel operation.
174 176
    /// By default, the so called \ref PARTIAL_AUGMENT
175 177
    /// "Partial Augment-Relabel" method is used, which proved to be
176 178
    /// the most efficient and the most robust on various test inputs.
177 179
    /// However, the other methods can be selected using the \ref run()
178 180
    /// function with the proper parameter.
179 181
    enum Method {
180 182
      /// Local push operations are used, i.e. flow is moved only on one
181 183
      /// admissible arc at once.
182 184
      PUSH,
183 185
      /// Augment operations are used, i.e. flow is moved on admissible
184 186
      /// paths from a node with excess to a node with deficit.
185 187
      AUGMENT,
186 188
      /// Partial augment operations are used, i.e. flow is moved on 
187 189
      /// admissible paths started from a node with excess, but the
188 190
      /// lengths of these paths are limited. This method can be viewed
189 191
      /// as a combined version of the previous two operations.
190 192
      PARTIAL_AUGMENT
191 193
    };
192 194

	
193 195
  private:
194 196

	
195 197
    TEMPLATE_DIGRAPH_TYPEDEFS(GR);
196 198

	
197 199
    typedef std::vector<int> IntVector;
198 200
    typedef std::vector<char> BoolVector;
199 201
    typedef std::vector<Value> ValueVector;
200 202
    typedef std::vector<Cost> CostVector;
201 203
    typedef std::vector<LargeCost> LargeCostVector;
202 204

	
203 205
  private:
204 206
  
205 207
    template <typename KT, typename VT>
206 208
    class VectorMap {
207 209
    public:
208 210
      typedef KT Key;
209 211
      typedef VT Value;
210 212
      
211 213
      VectorMap(std::vector<Value>& v) : _v(v) {}
212 214
      
213 215
      const Value& operator[](const Key& key) const {
214 216
        return _v[StaticDigraph::id(key)];
215 217
      }
216 218

	
217 219
      Value& operator[](const Key& key) {
218 220
        return _v[StaticDigraph::id(key)];
219 221
      }
220 222
      
221 223
      void set(const Key& key, const Value& val) {
222 224
        _v[StaticDigraph::id(key)] = val;
223 225
      }
224 226

	
225 227
    private:
226 228
      std::vector<Value>& _v;
227 229
    };
228 230

	
229 231
    typedef VectorMap<StaticDigraph::Node, LargeCost> LargeCostNodeMap;
230 232
    typedef VectorMap<StaticDigraph::Arc, LargeCost> LargeCostArcMap;
231 233

	
232 234
  private:
233 235

	
234 236
    // Data related to the underlying digraph
235 237
    const GR &_graph;
236 238
    int _node_num;
237 239
    int _arc_num;
238 240
    int _res_node_num;
239 241
    int _res_arc_num;
240 242
    int _root;
241 243

	
242 244
    // Parameters of the problem
243 245
    bool _have_lower;
244 246
    Value _sum_supply;
245 247

	
246 248
    // Data structures for storing the digraph
247 249
    IntNodeMap _node_id;
248 250
    IntArcMap _arc_idf;
249 251
    IntArcMap _arc_idb;
250 252
    IntVector _first_out;
251 253
    BoolVector _forward;
252 254
    IntVector _source;
253 255
    IntVector _target;
254 256
    IntVector _reverse;
255 257

	
256 258
    // Node and arc data
257 259
    ValueVector _lower;
258 260
    ValueVector _upper;
259 261
    CostVector _scost;
260 262
    ValueVector _supply;
261 263

	
262 264
    ValueVector _res_cap;
263 265
    LargeCostVector _cost;
264 266
    LargeCostVector _pi;
265 267
    ValueVector _excess;
266 268
    IntVector _next_out;
267 269
    std::deque<int> _active_nodes;
268 270

	
269 271
    // Data for scaling
270 272
    LargeCost _epsilon;
271 273
    int _alpha;
272 274

	
273 275
    // Data for a StaticDigraph structure
274 276
    typedef std::pair<int, int> IntPair;
275 277
    StaticDigraph _sgr;
276 278
    std::vector<IntPair> _arc_vec;
277 279
    std::vector<LargeCost> _cost_vec;
278 280
    LargeCostArcMap _cost_map;
279 281
    LargeCostNodeMap _pi_map;
280 282
  
281 283
  public:
282 284
  
283 285
    /// \brief Constant for infinite upper bounds (capacities).
284 286
    ///
285 287
    /// Constant for infinite upper bounds (capacities).
286 288
    /// It is \c std::numeric_limits<Value>::infinity() if available,
0 comments (0 inline)