0
8
0
4
4
4
4
12
5
| ... | ... |
@@ -282,217 +282,217 @@ |
| 282 | 282 |
\brief Algorithms for finding shortest paths. |
| 283 | 283 |
|
| 284 | 284 |
This group contains the algorithms for finding shortest paths in digraphs. |
| 285 | 285 |
|
| 286 | 286 |
- \ref Dijkstra algorithm for finding shortest paths from a source node |
| 287 | 287 |
when all arc lengths are non-negative. |
| 288 | 288 |
- \ref BellmanFord "Bellman-Ford" algorithm for finding shortest paths |
| 289 | 289 |
from a source node when arc lenghts can be either positive or negative, |
| 290 | 290 |
but the digraph should not contain directed cycles with negative total |
| 291 | 291 |
length. |
| 292 | 292 |
- \ref FloydWarshall "Floyd-Warshall" and \ref Johnson "Johnson" algorithms |
| 293 | 293 |
for solving the \e all-pairs \e shortest \e paths \e problem when arc |
| 294 | 294 |
lenghts can be either positive or negative, but the digraph should |
| 295 | 295 |
not contain directed cycles with negative total length. |
| 296 | 296 |
- \ref Suurballe A successive shortest path algorithm for finding |
| 297 | 297 |
arc-disjoint paths between two nodes having minimum total length. |
| 298 | 298 |
*/ |
| 299 | 299 |
|
| 300 | 300 |
/** |
| 301 | 301 |
@defgroup max_flow Maximum Flow Algorithms |
| 302 | 302 |
@ingroup algs |
| 303 | 303 |
\brief Algorithms for finding maximum flows. |
| 304 | 304 |
|
| 305 | 305 |
This group contains the algorithms for finding maximum flows and |
| 306 | 306 |
feasible circulations. |
| 307 | 307 |
|
| 308 | 308 |
The \e maximum \e flow \e problem is to find a flow of maximum value between |
| 309 | 309 |
a single source and a single target. Formally, there is a \f$G=(V,A)\f$ |
| 310 | 310 |
digraph, a \f$cap: A\rightarrow\mathbf{R}^+_0\f$ capacity function and
|
| 311 | 311 |
\f$s, t \in V\f$ source and target nodes. |
| 312 | 312 |
A maximum flow is an \f$f: A\rightarrow\mathbf{R}^+_0\f$ solution of the
|
| 313 | 313 |
following optimization problem. |
| 314 | 314 |
|
| 315 | 315 |
\f[ \max\sum_{sv\in A} f(sv) - \sum_{vs\in A} f(vs) \f]
|
| 316 | 316 |
\f[ \sum_{uv\in A} f(uv) = \sum_{vu\in A} f(vu)
|
| 317 | 317 |
\quad \forall u\in V\setminus\{s,t\} \f]
|
| 318 | 318 |
\f[ 0 \leq f(uv) \leq cap(uv) \quad \forall uv\in A \f] |
| 319 | 319 |
|
| 320 | 320 |
LEMON contains several algorithms for solving maximum flow problems: |
| 321 | 321 |
- \ref EdmondsKarp Edmonds-Karp algorithm. |
| 322 | 322 |
- \ref Preflow Goldberg-Tarjan's preflow push-relabel algorithm. |
| 323 | 323 |
- \ref DinitzSleatorTarjan Dinitz's blocking flow algorithm with dynamic trees. |
| 324 | 324 |
- \ref GoldbergTarjan Preflow push-relabel algorithm with dynamic trees. |
| 325 | 325 |
|
| 326 | 326 |
In most cases the \ref Preflow "Preflow" algorithm provides the |
| 327 | 327 |
fastest method for computing a maximum flow. All implementations |
| 328 | 328 |
also provide functions to query the minimum cut, which is the dual |
| 329 | 329 |
problem of maximum flow. |
| 330 | 330 |
|
| 331 | 331 |
\ref Circulation is a preflow push-relabel algorithm implemented directly |
| 332 | 332 |
for finding feasible circulations, which is a somewhat different problem, |
| 333 | 333 |
but it is strongly related to maximum flow. |
| 334 | 334 |
For more information, see \ref Circulation. |
| 335 | 335 |
*/ |
| 336 | 336 |
|
| 337 | 337 |
/** |
| 338 | 338 |
@defgroup min_cost_flow_algs Minimum Cost Flow Algorithms |
| 339 | 339 |
@ingroup algs |
| 340 | 340 |
|
| 341 | 341 |
\brief Algorithms for finding minimum cost flows and circulations. |
| 342 | 342 |
|
| 343 | 343 |
This group contains the algorithms for finding minimum cost flows and |
| 344 | 344 |
circulations. For more information about this problem and its dual |
| 345 | 345 |
solution see \ref min_cost_flow "Minimum Cost Flow Problem". |
| 346 | 346 |
|
| 347 | 347 |
LEMON contains several algorithms for this problem. |
| 348 | 348 |
- \ref NetworkSimplex Primal Network Simplex algorithm with various |
| 349 | 349 |
pivot strategies. |
| 350 | 350 |
- \ref CostScaling Push-Relabel and Augment-Relabel algorithms based on |
| 351 | 351 |
cost scaling. |
| 352 | 352 |
- \ref CapacityScaling Successive Shortest %Path algorithm with optional |
| 353 | 353 |
capacity scaling. |
| 354 | 354 |
- \ref CancelAndTighten The Cancel and Tighten algorithm. |
| 355 | 355 |
- \ref CycleCanceling Cycle-Canceling algorithms. |
| 356 | 356 |
|
| 357 | 357 |
In general NetworkSimplex is the most efficient implementation, |
| 358 | 358 |
but in special cases other algorithms could be faster. |
| 359 | 359 |
For example, if the total supply and/or capacities are rather small, |
| 360 | 360 |
CapacityScaling is usually the fastest algorithm (without effective scaling). |
| 361 | 361 |
*/ |
| 362 | 362 |
|
| 363 | 363 |
/** |
| 364 | 364 |
@defgroup min_cut Minimum Cut Algorithms |
| 365 | 365 |
@ingroup algs |
| 366 | 366 |
|
| 367 | 367 |
\brief Algorithms for finding minimum cut in graphs. |
| 368 | 368 |
|
| 369 | 369 |
This group contains the algorithms for finding minimum cut in graphs. |
| 370 | 370 |
|
| 371 | 371 |
The \e minimum \e cut \e problem is to find a non-empty and non-complete |
| 372 | 372 |
\f$X\f$ subset of the nodes with minimum overall capacity on |
| 373 | 373 |
outgoing arcs. Formally, there is a \f$G=(V,A)\f$ digraph, a |
| 374 | 374 |
\f$cap: A\rightarrow\mathbf{R}^+_0\f$ capacity function. The minimum
|
| 375 | 375 |
cut is the \f$X\f$ solution of the next optimization problem: |
| 376 | 376 |
|
| 377 | 377 |
\f[ \min_{X \subset V, X\not\in \{\emptyset, V\}}
|
| 378 |
\sum_{uv\in A
|
|
| 378 |
\sum_{uv\in A: u\in X, v\not\in X}cap(uv) \f]
|
|
| 379 | 379 |
|
| 380 | 380 |
LEMON contains several algorithms related to minimum cut problems: |
| 381 | 381 |
|
| 382 | 382 |
- \ref HaoOrlin "Hao-Orlin algorithm" for calculating minimum cut |
| 383 | 383 |
in directed graphs. |
| 384 | 384 |
- \ref NagamochiIbaraki "Nagamochi-Ibaraki algorithm" for |
| 385 | 385 |
calculating minimum cut in undirected graphs. |
| 386 | 386 |
- \ref GomoryHu "Gomory-Hu tree computation" for calculating |
| 387 | 387 |
all-pairs minimum cut in undirected graphs. |
| 388 | 388 |
|
| 389 | 389 |
If you want to find minimum cut just between two distinict nodes, |
| 390 | 390 |
see the \ref max_flow "maximum flow problem". |
| 391 | 391 |
*/ |
| 392 | 392 |
|
| 393 | 393 |
/** |
| 394 | 394 |
@defgroup graph_properties Connectivity and Other Graph Properties |
| 395 | 395 |
@ingroup algs |
| 396 | 396 |
\brief Algorithms for discovering the graph properties |
| 397 | 397 |
|
| 398 | 398 |
This group contains the algorithms for discovering the graph properties |
| 399 | 399 |
like connectivity, bipartiteness, euler property, simplicity etc. |
| 400 | 400 |
|
| 401 |
\image html edge_biconnected_components.png |
|
| 402 |
\image latex edge_biconnected_components.eps "bi-edge-connected components" width=\textwidth |
|
| 401 |
\image html connected_components.png |
|
| 402 |
\image latex connected_components.eps "Connected components" width=\textwidth |
|
| 403 | 403 |
*/ |
| 404 | 404 |
|
| 405 | 405 |
/** |
| 406 | 406 |
@defgroup planar Planarity Embedding and Drawing |
| 407 | 407 |
@ingroup algs |
| 408 | 408 |
\brief Algorithms for planarity checking, embedding and drawing |
| 409 | 409 |
|
| 410 | 410 |
This group contains the algorithms for planarity checking, |
| 411 | 411 |
embedding and drawing. |
| 412 | 412 |
|
| 413 | 413 |
\image html planar.png |
| 414 | 414 |
\image latex planar.eps "Plane graph" width=\textwidth |
| 415 | 415 |
*/ |
| 416 | 416 |
|
| 417 | 417 |
/** |
| 418 | 418 |
@defgroup matching Matching Algorithms |
| 419 | 419 |
@ingroup algs |
| 420 | 420 |
\brief Algorithms for finding matchings in graphs and bipartite graphs. |
| 421 | 421 |
|
| 422 | 422 |
This group contains the algorithms for calculating |
| 423 | 423 |
matchings in graphs and bipartite graphs. The general matching problem is |
| 424 | 424 |
finding a subset of the edges for which each node has at most one incident |
| 425 | 425 |
edge. |
| 426 | 426 |
|
| 427 | 427 |
There are several different algorithms for calculate matchings in |
| 428 | 428 |
graphs. The matching problems in bipartite graphs are generally |
| 429 | 429 |
easier than in general graphs. The goal of the matching optimization |
| 430 | 430 |
can be finding maximum cardinality, maximum weight or minimum cost |
| 431 | 431 |
matching. The search can be constrained to find perfect or |
| 432 | 432 |
maximum cardinality matching. |
| 433 | 433 |
|
| 434 | 434 |
The matching algorithms implemented in LEMON: |
| 435 | 435 |
- \ref MaxBipartiteMatching Hopcroft-Karp augmenting path algorithm |
| 436 | 436 |
for calculating maximum cardinality matching in bipartite graphs. |
| 437 | 437 |
- \ref PrBipartiteMatching Push-relabel algorithm |
| 438 | 438 |
for calculating maximum cardinality matching in bipartite graphs. |
| 439 | 439 |
- \ref MaxWeightedBipartiteMatching |
| 440 | 440 |
Successive shortest path algorithm for calculating maximum weighted |
| 441 | 441 |
matching and maximum weighted bipartite matching in bipartite graphs. |
| 442 | 442 |
- \ref MinCostMaxBipartiteMatching |
| 443 | 443 |
Successive shortest path algorithm for calculating minimum cost maximum |
| 444 | 444 |
matching in bipartite graphs. |
| 445 | 445 |
- \ref MaxMatching Edmond's blossom shrinking algorithm for calculating |
| 446 | 446 |
maximum cardinality matching in general graphs. |
| 447 | 447 |
- \ref MaxWeightedMatching Edmond's blossom shrinking algorithm for calculating |
| 448 | 448 |
maximum weighted matching in general graphs. |
| 449 | 449 |
- \ref MaxWeightedPerfectMatching |
| 450 | 450 |
Edmond's blossom shrinking algorithm for calculating maximum weighted |
| 451 | 451 |
perfect matching in general graphs. |
| 452 | 452 |
|
| 453 | 453 |
\image html bipartite_matching.png |
| 454 | 454 |
\image latex bipartite_matching.eps "Bipartite Matching" width=\textwidth |
| 455 | 455 |
*/ |
| 456 | 456 |
|
| 457 | 457 |
/** |
| 458 | 458 |
@defgroup spantree Minimum Spanning Tree Algorithms |
| 459 | 459 |
@ingroup algs |
| 460 | 460 |
\brief Algorithms for finding minimum cost spanning trees and arborescences. |
| 461 | 461 |
|
| 462 | 462 |
This group contains the algorithms for finding minimum cost spanning |
| 463 | 463 |
trees and arborescences. |
| 464 | 464 |
*/ |
| 465 | 465 |
|
| 466 | 466 |
/** |
| 467 | 467 |
@defgroup auxalg Auxiliary Algorithms |
| 468 | 468 |
@ingroup algs |
| 469 | 469 |
\brief Auxiliary algorithms implemented in LEMON. |
| 470 | 470 |
|
| 471 | 471 |
This group contains some algorithms implemented in LEMON |
| 472 | 472 |
in order to make it easier to implement complex algorithms. |
| 473 | 473 |
*/ |
| 474 | 474 |
|
| 475 | 475 |
/** |
| 476 | 476 |
@defgroup approx Approximation Algorithms |
| 477 | 477 |
@ingroup algs |
| 478 | 478 |
\brief Approximation algorithms. |
| 479 | 479 |
|
| 480 | 480 |
This group contains the approximation and heuristic algorithms |
| 481 | 481 |
implemented in LEMON. |
| 482 | 482 |
*/ |
| 483 | 483 |
|
| 484 | 484 |
/** |
| 485 | 485 |
@defgroup gen_opt_group General Optimization Tools |
| 486 | 486 |
\brief This group contains some general optimization frameworks |
| 487 | 487 |
implemented in LEMON. |
| 488 | 488 |
|
| 489 | 489 |
This group contains some general optimization frameworks |
| 490 | 490 |
implemented in LEMON. |
| 491 | 491 |
*/ |
| 492 | 492 |
|
| 493 | 493 |
/** |
| 494 | 494 |
@defgroup lp_group Lp and Mip Solvers |
| 495 | 495 |
@ingroup gen_opt_group |
| 496 | 496 |
\brief Lp and Mip solver interfaces for LEMON. |
| 497 | 497 |
|
| 498 | 498 |
This group contains Lp and Mip solver interfaces for LEMON. The |
| ... | ... |
@@ -320,194 +320,194 @@ |
| 320 | 320 |
///\param g The digraph the algorithm runs on. |
| 321 | 321 |
Bfs(const Digraph &g) : |
| 322 | 322 |
G(&g), |
| 323 | 323 |
_pred(NULL), local_pred(false), |
| 324 | 324 |
_dist(NULL), local_dist(false), |
| 325 | 325 |
_reached(NULL), local_reached(false), |
| 326 | 326 |
_processed(NULL), local_processed(false) |
| 327 | 327 |
{ }
|
| 328 | 328 |
|
| 329 | 329 |
///Destructor. |
| 330 | 330 |
~Bfs() |
| 331 | 331 |
{
|
| 332 | 332 |
if(local_pred) delete _pred; |
| 333 | 333 |
if(local_dist) delete _dist; |
| 334 | 334 |
if(local_reached) delete _reached; |
| 335 | 335 |
if(local_processed) delete _processed; |
| 336 | 336 |
} |
| 337 | 337 |
|
| 338 | 338 |
///Sets the map that stores the predecessor arcs. |
| 339 | 339 |
|
| 340 | 340 |
///Sets the map that stores the predecessor arcs. |
| 341 | 341 |
///If you don't use this function before calling \ref run(Node) "run()" |
| 342 | 342 |
///or \ref init(), an instance will be allocated automatically. |
| 343 | 343 |
///The destructor deallocates this automatically allocated map, |
| 344 | 344 |
///of course. |
| 345 | 345 |
///\return <tt> (*this) </tt> |
| 346 | 346 |
Bfs &predMap(PredMap &m) |
| 347 | 347 |
{
|
| 348 | 348 |
if(local_pred) {
|
| 349 | 349 |
delete _pred; |
| 350 | 350 |
local_pred=false; |
| 351 | 351 |
} |
| 352 | 352 |
_pred = &m; |
| 353 | 353 |
return *this; |
| 354 | 354 |
} |
| 355 | 355 |
|
| 356 | 356 |
///Sets the map that indicates which nodes are reached. |
| 357 | 357 |
|
| 358 | 358 |
///Sets the map that indicates which nodes are reached. |
| 359 | 359 |
///If you don't use this function before calling \ref run(Node) "run()" |
| 360 | 360 |
///or \ref init(), an instance will be allocated automatically. |
| 361 | 361 |
///The destructor deallocates this automatically allocated map, |
| 362 | 362 |
///of course. |
| 363 | 363 |
///\return <tt> (*this) </tt> |
| 364 | 364 |
Bfs &reachedMap(ReachedMap &m) |
| 365 | 365 |
{
|
| 366 | 366 |
if(local_reached) {
|
| 367 | 367 |
delete _reached; |
| 368 | 368 |
local_reached=false; |
| 369 | 369 |
} |
| 370 | 370 |
_reached = &m; |
| 371 | 371 |
return *this; |
| 372 | 372 |
} |
| 373 | 373 |
|
| 374 | 374 |
///Sets the map that indicates which nodes are processed. |
| 375 | 375 |
|
| 376 | 376 |
///Sets the map that indicates which nodes are processed. |
| 377 | 377 |
///If you don't use this function before calling \ref run(Node) "run()" |
| 378 | 378 |
///or \ref init(), an instance will be allocated automatically. |
| 379 | 379 |
///The destructor deallocates this automatically allocated map, |
| 380 | 380 |
///of course. |
| 381 | 381 |
///\return <tt> (*this) </tt> |
| 382 | 382 |
Bfs &processedMap(ProcessedMap &m) |
| 383 | 383 |
{
|
| 384 | 384 |
if(local_processed) {
|
| 385 | 385 |
delete _processed; |
| 386 | 386 |
local_processed=false; |
| 387 | 387 |
} |
| 388 | 388 |
_processed = &m; |
| 389 | 389 |
return *this; |
| 390 | 390 |
} |
| 391 | 391 |
|
| 392 | 392 |
///Sets the map that stores the distances of the nodes. |
| 393 | 393 |
|
| 394 | 394 |
///Sets the map that stores the distances of the nodes calculated by |
| 395 | 395 |
///the algorithm. |
| 396 | 396 |
///If you don't use this function before calling \ref run(Node) "run()" |
| 397 | 397 |
///or \ref init(), an instance will be allocated automatically. |
| 398 | 398 |
///The destructor deallocates this automatically allocated map, |
| 399 | 399 |
///of course. |
| 400 | 400 |
///\return <tt> (*this) </tt> |
| 401 | 401 |
Bfs &distMap(DistMap &m) |
| 402 | 402 |
{
|
| 403 | 403 |
if(local_dist) {
|
| 404 | 404 |
delete _dist; |
| 405 | 405 |
local_dist=false; |
| 406 | 406 |
} |
| 407 | 407 |
_dist = &m; |
| 408 | 408 |
return *this; |
| 409 | 409 |
} |
| 410 | 410 |
|
| 411 | 411 |
public: |
| 412 | 412 |
|
| 413 | 413 |
///\name Execution Control |
| 414 | 414 |
///The simplest way to execute the BFS algorithm is to use one of the |
| 415 | 415 |
///member functions called \ref run(Node) "run()".\n |
| 416 |
///If you need more control on the execution, first you have to call |
|
| 417 |
///\ref init(), then you can add several source nodes with |
|
| 416 |
///If you need better control on the execution, you have to call |
|
| 417 |
///\ref init() first, then you can add several source nodes with |
|
| 418 | 418 |
///\ref addSource(). Finally the actual path computation can be |
| 419 | 419 |
///performed with one of the \ref start() functions. |
| 420 | 420 |
|
| 421 | 421 |
///@{
|
| 422 | 422 |
|
| 423 | 423 |
///\brief Initializes the internal data structures. |
| 424 | 424 |
/// |
| 425 | 425 |
///Initializes the internal data structures. |
| 426 | 426 |
void init() |
| 427 | 427 |
{
|
| 428 | 428 |
create_maps(); |
| 429 | 429 |
_queue.resize(countNodes(*G)); |
| 430 | 430 |
_queue_head=_queue_tail=0; |
| 431 | 431 |
_curr_dist=1; |
| 432 | 432 |
for ( NodeIt u(*G) ; u!=INVALID ; ++u ) {
|
| 433 | 433 |
_pred->set(u,INVALID); |
| 434 | 434 |
_reached->set(u,false); |
| 435 | 435 |
_processed->set(u,false); |
| 436 | 436 |
} |
| 437 | 437 |
} |
| 438 | 438 |
|
| 439 | 439 |
///Adds a new source node. |
| 440 | 440 |
|
| 441 | 441 |
///Adds a new source node to the set of nodes to be processed. |
| 442 | 442 |
/// |
| 443 | 443 |
void addSource(Node s) |
| 444 | 444 |
{
|
| 445 | 445 |
if(!(*_reached)[s]) |
| 446 | 446 |
{
|
| 447 | 447 |
_reached->set(s,true); |
| 448 | 448 |
_pred->set(s,INVALID); |
| 449 | 449 |
_dist->set(s,0); |
| 450 | 450 |
_queue[_queue_head++]=s; |
| 451 | 451 |
_queue_next_dist=_queue_head; |
| 452 | 452 |
} |
| 453 | 453 |
} |
| 454 | 454 |
|
| 455 | 455 |
///Processes the next node. |
| 456 | 456 |
|
| 457 | 457 |
///Processes the next node. |
| 458 | 458 |
/// |
| 459 | 459 |
///\return The processed node. |
| 460 | 460 |
/// |
| 461 | 461 |
///\pre The queue must not be empty. |
| 462 | 462 |
Node processNextNode() |
| 463 | 463 |
{
|
| 464 | 464 |
if(_queue_tail==_queue_next_dist) {
|
| 465 | 465 |
_curr_dist++; |
| 466 | 466 |
_queue_next_dist=_queue_head; |
| 467 | 467 |
} |
| 468 | 468 |
Node n=_queue[_queue_tail++]; |
| 469 | 469 |
_processed->set(n,true); |
| 470 | 470 |
Node m; |
| 471 | 471 |
for(OutArcIt e(*G,n);e!=INVALID;++e) |
| 472 | 472 |
if(!(*_reached)[m=G->target(e)]) {
|
| 473 | 473 |
_queue[_queue_head++]=m; |
| 474 | 474 |
_reached->set(m,true); |
| 475 | 475 |
_pred->set(m,e); |
| 476 | 476 |
_dist->set(m,_curr_dist); |
| 477 | 477 |
} |
| 478 | 478 |
return n; |
| 479 | 479 |
} |
| 480 | 480 |
|
| 481 | 481 |
///Processes the next node. |
| 482 | 482 |
|
| 483 | 483 |
///Processes the next node and checks if the given target node |
| 484 | 484 |
///is reached. If the target node is reachable from the processed |
| 485 | 485 |
///node, then the \c reach parameter will be set to \c true. |
| 486 | 486 |
/// |
| 487 | 487 |
///\param target The target node. |
| 488 | 488 |
///\retval reach Indicates if the target node is reached. |
| 489 | 489 |
///It should be initially \c false. |
| 490 | 490 |
/// |
| 491 | 491 |
///\return The processed node. |
| 492 | 492 |
/// |
| 493 | 493 |
///\pre The queue must not be empty. |
| 494 | 494 |
Node processNextNode(Node target, bool& reach) |
| 495 | 495 |
{
|
| 496 | 496 |
if(_queue_tail==_queue_next_dist) {
|
| 497 | 497 |
_curr_dist++; |
| 498 | 498 |
_queue_next_dist=_queue_head; |
| 499 | 499 |
} |
| 500 | 500 |
Node n=_queue[_queue_tail++]; |
| 501 | 501 |
_processed->set(n,true); |
| 502 | 502 |
Node m; |
| 503 | 503 |
for(OutArcIt e(*G,n);e!=INVALID;++e) |
| 504 | 504 |
if(!(*_reached)[m=G->target(e)]) {
|
| 505 | 505 |
_queue[_queue_head++]=m; |
| 506 | 506 |
_reached->set(m,true); |
| 507 | 507 |
_pred->set(m,e); |
| 508 | 508 |
_dist->set(m,_curr_dist); |
| 509 | 509 |
reach = reach || (target == m); |
| 510 | 510 |
} |
| 511 | 511 |
return n; |
| 512 | 512 |
} |
| 513 | 513 |
|
| ... | ... |
@@ -1332,194 +1332,194 @@ |
| 1332 | 1332 |
private: |
| 1333 | 1333 |
|
| 1334 | 1334 |
typedef typename Digraph::Node Node; |
| 1335 | 1335 |
typedef typename Digraph::NodeIt NodeIt; |
| 1336 | 1336 |
typedef typename Digraph::Arc Arc; |
| 1337 | 1337 |
typedef typename Digraph::OutArcIt OutArcIt; |
| 1338 | 1338 |
|
| 1339 | 1339 |
//Pointer to the underlying digraph. |
| 1340 | 1340 |
const Digraph *_digraph; |
| 1341 | 1341 |
//Pointer to the visitor object. |
| 1342 | 1342 |
Visitor *_visitor; |
| 1343 | 1343 |
//Pointer to the map of reached status of the nodes. |
| 1344 | 1344 |
ReachedMap *_reached; |
| 1345 | 1345 |
//Indicates if _reached is locally allocated (true) or not. |
| 1346 | 1346 |
bool local_reached; |
| 1347 | 1347 |
|
| 1348 | 1348 |
std::vector<typename Digraph::Node> _list; |
| 1349 | 1349 |
int _list_front, _list_back; |
| 1350 | 1350 |
|
| 1351 | 1351 |
//Creates the maps if necessary. |
| 1352 | 1352 |
void create_maps() {
|
| 1353 | 1353 |
if(!_reached) {
|
| 1354 | 1354 |
local_reached = true; |
| 1355 | 1355 |
_reached = Traits::createReachedMap(*_digraph); |
| 1356 | 1356 |
} |
| 1357 | 1357 |
} |
| 1358 | 1358 |
|
| 1359 | 1359 |
protected: |
| 1360 | 1360 |
|
| 1361 | 1361 |
BfsVisit() {}
|
| 1362 | 1362 |
|
| 1363 | 1363 |
public: |
| 1364 | 1364 |
|
| 1365 | 1365 |
typedef BfsVisit Create; |
| 1366 | 1366 |
|
| 1367 | 1367 |
/// \name Named Template Parameters |
| 1368 | 1368 |
|
| 1369 | 1369 |
///@{
|
| 1370 | 1370 |
template <class T> |
| 1371 | 1371 |
struct SetReachedMapTraits : public Traits {
|
| 1372 | 1372 |
typedef T ReachedMap; |
| 1373 | 1373 |
static ReachedMap *createReachedMap(const Digraph &digraph) {
|
| 1374 | 1374 |
LEMON_ASSERT(false, "ReachedMap is not initialized"); |
| 1375 | 1375 |
return 0; // ignore warnings |
| 1376 | 1376 |
} |
| 1377 | 1377 |
}; |
| 1378 | 1378 |
/// \brief \ref named-templ-param "Named parameter" for setting |
| 1379 | 1379 |
/// ReachedMap type. |
| 1380 | 1380 |
/// |
| 1381 | 1381 |
/// \ref named-templ-param "Named parameter" for setting ReachedMap type. |
| 1382 | 1382 |
template <class T> |
| 1383 | 1383 |
struct SetReachedMap : public BfsVisit< Digraph, Visitor, |
| 1384 | 1384 |
SetReachedMapTraits<T> > {
|
| 1385 | 1385 |
typedef BfsVisit< Digraph, Visitor, SetReachedMapTraits<T> > Create; |
| 1386 | 1386 |
}; |
| 1387 | 1387 |
///@} |
| 1388 | 1388 |
|
| 1389 | 1389 |
public: |
| 1390 | 1390 |
|
| 1391 | 1391 |
/// \brief Constructor. |
| 1392 | 1392 |
/// |
| 1393 | 1393 |
/// Constructor. |
| 1394 | 1394 |
/// |
| 1395 | 1395 |
/// \param digraph The digraph the algorithm runs on. |
| 1396 | 1396 |
/// \param visitor The visitor object of the algorithm. |
| 1397 | 1397 |
BfsVisit(const Digraph& digraph, Visitor& visitor) |
| 1398 | 1398 |
: _digraph(&digraph), _visitor(&visitor), |
| 1399 | 1399 |
_reached(0), local_reached(false) {}
|
| 1400 | 1400 |
|
| 1401 | 1401 |
/// \brief Destructor. |
| 1402 | 1402 |
~BfsVisit() {
|
| 1403 | 1403 |
if(local_reached) delete _reached; |
| 1404 | 1404 |
} |
| 1405 | 1405 |
|
| 1406 | 1406 |
/// \brief Sets the map that indicates which nodes are reached. |
| 1407 | 1407 |
/// |
| 1408 | 1408 |
/// Sets the map that indicates which nodes are reached. |
| 1409 | 1409 |
/// If you don't use this function before calling \ref run(Node) "run()" |
| 1410 | 1410 |
/// or \ref init(), an instance will be allocated automatically. |
| 1411 | 1411 |
/// The destructor deallocates this automatically allocated map, |
| 1412 | 1412 |
/// of course. |
| 1413 | 1413 |
/// \return <tt> (*this) </tt> |
| 1414 | 1414 |
BfsVisit &reachedMap(ReachedMap &m) {
|
| 1415 | 1415 |
if(local_reached) {
|
| 1416 | 1416 |
delete _reached; |
| 1417 | 1417 |
local_reached = false; |
| 1418 | 1418 |
} |
| 1419 | 1419 |
_reached = &m; |
| 1420 | 1420 |
return *this; |
| 1421 | 1421 |
} |
| 1422 | 1422 |
|
| 1423 | 1423 |
public: |
| 1424 | 1424 |
|
| 1425 | 1425 |
/// \name Execution Control |
| 1426 | 1426 |
/// The simplest way to execute the BFS algorithm is to use one of the |
| 1427 | 1427 |
/// member functions called \ref run(Node) "run()".\n |
| 1428 |
/// If you need more control on the execution, first you have to call |
|
| 1429 |
/// \ref init(), then you can add several source nodes with |
|
| 1428 |
/// If you need better control on the execution, you have to call |
|
| 1429 |
/// \ref init() first, then you can add several source nodes with |
|
| 1430 | 1430 |
/// \ref addSource(). Finally the actual path computation can be |
| 1431 | 1431 |
/// performed with one of the \ref start() functions. |
| 1432 | 1432 |
|
| 1433 | 1433 |
/// @{
|
| 1434 | 1434 |
|
| 1435 | 1435 |
/// \brief Initializes the internal data structures. |
| 1436 | 1436 |
/// |
| 1437 | 1437 |
/// Initializes the internal data structures. |
| 1438 | 1438 |
void init() {
|
| 1439 | 1439 |
create_maps(); |
| 1440 | 1440 |
_list.resize(countNodes(*_digraph)); |
| 1441 | 1441 |
_list_front = _list_back = -1; |
| 1442 | 1442 |
for (NodeIt u(*_digraph) ; u != INVALID ; ++u) {
|
| 1443 | 1443 |
_reached->set(u, false); |
| 1444 | 1444 |
} |
| 1445 | 1445 |
} |
| 1446 | 1446 |
|
| 1447 | 1447 |
/// \brief Adds a new source node. |
| 1448 | 1448 |
/// |
| 1449 | 1449 |
/// Adds a new source node to the set of nodes to be processed. |
| 1450 | 1450 |
void addSource(Node s) {
|
| 1451 | 1451 |
if(!(*_reached)[s]) {
|
| 1452 | 1452 |
_reached->set(s,true); |
| 1453 | 1453 |
_visitor->start(s); |
| 1454 | 1454 |
_visitor->reach(s); |
| 1455 | 1455 |
_list[++_list_back] = s; |
| 1456 | 1456 |
} |
| 1457 | 1457 |
} |
| 1458 | 1458 |
|
| 1459 | 1459 |
/// \brief Processes the next node. |
| 1460 | 1460 |
/// |
| 1461 | 1461 |
/// Processes the next node. |
| 1462 | 1462 |
/// |
| 1463 | 1463 |
/// \return The processed node. |
| 1464 | 1464 |
/// |
| 1465 | 1465 |
/// \pre The queue must not be empty. |
| 1466 | 1466 |
Node processNextNode() {
|
| 1467 | 1467 |
Node n = _list[++_list_front]; |
| 1468 | 1468 |
_visitor->process(n); |
| 1469 | 1469 |
Arc e; |
| 1470 | 1470 |
for (_digraph->firstOut(e, n); e != INVALID; _digraph->nextOut(e)) {
|
| 1471 | 1471 |
Node m = _digraph->target(e); |
| 1472 | 1472 |
if (!(*_reached)[m]) {
|
| 1473 | 1473 |
_visitor->discover(e); |
| 1474 | 1474 |
_visitor->reach(m); |
| 1475 | 1475 |
_reached->set(m, true); |
| 1476 | 1476 |
_list[++_list_back] = m; |
| 1477 | 1477 |
} else {
|
| 1478 | 1478 |
_visitor->examine(e); |
| 1479 | 1479 |
} |
| 1480 | 1480 |
} |
| 1481 | 1481 |
return n; |
| 1482 | 1482 |
} |
| 1483 | 1483 |
|
| 1484 | 1484 |
/// \brief Processes the next node. |
| 1485 | 1485 |
/// |
| 1486 | 1486 |
/// Processes the next node and checks if the given target node |
| 1487 | 1487 |
/// is reached. If the target node is reachable from the processed |
| 1488 | 1488 |
/// node, then the \c reach parameter will be set to \c true. |
| 1489 | 1489 |
/// |
| 1490 | 1490 |
/// \param target The target node. |
| 1491 | 1491 |
/// \retval reach Indicates if the target node is reached. |
| 1492 | 1492 |
/// It should be initially \c false. |
| 1493 | 1493 |
/// |
| 1494 | 1494 |
/// \return The processed node. |
| 1495 | 1495 |
/// |
| 1496 | 1496 |
/// \pre The queue must not be empty. |
| 1497 | 1497 |
Node processNextNode(Node target, bool& reach) {
|
| 1498 | 1498 |
Node n = _list[++_list_front]; |
| 1499 | 1499 |
_visitor->process(n); |
| 1500 | 1500 |
Arc e; |
| 1501 | 1501 |
for (_digraph->firstOut(e, n); e != INVALID; _digraph->nextOut(e)) {
|
| 1502 | 1502 |
Node m = _digraph->target(e); |
| 1503 | 1503 |
if (!(*_reached)[m]) {
|
| 1504 | 1504 |
_visitor->discover(e); |
| 1505 | 1505 |
_visitor->reach(m); |
| 1506 | 1506 |
_reached->set(m, true); |
| 1507 | 1507 |
_list[++_list_back] = m; |
| 1508 | 1508 |
reach = reach || (target == m); |
| 1509 | 1509 |
} else {
|
| 1510 | 1510 |
_visitor->examine(e); |
| 1511 | 1511 |
} |
| 1512 | 1512 |
} |
| 1513 | 1513 |
return n; |
| 1514 | 1514 |
} |
| 1515 | 1515 |
|
| 1516 | 1516 |
/// \brief Processes the next node. |
| 1517 | 1517 |
/// |
| 1518 | 1518 |
/// Processes the next node and checks if at least one of reached |
| 1519 | 1519 |
/// nodes has \c true value in the \c nm node map. If one node |
| 1520 | 1520 |
/// with \c true value is reachable from the processed node, then the |
| 1521 | 1521 |
/// \c rnode parameter will be set to the first of such nodes. |
| 1522 | 1522 |
/// |
| 1523 | 1523 |
/// \param nm A \c bool (or convertible) node map that indicates the |
| 1524 | 1524 |
/// possible targets. |
| 1525 | 1525 |
/// \retval rnode The reached target node. |
| 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-2009 |
| 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_CIRCULATION_H |
| 20 | 20 |
#define LEMON_CIRCULATION_H |
| 21 | 21 |
|
| 22 | 22 |
#include <lemon/tolerance.h> |
| 23 | 23 |
#include <lemon/elevator.h> |
| 24 | 24 |
#include <limits> |
| 25 | 25 |
|
| 26 | 26 |
///\ingroup max_flow |
| 27 | 27 |
///\file |
| 28 | 28 |
///\brief Push-relabel algorithm for finding a feasible circulation. |
| 29 | 29 |
/// |
| 30 | 30 |
namespace lemon {
|
| 31 | 31 |
|
| 32 | 32 |
/// \brief Default traits class of Circulation class. |
| 33 | 33 |
/// |
| 34 | 34 |
/// Default traits class of Circulation class. |
| 35 | 35 |
/// |
| 36 | 36 |
/// \tparam GR Type of the digraph the algorithm runs on. |
| 37 | 37 |
/// \tparam LM The type of the lower bound map. |
| 38 | 38 |
/// \tparam UM The type of the upper bound (capacity) map. |
| 39 | 39 |
/// \tparam SM The type of the supply map. |
| 40 | 40 |
template <typename GR, typename LM, |
| 41 | 41 |
typename UM, typename SM> |
| 42 | 42 |
struct CirculationDefaultTraits {
|
| 43 | 43 |
|
| 44 | 44 |
/// \brief The type of the digraph the algorithm runs on. |
| 45 | 45 |
typedef GR Digraph; |
| 46 | 46 |
|
| 47 | 47 |
/// \brief The type of the lower bound map. |
| 48 | 48 |
/// |
| 49 | 49 |
/// The type of the map that stores the lower bounds on the arcs. |
| 50 | 50 |
/// It must conform to the \ref concepts::ReadMap "ReadMap" concept. |
| 51 | 51 |
typedef LM LowerMap; |
| 52 | 52 |
|
| 53 | 53 |
/// \brief The type of the upper bound (capacity) map. |
| 54 | 54 |
/// |
| 55 | 55 |
/// The type of the map that stores the upper bounds (capacities) |
| 56 | 56 |
/// on the arcs. |
| 57 | 57 |
/// It must conform to the \ref concepts::ReadMap "ReadMap" concept. |
| 58 | 58 |
typedef UM UpperMap; |
| 59 | 59 |
|
| 60 | 60 |
/// \brief The type of supply map. |
| 61 | 61 |
/// |
| 62 | 62 |
/// The type of the map that stores the signed supply values of the |
| 63 | 63 |
/// nodes. |
| 64 | 64 |
/// It must conform to the \ref concepts::ReadMap "ReadMap" concept. |
| 65 | 65 |
typedef SM SupplyMap; |
| 66 | 66 |
|
| 67 | 67 |
/// \brief The type of the flow and supply values. |
| 68 | 68 |
typedef typename SupplyMap::Value Value; |
| 69 | 69 |
|
| 70 | 70 |
/// \brief The type of the map that stores the flow values. |
| 71 | 71 |
/// |
| 72 | 72 |
/// The type of the map that stores the flow values. |
| 73 | 73 |
/// It must conform to the \ref concepts::ReadWriteMap "ReadWriteMap" |
| 74 | 74 |
/// concept. |
| 75 |
#ifdef DOXYGEN |
|
| 76 |
typedef GR::ArcMap<Value> FlowMap; |
|
| 77 |
#else |
|
| 75 | 78 |
typedef typename Digraph::template ArcMap<Value> FlowMap; |
| 79 |
#endif |
|
| 76 | 80 |
|
| 77 | 81 |
/// \brief Instantiates a FlowMap. |
| 78 | 82 |
/// |
| 79 | 83 |
/// This function instantiates a \ref FlowMap. |
| 80 | 84 |
/// \param digraph The digraph for which we would like to define |
| 81 | 85 |
/// the flow map. |
| 82 | 86 |
static FlowMap* createFlowMap(const Digraph& digraph) {
|
| 83 | 87 |
return new FlowMap(digraph); |
| 84 | 88 |
} |
| 85 | 89 |
|
| 86 | 90 |
/// \brief The elevator type used by the algorithm. |
| 87 | 91 |
/// |
| 88 | 92 |
/// The elevator type used by the algorithm. |
| 89 | 93 |
/// |
| 90 |
/// \sa Elevator |
|
| 91 |
/// \sa LinkedElevator |
|
| 94 |
/// \sa Elevator, LinkedElevator |
|
| 95 |
#ifdef DOXYGEN |
|
| 96 |
typedef lemon::Elevator<GR, GR::Node> Elevator; |
|
| 97 |
#else |
|
| 92 | 98 |
typedef lemon::Elevator<Digraph, typename Digraph::Node> Elevator; |
| 99 |
#endif |
|
| 93 | 100 |
|
| 94 | 101 |
/// \brief Instantiates an Elevator. |
| 95 | 102 |
/// |
| 96 | 103 |
/// This function instantiates an \ref Elevator. |
| 97 | 104 |
/// \param digraph The digraph for which we would like to define |
| 98 | 105 |
/// the elevator. |
| 99 | 106 |
/// \param max_level The maximum level of the elevator. |
| 100 | 107 |
static Elevator* createElevator(const Digraph& digraph, int max_level) {
|
| 101 | 108 |
return new Elevator(digraph, max_level); |
| 102 | 109 |
} |
| 103 | 110 |
|
| 104 | 111 |
/// \brief The tolerance used by the algorithm |
| 105 | 112 |
/// |
| 106 | 113 |
/// The tolerance used by the algorithm to handle inexact computation. |
| 107 | 114 |
typedef lemon::Tolerance<Value> Tolerance; |
| 108 | 115 |
|
| 109 | 116 |
}; |
| 110 | 117 |
|
| 111 | 118 |
/** |
| 112 | 119 |
\brief Push-relabel algorithm for the network circulation problem. |
| 113 | 120 |
|
| 114 | 121 |
\ingroup max_flow |
| 115 | 122 |
This class implements a push-relabel algorithm for the \e network |
| 116 | 123 |
\e circulation problem. |
| 117 | 124 |
It is to find a feasible circulation when lower and upper bounds |
| 118 | 125 |
are given for the flow values on the arcs and lower bounds are |
| 119 | 126 |
given for the difference between the outgoing and incoming flow |
| 120 | 127 |
at the nodes. |
| 121 | 128 |
|
| 122 | 129 |
The exact formulation of this problem is the following. |
| 123 | 130 |
Let \f$G=(V,A)\f$ be a digraph, \f$lower: A\rightarrow\mathbf{R}\f$
|
| 124 | 131 |
\f$upper: A\rightarrow\mathbf{R}\cup\{\infty\}\f$ denote the lower and
|
| 125 | 132 |
upper bounds on the arcs, for which \f$lower(uv) \leq upper(uv)\f$ |
| 126 | 133 |
holds for all \f$uv\in A\f$, and \f$sup: V\rightarrow\mathbf{R}\f$
|
| 127 | 134 |
denotes the signed supply values of the nodes. |
| 128 | 135 |
If \f$sup(u)>0\f$, then \f$u\f$ is a supply node with \f$sup(u)\f$ |
| 129 | 136 |
supply, if \f$sup(u)<0\f$, then \f$u\f$ is a demand node with |
| 130 | 137 |
\f$-sup(u)\f$ demand. |
| 131 | 138 |
A feasible circulation is an \f$f: A\rightarrow\mathbf{R}\f$
|
| 132 | 139 |
solution of the following problem. |
| 133 | 140 |
|
| 134 | 141 |
\f[ \sum_{uv\in A} f(uv) - \sum_{vu\in A} f(vu)
|
| 135 | 142 |
\geq sup(u) \quad \forall u\in V, \f] |
| 136 | 143 |
\f[ lower(uv) \leq f(uv) \leq upper(uv) \quad \forall uv\in A. \f] |
| 137 | 144 |
|
| 138 | 145 |
The sum of the supply values, i.e. \f$\sum_{u\in V} sup(u)\f$ must be
|
| 139 | 146 |
zero or negative in order to have a feasible solution (since the sum |
| 140 | 147 |
of the expressions on the left-hand side of the inequalities is zero). |
| 141 | 148 |
It means that the total demand must be greater or equal to the total |
| 142 | 149 |
supply and all the supplies have to be carried out from the supply nodes, |
| 143 | 150 |
but there could be demands that are not satisfied. |
| 144 | 151 |
If \f$\sum_{u\in V} sup(u)\f$ is zero, then all the supply/demand
|
| 145 | 152 |
constraints have to be satisfied with equality, i.e. all demands |
| 146 | 153 |
have to be satisfied and all supplies have to be used. |
| 147 | 154 |
|
| 148 | 155 |
If you need the opposite inequalities in the supply/demand constraints |
| 149 | 156 |
(i.e. the total demand is less than the total supply and all the demands |
| 150 | 157 |
have to be satisfied while there could be supplies that are not used), |
| 151 | 158 |
then you could easily transform the problem to the above form by reversing |
| 152 | 159 |
the direction of the arcs and taking the negative of the supply values |
| 153 | 160 |
(e.g. using \ref ReverseDigraph and \ref NegMap adaptors). |
| 154 | 161 |
|
| 155 | 162 |
This algorithm either calculates a feasible circulation, or provides |
| 156 | 163 |
a \ref barrier() "barrier", which prooves that a feasible soultion |
| 157 | 164 |
cannot exist. |
| 158 | 165 |
|
| 159 | 166 |
Note that this algorithm also provides a feasible solution for the |
| 160 | 167 |
\ref min_cost_flow "minimum cost flow problem". |
| 161 | 168 |
|
| 162 | 169 |
\tparam GR The type of the digraph the algorithm runs on. |
| 163 | 170 |
\tparam LM The type of the lower bound map. The default |
| 164 | 171 |
map type is \ref concepts::Digraph::ArcMap "GR::ArcMap<int>". |
| 165 | 172 |
\tparam UM The type of the upper bound (capacity) map. |
| 166 | 173 |
The default map type is \c LM. |
| 167 | 174 |
\tparam SM The type of the supply map. The default map type is |
| 168 | 175 |
\ref concepts::Digraph::NodeMap "GR::NodeMap<UM::Value>". |
| 169 | 176 |
*/ |
| 170 | 177 |
#ifdef DOXYGEN |
| 171 | 178 |
template< typename GR, |
| 172 | 179 |
typename LM, |
| 173 | 180 |
typename UM, |
| 174 | 181 |
typename SM, |
| 175 | 182 |
typename TR > |
| 176 | 183 |
#else |
| 177 | 184 |
template< typename GR, |
| 178 | 185 |
typename LM = typename GR::template ArcMap<int>, |
| 179 | 186 |
typename UM = LM, |
| 180 | 187 |
typename SM = typename GR::template NodeMap<typename UM::Value>, |
| 181 | 188 |
typename TR = CirculationDefaultTraits<GR, LM, UM, SM> > |
| 182 | 189 |
#endif |
| 183 | 190 |
class Circulation {
|
| 184 | 191 |
public: |
| 185 | 192 |
|
| 186 | 193 |
///The \ref CirculationDefaultTraits "traits class" of the algorithm. |
| 187 | 194 |
typedef TR Traits; |
| 188 | 195 |
///The type of the digraph the algorithm runs on. |
| ... | ... |
@@ -374,194 +381,194 @@ |
| 374 | 381 |
} |
| 375 | 382 |
if (_excess) {
|
| 376 | 383 |
delete _excess; |
| 377 | 384 |
} |
| 378 | 385 |
} |
| 379 | 386 |
|
| 380 | 387 |
public: |
| 381 | 388 |
|
| 382 | 389 |
/// Sets the lower bound map. |
| 383 | 390 |
|
| 384 | 391 |
/// Sets the lower bound map. |
| 385 | 392 |
/// \return <tt>(*this)</tt> |
| 386 | 393 |
Circulation& lowerMap(const LowerMap& map) {
|
| 387 | 394 |
_lo = ↦ |
| 388 | 395 |
return *this; |
| 389 | 396 |
} |
| 390 | 397 |
|
| 391 | 398 |
/// Sets the upper bound (capacity) map. |
| 392 | 399 |
|
| 393 | 400 |
/// Sets the upper bound (capacity) map. |
| 394 | 401 |
/// \return <tt>(*this)</tt> |
| 395 | 402 |
Circulation& upperMap(const UpperMap& map) {
|
| 396 | 403 |
_up = ↦ |
| 397 | 404 |
return *this; |
| 398 | 405 |
} |
| 399 | 406 |
|
| 400 | 407 |
/// Sets the supply map. |
| 401 | 408 |
|
| 402 | 409 |
/// Sets the supply map. |
| 403 | 410 |
/// \return <tt>(*this)</tt> |
| 404 | 411 |
Circulation& supplyMap(const SupplyMap& map) {
|
| 405 | 412 |
_supply = ↦ |
| 406 | 413 |
return *this; |
| 407 | 414 |
} |
| 408 | 415 |
|
| 409 | 416 |
/// \brief Sets the flow map. |
| 410 | 417 |
/// |
| 411 | 418 |
/// Sets the flow map. |
| 412 | 419 |
/// If you don't use this function before calling \ref run() or |
| 413 | 420 |
/// \ref init(), an instance will be allocated automatically. |
| 414 | 421 |
/// The destructor deallocates this automatically allocated map, |
| 415 | 422 |
/// of course. |
| 416 | 423 |
/// \return <tt>(*this)</tt> |
| 417 | 424 |
Circulation& flowMap(FlowMap& map) {
|
| 418 | 425 |
if (_local_flow) {
|
| 419 | 426 |
delete _flow; |
| 420 | 427 |
_local_flow = false; |
| 421 | 428 |
} |
| 422 | 429 |
_flow = ↦ |
| 423 | 430 |
return *this; |
| 424 | 431 |
} |
| 425 | 432 |
|
| 426 | 433 |
/// \brief Sets the elevator used by algorithm. |
| 427 | 434 |
/// |
| 428 | 435 |
/// Sets the elevator used by algorithm. |
| 429 | 436 |
/// If you don't use this function before calling \ref run() or |
| 430 | 437 |
/// \ref init(), an instance will be allocated automatically. |
| 431 | 438 |
/// The destructor deallocates this automatically allocated elevator, |
| 432 | 439 |
/// of course. |
| 433 | 440 |
/// \return <tt>(*this)</tt> |
| 434 | 441 |
Circulation& elevator(Elevator& elevator) {
|
| 435 | 442 |
if (_local_level) {
|
| 436 | 443 |
delete _level; |
| 437 | 444 |
_local_level = false; |
| 438 | 445 |
} |
| 439 | 446 |
_level = &elevator; |
| 440 | 447 |
return *this; |
| 441 | 448 |
} |
| 442 | 449 |
|
| 443 | 450 |
/// \brief Returns a const reference to the elevator. |
| 444 | 451 |
/// |
| 445 | 452 |
/// Returns a const reference to the elevator. |
| 446 | 453 |
/// |
| 447 | 454 |
/// \pre Either \ref run() or \ref init() must be called before |
| 448 | 455 |
/// using this function. |
| 449 | 456 |
const Elevator& elevator() const {
|
| 450 | 457 |
return *_level; |
| 451 | 458 |
} |
| 452 | 459 |
|
| 453 | 460 |
/// \brief Sets the tolerance used by algorithm. |
| 454 | 461 |
/// |
| 455 | 462 |
/// Sets the tolerance used by algorithm. |
| 456 | 463 |
Circulation& tolerance(const Tolerance& tolerance) const {
|
| 457 | 464 |
_tol = tolerance; |
| 458 | 465 |
return *this; |
| 459 | 466 |
} |
| 460 | 467 |
|
| 461 | 468 |
/// \brief Returns a const reference to the tolerance. |
| 462 | 469 |
/// |
| 463 | 470 |
/// Returns a const reference to the tolerance. |
| 464 | 471 |
const Tolerance& tolerance() const {
|
| 465 | 472 |
return tolerance; |
| 466 | 473 |
} |
| 467 | 474 |
|
| 468 | 475 |
/// \name Execution Control |
| 469 | 476 |
/// The simplest way to execute the algorithm is to call \ref run().\n |
| 470 |
/// If you need more control on the initial solution or the execution, |
|
| 471 |
/// first you have to call one of the \ref init() functions, then |
|
| 477 |
/// If you need better control on the initial solution or the execution, |
|
| 478 |
/// you have to call one of the \ref init() functions first, then |
|
| 472 | 479 |
/// the \ref start() function. |
| 473 | 480 |
|
| 474 | 481 |
///@{
|
| 475 | 482 |
|
| 476 | 483 |
/// Initializes the internal data structures. |
| 477 | 484 |
|
| 478 | 485 |
/// Initializes the internal data structures and sets all flow values |
| 479 | 486 |
/// to the lower bound. |
| 480 | 487 |
void init() |
| 481 | 488 |
{
|
| 482 | 489 |
LEMON_DEBUG(checkBoundMaps(), |
| 483 | 490 |
"Upper bounds must be greater or equal to the lower bounds"); |
| 484 | 491 |
|
| 485 | 492 |
createStructures(); |
| 486 | 493 |
|
| 487 | 494 |
for(NodeIt n(_g);n!=INVALID;++n) {
|
| 488 | 495 |
(*_excess)[n] = (*_supply)[n]; |
| 489 | 496 |
} |
| 490 | 497 |
|
| 491 | 498 |
for (ArcIt e(_g);e!=INVALID;++e) {
|
| 492 | 499 |
_flow->set(e, (*_lo)[e]); |
| 493 | 500 |
(*_excess)[_g.target(e)] += (*_flow)[e]; |
| 494 | 501 |
(*_excess)[_g.source(e)] -= (*_flow)[e]; |
| 495 | 502 |
} |
| 496 | 503 |
|
| 497 | 504 |
// global relabeling tested, but in general case it provides |
| 498 | 505 |
// worse performance for random digraphs |
| 499 | 506 |
_level->initStart(); |
| 500 | 507 |
for(NodeIt n(_g);n!=INVALID;++n) |
| 501 | 508 |
_level->initAddItem(n); |
| 502 | 509 |
_level->initFinish(); |
| 503 | 510 |
for(NodeIt n(_g);n!=INVALID;++n) |
| 504 | 511 |
if(_tol.positive((*_excess)[n])) |
| 505 | 512 |
_level->activate(n); |
| 506 | 513 |
} |
| 507 | 514 |
|
| 508 | 515 |
/// Initializes the internal data structures using a greedy approach. |
| 509 | 516 |
|
| 510 | 517 |
/// Initializes the internal data structures using a greedy approach |
| 511 | 518 |
/// to construct the initial solution. |
| 512 | 519 |
void greedyInit() |
| 513 | 520 |
{
|
| 514 | 521 |
LEMON_DEBUG(checkBoundMaps(), |
| 515 | 522 |
"Upper bounds must be greater or equal to the lower bounds"); |
| 516 | 523 |
|
| 517 | 524 |
createStructures(); |
| 518 | 525 |
|
| 519 | 526 |
for(NodeIt n(_g);n!=INVALID;++n) {
|
| 520 | 527 |
(*_excess)[n] = (*_supply)[n]; |
| 521 | 528 |
} |
| 522 | 529 |
|
| 523 | 530 |
for (ArcIt e(_g);e!=INVALID;++e) {
|
| 524 | 531 |
if (!_tol.less(-(*_excess)[_g.target(e)], (*_up)[e])) {
|
| 525 | 532 |
_flow->set(e, (*_up)[e]); |
| 526 | 533 |
(*_excess)[_g.target(e)] += (*_up)[e]; |
| 527 | 534 |
(*_excess)[_g.source(e)] -= (*_up)[e]; |
| 528 | 535 |
} else if (_tol.less(-(*_excess)[_g.target(e)], (*_lo)[e])) {
|
| 529 | 536 |
_flow->set(e, (*_lo)[e]); |
| 530 | 537 |
(*_excess)[_g.target(e)] += (*_lo)[e]; |
| 531 | 538 |
(*_excess)[_g.source(e)] -= (*_lo)[e]; |
| 532 | 539 |
} else {
|
| 533 | 540 |
Value fc = -(*_excess)[_g.target(e)]; |
| 534 | 541 |
_flow->set(e, fc); |
| 535 | 542 |
(*_excess)[_g.target(e)] = 0; |
| 536 | 543 |
(*_excess)[_g.source(e)] -= fc; |
| 537 | 544 |
} |
| 538 | 545 |
} |
| 539 | 546 |
|
| 540 | 547 |
_level->initStart(); |
| 541 | 548 |
for(NodeIt n(_g);n!=INVALID;++n) |
| 542 | 549 |
_level->initAddItem(n); |
| 543 | 550 |
_level->initFinish(); |
| 544 | 551 |
for(NodeIt n(_g);n!=INVALID;++n) |
| 545 | 552 |
if(_tol.positive((*_excess)[n])) |
| 546 | 553 |
_level->activate(n); |
| 547 | 554 |
} |
| 548 | 555 |
|
| 549 | 556 |
///Executes the algorithm |
| 550 | 557 |
|
| 551 | 558 |
///This function executes the algorithm. |
| 552 | 559 |
/// |
| 553 | 560 |
///\return \c true if a feasible circulation is found. |
| 554 | 561 |
/// |
| 555 | 562 |
///\sa barrier() |
| 556 | 563 |
///\sa barrierMap() |
| 557 | 564 |
bool start() |
| 558 | 565 |
{
|
| 559 | 566 |
|
| 560 | 567 |
Node act; |
| 561 | 568 |
Node bact=INVALID; |
| 562 | 569 |
Node last_activated=INVALID; |
| 563 | 570 |
while((act=_level->highestActive())!=INVALID) {
|
| 564 | 571 |
int actlevel=(*_level)[act]; |
| 565 | 572 |
int mlevel=_node_num; |
| 566 | 573 |
Value exc=(*_excess)[act]; |
| 567 | 574 |
| ... | ... |
@@ -318,194 +318,194 @@ |
| 318 | 318 |
///\param g The digraph the algorithm runs on. |
| 319 | 319 |
Dfs(const Digraph &g) : |
| 320 | 320 |
G(&g), |
| 321 | 321 |
_pred(NULL), local_pred(false), |
| 322 | 322 |
_dist(NULL), local_dist(false), |
| 323 | 323 |
_reached(NULL), local_reached(false), |
| 324 | 324 |
_processed(NULL), local_processed(false) |
| 325 | 325 |
{ }
|
| 326 | 326 |
|
| 327 | 327 |
///Destructor. |
| 328 | 328 |
~Dfs() |
| 329 | 329 |
{
|
| 330 | 330 |
if(local_pred) delete _pred; |
| 331 | 331 |
if(local_dist) delete _dist; |
| 332 | 332 |
if(local_reached) delete _reached; |
| 333 | 333 |
if(local_processed) delete _processed; |
| 334 | 334 |
} |
| 335 | 335 |
|
| 336 | 336 |
///Sets the map that stores the predecessor arcs. |
| 337 | 337 |
|
| 338 | 338 |
///Sets the map that stores the predecessor arcs. |
| 339 | 339 |
///If you don't use this function before calling \ref run(Node) "run()" |
| 340 | 340 |
///or \ref init(), an instance will be allocated automatically. |
| 341 | 341 |
///The destructor deallocates this automatically allocated map, |
| 342 | 342 |
///of course. |
| 343 | 343 |
///\return <tt> (*this) </tt> |
| 344 | 344 |
Dfs &predMap(PredMap &m) |
| 345 | 345 |
{
|
| 346 | 346 |
if(local_pred) {
|
| 347 | 347 |
delete _pred; |
| 348 | 348 |
local_pred=false; |
| 349 | 349 |
} |
| 350 | 350 |
_pred = &m; |
| 351 | 351 |
return *this; |
| 352 | 352 |
} |
| 353 | 353 |
|
| 354 | 354 |
///Sets the map that indicates which nodes are reached. |
| 355 | 355 |
|
| 356 | 356 |
///Sets the map that indicates which nodes are reached. |
| 357 | 357 |
///If you don't use this function before calling \ref run(Node) "run()" |
| 358 | 358 |
///or \ref init(), an instance will be allocated automatically. |
| 359 | 359 |
///The destructor deallocates this automatically allocated map, |
| 360 | 360 |
///of course. |
| 361 | 361 |
///\return <tt> (*this) </tt> |
| 362 | 362 |
Dfs &reachedMap(ReachedMap &m) |
| 363 | 363 |
{
|
| 364 | 364 |
if(local_reached) {
|
| 365 | 365 |
delete _reached; |
| 366 | 366 |
local_reached=false; |
| 367 | 367 |
} |
| 368 | 368 |
_reached = &m; |
| 369 | 369 |
return *this; |
| 370 | 370 |
} |
| 371 | 371 |
|
| 372 | 372 |
///Sets the map that indicates which nodes are processed. |
| 373 | 373 |
|
| 374 | 374 |
///Sets the map that indicates which nodes are processed. |
| 375 | 375 |
///If you don't use this function before calling \ref run(Node) "run()" |
| 376 | 376 |
///or \ref init(), an instance will be allocated automatically. |
| 377 | 377 |
///The destructor deallocates this automatically allocated map, |
| 378 | 378 |
///of course. |
| 379 | 379 |
///\return <tt> (*this) </tt> |
| 380 | 380 |
Dfs &processedMap(ProcessedMap &m) |
| 381 | 381 |
{
|
| 382 | 382 |
if(local_processed) {
|
| 383 | 383 |
delete _processed; |
| 384 | 384 |
local_processed=false; |
| 385 | 385 |
} |
| 386 | 386 |
_processed = &m; |
| 387 | 387 |
return *this; |
| 388 | 388 |
} |
| 389 | 389 |
|
| 390 | 390 |
///Sets the map that stores the distances of the nodes. |
| 391 | 391 |
|
| 392 | 392 |
///Sets the map that stores the distances of the nodes calculated by |
| 393 | 393 |
///the algorithm. |
| 394 | 394 |
///If you don't use this function before calling \ref run(Node) "run()" |
| 395 | 395 |
///or \ref init(), an instance will be allocated automatically. |
| 396 | 396 |
///The destructor deallocates this automatically allocated map, |
| 397 | 397 |
///of course. |
| 398 | 398 |
///\return <tt> (*this) </tt> |
| 399 | 399 |
Dfs &distMap(DistMap &m) |
| 400 | 400 |
{
|
| 401 | 401 |
if(local_dist) {
|
| 402 | 402 |
delete _dist; |
| 403 | 403 |
local_dist=false; |
| 404 | 404 |
} |
| 405 | 405 |
_dist = &m; |
| 406 | 406 |
return *this; |
| 407 | 407 |
} |
| 408 | 408 |
|
| 409 | 409 |
public: |
| 410 | 410 |
|
| 411 | 411 |
///\name Execution Control |
| 412 | 412 |
///The simplest way to execute the DFS algorithm is to use one of the |
| 413 | 413 |
///member functions called \ref run(Node) "run()".\n |
| 414 |
///If you need more control on the execution, first you have to call |
|
| 415 |
///\ref init(), then you can add a source node with \ref addSource() |
|
| 414 |
///If you need better control on the execution, you have to call |
|
| 415 |
///\ref init() first, then you can add a source node with \ref addSource() |
|
| 416 | 416 |
///and perform the actual computation with \ref start(). |
| 417 | 417 |
///This procedure can be repeated if there are nodes that have not |
| 418 | 418 |
///been reached. |
| 419 | 419 |
|
| 420 | 420 |
///@{
|
| 421 | 421 |
|
| 422 | 422 |
///\brief Initializes the internal data structures. |
| 423 | 423 |
/// |
| 424 | 424 |
///Initializes the internal data structures. |
| 425 | 425 |
void init() |
| 426 | 426 |
{
|
| 427 | 427 |
create_maps(); |
| 428 | 428 |
_stack.resize(countNodes(*G)); |
| 429 | 429 |
_stack_head=-1; |
| 430 | 430 |
for ( NodeIt u(*G) ; u!=INVALID ; ++u ) {
|
| 431 | 431 |
_pred->set(u,INVALID); |
| 432 | 432 |
_reached->set(u,false); |
| 433 | 433 |
_processed->set(u,false); |
| 434 | 434 |
} |
| 435 | 435 |
} |
| 436 | 436 |
|
| 437 | 437 |
///Adds a new source node. |
| 438 | 438 |
|
| 439 | 439 |
///Adds a new source node to the set of nodes to be processed. |
| 440 | 440 |
/// |
| 441 | 441 |
///\pre The stack must be empty. Otherwise the algorithm gives |
| 442 | 442 |
///wrong results. (One of the outgoing arcs of all the source nodes |
| 443 | 443 |
///except for the last one will not be visited and distances will |
| 444 | 444 |
///also be wrong.) |
| 445 | 445 |
void addSource(Node s) |
| 446 | 446 |
{
|
| 447 | 447 |
LEMON_DEBUG(emptyQueue(), "The stack is not empty."); |
| 448 | 448 |
if(!(*_reached)[s]) |
| 449 | 449 |
{
|
| 450 | 450 |
_reached->set(s,true); |
| 451 | 451 |
_pred->set(s,INVALID); |
| 452 | 452 |
OutArcIt e(*G,s); |
| 453 | 453 |
if(e!=INVALID) {
|
| 454 | 454 |
_stack[++_stack_head]=e; |
| 455 | 455 |
_dist->set(s,_stack_head); |
| 456 | 456 |
} |
| 457 | 457 |
else {
|
| 458 | 458 |
_processed->set(s,true); |
| 459 | 459 |
_dist->set(s,0); |
| 460 | 460 |
} |
| 461 | 461 |
} |
| 462 | 462 |
} |
| 463 | 463 |
|
| 464 | 464 |
///Processes the next arc. |
| 465 | 465 |
|
| 466 | 466 |
///Processes the next arc. |
| 467 | 467 |
/// |
| 468 | 468 |
///\return The processed arc. |
| 469 | 469 |
/// |
| 470 | 470 |
///\pre The stack must not be empty. |
| 471 | 471 |
Arc processNextArc() |
| 472 | 472 |
{
|
| 473 | 473 |
Node m; |
| 474 | 474 |
Arc e=_stack[_stack_head]; |
| 475 | 475 |
if(!(*_reached)[m=G->target(e)]) {
|
| 476 | 476 |
_pred->set(m,e); |
| 477 | 477 |
_reached->set(m,true); |
| 478 | 478 |
++_stack_head; |
| 479 | 479 |
_stack[_stack_head] = OutArcIt(*G, m); |
| 480 | 480 |
_dist->set(m,_stack_head); |
| 481 | 481 |
} |
| 482 | 482 |
else {
|
| 483 | 483 |
m=G->source(e); |
| 484 | 484 |
++_stack[_stack_head]; |
| 485 | 485 |
} |
| 486 | 486 |
while(_stack_head>=0 && _stack[_stack_head]==INVALID) {
|
| 487 | 487 |
_processed->set(m,true); |
| 488 | 488 |
--_stack_head; |
| 489 | 489 |
if(_stack_head>=0) {
|
| 490 | 490 |
m=G->source(_stack[_stack_head]); |
| 491 | 491 |
++_stack[_stack_head]; |
| 492 | 492 |
} |
| 493 | 493 |
} |
| 494 | 494 |
return e; |
| 495 | 495 |
} |
| 496 | 496 |
|
| 497 | 497 |
///Next arc to be processed. |
| 498 | 498 |
|
| 499 | 499 |
///Next arc to be processed. |
| 500 | 500 |
/// |
| 501 | 501 |
///\return The next arc to be processed or \c INVALID if the stack |
| 502 | 502 |
///is empty. |
| 503 | 503 |
OutArcIt nextArc() const |
| 504 | 504 |
{
|
| 505 | 505 |
return _stack_head>=0?_stack[_stack_head]:INVALID; |
| 506 | 506 |
} |
| 507 | 507 |
|
| 508 | 508 |
///Returns \c false if there are nodes to be processed. |
| 509 | 509 |
|
| 510 | 510 |
///Returns \c false if there are nodes to be processed |
| 511 | 511 |
///in the queue (stack). |
| ... | ... |
@@ -1276,194 +1276,194 @@ |
| 1276 | 1276 |
private: |
| 1277 | 1277 |
|
| 1278 | 1278 |
typedef typename Digraph::Node Node; |
| 1279 | 1279 |
typedef typename Digraph::NodeIt NodeIt; |
| 1280 | 1280 |
typedef typename Digraph::Arc Arc; |
| 1281 | 1281 |
typedef typename Digraph::OutArcIt OutArcIt; |
| 1282 | 1282 |
|
| 1283 | 1283 |
//Pointer to the underlying digraph. |
| 1284 | 1284 |
const Digraph *_digraph; |
| 1285 | 1285 |
//Pointer to the visitor object. |
| 1286 | 1286 |
Visitor *_visitor; |
| 1287 | 1287 |
//Pointer to the map of reached status of the nodes. |
| 1288 | 1288 |
ReachedMap *_reached; |
| 1289 | 1289 |
//Indicates if _reached is locally allocated (true) or not. |
| 1290 | 1290 |
bool local_reached; |
| 1291 | 1291 |
|
| 1292 | 1292 |
std::vector<typename Digraph::Arc> _stack; |
| 1293 | 1293 |
int _stack_head; |
| 1294 | 1294 |
|
| 1295 | 1295 |
//Creates the maps if necessary. |
| 1296 | 1296 |
void create_maps() {
|
| 1297 | 1297 |
if(!_reached) {
|
| 1298 | 1298 |
local_reached = true; |
| 1299 | 1299 |
_reached = Traits::createReachedMap(*_digraph); |
| 1300 | 1300 |
} |
| 1301 | 1301 |
} |
| 1302 | 1302 |
|
| 1303 | 1303 |
protected: |
| 1304 | 1304 |
|
| 1305 | 1305 |
DfsVisit() {}
|
| 1306 | 1306 |
|
| 1307 | 1307 |
public: |
| 1308 | 1308 |
|
| 1309 | 1309 |
typedef DfsVisit Create; |
| 1310 | 1310 |
|
| 1311 | 1311 |
/// \name Named Template Parameters |
| 1312 | 1312 |
|
| 1313 | 1313 |
///@{
|
| 1314 | 1314 |
template <class T> |
| 1315 | 1315 |
struct SetReachedMapTraits : public Traits {
|
| 1316 | 1316 |
typedef T ReachedMap; |
| 1317 | 1317 |
static ReachedMap *createReachedMap(const Digraph &digraph) {
|
| 1318 | 1318 |
LEMON_ASSERT(false, "ReachedMap is not initialized"); |
| 1319 | 1319 |
return 0; // ignore warnings |
| 1320 | 1320 |
} |
| 1321 | 1321 |
}; |
| 1322 | 1322 |
/// \brief \ref named-templ-param "Named parameter" for setting |
| 1323 | 1323 |
/// ReachedMap type. |
| 1324 | 1324 |
/// |
| 1325 | 1325 |
/// \ref named-templ-param "Named parameter" for setting ReachedMap type. |
| 1326 | 1326 |
template <class T> |
| 1327 | 1327 |
struct SetReachedMap : public DfsVisit< Digraph, Visitor, |
| 1328 | 1328 |
SetReachedMapTraits<T> > {
|
| 1329 | 1329 |
typedef DfsVisit< Digraph, Visitor, SetReachedMapTraits<T> > Create; |
| 1330 | 1330 |
}; |
| 1331 | 1331 |
///@} |
| 1332 | 1332 |
|
| 1333 | 1333 |
public: |
| 1334 | 1334 |
|
| 1335 | 1335 |
/// \brief Constructor. |
| 1336 | 1336 |
/// |
| 1337 | 1337 |
/// Constructor. |
| 1338 | 1338 |
/// |
| 1339 | 1339 |
/// \param digraph The digraph the algorithm runs on. |
| 1340 | 1340 |
/// \param visitor The visitor object of the algorithm. |
| 1341 | 1341 |
DfsVisit(const Digraph& digraph, Visitor& visitor) |
| 1342 | 1342 |
: _digraph(&digraph), _visitor(&visitor), |
| 1343 | 1343 |
_reached(0), local_reached(false) {}
|
| 1344 | 1344 |
|
| 1345 | 1345 |
/// \brief Destructor. |
| 1346 | 1346 |
~DfsVisit() {
|
| 1347 | 1347 |
if(local_reached) delete _reached; |
| 1348 | 1348 |
} |
| 1349 | 1349 |
|
| 1350 | 1350 |
/// \brief Sets the map that indicates which nodes are reached. |
| 1351 | 1351 |
/// |
| 1352 | 1352 |
/// Sets the map that indicates which nodes are reached. |
| 1353 | 1353 |
/// If you don't use this function before calling \ref run(Node) "run()" |
| 1354 | 1354 |
/// or \ref init(), an instance will be allocated automatically. |
| 1355 | 1355 |
/// The destructor deallocates this automatically allocated map, |
| 1356 | 1356 |
/// of course. |
| 1357 | 1357 |
/// \return <tt> (*this) </tt> |
| 1358 | 1358 |
DfsVisit &reachedMap(ReachedMap &m) {
|
| 1359 | 1359 |
if(local_reached) {
|
| 1360 | 1360 |
delete _reached; |
| 1361 | 1361 |
local_reached=false; |
| 1362 | 1362 |
} |
| 1363 | 1363 |
_reached = &m; |
| 1364 | 1364 |
return *this; |
| 1365 | 1365 |
} |
| 1366 | 1366 |
|
| 1367 | 1367 |
public: |
| 1368 | 1368 |
|
| 1369 | 1369 |
/// \name Execution Control |
| 1370 | 1370 |
/// The simplest way to execute the DFS algorithm is to use one of the |
| 1371 | 1371 |
/// member functions called \ref run(Node) "run()".\n |
| 1372 |
/// If you need more control on the execution, first you have to call |
|
| 1373 |
/// \ref init(), then you can add a source node with \ref addSource() |
|
| 1372 |
/// If you need better control on the execution, you have to call |
|
| 1373 |
/// \ref init() first, then you can add a source node with \ref addSource() |
|
| 1374 | 1374 |
/// and perform the actual computation with \ref start(). |
| 1375 | 1375 |
/// This procedure can be repeated if there are nodes that have not |
| 1376 | 1376 |
/// been reached. |
| 1377 | 1377 |
|
| 1378 | 1378 |
/// @{
|
| 1379 | 1379 |
|
| 1380 | 1380 |
/// \brief Initializes the internal data structures. |
| 1381 | 1381 |
/// |
| 1382 | 1382 |
/// Initializes the internal data structures. |
| 1383 | 1383 |
void init() {
|
| 1384 | 1384 |
create_maps(); |
| 1385 | 1385 |
_stack.resize(countNodes(*_digraph)); |
| 1386 | 1386 |
_stack_head = -1; |
| 1387 | 1387 |
for (NodeIt u(*_digraph) ; u != INVALID ; ++u) {
|
| 1388 | 1388 |
_reached->set(u, false); |
| 1389 | 1389 |
} |
| 1390 | 1390 |
} |
| 1391 | 1391 |
|
| 1392 | 1392 |
/// \brief Adds a new source node. |
| 1393 | 1393 |
/// |
| 1394 | 1394 |
/// Adds a new source node to the set of nodes to be processed. |
| 1395 | 1395 |
/// |
| 1396 | 1396 |
/// \pre The stack must be empty. Otherwise the algorithm gives |
| 1397 | 1397 |
/// wrong results. (One of the outgoing arcs of all the source nodes |
| 1398 | 1398 |
/// except for the last one will not be visited and distances will |
| 1399 | 1399 |
/// also be wrong.) |
| 1400 | 1400 |
void addSource(Node s) |
| 1401 | 1401 |
{
|
| 1402 | 1402 |
LEMON_DEBUG(emptyQueue(), "The stack is not empty."); |
| 1403 | 1403 |
if(!(*_reached)[s]) {
|
| 1404 | 1404 |
_reached->set(s,true); |
| 1405 | 1405 |
_visitor->start(s); |
| 1406 | 1406 |
_visitor->reach(s); |
| 1407 | 1407 |
Arc e; |
| 1408 | 1408 |
_digraph->firstOut(e, s); |
| 1409 | 1409 |
if (e != INVALID) {
|
| 1410 | 1410 |
_stack[++_stack_head] = e; |
| 1411 | 1411 |
} else {
|
| 1412 | 1412 |
_visitor->leave(s); |
| 1413 | 1413 |
_visitor->stop(s); |
| 1414 | 1414 |
} |
| 1415 | 1415 |
} |
| 1416 | 1416 |
} |
| 1417 | 1417 |
|
| 1418 | 1418 |
/// \brief Processes the next arc. |
| 1419 | 1419 |
/// |
| 1420 | 1420 |
/// Processes the next arc. |
| 1421 | 1421 |
/// |
| 1422 | 1422 |
/// \return The processed arc. |
| 1423 | 1423 |
/// |
| 1424 | 1424 |
/// \pre The stack must not be empty. |
| 1425 | 1425 |
Arc processNextArc() {
|
| 1426 | 1426 |
Arc e = _stack[_stack_head]; |
| 1427 | 1427 |
Node m = _digraph->target(e); |
| 1428 | 1428 |
if(!(*_reached)[m]) {
|
| 1429 | 1429 |
_visitor->discover(e); |
| 1430 | 1430 |
_visitor->reach(m); |
| 1431 | 1431 |
_reached->set(m, true); |
| 1432 | 1432 |
_digraph->firstOut(_stack[++_stack_head], m); |
| 1433 | 1433 |
} else {
|
| 1434 | 1434 |
_visitor->examine(e); |
| 1435 | 1435 |
m = _digraph->source(e); |
| 1436 | 1436 |
_digraph->nextOut(_stack[_stack_head]); |
| 1437 | 1437 |
} |
| 1438 | 1438 |
while (_stack_head>=0 && _stack[_stack_head] == INVALID) {
|
| 1439 | 1439 |
_visitor->leave(m); |
| 1440 | 1440 |
--_stack_head; |
| 1441 | 1441 |
if (_stack_head >= 0) {
|
| 1442 | 1442 |
_visitor->backtrack(_stack[_stack_head]); |
| 1443 | 1443 |
m = _digraph->source(_stack[_stack_head]); |
| 1444 | 1444 |
_digraph->nextOut(_stack[_stack_head]); |
| 1445 | 1445 |
} else {
|
| 1446 | 1446 |
_visitor->stop(m); |
| 1447 | 1447 |
} |
| 1448 | 1448 |
} |
| 1449 | 1449 |
return e; |
| 1450 | 1450 |
} |
| 1451 | 1451 |
|
| 1452 | 1452 |
/// \brief Next arc to be processed. |
| 1453 | 1453 |
/// |
| 1454 | 1454 |
/// Next arc to be processed. |
| 1455 | 1455 |
/// |
| 1456 | 1456 |
/// \return The next arc to be processed or INVALID if the stack is |
| 1457 | 1457 |
/// empty. |
| 1458 | 1458 |
Arc nextArc() const {
|
| 1459 | 1459 |
return _stack_head >= 0 ? _stack[_stack_head] : INVALID; |
| 1460 | 1460 |
} |
| 1461 | 1461 |
|
| 1462 | 1462 |
/// \brief Returns \c false if there are nodes |
| 1463 | 1463 |
/// to be processed. |
| 1464 | 1464 |
/// |
| 1465 | 1465 |
/// Returns \c false if there are nodes |
| 1466 | 1466 |
/// to be processed in the queue (stack). |
| 1467 | 1467 |
bool emptyQueue() const { return _stack_head < 0; }
|
| 1468 | 1468 |
|
| 1469 | 1469 |
/// \brief Returns the number of the nodes to be processed. |
| ... | ... |
@@ -491,194 +491,194 @@ |
| 491 | 491 |
_length = &m; |
| 492 | 492 |
return *this; |
| 493 | 493 |
} |
| 494 | 494 |
|
| 495 | 495 |
///Sets the map that stores the predecessor arcs. |
| 496 | 496 |
|
| 497 | 497 |
///Sets the map that stores the predecessor arcs. |
| 498 | 498 |
///If you don't use this function before calling \ref run(Node) "run()" |
| 499 | 499 |
///or \ref init(), an instance will be allocated automatically. |
| 500 | 500 |
///The destructor deallocates this automatically allocated map, |
| 501 | 501 |
///of course. |
| 502 | 502 |
///\return <tt> (*this) </tt> |
| 503 | 503 |
Dijkstra &predMap(PredMap &m) |
| 504 | 504 |
{
|
| 505 | 505 |
if(local_pred) {
|
| 506 | 506 |
delete _pred; |
| 507 | 507 |
local_pred=false; |
| 508 | 508 |
} |
| 509 | 509 |
_pred = &m; |
| 510 | 510 |
return *this; |
| 511 | 511 |
} |
| 512 | 512 |
|
| 513 | 513 |
///Sets the map that indicates which nodes are processed. |
| 514 | 514 |
|
| 515 | 515 |
///Sets the map that indicates which nodes are processed. |
| 516 | 516 |
///If you don't use this function before calling \ref run(Node) "run()" |
| 517 | 517 |
///or \ref init(), an instance will be allocated automatically. |
| 518 | 518 |
///The destructor deallocates this automatically allocated map, |
| 519 | 519 |
///of course. |
| 520 | 520 |
///\return <tt> (*this) </tt> |
| 521 | 521 |
Dijkstra &processedMap(ProcessedMap &m) |
| 522 | 522 |
{
|
| 523 | 523 |
if(local_processed) {
|
| 524 | 524 |
delete _processed; |
| 525 | 525 |
local_processed=false; |
| 526 | 526 |
} |
| 527 | 527 |
_processed = &m; |
| 528 | 528 |
return *this; |
| 529 | 529 |
} |
| 530 | 530 |
|
| 531 | 531 |
///Sets the map that stores the distances of the nodes. |
| 532 | 532 |
|
| 533 | 533 |
///Sets the map that stores the distances of the nodes calculated by the |
| 534 | 534 |
///algorithm. |
| 535 | 535 |
///If you don't use this function before calling \ref run(Node) "run()" |
| 536 | 536 |
///or \ref init(), an instance will be allocated automatically. |
| 537 | 537 |
///The destructor deallocates this automatically allocated map, |
| 538 | 538 |
///of course. |
| 539 | 539 |
///\return <tt> (*this) </tt> |
| 540 | 540 |
Dijkstra &distMap(DistMap &m) |
| 541 | 541 |
{
|
| 542 | 542 |
if(local_dist) {
|
| 543 | 543 |
delete _dist; |
| 544 | 544 |
local_dist=false; |
| 545 | 545 |
} |
| 546 | 546 |
_dist = &m; |
| 547 | 547 |
return *this; |
| 548 | 548 |
} |
| 549 | 549 |
|
| 550 | 550 |
///Sets the heap and the cross reference used by algorithm. |
| 551 | 551 |
|
| 552 | 552 |
///Sets the heap and the cross reference used by algorithm. |
| 553 | 553 |
///If you don't use this function before calling \ref run(Node) "run()" |
| 554 | 554 |
///or \ref init(), heap and cross reference instances will be |
| 555 | 555 |
///allocated automatically. |
| 556 | 556 |
///The destructor deallocates these automatically allocated objects, |
| 557 | 557 |
///of course. |
| 558 | 558 |
///\return <tt> (*this) </tt> |
| 559 | 559 |
Dijkstra &heap(Heap& hp, HeapCrossRef &cr) |
| 560 | 560 |
{
|
| 561 | 561 |
if(local_heap_cross_ref) {
|
| 562 | 562 |
delete _heap_cross_ref; |
| 563 | 563 |
local_heap_cross_ref=false; |
| 564 | 564 |
} |
| 565 | 565 |
_heap_cross_ref = &cr; |
| 566 | 566 |
if(local_heap) {
|
| 567 | 567 |
delete _heap; |
| 568 | 568 |
local_heap=false; |
| 569 | 569 |
} |
| 570 | 570 |
_heap = &hp; |
| 571 | 571 |
return *this; |
| 572 | 572 |
} |
| 573 | 573 |
|
| 574 | 574 |
private: |
| 575 | 575 |
|
| 576 | 576 |
void finalizeNodeData(Node v,Value dst) |
| 577 | 577 |
{
|
| 578 | 578 |
_processed->set(v,true); |
| 579 | 579 |
_dist->set(v, dst); |
| 580 | 580 |
} |
| 581 | 581 |
|
| 582 | 582 |
public: |
| 583 | 583 |
|
| 584 | 584 |
///\name Execution Control |
| 585 | 585 |
///The simplest way to execute the %Dijkstra algorithm is to use |
| 586 | 586 |
///one of the member functions called \ref run(Node) "run()".\n |
| 587 |
///If you need more control on the execution, first you have to call |
|
| 588 |
///\ref init(), then you can add several source nodes with |
|
| 587 |
///If you need better control on the execution, you have to call |
|
| 588 |
///\ref init() first, then you can add several source nodes with |
|
| 589 | 589 |
///\ref addSource(). Finally the actual path computation can be |
| 590 | 590 |
///performed with one of the \ref start() functions. |
| 591 | 591 |
|
| 592 | 592 |
///@{
|
| 593 | 593 |
|
| 594 | 594 |
///\brief Initializes the internal data structures. |
| 595 | 595 |
/// |
| 596 | 596 |
///Initializes the internal data structures. |
| 597 | 597 |
void init() |
| 598 | 598 |
{
|
| 599 | 599 |
create_maps(); |
| 600 | 600 |
_heap->clear(); |
| 601 | 601 |
for ( NodeIt u(*G) ; u!=INVALID ; ++u ) {
|
| 602 | 602 |
_pred->set(u,INVALID); |
| 603 | 603 |
_processed->set(u,false); |
| 604 | 604 |
_heap_cross_ref->set(u,Heap::PRE_HEAP); |
| 605 | 605 |
} |
| 606 | 606 |
} |
| 607 | 607 |
|
| 608 | 608 |
///Adds a new source node. |
| 609 | 609 |
|
| 610 | 610 |
///Adds a new source node to the priority heap. |
| 611 | 611 |
///The optional second parameter is the initial distance of the node. |
| 612 | 612 |
/// |
| 613 | 613 |
///The function checks if the node has already been added to the heap and |
| 614 | 614 |
///it is pushed to the heap only if either it was not in the heap |
| 615 | 615 |
///or the shortest path found till then is shorter than \c dst. |
| 616 | 616 |
void addSource(Node s,Value dst=OperationTraits::zero()) |
| 617 | 617 |
{
|
| 618 | 618 |
if(_heap->state(s) != Heap::IN_HEAP) {
|
| 619 | 619 |
_heap->push(s,dst); |
| 620 | 620 |
} else if(OperationTraits::less((*_heap)[s], dst)) {
|
| 621 | 621 |
_heap->set(s,dst); |
| 622 | 622 |
_pred->set(s,INVALID); |
| 623 | 623 |
} |
| 624 | 624 |
} |
| 625 | 625 |
|
| 626 | 626 |
///Processes the next node in the priority heap |
| 627 | 627 |
|
| 628 | 628 |
///Processes the next node in the priority heap. |
| 629 | 629 |
/// |
| 630 | 630 |
///\return The processed node. |
| 631 | 631 |
/// |
| 632 | 632 |
///\warning The priority heap must not be empty. |
| 633 | 633 |
Node processNextNode() |
| 634 | 634 |
{
|
| 635 | 635 |
Node v=_heap->top(); |
| 636 | 636 |
Value oldvalue=_heap->prio(); |
| 637 | 637 |
_heap->pop(); |
| 638 | 638 |
finalizeNodeData(v,oldvalue); |
| 639 | 639 |
|
| 640 | 640 |
for(OutArcIt e(*G,v); e!=INVALID; ++e) {
|
| 641 | 641 |
Node w=G->target(e); |
| 642 | 642 |
switch(_heap->state(w)) {
|
| 643 | 643 |
case Heap::PRE_HEAP: |
| 644 | 644 |
_heap->push(w,OperationTraits::plus(oldvalue, (*_length)[e])); |
| 645 | 645 |
_pred->set(w,e); |
| 646 | 646 |
break; |
| 647 | 647 |
case Heap::IN_HEAP: |
| 648 | 648 |
{
|
| 649 | 649 |
Value newvalue = OperationTraits::plus(oldvalue, (*_length)[e]); |
| 650 | 650 |
if ( OperationTraits::less(newvalue, (*_heap)[w]) ) {
|
| 651 | 651 |
_heap->decrease(w, newvalue); |
| 652 | 652 |
_pred->set(w,e); |
| 653 | 653 |
} |
| 654 | 654 |
} |
| 655 | 655 |
break; |
| 656 | 656 |
case Heap::POST_HEAP: |
| 657 | 657 |
break; |
| 658 | 658 |
} |
| 659 | 659 |
} |
| 660 | 660 |
return v; |
| 661 | 661 |
} |
| 662 | 662 |
|
| 663 | 663 |
///The next node to be processed. |
| 664 | 664 |
|
| 665 | 665 |
///Returns the next node to be processed or \c INVALID if the |
| 666 | 666 |
///priority heap is empty. |
| 667 | 667 |
Node nextNode() const |
| 668 | 668 |
{
|
| 669 | 669 |
return !_heap->empty()?_heap->top():INVALID; |
| 670 | 670 |
} |
| 671 | 671 |
|
| 672 | 672 |
///Returns \c false if there are nodes to be processed. |
| 673 | 673 |
|
| 674 | 674 |
///Returns \c false if there are nodes to be processed |
| 675 | 675 |
///in the priority heap. |
| 676 | 676 |
bool emptyQueue() const { return _heap->empty(); }
|
| 677 | 677 |
|
| 678 | 678 |
///Returns the number of the nodes to be processed. |
| 679 | 679 |
|
| 680 | 680 |
///Returns the number of the nodes to be processed |
| 681 | 681 |
///in the priority heap. |
| 682 | 682 |
int queueSize() const { return _heap->size(); }
|
| 683 | 683 |
|
| 684 | 684 |
///Executes the algorithm. |
| ... | ... |
@@ -266,293 +266,293 @@ |
| 266 | 266 |
|
| 267 | 267 |
while (sn != tn) {
|
| 268 | 268 |
if ((*_order)[sn] < (*_order)[tn]) {
|
| 269 | 269 |
if ((*_weight)[tn] <= value) value = (*_weight)[tn]; |
| 270 | 270 |
tn = (*_pred)[tn]; |
| 271 | 271 |
} else {
|
| 272 | 272 |
if ((*_weight)[sn] <= value) value = (*_weight)[sn]; |
| 273 | 273 |
sn = (*_pred)[sn]; |
| 274 | 274 |
} |
| 275 | 275 |
} |
| 276 | 276 |
return value; |
| 277 | 277 |
} |
| 278 | 278 |
|
| 279 | 279 |
/// \brief Return the minimum cut between two nodes |
| 280 | 280 |
/// |
| 281 | 281 |
/// This function returns the minimum cut between the nodes \c s and \c t |
| 282 | 282 |
/// in the \c cutMap parameter by setting the nodes in the component of |
| 283 | 283 |
/// \c s to \c true and the other nodes to \c false. |
| 284 | 284 |
/// |
| 285 | 285 |
/// For higher level interfaces see MinCutNodeIt and MinCutEdgeIt. |
| 286 | 286 |
/// |
| 287 | 287 |
/// \param s The base node. |
| 288 | 288 |
/// \param t The node you want to separate from node \c s. |
| 289 | 289 |
/// \param cutMap The cut will be returned in this map. |
| 290 | 290 |
/// It must be a \c bool (or convertible) \ref concepts::ReadWriteMap |
| 291 | 291 |
/// "ReadWriteMap" on the graph nodes. |
| 292 | 292 |
/// |
| 293 | 293 |
/// \return The value of the minimum cut between \c s and \c t. |
| 294 | 294 |
/// |
| 295 | 295 |
/// \pre \ref run() must be called before using this function. |
| 296 | 296 |
template <typename CutMap> |
| 297 | 297 |
Value minCutMap(const Node& s, ///< |
| 298 | 298 |
const Node& t, |
| 299 | 299 |
///< |
| 300 | 300 |
CutMap& cutMap |
| 301 | 301 |
///< |
| 302 | 302 |
) const {
|
| 303 | 303 |
Node sn = s, tn = t; |
| 304 | 304 |
bool s_root=false; |
| 305 | 305 |
Node rn = INVALID; |
| 306 | 306 |
Value value = std::numeric_limits<Value>::max(); |
| 307 | 307 |
|
| 308 | 308 |
while (sn != tn) {
|
| 309 | 309 |
if ((*_order)[sn] < (*_order)[tn]) {
|
| 310 | 310 |
if ((*_weight)[tn] <= value) {
|
| 311 | 311 |
rn = tn; |
| 312 | 312 |
s_root = false; |
| 313 | 313 |
value = (*_weight)[tn]; |
| 314 | 314 |
} |
| 315 | 315 |
tn = (*_pred)[tn]; |
| 316 | 316 |
} else {
|
| 317 | 317 |
if ((*_weight)[sn] <= value) {
|
| 318 | 318 |
rn = sn; |
| 319 | 319 |
s_root = true; |
| 320 | 320 |
value = (*_weight)[sn]; |
| 321 | 321 |
} |
| 322 | 322 |
sn = (*_pred)[sn]; |
| 323 | 323 |
} |
| 324 | 324 |
} |
| 325 | 325 |
|
| 326 | 326 |
typename Graph::template NodeMap<bool> reached(_graph, false); |
| 327 | 327 |
reached[_root] = true; |
| 328 | 328 |
cutMap.set(_root, !s_root); |
| 329 | 329 |
reached[rn] = true; |
| 330 | 330 |
cutMap.set(rn, s_root); |
| 331 | 331 |
|
| 332 | 332 |
std::vector<Node> st; |
| 333 | 333 |
for (NodeIt n(_graph); n != INVALID; ++n) {
|
| 334 | 334 |
st.clear(); |
| 335 | 335 |
Node nn = n; |
| 336 | 336 |
while (!reached[nn]) {
|
| 337 | 337 |
st.push_back(nn); |
| 338 | 338 |
nn = (*_pred)[nn]; |
| 339 | 339 |
} |
| 340 | 340 |
while (!st.empty()) {
|
| 341 | 341 |
cutMap.set(st.back(), cutMap[nn]); |
| 342 | 342 |
st.pop_back(); |
| 343 | 343 |
} |
| 344 | 344 |
} |
| 345 | 345 |
|
| 346 | 346 |
return value; |
| 347 | 347 |
} |
| 348 | 348 |
|
| 349 | 349 |
///@} |
| 350 | 350 |
|
| 351 | 351 |
friend class MinCutNodeIt; |
| 352 | 352 |
|
| 353 | 353 |
/// Iterate on the nodes of a minimum cut |
| 354 | 354 |
|
| 355 | 355 |
/// This iterator class lists the nodes of a minimum cut found by |
| 356 | 356 |
/// GomoryHu. Before using it, you must allocate a GomoryHu class |
| 357 | 357 |
/// and call its \ref GomoryHu::run() "run()" method. |
| 358 | 358 |
/// |
| 359 | 359 |
/// This example counts the nodes in the minimum cut separating \c s from |
| 360 | 360 |
/// \c t. |
| 361 | 361 |
/// \code |
| 362 |
/// |
|
| 362 |
/// GomoryHu<Graph> gom(g, capacities); |
|
| 363 | 363 |
/// gom.run(); |
| 364 | 364 |
/// int cnt=0; |
| 365 |
/// for( |
|
| 365 |
/// for(GomoryHu<Graph>::MinCutNodeIt n(gom,s,t); n!=INVALID; ++n) ++cnt; |
|
| 366 | 366 |
/// \endcode |
| 367 | 367 |
class MinCutNodeIt |
| 368 | 368 |
{
|
| 369 | 369 |
bool _side; |
| 370 | 370 |
typename Graph::NodeIt _node_it; |
| 371 | 371 |
typename Graph::template NodeMap<bool> _cut; |
| 372 | 372 |
public: |
| 373 | 373 |
/// Constructor |
| 374 | 374 |
|
| 375 | 375 |
/// Constructor. |
| 376 | 376 |
/// |
| 377 | 377 |
MinCutNodeIt(GomoryHu const &gomory, |
| 378 | 378 |
///< The GomoryHu class. You must call its |
| 379 | 379 |
/// run() method |
| 380 | 380 |
/// before initializing this iterator. |
| 381 | 381 |
const Node& s, ///< The base node. |
| 382 | 382 |
const Node& t, |
| 383 | 383 |
///< The node you want to separate from node \c s. |
| 384 | 384 |
bool side=true |
| 385 | 385 |
///< If it is \c true (default) then the iterator lists |
| 386 | 386 |
/// the nodes of the component containing \c s, |
| 387 | 387 |
/// otherwise it lists the other component. |
| 388 | 388 |
/// \note As the minimum cut is not always unique, |
| 389 | 389 |
/// \code |
| 390 | 390 |
/// MinCutNodeIt(gomory, s, t, true); |
| 391 | 391 |
/// \endcode |
| 392 | 392 |
/// and |
| 393 | 393 |
/// \code |
| 394 | 394 |
/// MinCutNodeIt(gomory, t, s, false); |
| 395 | 395 |
/// \endcode |
| 396 | 396 |
/// does not necessarily give the same set of nodes. |
| 397 | 397 |
/// However it is ensured that |
| 398 | 398 |
/// \code |
| 399 | 399 |
/// MinCutNodeIt(gomory, s, t, true); |
| 400 | 400 |
/// \endcode |
| 401 | 401 |
/// and |
| 402 | 402 |
/// \code |
| 403 | 403 |
/// MinCutNodeIt(gomory, s, t, false); |
| 404 | 404 |
/// \endcode |
| 405 | 405 |
/// together list each node exactly once. |
| 406 | 406 |
) |
| 407 | 407 |
: _side(side), _cut(gomory._graph) |
| 408 | 408 |
{
|
| 409 | 409 |
gomory.minCutMap(s,t,_cut); |
| 410 | 410 |
for(_node_it=typename Graph::NodeIt(gomory._graph); |
| 411 | 411 |
_node_it!=INVALID && _cut[_node_it]!=_side; |
| 412 | 412 |
++_node_it) {}
|
| 413 | 413 |
} |
| 414 | 414 |
/// Conversion to \c Node |
| 415 | 415 |
|
| 416 | 416 |
/// Conversion to \c Node. |
| 417 | 417 |
/// |
| 418 | 418 |
operator typename Graph::Node() const |
| 419 | 419 |
{
|
| 420 | 420 |
return _node_it; |
| 421 | 421 |
} |
| 422 | 422 |
bool operator==(Invalid) { return _node_it==INVALID; }
|
| 423 | 423 |
bool operator!=(Invalid) { return _node_it!=INVALID; }
|
| 424 | 424 |
/// Next node |
| 425 | 425 |
|
| 426 | 426 |
/// Next node. |
| 427 | 427 |
/// |
| 428 | 428 |
MinCutNodeIt &operator++() |
| 429 | 429 |
{
|
| 430 | 430 |
for(++_node_it;_node_it!=INVALID&&_cut[_node_it]!=_side;++_node_it) {}
|
| 431 | 431 |
return *this; |
| 432 | 432 |
} |
| 433 | 433 |
/// Postfix incrementation |
| 434 | 434 |
|
| 435 | 435 |
/// Postfix incrementation. |
| 436 | 436 |
/// |
| 437 | 437 |
/// \warning This incrementation |
| 438 | 438 |
/// returns a \c Node, not a \c MinCutNodeIt, as one may |
| 439 | 439 |
/// expect. |
| 440 | 440 |
typename Graph::Node operator++(int) |
| 441 | 441 |
{
|
| 442 | 442 |
typename Graph::Node n=*this; |
| 443 | 443 |
++(*this); |
| 444 | 444 |
return n; |
| 445 | 445 |
} |
| 446 | 446 |
}; |
| 447 | 447 |
|
| 448 | 448 |
friend class MinCutEdgeIt; |
| 449 | 449 |
|
| 450 | 450 |
/// Iterate on the edges of a minimum cut |
| 451 | 451 |
|
| 452 | 452 |
/// This iterator class lists the edges of a minimum cut found by |
| 453 | 453 |
/// GomoryHu. Before using it, you must allocate a GomoryHu class |
| 454 | 454 |
/// and call its \ref GomoryHu::run() "run()" method. |
| 455 | 455 |
/// |
| 456 | 456 |
/// This example computes the value of the minimum cut separating \c s from |
| 457 | 457 |
/// \c t. |
| 458 | 458 |
/// \code |
| 459 |
/// |
|
| 459 |
/// GomoryHu<Graph> gom(g, capacities); |
|
| 460 | 460 |
/// gom.run(); |
| 461 | 461 |
/// int value=0; |
| 462 |
/// for( |
|
| 462 |
/// for(GomoryHu<Graph>::MinCutEdgeIt e(gom,s,t); e!=INVALID; ++e) |
|
| 463 | 463 |
/// value+=capacities[e]; |
| 464 | 464 |
/// \endcode |
| 465 | 465 |
/// The result will be the same as the value returned by |
| 466 | 466 |
/// \ref GomoryHu::minCutValue() "gom.minCutValue(s,t)". |
| 467 | 467 |
class MinCutEdgeIt |
| 468 | 468 |
{
|
| 469 | 469 |
bool _side; |
| 470 | 470 |
const Graph &_graph; |
| 471 | 471 |
typename Graph::NodeIt _node_it; |
| 472 | 472 |
typename Graph::OutArcIt _arc_it; |
| 473 | 473 |
typename Graph::template NodeMap<bool> _cut; |
| 474 | 474 |
void step() |
| 475 | 475 |
{
|
| 476 | 476 |
++_arc_it; |
| 477 | 477 |
while(_node_it!=INVALID && _arc_it==INVALID) |
| 478 | 478 |
{
|
| 479 | 479 |
for(++_node_it;_node_it!=INVALID&&!_cut[_node_it];++_node_it) {}
|
| 480 | 480 |
if(_node_it!=INVALID) |
| 481 | 481 |
_arc_it=typename Graph::OutArcIt(_graph,_node_it); |
| 482 | 482 |
} |
| 483 | 483 |
} |
| 484 | 484 |
|
| 485 | 485 |
public: |
| 486 | 486 |
/// Constructor |
| 487 | 487 |
|
| 488 | 488 |
/// Constructor. |
| 489 | 489 |
/// |
| 490 | 490 |
MinCutEdgeIt(GomoryHu const &gomory, |
| 491 | 491 |
///< The GomoryHu class. You must call its |
| 492 | 492 |
/// run() method |
| 493 | 493 |
/// before initializing this iterator. |
| 494 | 494 |
const Node& s, ///< The base node. |
| 495 | 495 |
const Node& t, |
| 496 | 496 |
///< The node you want to separate from node \c s. |
| 497 | 497 |
bool side=true |
| 498 | 498 |
///< If it is \c true (default) then the listed arcs |
| 499 | 499 |
/// will be oriented from the |
| 500 | 500 |
/// nodes of the component containing \c s, |
| 501 | 501 |
/// otherwise they will be oriented in the opposite |
| 502 | 502 |
/// direction. |
| 503 | 503 |
) |
| 504 | 504 |
: _graph(gomory._graph), _cut(_graph) |
| 505 | 505 |
{
|
| 506 | 506 |
gomory.minCutMap(s,t,_cut); |
| 507 | 507 |
if(!side) |
| 508 | 508 |
for(typename Graph::NodeIt n(_graph);n!=INVALID;++n) |
| 509 | 509 |
_cut[n]=!_cut[n]; |
| 510 | 510 |
|
| 511 | 511 |
for(_node_it=typename Graph::NodeIt(_graph); |
| 512 | 512 |
_node_it!=INVALID && !_cut[_node_it]; |
| 513 | 513 |
++_node_it) {}
|
| 514 | 514 |
_arc_it = _node_it!=INVALID ? |
| 515 | 515 |
typename Graph::OutArcIt(_graph,_node_it) : INVALID; |
| 516 | 516 |
while(_node_it!=INVALID && _arc_it == INVALID) |
| 517 | 517 |
{
|
| 518 | 518 |
for(++_node_it; _node_it!=INVALID&&!_cut[_node_it]; ++_node_it) {}
|
| 519 | 519 |
if(_node_it!=INVALID) |
| 520 | 520 |
_arc_it= typename Graph::OutArcIt(_graph,_node_it); |
| 521 | 521 |
} |
| 522 | 522 |
while(_arc_it!=INVALID && _cut[_graph.target(_arc_it)]) step(); |
| 523 | 523 |
} |
| 524 | 524 |
/// Conversion to \c Arc |
| 525 | 525 |
|
| 526 | 526 |
/// Conversion to \c Arc. |
| 527 | 527 |
/// |
| 528 | 528 |
operator typename Graph::Arc() const |
| 529 | 529 |
{
|
| 530 | 530 |
return _arc_it; |
| 531 | 531 |
} |
| 532 | 532 |
/// Conversion to \c Edge |
| 533 | 533 |
|
| 534 | 534 |
/// Conversion to \c Edge. |
| 535 | 535 |
/// |
| 536 | 536 |
operator typename Graph::Edge() const |
| 537 | 537 |
{
|
| 538 | 538 |
return _arc_it; |
| 539 | 539 |
} |
| 540 | 540 |
bool operator==(Invalid) { return _node_it==INVALID; }
|
| 541 | 541 |
bool operator!=(Invalid) { return _node_it!=INVALID; }
|
| 542 | 542 |
/// Next edge |
| 543 | 543 |
|
| 544 | 544 |
/// Next edge. |
| 545 | 545 |
/// |
| 546 | 546 |
MinCutEdgeIt &operator++() |
| 547 | 547 |
{
|
| 548 | 548 |
step(); |
| 549 | 549 |
while(_arc_it!=INVALID && _cut[_graph.target(_arc_it)]) step(); |
| 550 | 550 |
return *this; |
| 551 | 551 |
} |
| 552 | 552 |
/// Postfix incrementation |
| 553 | 553 |
|
| 554 | 554 |
/// Postfix incrementation. |
| 555 | 555 |
/// |
| 556 | 556 |
/// \warning This incrementation |
| 557 | 557 |
/// returns an \c Arc, not a \c MinCutEdgeIt, as one may expect. |
| 558 | 558 |
typename Graph::Arc operator++(int) |
| ... | ... |
@@ -395,194 +395,194 @@ |
| 395 | 395 |
|
| 396 | 396 |
/// @{
|
| 397 | 397 |
|
| 398 | 398 |
template <class T> |
| 399 | 399 |
struct SetArborescenceMapTraits : public Traits {
|
| 400 | 400 |
typedef T ArborescenceMap; |
| 401 | 401 |
static ArborescenceMap *createArborescenceMap(const Digraph &) |
| 402 | 402 |
{
|
| 403 | 403 |
LEMON_ASSERT(false, "ArborescenceMap is not initialized"); |
| 404 | 404 |
return 0; // ignore warnings |
| 405 | 405 |
} |
| 406 | 406 |
}; |
| 407 | 407 |
|
| 408 | 408 |
/// \brief \ref named-templ-param "Named parameter" for |
| 409 | 409 |
/// setting \c ArborescenceMap type |
| 410 | 410 |
/// |
| 411 | 411 |
/// \ref named-templ-param "Named parameter" for setting |
| 412 | 412 |
/// \c ArborescenceMap type. |
| 413 | 413 |
/// It must conform to the \ref concepts::WriteMap "WriteMap" concept, |
| 414 | 414 |
/// and its value type must be \c bool (or convertible). |
| 415 | 415 |
/// Initially it will be set to \c false on each arc, |
| 416 | 416 |
/// then it will be set on each arborescence arc once. |
| 417 | 417 |
template <class T> |
| 418 | 418 |
struct SetArborescenceMap |
| 419 | 419 |
: public MinCostArborescence<Digraph, CostMap, |
| 420 | 420 |
SetArborescenceMapTraits<T> > {
|
| 421 | 421 |
}; |
| 422 | 422 |
|
| 423 | 423 |
template <class T> |
| 424 | 424 |
struct SetPredMapTraits : public Traits {
|
| 425 | 425 |
typedef T PredMap; |
| 426 | 426 |
static PredMap *createPredMap(const Digraph &) |
| 427 | 427 |
{
|
| 428 | 428 |
LEMON_ASSERT(false, "PredMap is not initialized"); |
| 429 | 429 |
return 0; // ignore warnings |
| 430 | 430 |
} |
| 431 | 431 |
}; |
| 432 | 432 |
|
| 433 | 433 |
/// \brief \ref named-templ-param "Named parameter" for |
| 434 | 434 |
/// setting \c PredMap type |
| 435 | 435 |
/// |
| 436 | 436 |
/// \ref named-templ-param "Named parameter" for setting |
| 437 | 437 |
/// \c PredMap type. |
| 438 | 438 |
/// It must meet the \ref concepts::WriteMap "WriteMap" concept, |
| 439 | 439 |
/// and its value type must be the \c Arc type of the digraph. |
| 440 | 440 |
template <class T> |
| 441 | 441 |
struct SetPredMap |
| 442 | 442 |
: public MinCostArborescence<Digraph, CostMap, SetPredMapTraits<T> > {
|
| 443 | 443 |
}; |
| 444 | 444 |
|
| 445 | 445 |
/// @} |
| 446 | 446 |
|
| 447 | 447 |
/// \brief Constructor. |
| 448 | 448 |
/// |
| 449 | 449 |
/// \param digraph The digraph the algorithm will run on. |
| 450 | 450 |
/// \param cost The cost map used by the algorithm. |
| 451 | 451 |
MinCostArborescence(const Digraph& digraph, const CostMap& cost) |
| 452 | 452 |
: _digraph(&digraph), _cost(&cost), _pred(0), local_pred(false), |
| 453 | 453 |
_arborescence(0), local_arborescence(false), |
| 454 | 454 |
_arc_order(0), _node_order(0), _cost_arcs(0), |
| 455 | 455 |
_heap_cross_ref(0), _heap(0) {}
|
| 456 | 456 |
|
| 457 | 457 |
/// \brief Destructor. |
| 458 | 458 |
~MinCostArborescence() {
|
| 459 | 459 |
destroyStructures(); |
| 460 | 460 |
} |
| 461 | 461 |
|
| 462 | 462 |
/// \brief Sets the arborescence map. |
| 463 | 463 |
/// |
| 464 | 464 |
/// Sets the arborescence map. |
| 465 | 465 |
/// \return <tt>(*this)</tt> |
| 466 | 466 |
MinCostArborescence& arborescenceMap(ArborescenceMap& m) {
|
| 467 | 467 |
if (local_arborescence) {
|
| 468 | 468 |
delete _arborescence; |
| 469 | 469 |
} |
| 470 | 470 |
local_arborescence = false; |
| 471 | 471 |
_arborescence = &m; |
| 472 | 472 |
return *this; |
| 473 | 473 |
} |
| 474 | 474 |
|
| 475 | 475 |
/// \brief Sets the predecessor map. |
| 476 | 476 |
/// |
| 477 | 477 |
/// Sets the predecessor map. |
| 478 | 478 |
/// \return <tt>(*this)</tt> |
| 479 | 479 |
MinCostArborescence& predMap(PredMap& m) {
|
| 480 | 480 |
if (local_pred) {
|
| 481 | 481 |
delete _pred; |
| 482 | 482 |
} |
| 483 | 483 |
local_pred = false; |
| 484 | 484 |
_pred = &m; |
| 485 | 485 |
return *this; |
| 486 | 486 |
} |
| 487 | 487 |
|
| 488 | 488 |
/// \name Execution Control |
| 489 | 489 |
/// The simplest way to execute the algorithm is to use |
| 490 | 490 |
/// one of the member functions called \c run(...). \n |
| 491 |
/// If you need more control on the execution, |
|
| 492 |
/// first you must call \ref init(), then you can add several |
|
| 491 |
/// If you need better control on the execution, |
|
| 492 |
/// you have to call \ref init() first, then you can add several |
|
| 493 | 493 |
/// source nodes with \ref addSource(). |
| 494 | 494 |
/// Finally \ref start() will perform the arborescence |
| 495 | 495 |
/// computation. |
| 496 | 496 |
|
| 497 | 497 |
///@{
|
| 498 | 498 |
|
| 499 | 499 |
/// \brief Initializes the internal data structures. |
| 500 | 500 |
/// |
| 501 | 501 |
/// Initializes the internal data structures. |
| 502 | 502 |
/// |
| 503 | 503 |
void init() {
|
| 504 | 504 |
createStructures(); |
| 505 | 505 |
_heap->clear(); |
| 506 | 506 |
for (NodeIt it(*_digraph); it != INVALID; ++it) {
|
| 507 | 507 |
(*_cost_arcs)[it].arc = INVALID; |
| 508 | 508 |
(*_node_order)[it] = -3; |
| 509 | 509 |
(*_heap_cross_ref)[it] = Heap::PRE_HEAP; |
| 510 | 510 |
_pred->set(it, INVALID); |
| 511 | 511 |
} |
| 512 | 512 |
for (ArcIt it(*_digraph); it != INVALID; ++it) {
|
| 513 | 513 |
_arborescence->set(it, false); |
| 514 | 514 |
(*_arc_order)[it] = -1; |
| 515 | 515 |
} |
| 516 | 516 |
_dual_node_list.clear(); |
| 517 | 517 |
_dual_variables.clear(); |
| 518 | 518 |
} |
| 519 | 519 |
|
| 520 | 520 |
/// \brief Adds a new source node. |
| 521 | 521 |
/// |
| 522 | 522 |
/// Adds a new source node to the algorithm. |
| 523 | 523 |
void addSource(Node source) {
|
| 524 | 524 |
std::vector<Node> nodes; |
| 525 | 525 |
nodes.push_back(source); |
| 526 | 526 |
while (!nodes.empty()) {
|
| 527 | 527 |
Node node = nodes.back(); |
| 528 | 528 |
nodes.pop_back(); |
| 529 | 529 |
for (OutArcIt it(*_digraph, node); it != INVALID; ++it) {
|
| 530 | 530 |
Node target = _digraph->target(it); |
| 531 | 531 |
if ((*_node_order)[target] == -3) {
|
| 532 | 532 |
(*_node_order)[target] = -2; |
| 533 | 533 |
nodes.push_back(target); |
| 534 | 534 |
queue.push_back(target); |
| 535 | 535 |
} |
| 536 | 536 |
} |
| 537 | 537 |
} |
| 538 | 538 |
(*_node_order)[source] = -1; |
| 539 | 539 |
} |
| 540 | 540 |
|
| 541 | 541 |
/// \brief Processes the next node in the priority queue. |
| 542 | 542 |
/// |
| 543 | 543 |
/// Processes the next node in the priority queue. |
| 544 | 544 |
/// |
| 545 | 545 |
/// \return The processed node. |
| 546 | 546 |
/// |
| 547 | 547 |
/// \warning The queue must not be empty. |
| 548 | 548 |
Node processNextNode() {
|
| 549 | 549 |
Node node = queue.back(); |
| 550 | 550 |
queue.pop_back(); |
| 551 | 551 |
if ((*_node_order)[node] == -2) {
|
| 552 | 552 |
Arc arc = prepare(node); |
| 553 | 553 |
Node source = _digraph->source(arc); |
| 554 | 554 |
while ((*_node_order)[source] != -1) {
|
| 555 | 555 |
if ((*_node_order)[source] >= 0) {
|
| 556 | 556 |
arc = contract(source); |
| 557 | 557 |
} else {
|
| 558 | 558 |
arc = prepare(source); |
| 559 | 559 |
} |
| 560 | 560 |
source = _digraph->source(arc); |
| 561 | 561 |
} |
| 562 | 562 |
finalize(arc); |
| 563 | 563 |
level_stack.clear(); |
| 564 | 564 |
} |
| 565 | 565 |
return node; |
| 566 | 566 |
} |
| 567 | 567 |
|
| 568 | 568 |
/// \brief Returns the number of the nodes to be processed. |
| 569 | 569 |
/// |
| 570 | 570 |
/// Returns the number of the nodes to be processed in the priority |
| 571 | 571 |
/// queue. |
| 572 | 572 |
int queueSize() const {
|
| 573 | 573 |
return queue.size(); |
| 574 | 574 |
} |
| 575 | 575 |
|
| 576 | 576 |
/// \brief Returns \c false if there are nodes to be processed. |
| 577 | 577 |
/// |
| 578 | 578 |
/// Returns \c false if there are nodes to be processed. |
| 579 | 579 |
bool emptyQueue() const {
|
| 580 | 580 |
return queue.empty(); |
| 581 | 581 |
} |
| 582 | 582 |
|
| 583 | 583 |
/// \brief Executes the algorithm. |
| 584 | 584 |
/// |
| 585 | 585 |
/// Executes the algorithm. |
| 586 | 586 |
/// |
| 587 | 587 |
/// \pre init() must be called and at least one node should be added |
| 588 | 588 |
/// with addSource() before using this function. |
| 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-2009 |
| 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_PREFLOW_H |
| 20 | 20 |
#define LEMON_PREFLOW_H |
| 21 | 21 |
|
| 22 | 22 |
#include <lemon/tolerance.h> |
| 23 | 23 |
#include <lemon/elevator.h> |
| 24 | 24 |
|
| 25 | 25 |
/// \file |
| 26 | 26 |
/// \ingroup max_flow |
| 27 | 27 |
/// \brief Implementation of the preflow algorithm. |
| 28 | 28 |
|
| 29 | 29 |
namespace lemon {
|
| 30 | 30 |
|
| 31 | 31 |
/// \brief Default traits class of Preflow class. |
| 32 | 32 |
/// |
| 33 | 33 |
/// Default traits class of Preflow class. |
| 34 | 34 |
/// \tparam GR Digraph type. |
| 35 | 35 |
/// \tparam CAP Capacity map type. |
| 36 | 36 |
template <typename GR, typename CAP> |
| 37 | 37 |
struct PreflowDefaultTraits {
|
| 38 | 38 |
|
| 39 | 39 |
/// \brief The type of the digraph the algorithm runs on. |
| 40 | 40 |
typedef GR Digraph; |
| 41 | 41 |
|
| 42 | 42 |
/// \brief The type of the map that stores the arc capacities. |
| 43 | 43 |
/// |
| 44 | 44 |
/// The type of the map that stores the arc capacities. |
| 45 | 45 |
/// It must meet the \ref concepts::ReadMap "ReadMap" concept. |
| 46 | 46 |
typedef CAP CapacityMap; |
| 47 | 47 |
|
| 48 | 48 |
/// \brief The type of the flow values. |
| 49 | 49 |
typedef typename CapacityMap::Value Value; |
| 50 | 50 |
|
| 51 | 51 |
/// \brief The type of the map that stores the flow values. |
| 52 | 52 |
/// |
| 53 | 53 |
/// The type of the map that stores the flow values. |
| 54 | 54 |
/// It must meet the \ref concepts::ReadWriteMap "ReadWriteMap" concept. |
| 55 |
#ifdef DOXYGEN |
|
| 56 |
typedef GR::ArcMap<Value> FlowMap; |
|
| 57 |
#else |
|
| 55 | 58 |
typedef typename Digraph::template ArcMap<Value> FlowMap; |
| 59 |
#endif |
|
| 56 | 60 |
|
| 57 | 61 |
/// \brief Instantiates a FlowMap. |
| 58 | 62 |
/// |
| 59 | 63 |
/// This function instantiates a \ref FlowMap. |
| 60 | 64 |
/// \param digraph The digraph for which we would like to define |
| 61 | 65 |
/// the flow map. |
| 62 | 66 |
static FlowMap* createFlowMap(const Digraph& digraph) {
|
| 63 | 67 |
return new FlowMap(digraph); |
| 64 | 68 |
} |
| 65 | 69 |
|
| 66 | 70 |
/// \brief The elevator type used by Preflow algorithm. |
| 67 | 71 |
/// |
| 68 | 72 |
/// The elevator type used by Preflow algorithm. |
| 69 | 73 |
/// |
| 70 |
/// \sa Elevator |
|
| 71 |
/// \sa LinkedElevator |
|
| 72 |
|
|
| 74 |
/// \sa Elevator, LinkedElevator |
|
| 75 |
#ifdef DOXYGEN |
|
| 76 |
typedef lemon::Elevator<GR, GR::Node> Elevator; |
|
| 77 |
#else |
|
| 78 |
typedef lemon::Elevator<Digraph, typename Digraph::Node> Elevator; |
|
| 79 |
#endif |
|
| 73 | 80 |
|
| 74 | 81 |
/// \brief Instantiates an Elevator. |
| 75 | 82 |
/// |
| 76 | 83 |
/// This function instantiates an \ref Elevator. |
| 77 | 84 |
/// \param digraph The digraph for which we would like to define |
| 78 | 85 |
/// the elevator. |
| 79 | 86 |
/// \param max_level The maximum level of the elevator. |
| 80 | 87 |
static Elevator* createElevator(const Digraph& digraph, int max_level) {
|
| 81 | 88 |
return new Elevator(digraph, max_level); |
| 82 | 89 |
} |
| 83 | 90 |
|
| 84 | 91 |
/// \brief The tolerance used by the algorithm |
| 85 | 92 |
/// |
| 86 | 93 |
/// The tolerance used by the algorithm to handle inexact computation. |
| 87 | 94 |
typedef lemon::Tolerance<Value> Tolerance; |
| 88 | 95 |
|
| 89 | 96 |
}; |
| 90 | 97 |
|
| 91 | 98 |
|
| 92 | 99 |
/// \ingroup max_flow |
| 93 | 100 |
/// |
| 94 | 101 |
/// \brief %Preflow algorithm class. |
| 95 | 102 |
/// |
| 96 | 103 |
/// This class provides an implementation of Goldberg-Tarjan's \e preflow |
| 97 | 104 |
/// \e push-relabel algorithm producing a \ref max_flow |
| 98 | 105 |
/// "flow of maximum value" in a digraph. |
| 99 | 106 |
/// The preflow algorithms are the fastest known maximum |
| 100 | 107 |
/// flow algorithms. The current implementation use a mixture of the |
| 101 | 108 |
/// \e "highest label" and the \e "bound decrease" heuristics. |
| 102 | 109 |
/// The worst case time complexity of the algorithm is \f$O(n^2\sqrt{e})\f$.
|
| 103 | 110 |
/// |
| 104 | 111 |
/// The algorithm consists of two phases. After the first phase |
| 105 | 112 |
/// the maximum flow value and the minimum cut is obtained. The |
| 106 | 113 |
/// second phase constructs a feasible maximum flow on each arc. |
| 107 | 114 |
/// |
| 108 | 115 |
/// \tparam GR The type of the digraph the algorithm runs on. |
| 109 | 116 |
/// \tparam CAP The type of the capacity map. The default map |
| 110 | 117 |
/// type is \ref concepts::Digraph::ArcMap "GR::ArcMap<int>". |
| 111 | 118 |
#ifdef DOXYGEN |
| 112 | 119 |
template <typename GR, typename CAP, typename TR> |
| 113 | 120 |
#else |
| 114 | 121 |
template <typename GR, |
| 115 | 122 |
typename CAP = typename GR::template ArcMap<int>, |
| 116 | 123 |
typename TR = PreflowDefaultTraits<GR, CAP> > |
| 117 | 124 |
#endif |
| 118 | 125 |
class Preflow {
|
| 119 | 126 |
public: |
| 120 | 127 |
|
| 121 | 128 |
///The \ref PreflowDefaultTraits "traits class" of the algorithm. |
| 122 | 129 |
typedef TR Traits; |
| 123 | 130 |
///The type of the digraph the algorithm runs on. |
| 124 | 131 |
typedef typename Traits::Digraph Digraph; |
| 125 | 132 |
///The type of the capacity map. |
| 126 | 133 |
typedef typename Traits::CapacityMap CapacityMap; |
| 127 | 134 |
///The type of the flow values. |
| 128 | 135 |
typedef typename Traits::Value Value; |
| 129 | 136 |
|
| 130 | 137 |
///The type of the flow map. |
| 131 | 138 |
typedef typename Traits::FlowMap FlowMap; |
| 132 | 139 |
///The type of the elevator. |
| 133 | 140 |
typedef typename Traits::Elevator Elevator; |
| 134 | 141 |
///The type of the tolerance. |
| 135 | 142 |
typedef typename Traits::Tolerance Tolerance; |
| 136 | 143 |
|
| 137 | 144 |
private: |
| 138 | 145 |
|
| 139 | 146 |
TEMPLATE_DIGRAPH_TYPEDEFS(Digraph); |
| 140 | 147 |
|
| 141 | 148 |
const Digraph& _graph; |
| 142 | 149 |
const CapacityMap* _capacity; |
| 143 | 150 |
|
| 144 | 151 |
int _node_num; |
| 145 | 152 |
|
| 146 | 153 |
Node _source, _target; |
| 147 | 154 |
|
| 148 | 155 |
FlowMap* _flow; |
| 149 | 156 |
bool _local_flow; |
| 150 | 157 |
|
| 151 | 158 |
Elevator* _level; |
| 152 | 159 |
bool _local_level; |
| 153 | 160 |
|
| 154 | 161 |
typedef typename Digraph::template NodeMap<Value> ExcessMap; |
| 155 | 162 |
ExcessMap* _excess; |
| 156 | 163 |
|
| 157 | 164 |
Tolerance _tolerance; |
| 158 | 165 |
|
| 159 | 166 |
bool _phase; |
| 160 | 167 |
|
| 161 | 168 |
|
| 162 | 169 |
void createStructures() {
|
| 163 | 170 |
_node_num = countNodes(_graph); |
| 164 | 171 |
|
| 165 | 172 |
if (!_flow) {
|
| 166 | 173 |
_flow = Traits::createFlowMap(_graph); |
| 167 | 174 |
_local_flow = true; |
| 168 | 175 |
} |
| ... | ... |
@@ -296,194 +303,194 @@ |
| 296 | 303 |
/// \brief Destructor. |
| 297 | 304 |
/// |
| 298 | 305 |
/// Destructor. |
| 299 | 306 |
~Preflow() {
|
| 300 | 307 |
destroyStructures(); |
| 301 | 308 |
} |
| 302 | 309 |
|
| 303 | 310 |
/// \brief Sets the capacity map. |
| 304 | 311 |
/// |
| 305 | 312 |
/// Sets the capacity map. |
| 306 | 313 |
/// \return <tt>(*this)</tt> |
| 307 | 314 |
Preflow& capacityMap(const CapacityMap& map) {
|
| 308 | 315 |
_capacity = ↦ |
| 309 | 316 |
return *this; |
| 310 | 317 |
} |
| 311 | 318 |
|
| 312 | 319 |
/// \brief Sets the flow map. |
| 313 | 320 |
/// |
| 314 | 321 |
/// Sets the flow map. |
| 315 | 322 |
/// If you don't use this function before calling \ref run() or |
| 316 | 323 |
/// \ref init(), an instance will be allocated automatically. |
| 317 | 324 |
/// The destructor deallocates this automatically allocated map, |
| 318 | 325 |
/// of course. |
| 319 | 326 |
/// \return <tt>(*this)</tt> |
| 320 | 327 |
Preflow& flowMap(FlowMap& map) {
|
| 321 | 328 |
if (_local_flow) {
|
| 322 | 329 |
delete _flow; |
| 323 | 330 |
_local_flow = false; |
| 324 | 331 |
} |
| 325 | 332 |
_flow = ↦ |
| 326 | 333 |
return *this; |
| 327 | 334 |
} |
| 328 | 335 |
|
| 329 | 336 |
/// \brief Sets the source node. |
| 330 | 337 |
/// |
| 331 | 338 |
/// Sets the source node. |
| 332 | 339 |
/// \return <tt>(*this)</tt> |
| 333 | 340 |
Preflow& source(const Node& node) {
|
| 334 | 341 |
_source = node; |
| 335 | 342 |
return *this; |
| 336 | 343 |
} |
| 337 | 344 |
|
| 338 | 345 |
/// \brief Sets the target node. |
| 339 | 346 |
/// |
| 340 | 347 |
/// Sets the target node. |
| 341 | 348 |
/// \return <tt>(*this)</tt> |
| 342 | 349 |
Preflow& target(const Node& node) {
|
| 343 | 350 |
_target = node; |
| 344 | 351 |
return *this; |
| 345 | 352 |
} |
| 346 | 353 |
|
| 347 | 354 |
/// \brief Sets the elevator used by algorithm. |
| 348 | 355 |
/// |
| 349 | 356 |
/// Sets the elevator used by algorithm. |
| 350 | 357 |
/// If you don't use this function before calling \ref run() or |
| 351 | 358 |
/// \ref init(), an instance will be allocated automatically. |
| 352 | 359 |
/// The destructor deallocates this automatically allocated elevator, |
| 353 | 360 |
/// of course. |
| 354 | 361 |
/// \return <tt>(*this)</tt> |
| 355 | 362 |
Preflow& elevator(Elevator& elevator) {
|
| 356 | 363 |
if (_local_level) {
|
| 357 | 364 |
delete _level; |
| 358 | 365 |
_local_level = false; |
| 359 | 366 |
} |
| 360 | 367 |
_level = &elevator; |
| 361 | 368 |
return *this; |
| 362 | 369 |
} |
| 363 | 370 |
|
| 364 | 371 |
/// \brief Returns a const reference to the elevator. |
| 365 | 372 |
/// |
| 366 | 373 |
/// Returns a const reference to the elevator. |
| 367 | 374 |
/// |
| 368 | 375 |
/// \pre Either \ref run() or \ref init() must be called before |
| 369 | 376 |
/// using this function. |
| 370 | 377 |
const Elevator& elevator() const {
|
| 371 | 378 |
return *_level; |
| 372 | 379 |
} |
| 373 | 380 |
|
| 374 | 381 |
/// \brief Sets the tolerance used by algorithm. |
| 375 | 382 |
/// |
| 376 | 383 |
/// Sets the tolerance used by algorithm. |
| 377 | 384 |
Preflow& tolerance(const Tolerance& tolerance) const {
|
| 378 | 385 |
_tolerance = tolerance; |
| 379 | 386 |
return *this; |
| 380 | 387 |
} |
| 381 | 388 |
|
| 382 | 389 |
/// \brief Returns a const reference to the tolerance. |
| 383 | 390 |
/// |
| 384 | 391 |
/// Returns a const reference to the tolerance. |
| 385 | 392 |
const Tolerance& tolerance() const {
|
| 386 | 393 |
return tolerance; |
| 387 | 394 |
} |
| 388 | 395 |
|
| 389 | 396 |
/// \name Execution Control |
| 390 | 397 |
/// The simplest way to execute the preflow algorithm is to use |
| 391 | 398 |
/// \ref run() or \ref runMinCut().\n |
| 392 |
/// If you need more control on the initial solution or the execution, |
|
| 393 |
/// first you have to call one of the \ref init() functions, then |
|
| 399 |
/// If you need better control on the initial solution or the execution, |
|
| 400 |
/// you have to call one of the \ref init() functions first, then |
|
| 394 | 401 |
/// \ref startFirstPhase() and if you need it \ref startSecondPhase(). |
| 395 | 402 |
|
| 396 | 403 |
///@{
|
| 397 | 404 |
|
| 398 | 405 |
/// \brief Initializes the internal data structures. |
| 399 | 406 |
/// |
| 400 | 407 |
/// Initializes the internal data structures and sets the initial |
| 401 | 408 |
/// flow to zero on each arc. |
| 402 | 409 |
void init() {
|
| 403 | 410 |
createStructures(); |
| 404 | 411 |
|
| 405 | 412 |
_phase = true; |
| 406 | 413 |
for (NodeIt n(_graph); n != INVALID; ++n) {
|
| 407 | 414 |
(*_excess)[n] = 0; |
| 408 | 415 |
} |
| 409 | 416 |
|
| 410 | 417 |
for (ArcIt e(_graph); e != INVALID; ++e) {
|
| 411 | 418 |
_flow->set(e, 0); |
| 412 | 419 |
} |
| 413 | 420 |
|
| 414 | 421 |
typename Digraph::template NodeMap<bool> reached(_graph, false); |
| 415 | 422 |
|
| 416 | 423 |
_level->initStart(); |
| 417 | 424 |
_level->initAddItem(_target); |
| 418 | 425 |
|
| 419 | 426 |
std::vector<Node> queue; |
| 420 | 427 |
reached[_source] = true; |
| 421 | 428 |
|
| 422 | 429 |
queue.push_back(_target); |
| 423 | 430 |
reached[_target] = true; |
| 424 | 431 |
while (!queue.empty()) {
|
| 425 | 432 |
_level->initNewLevel(); |
| 426 | 433 |
std::vector<Node> nqueue; |
| 427 | 434 |
for (int i = 0; i < int(queue.size()); ++i) {
|
| 428 | 435 |
Node n = queue[i]; |
| 429 | 436 |
for (InArcIt e(_graph, n); e != INVALID; ++e) {
|
| 430 | 437 |
Node u = _graph.source(e); |
| 431 | 438 |
if (!reached[u] && _tolerance.positive((*_capacity)[e])) {
|
| 432 | 439 |
reached[u] = true; |
| 433 | 440 |
_level->initAddItem(u); |
| 434 | 441 |
nqueue.push_back(u); |
| 435 | 442 |
} |
| 436 | 443 |
} |
| 437 | 444 |
} |
| 438 | 445 |
queue.swap(nqueue); |
| 439 | 446 |
} |
| 440 | 447 |
_level->initFinish(); |
| 441 | 448 |
|
| 442 | 449 |
for (OutArcIt e(_graph, _source); e != INVALID; ++e) {
|
| 443 | 450 |
if (_tolerance.positive((*_capacity)[e])) {
|
| 444 | 451 |
Node u = _graph.target(e); |
| 445 | 452 |
if ((*_level)[u] == _level->maxLevel()) continue; |
| 446 | 453 |
_flow->set(e, (*_capacity)[e]); |
| 447 | 454 |
(*_excess)[u] += (*_capacity)[e]; |
| 448 | 455 |
if (u != _target && !_level->active(u)) {
|
| 449 | 456 |
_level->activate(u); |
| 450 | 457 |
} |
| 451 | 458 |
} |
| 452 | 459 |
} |
| 453 | 460 |
} |
| 454 | 461 |
|
| 455 | 462 |
/// \brief Initializes the internal data structures using the |
| 456 | 463 |
/// given flow map. |
| 457 | 464 |
/// |
| 458 | 465 |
/// Initializes the internal data structures and sets the initial |
| 459 | 466 |
/// flow to the given \c flowMap. The \c flowMap should contain a |
| 460 | 467 |
/// flow or at least a preflow, i.e. at each node excluding the |
| 461 | 468 |
/// source node the incoming flow should greater or equal to the |
| 462 | 469 |
/// outgoing flow. |
| 463 | 470 |
/// \return \c false if the given \c flowMap is not a preflow. |
| 464 | 471 |
template <typename FlowMap> |
| 465 | 472 |
bool init(const FlowMap& flowMap) {
|
| 466 | 473 |
createStructures(); |
| 467 | 474 |
|
| 468 | 475 |
for (ArcIt e(_graph); e != INVALID; ++e) {
|
| 469 | 476 |
_flow->set(e, flowMap[e]); |
| 470 | 477 |
} |
| 471 | 478 |
|
| 472 | 479 |
for (NodeIt n(_graph); n != INVALID; ++n) {
|
| 473 | 480 |
Value excess = 0; |
| 474 | 481 |
for (InArcIt e(_graph, n); e != INVALID; ++e) {
|
| 475 | 482 |
excess += (*_flow)[e]; |
| 476 | 483 |
} |
| 477 | 484 |
for (OutArcIt e(_graph, n); e != INVALID; ++e) {
|
| 478 | 485 |
excess -= (*_flow)[e]; |
| 479 | 486 |
} |
| 480 | 487 |
if (excess < 0 && n != _source) return false; |
| 481 | 488 |
(*_excess)[n] = excess; |
| 482 | 489 |
} |
| 483 | 490 |
|
| 484 | 491 |
typename Digraph::template NodeMap<bool> reached(_graph, false); |
| 485 | 492 |
|
| 486 | 493 |
_level->initStart(); |
| 487 | 494 |
_level->initAddItem(_target); |
| 488 | 495 |
|
| 489 | 496 |
std::vector<Node> queue; |
0 comments (0 inline)