| ... | ... |
@@ -293,265 +293,274 @@ |
| 293 | 293 |
like connectivity, bipartiteness, euler property, simplicity etc. |
| 294 | 294 |
|
| 295 | 295 |
\image html edge_biconnected_components.png |
| 296 | 296 |
\image latex edge_biconnected_components.eps "bi-edge-connected components" width=\textwidth |
| 297 | 297 |
*/ |
| 298 | 298 |
|
| 299 | 299 |
/** |
| 300 | 300 |
@defgroup planar Planarity Embedding and Drawing |
| 301 | 301 |
@ingroup algs |
| 302 | 302 |
\brief Algorithms for planarity checking, embedding and drawing |
| 303 | 303 |
|
| 304 | 304 |
This group describes the algorithms for planarity checking, |
| 305 | 305 |
embedding and drawing. |
| 306 | 306 |
|
| 307 | 307 |
\image html planar.png |
| 308 | 308 |
\image latex planar.eps "Plane graph" width=\textwidth |
| 309 | 309 |
*/ |
| 310 | 310 |
|
| 311 | 311 |
/** |
| 312 | 312 |
@defgroup matching Matching Algorithms |
| 313 | 313 |
@ingroup algs |
| 314 | 314 |
\brief Algorithms for finding matchings in graphs and bipartite graphs. |
| 315 | 315 |
|
| 316 | 316 |
This group contains algorithm objects and functions to calculate |
| 317 | 317 |
matchings in graphs and bipartite graphs. The general matching problem is |
| 318 | 318 |
finding a subset of the arcs which does not shares common endpoints. |
| 319 | 319 |
|
| 320 | 320 |
There are several different algorithms for calculate matchings in |
| 321 | 321 |
graphs. The matching problems in bipartite graphs are generally |
| 322 | 322 |
easier than in general graphs. The goal of the matching optimization |
| 323 | 323 |
can be the finding maximum cardinality, maximum weight or minimum cost |
| 324 | 324 |
matching. The search can be constrained to find perfect or |
| 325 | 325 |
maximum cardinality matching. |
| 326 | 326 |
|
| 327 | 327 |
LEMON contains the next algorithms: |
| 328 | 328 |
- \ref lemon::MaxBipartiteMatching "MaxBipartiteMatching" Hopcroft-Karp |
| 329 | 329 |
augmenting path algorithm for calculate maximum cardinality matching in |
| 330 | 330 |
bipartite graphs |
| 331 | 331 |
- \ref lemon::PrBipartiteMatching "PrBipartiteMatching" Push-Relabel |
| 332 | 332 |
algorithm for calculate maximum cardinality matching in bipartite graphs |
| 333 | 333 |
- \ref lemon::MaxWeightedBipartiteMatching "MaxWeightedBipartiteMatching" |
| 334 | 334 |
Successive shortest path algorithm for calculate maximum weighted matching |
| 335 | 335 |
and maximum weighted bipartite matching in bipartite graph |
| 336 | 336 |
- \ref lemon::MinCostMaxBipartiteMatching "MinCostMaxBipartiteMatching" |
| 337 | 337 |
Successive shortest path algorithm for calculate minimum cost maximum |
| 338 | 338 |
matching in bipartite graph |
| 339 | 339 |
- \ref lemon::MaxMatching "MaxMatching" Edmond's blossom shrinking algorithm |
| 340 | 340 |
for calculate maximum cardinality matching in general graph |
| 341 | 341 |
- \ref lemon::MaxWeightedMatching "MaxWeightedMatching" Edmond's blossom |
| 342 | 342 |
shrinking algorithm for calculate maximum weighted matching in general |
| 343 | 343 |
graph |
| 344 | 344 |
- \ref lemon::MaxWeightedPerfectMatching "MaxWeightedPerfectMatching" |
| 345 | 345 |
Edmond's blossom shrinking algorithm for calculate maximum weighted |
| 346 | 346 |
perfect matching in general graph |
| 347 | 347 |
|
| 348 | 348 |
\image html bipartite_matching.png |
| 349 | 349 |
\image latex bipartite_matching.eps "Bipartite Matching" width=\textwidth |
| 350 | 350 |
*/ |
| 351 | 351 |
|
| 352 | 352 |
/** |
| 353 | 353 |
@defgroup spantree Minimum Spanning Tree Algorithms |
| 354 | 354 |
@ingroup algs |
| 355 | 355 |
\brief Algorithms for finding a minimum cost spanning tree in a graph. |
| 356 | 356 |
|
| 357 | 357 |
This group describes the algorithms for finding a minimum cost spanning |
| 358 | 358 |
tree in a graph |
| 359 | 359 |
*/ |
| 360 | 360 |
|
| 361 | 361 |
/** |
| 362 | 362 |
@defgroup auxalg Auxiliary Algorithms |
| 363 | 363 |
@ingroup algs |
| 364 | 364 |
\brief Auxiliary algorithms implemented in LEMON. |
| 365 | 365 |
|
| 366 | 366 |
This group describes some algorithms implemented in LEMON |
| 367 | 367 |
in order to make it easier to implement complex algorithms. |
| 368 | 368 |
*/ |
| 369 | 369 |
|
| 370 | 370 |
/** |
| 371 | 371 |
@defgroup approx Approximation Algorithms |
| 372 | 372 |
@ingroup algs |
| 373 | 373 |
\brief Approximation algorithms. |
| 374 | 374 |
|
| 375 | 375 |
This group describes the approximation and heuristic algorithms |
| 376 | 376 |
implemented in LEMON. |
| 377 | 377 |
*/ |
| 378 | 378 |
|
| 379 | 379 |
/** |
| 380 | 380 |
@defgroup gen_opt_group General Optimization Tools |
| 381 | 381 |
\brief This group describes some general optimization frameworks |
| 382 | 382 |
implemented in LEMON. |
| 383 | 383 |
|
| 384 | 384 |
This group describes some general optimization frameworks |
| 385 | 385 |
implemented in LEMON. |
| 386 | 386 |
*/ |
| 387 | 387 |
|
| 388 | 388 |
/** |
| 389 | 389 |
@defgroup lp_group Lp and Mip Solvers |
| 390 | 390 |
@ingroup gen_opt_group |
| 391 | 391 |
\brief Lp and Mip solver interfaces for LEMON. |
| 392 | 392 |
|
| 393 | 393 |
This group describes Lp and Mip solver interfaces for LEMON. The |
| 394 | 394 |
various LP solvers could be used in the same manner with this |
| 395 | 395 |
interface. |
| 396 | 396 |
*/ |
| 397 | 397 |
|
| 398 | 398 |
/** |
| 399 | 399 |
@defgroup lp_utils Tools for Lp and Mip Solvers |
| 400 | 400 |
@ingroup lp_group |
| 401 | 401 |
\brief Helper tools to the Lp and Mip solvers. |
| 402 | 402 |
|
| 403 | 403 |
This group adds some helper tools to general optimization framework |
| 404 | 404 |
implemented in LEMON. |
| 405 | 405 |
*/ |
| 406 | 406 |
|
| 407 | 407 |
/** |
| 408 | 408 |
@defgroup metah Metaheuristics |
| 409 | 409 |
@ingroup gen_opt_group |
| 410 | 410 |
\brief Metaheuristics for LEMON library. |
| 411 | 411 |
|
| 412 | 412 |
This group describes some metaheuristic optimization tools. |
| 413 | 413 |
*/ |
| 414 | 414 |
|
| 415 | 415 |
/** |
| 416 | 416 |
@defgroup utils Tools and Utilities |
| 417 | 417 |
\brief Tools and utilities for programming in LEMON |
| 418 | 418 |
|
| 419 | 419 |
Tools and utilities for programming in LEMON. |
| 420 | 420 |
*/ |
| 421 | 421 |
|
| 422 | 422 |
/** |
| 423 | 423 |
@defgroup gutils Basic Graph Utilities |
| 424 | 424 |
@ingroup utils |
| 425 | 425 |
\brief Simple basic graph utilities. |
| 426 | 426 |
|
| 427 | 427 |
This group describes some simple basic graph utilities. |
| 428 | 428 |
*/ |
| 429 | 429 |
|
| 430 | 430 |
/** |
| 431 | 431 |
@defgroup misc Miscellaneous Tools |
| 432 | 432 |
@ingroup utils |
| 433 | 433 |
\brief Tools for development, debugging and testing. |
| 434 | 434 |
|
| 435 | 435 |
This group describes several useful tools for development, |
| 436 | 436 |
debugging and testing. |
| 437 | 437 |
*/ |
| 438 | 438 |
|
| 439 | 439 |
/** |
| 440 | 440 |
@defgroup timecount Time Measuring and Counting |
| 441 | 441 |
@ingroup misc |
| 442 | 442 |
\brief Simple tools for measuring the performance of algorithms. |
| 443 | 443 |
|
| 444 | 444 |
This group describes simple tools for measuring the performance |
| 445 | 445 |
of algorithms. |
| 446 | 446 |
*/ |
| 447 | 447 |
|
| 448 | 448 |
/** |
| 449 | 449 |
@defgroup exceptions Exceptions |
| 450 | 450 |
@ingroup utils |
| 451 | 451 |
\brief Exceptions defined in LEMON. |
| 452 | 452 |
|
| 453 | 453 |
This group describes the exceptions defined in LEMON. |
| 454 | 454 |
*/ |
| 455 | 455 |
|
| 456 | 456 |
/** |
| 457 | 457 |
@defgroup io_group Input-Output |
| 458 | 458 |
\brief Graph Input-Output methods |
| 459 | 459 |
|
| 460 | 460 |
This group describes the tools for importing and exporting graphs |
| 461 | 461 |
and graph related data. Now it supports the \ref lgf-format |
| 462 | 462 |
"LEMON Graph Format", the \c DIMACS format and the encapsulated |
| 463 | 463 |
postscript (EPS) format. |
| 464 | 464 |
*/ |
| 465 | 465 |
|
| 466 | 466 |
/** |
| 467 | 467 |
@defgroup lemon_io LEMON Graph Format |
| 468 | 468 |
@ingroup io_group |
| 469 | 469 |
\brief Reading and writing LEMON Graph Format. |
| 470 | 470 |
|
| 471 | 471 |
This group describes methods for reading and writing |
| 472 | 472 |
\ref lgf-format "LEMON Graph Format". |
| 473 | 473 |
*/ |
| 474 | 474 |
|
| 475 | 475 |
/** |
| 476 | 476 |
@defgroup eps_io Postscript Exporting |
| 477 | 477 |
@ingroup io_group |
| 478 | 478 |
\brief General \c EPS drawer and graph exporter |
| 479 | 479 |
|
| 480 | 480 |
This group describes general \c EPS drawing methods and special |
| 481 | 481 |
graph exporting tools. |
| 482 | 482 |
*/ |
| 483 | 483 |
|
| 484 | 484 |
/** |
| 485 |
@defgroup dimacs_group DIMACS format |
|
| 486 |
@ingroup io_group |
|
| 487 |
\brief Read and write files in DIMACS format |
|
| 488 |
|
|
| 489 |
Tools to read a digraph from or write it to a file in DIMACS format data. |
|
| 490 |
*/ |
|
| 491 |
|
|
| 492 |
/** |
|
| 485 | 493 |
@defgroup nauty_group NAUTY Format |
| 486 | 494 |
@ingroup io_group |
| 487 | 495 |
\brief Read \e Nauty format |
| 496 |
|
|
| 488 | 497 |
Tool to read graphs from \e Nauty format data. |
| 489 | 498 |
*/ |
| 490 | 499 |
|
| 491 | 500 |
/** |
| 492 | 501 |
@defgroup concept Concepts |
| 493 | 502 |
\brief Skeleton classes and concept checking classes |
| 494 | 503 |
|
| 495 | 504 |
This group describes the data/algorithm skeletons and concept checking |
| 496 | 505 |
classes implemented in LEMON. |
| 497 | 506 |
|
| 498 | 507 |
The purpose of the classes in this group is fourfold. |
| 499 | 508 |
|
| 500 | 509 |
- These classes contain the documentations of the %concepts. In order |
| 501 | 510 |
to avoid document multiplications, an implementation of a concept |
| 502 | 511 |
simply refers to the corresponding concept class. |
| 503 | 512 |
|
| 504 | 513 |
- These classes declare every functions, <tt>typedef</tt>s etc. an |
| 505 | 514 |
implementation of the %concepts should provide, however completely |
| 506 | 515 |
without implementations and real data structures behind the |
| 507 | 516 |
interface. On the other hand they should provide nothing else. All |
| 508 | 517 |
the algorithms working on a data structure meeting a certain concept |
| 509 | 518 |
should compile with these classes. (Though it will not run properly, |
| 510 | 519 |
of course.) In this way it is easily to check if an algorithm |
| 511 | 520 |
doesn't use any extra feature of a certain implementation. |
| 512 | 521 |
|
| 513 | 522 |
- The concept descriptor classes also provide a <em>checker class</em> |
| 514 | 523 |
that makes it possible to check whether a certain implementation of a |
| 515 | 524 |
concept indeed provides all the required features. |
| 516 | 525 |
|
| 517 | 526 |
- Finally, They can serve as a skeleton of a new implementation of a concept. |
| 518 | 527 |
*/ |
| 519 | 528 |
|
| 520 | 529 |
/** |
| 521 | 530 |
@defgroup graph_concepts Graph Structure Concepts |
| 522 | 531 |
@ingroup concept |
| 523 | 532 |
\brief Skeleton and concept checking classes for graph structures |
| 524 | 533 |
|
| 525 | 534 |
This group describes the skeletons and concept checking classes of LEMON's |
| 526 | 535 |
graph structures and helper classes used to implement these. |
| 527 | 536 |
*/ |
| 528 | 537 |
|
| 529 | 538 |
/** |
| 530 | 539 |
@defgroup map_concepts Map Concepts |
| 531 | 540 |
@ingroup concept |
| 532 | 541 |
\brief Skeleton and concept checking classes for maps |
| 533 | 542 |
|
| 534 | 543 |
This group describes the skeletons and concept checking classes of maps. |
| 535 | 544 |
*/ |
| 536 | 545 |
|
| 537 | 546 |
/** |
| 538 | 547 |
\anchor demoprograms |
| 539 | 548 |
|
| 540 | 549 |
@defgroup demos Demo programs |
| 541 | 550 |
|
| 542 | 551 |
Some demo programs are listed here. Their full source codes can be found in |
| 543 | 552 |
the \c demo subdirectory of the source tree. |
| 544 | 553 |
|
| 545 | 554 |
It order to compile them, use <tt>--enable-demo</tt> configure option when |
| 546 | 555 |
build the library. |
| 547 | 556 |
*/ |
| 548 | 557 |
|
| 549 | 558 |
/** |
| 550 | 559 |
@defgroup tools Standalone utility applications |
| 551 | 560 |
|
| 552 | 561 |
Some utility applications are listed here. |
| 553 | 562 |
|
| 554 | 563 |
The standard compilation procedure (<tt>./configure;make</tt>) will compile |
| 555 | 564 |
them, as well. |
| 556 | 565 |
*/ |
| 557 | 566 |
| 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-2008 |
| 6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
| 7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
| 8 | 8 |
* |
| 9 | 9 |
* Permission to use, modify and distribute this software is granted |
| 10 | 10 |
* provided that this copyright notice appears in all copies. For |
| 11 | 11 |
* precise terms see the accompanying LICENSE file. |
| 12 | 12 |
* |
| 13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
| 14 | 14 |
* express or implied, and with no claim as to its suitability for any |
| 15 | 15 |
* purpose. |
| 16 | 16 |
* |
| 17 | 17 |
*/ |
| 18 | 18 |
|
| 19 | 19 |
#ifndef LEMON_DIMACS_H |
| 20 | 20 |
#define LEMON_DIMACS_H |
| 21 | 21 |
|
| 22 | 22 |
#include <iostream> |
| 23 | 23 |
#include <string> |
| 24 | 24 |
#include <vector> |
| 25 | 25 |
#include <lemon/maps.h> |
| 26 | 26 |
#include <lemon/error.h> |
| 27 | 27 |
|
| 28 | 28 |
/// \ingroup dimacs_group |
| 29 | 29 |
/// \file |
| 30 | 30 |
/// \brief DIMACS file format reader. |
| 31 | 31 |
|
| 32 | 32 |
namespace lemon {
|
| 33 | 33 |
|
| 34 |
///@defgroup dimacs_group DIMACS format |
|
| 35 |
///\brief Read and write files in DIMACS format |
|
| 36 |
/// |
|
| 37 |
///Tools to read a digraph from or write it to a file in DIMACS format |
|
| 38 |
///data |
|
| 39 |
///\ingroup io_group |
|
| 40 |
|
|
| 41 | 34 |
/// \addtogroup dimacs_group |
| 42 | 35 |
/// @{
|
| 43 | 36 |
|
| 44 |
|
|
| 45 | 37 |
/// DIMACS file type descriptor. |
| 46 | 38 |
struct DimacsDescriptor |
| 47 | 39 |
{
|
| 48 | 40 |
///File type enum |
| 49 | 41 |
enum Type |
| 50 | 42 |
{
|
| 51 | 43 |
NONE, MIN, MAX, SP, MAT |
| 52 | 44 |
}; |
| 53 | 45 |
///The file type |
| 54 | 46 |
Type type; |
| 55 |
///The number of nodes |
|
| 47 |
///The number of nodes in the graph |
|
| 56 | 48 |
int nodeNum; |
| 57 |
///The number of edges |
|
| 49 |
///The number of edges in the graph |
|
| 58 | 50 |
int edgeNum; |
| 59 | 51 |
int lineShift; |
| 60 | 52 |
/// Constructor. Sets the type to NONE. |
| 61 | 53 |
DimacsDescriptor() : type(NONE) {}
|
| 62 | 54 |
}; |
| 63 | 55 |
|
| 64 | 56 |
///Discover the type of a DIMACS file |
| 65 | 57 |
|
| 66 | 58 |
///It starts seeking the begining of the file for the problem type |
| 67 |
///and size info. The found |
|
| 59 |
///and size info. The found data is returned in a special struct |
|
| 68 | 60 |
///that can be evaluated and passed to the appropriate reader |
| 69 | 61 |
///function. |
| 70 | 62 |
DimacsDescriptor dimacsType(std::istream& is) |
| 71 | 63 |
{
|
| 72 | 64 |
DimacsDescriptor r; |
| 73 | 65 |
std::string problem,str; |
| 74 | 66 |
char c; |
| 75 | 67 |
r.lineShift=0; |
| 76 | 68 |
while (is >> c) |
| 77 | 69 |
switch(c) |
| 78 | 70 |
{
|
| 79 | 71 |
case 'p': |
| 80 | 72 |
if(is >> problem >> r.nodeNum >> r.edgeNum) |
| 81 | 73 |
{
|
| 82 | 74 |
getline(is, str); |
| 83 | 75 |
r.lineShift++; |
| 84 | 76 |
if(problem=="min") r.type=DimacsDescriptor::MIN; |
| 85 | 77 |
else if(problem=="max") r.type=DimacsDescriptor::MAX; |
| 86 | 78 |
else if(problem=="sp") r.type=DimacsDescriptor::SP; |
| 87 | 79 |
else if(problem=="mat") r.type=DimacsDescriptor::MAT; |
| 88 | 80 |
else throw FormatError("Unknown problem type");
|
| 89 | 81 |
return r; |
| 90 | 82 |
} |
| 91 | 83 |
else |
| 92 | 84 |
{
|
| 93 | 85 |
throw FormatError("Missing or wrong problem type declaration.");
|
| 94 | 86 |
} |
| 95 | 87 |
break; |
| 96 | 88 |
case 'c': |
| 97 | 89 |
getline(is, str); |
| 98 | 90 |
r.lineShift++; |
| 99 | 91 |
break; |
| 100 | 92 |
default: |
| 101 | 93 |
throw FormatError("Unknown DIMACS declaration.");
|
| 102 | 94 |
} |
| 103 | 95 |
throw FormatError("Missing problem type declaration.");
|
| 104 | 96 |
} |
| 105 | 97 |
|
| 106 | 98 |
|
| 107 | 99 |
|
| 108 |
/// DIMACS |
|
| 100 |
/// DIMACS minimum cost flow reader function. |
|
| 109 | 101 |
/// |
| 110 |
/// This function reads a min cost flow instance from DIMACS format, |
|
| 111 |
/// i.e. from DIMACS files having a line starting with |
|
| 102 |
/// This function reads a minimum cost flow instance from DIMACS format, |
|
| 103 |
/// i.e. from a DIMACS file having a line starting with |
|
| 112 | 104 |
/// \code |
| 113 | 105 |
/// p min |
| 114 | 106 |
/// \endcode |
| 115 | 107 |
/// At the beginning, \c g is cleared by \c g.clear(). The supply |
| 116 | 108 |
/// amount of the nodes are written to \c supply (signed). The |
| 117 | 109 |
/// lower bounds, capacities and costs of the arcs are written to |
| 118 | 110 |
/// \c lower, \c capacity and \c cost. |
| 119 | 111 |
/// |
| 120 | 112 |
/// If the file type was previously evaluated by dimacsType(), then |
| 121 | 113 |
/// the descriptor struct should be given by the \c dest parameter. |
| 122 | 114 |
template <typename Digraph, typename LowerMap, |
| 123 | 115 |
typename CapacityMap, typename CostMap, |
| 124 | 116 |
typename SupplyMap> |
| 125 | 117 |
void readDimacsMin(std::istream& is, |
| 126 | 118 |
Digraph &g, |
| 127 | 119 |
LowerMap& lower, |
| 128 | 120 |
CapacityMap& capacity, |
| 129 | 121 |
CostMap& cost, |
| 130 | 122 |
SupplyMap& supply, |
| 131 | 123 |
DimacsDescriptor desc=DimacsDescriptor()) |
| 132 | 124 |
{
|
| 133 | 125 |
g.clear(); |
| 134 | 126 |
std::vector<typename Digraph::Node> nodes; |
| 135 | 127 |
typename Digraph::Arc e; |
| 136 | 128 |
std::string problem, str; |
| 137 | 129 |
char c; |
| 138 | 130 |
int i, j; |
| 139 | 131 |
if(desc.type==DimacsDescriptor::NONE) desc=dimacsType(is); |
| 140 | 132 |
if(desc.type!=DimacsDescriptor::MIN) |
| 141 | 133 |
throw FormatError("Problem type mismatch");
|
| 142 | 134 |
|
| 143 | 135 |
nodes.resize(desc.nodeNum + 1); |
| 144 | 136 |
for (int k = 1; k <= desc.nodeNum; ++k) {
|
| 145 | 137 |
nodes[k] = g.addNode(); |
| 146 | 138 |
supply.set(nodes[k], 0); |
| 147 | 139 |
} |
| 148 | 140 |
|
| 149 | 141 |
typename SupplyMap::Value sup; |
| 150 | 142 |
typename CapacityMap::Value low; |
| 151 | 143 |
typename CapacityMap::Value cap; |
| 152 | 144 |
typename CostMap::Value co; |
| 153 | 145 |
while (is >> c) {
|
| 154 | 146 |
switch (c) {
|
| 155 | 147 |
case 'c': // comment line |
| 156 | 148 |
getline(is, str); |
| 157 | 149 |
break; |
| 158 | 150 |
case 'n': // node definition line |
| 159 | 151 |
is >> i >> sup; |
| 160 | 152 |
getline(is, str); |
| 161 | 153 |
supply.set(nodes[i], sup); |
| 162 | 154 |
break; |
| 163 | 155 |
case 'a': // arc (arc) definition line |
| 164 | 156 |
is >> i >> j >> low >> cap >> co; |
| 165 | 157 |
getline(is, str); |
| 166 | 158 |
e = g.addArc(nodes[i], nodes[j]); |
| 167 | 159 |
lower.set(e, low); |
| 168 | 160 |
if (cap >= 0) |
| 169 | 161 |
capacity.set(e, cap); |
| 170 | 162 |
else |
| 171 | 163 |
capacity.set(e, -1); |
| 172 | 164 |
cost.set(e, co); |
| 173 | 165 |
break; |
| 174 | 166 |
} |
| 175 | 167 |
} |
| 176 | 168 |
} |
| 177 | 169 |
|
| 178 | 170 |
template<typename Digraph, typename CapacityMap> |
| 179 | 171 |
void _readDimacs(std::istream& is, |
| 180 | 172 |
Digraph &g, |
| 181 | 173 |
CapacityMap& capacity, |
| 182 | 174 |
typename Digraph::Node &s, |
| 183 | 175 |
typename Digraph::Node &t, |
| 184 | 176 |
DimacsDescriptor desc=DimacsDescriptor()) {
|
| 185 | 177 |
g.clear(); |
| 186 | 178 |
s=t=INVALID; |
| 187 | 179 |
std::vector<typename Digraph::Node> nodes; |
| 188 | 180 |
typename Digraph::Arc e; |
| 189 | 181 |
char c, d; |
| 190 | 182 |
int i, j; |
| 191 | 183 |
typename CapacityMap::Value _cap; |
| 192 | 184 |
std::string str; |
| 193 | 185 |
nodes.resize(desc.nodeNum + 1); |
| 194 | 186 |
for (int k = 1; k <= desc.nodeNum; ++k) {
|
| 195 | 187 |
nodes[k] = g.addNode(); |
| 196 | 188 |
} |
| 197 | 189 |
|
| 198 | 190 |
while (is >> c) {
|
| 199 | 191 |
switch (c) {
|
| 200 | 192 |
case 'c': // comment line |
| 201 | 193 |
getline(is, str); |
| 202 | 194 |
break; |
| 203 | 195 |
case 'n': // node definition line |
| 204 | 196 |
if (desc.type==DimacsDescriptor::SP) { // shortest path problem
|
| 205 | 197 |
is >> i; |
| 206 | 198 |
getline(is, str); |
| 207 | 199 |
s = nodes[i]; |
| 208 | 200 |
} |
| 209 | 201 |
if (desc.type==DimacsDescriptor::MAX) { // max flow problem
|
| 210 | 202 |
is >> i >> d; |
| 211 | 203 |
getline(is, str); |
| 212 | 204 |
if (d == 's') s = nodes[i]; |
| 213 | 205 |
if (d == 't') t = nodes[i]; |
| 214 | 206 |
} |
| 215 | 207 |
break; |
| 216 | 208 |
case 'a': // arc (arc) definition line |
| 217 | 209 |
if (desc.type==DimacsDescriptor::SP || |
| 218 | 210 |
desc.type==DimacsDescriptor::MAX) {
|
| 219 | 211 |
is >> i >> j >> _cap; |
| 220 | 212 |
getline(is, str); |
| 221 | 213 |
e = g.addArc(nodes[i], nodes[j]); |
| 222 | 214 |
capacity.set(e, _cap); |
| 223 | 215 |
} else {
|
| 224 | 216 |
is >> i >> j; |
| 225 | 217 |
getline(is, str); |
| 226 | 218 |
g.addArc(nodes[i], nodes[j]); |
| 227 | 219 |
} |
| 228 | 220 |
break; |
| 229 | 221 |
} |
| 230 | 222 |
} |
| 231 | 223 |
} |
| 232 | 224 |
|
| 233 |
/// DIMACS |
|
| 225 |
/// DIMACS maximum flow reader function. |
|
| 234 | 226 |
/// |
| 235 |
/// This function reads a max flow instance from DIMACS format, |
|
| 236 |
/// i.e. from DIMACS files having a line starting with |
|
| 227 |
/// This function reads a maximum flow instance from DIMACS format, |
|
| 228 |
/// i.e. from a DIMACS file having a line starting with |
|
| 237 | 229 |
/// \code |
| 238 | 230 |
/// p max |
| 239 | 231 |
/// \endcode |
| 240 | 232 |
/// At the beginning, \c g is cleared by \c g.clear(). The arc |
| 241 | 233 |
/// capacities are written to \c capacity and \c s and \c t are |
| 242 | 234 |
/// set to the source and the target nodes. |
| 243 | 235 |
/// |
| 244 | 236 |
/// If the file type was previously evaluated by dimacsType(), then |
| 245 | 237 |
/// the descriptor struct should be given by the \c dest parameter. |
| 246 | 238 |
template<typename Digraph, typename CapacityMap> |
| 247 | 239 |
void readDimacsMax(std::istream& is, |
| 248 | 240 |
Digraph &g, |
| 249 | 241 |
CapacityMap& capacity, |
| 250 | 242 |
typename Digraph::Node &s, |
| 251 | 243 |
typename Digraph::Node &t, |
| 252 | 244 |
DimacsDescriptor desc=DimacsDescriptor()) {
|
| 253 | 245 |
if(desc.type==DimacsDescriptor::NONE) desc=dimacsType(is); |
| 254 | 246 |
if(desc.type!=DimacsDescriptor::MAX) |
| 255 | 247 |
throw FormatError("Problem type mismatch");
|
| 256 | 248 |
_readDimacs(is,g,capacity,s,t,desc); |
| 257 | 249 |
} |
| 258 | 250 |
|
| 259 | 251 |
/// DIMACS shortest path reader function. |
| 260 | 252 |
/// |
| 261 | 253 |
/// This function reads a shortest path instance from DIMACS format, |
| 262 |
/// i.e. from DIMACS |
|
| 254 |
/// i.e. from a DIMACS file having a line starting with |
|
| 263 | 255 |
/// \code |
| 264 | 256 |
/// p sp |
| 265 | 257 |
/// \endcode |
| 266 | 258 |
/// At the beginning, \c g is cleared by \c g.clear(). The arc |
| 267 |
/// lengths are written to \c |
|
| 259 |
/// lengths are written to \c length and \c s is set to the |
|
| 268 | 260 |
/// source node. |
| 269 | 261 |
/// |
| 270 | 262 |
/// If the file type was previously evaluated by dimacsType(), then |
| 271 | 263 |
/// the descriptor struct should be given by the \c dest parameter. |
| 272 | 264 |
template<typename Digraph, typename LengthMap> |
| 273 | 265 |
void readDimacsSp(std::istream& is, |
| 274 | 266 |
Digraph &g, |
| 275 | 267 |
LengthMap& length, |
| 276 | 268 |
typename Digraph::Node &s, |
| 277 | 269 |
DimacsDescriptor desc=DimacsDescriptor()) {
|
| 278 | 270 |
typename Digraph::Node t; |
| 279 | 271 |
if(desc.type==DimacsDescriptor::NONE) desc=dimacsType(is); |
| 280 | 272 |
if(desc.type!=DimacsDescriptor::SP) |
| 281 | 273 |
throw FormatError("Problem type mismatch");
|
| 282 | 274 |
_readDimacs(is, g, length, s, t,desc); |
| 283 | 275 |
} |
| 284 | 276 |
|
| 285 | 277 |
/// DIMACS capacitated digraph reader function. |
| 286 | 278 |
/// |
| 287 | 279 |
/// This function reads an arc capacitated digraph instance from |
| 288 | 280 |
/// DIMACS 'mat' or 'sp' format. |
| 289 | 281 |
/// At the beginning, \c g is cleared by \c g.clear() |
| 290 | 282 |
/// and the arc capacities/lengths are written to \c capacity. |
| 291 | 283 |
/// |
| 292 | 284 |
/// If the file type was previously evaluated by dimacsType(), then |
| 293 | 285 |
/// the descriptor struct should be given by the \c dest parameter. |
| 294 | 286 |
template<typename Digraph, typename CapacityMap> |
| 295 | 287 |
void readDimacsCap(std::istream& is, |
| 296 | 288 |
Digraph &g, |
| 297 | 289 |
CapacityMap& capacity, |
| 298 | 290 |
DimacsDescriptor desc=DimacsDescriptor()) {
|
| 299 | 291 |
typename Digraph::Node u,v; |
| 300 | 292 |
if(desc.type==DimacsDescriptor::NONE) desc=dimacsType(is); |
| 301 | 293 |
if(desc.type!=DimacsDescriptor::MAX || desc.type!=DimacsDescriptor::SP) |
| 302 | 294 |
throw FormatError("Problem type mismatch");
|
| 303 | 295 |
_readDimacs(is, g, capacity, u, v, desc); |
| 304 | 296 |
} |
| 305 | 297 |
|
| 306 | 298 |
/// DIMACS plain digraph reader function. |
| 307 | 299 |
/// |
| 308 | 300 |
/// This function reads a digraph without any designated nodes and |
| 309 | 301 |
/// maps from DIMACS format, i.e. from DIMACS files having a line |
| 310 | 302 |
/// starting with |
| 311 | 303 |
/// \code |
| 312 | 304 |
/// p mat |
| 313 | 305 |
/// \endcode |
| 314 | 306 |
/// At the beginning, \c g is cleared by \c g.clear(). |
| 315 | 307 |
/// |
| 316 | 308 |
/// If the file type was previously evaluated by dimacsType(), then |
| 317 | 309 |
/// the descriptor struct should be given by the \c dest parameter. |
| 318 | 310 |
template<typename Digraph> |
| 319 | 311 |
void readDimacsMat(std::istream& is, Digraph &g, |
| 320 | 312 |
DimacsDescriptor desc=DimacsDescriptor()) {
|
| 321 | 313 |
typename Digraph::Node u,v; |
| 322 | 314 |
NullMap<typename Digraph::Arc, int> n; |
| 323 | 315 |
if(desc.type==DimacsDescriptor::NONE) desc=dimacsType(is); |
| 324 | 316 |
if(desc.type!=DimacsDescriptor::MAT) |
| 325 | 317 |
throw FormatError("Problem type mismatch");
|
| 326 | 318 |
_readDimacs(is, g, n, u, v, desc); |
| 327 | 319 |
} |
| 328 | 320 |
|
| 329 | 321 |
/// DIMACS plain digraph writer function. |
| 330 | 322 |
/// |
| 331 | 323 |
/// This function writes a digraph without any designated nodes and |
| 332 | 324 |
/// maps into DIMACS format, i.e. into DIMACS file having a line |
| 333 | 325 |
/// starting with |
| 334 | 326 |
/// \code |
| 335 | 327 |
/// p mat |
| 336 | 328 |
/// \endcode |
| 337 | 329 |
/// If \c comment is not empty, then it will be printed in the first line |
| 338 | 330 |
/// prefixed by 'c'. |
| 339 | 331 |
template<typename Digraph> |
| 340 | 332 |
void writeDimacsMat(std::ostream& os, const Digraph &g, |
| 341 | 333 |
std::string comment="") {
|
| 342 | 334 |
typedef typename Digraph::NodeIt NodeIt; |
| 343 | 335 |
typedef typename Digraph::ArcIt ArcIt; |
| 344 | 336 |
|
| 345 | 337 |
if(!comment.empty()) |
| 346 | 338 |
os << "c " << comment << std::endl; |
| 347 | 339 |
os << "p mat " << g.nodeNum() << " " << g.arcNum() << std::endl; |
| 348 | 340 |
|
| 349 | 341 |
typename Digraph::template NodeMap<int> nodes(g); |
| 350 | 342 |
int i = 1; |
| 351 | 343 |
for(NodeIt v(g); v != INVALID; ++v) {
|
| 352 | 344 |
nodes.set(v, i); |
| 353 | 345 |
++i; |
| 354 | 346 |
} |
| 355 | 347 |
for(ArcIt e(g); e != INVALID; ++e) {
|
| 356 | 348 |
os << "a " << nodes[g.source(e)] << " " << nodes[g.target(e)] |
| 357 | 349 |
<< std::endl; |
| 358 | 350 |
} |
| 359 | 351 |
} |
| 360 | 352 |
|
| 361 | 353 |
/// @} |
| 362 | 354 |
|
| 363 | 355 |
} //namespace lemon |
| 364 | 356 |
|
| 365 | 357 |
#endif //LEMON_DIMACS_H |
0 comments (0 inline)