src/lemon/dimacs.h
author alpar
Fri, 05 Nov 2004 07:26:20 +0000
changeset 964 2c0c20e90116
parent 921 818510fa3d99
child 986 e997802b855c
permissions -rw-r--r--
Doc improvements
alpar@906
     1
/* -*- C++ -*-
alpar@921
     2
 * src/lemon/dimacs.h - Part of LEMON, a generic C++ optimization library
alpar@906
     3
 *
alpar@906
     4
 * Copyright (C) 2004 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
alpar@906
     5
 * (Egervary Combinatorial Optimization Research Group, EGRES).
alpar@906
     6
 *
alpar@906
     7
 * Permission to use, modify and distribute this software is granted
alpar@906
     8
 * provided that this copyright notice appears in all copies. For
alpar@906
     9
 * precise terms see the accompanying LICENSE file.
alpar@906
    10
 *
alpar@906
    11
 * This software is provided "AS IS" with no warranty of any kind,
alpar@906
    12
 * express or implied, and with no claim as to its suitability for any
alpar@906
    13
 * purpose.
alpar@906
    14
 *
alpar@906
    15
 */
alpar@906
    16
alpar@921
    17
#ifndef LEMON_DIMACS_H
alpar@921
    18
#define LEMON_DIMACS_H
marci@423
    19
marci@423
    20
#include <iostream>
marci@423
    21
#include <string>
marci@423
    22
#include <vector>
alpar@921
    23
#include <lemon/maps.h>
marci@423
    24
jacint@575
    25
/// \ingroup misc
klao@442
    26
/// \file
klao@442
    27
/// \brief Dimacs file format reader.
klao@427
    28
alpar@921
    29
namespace lemon {
marci@423
    30
marci@423
    31
jacint@575
    32
  /// \addtogroup misc
jacint@575
    33
  /// @{
jacint@575
    34
jacint@575
    35
  /// Dimacs min cost flow reader function.
jacint@575
    36
jacint@575
    37
  /// This function reads a min cost flow instance from dimacs format,
alpar@964
    38
  /// i.e. from dimacs files having a line starting with
alpar@964
    39
  /// \code
alpar@964
    40
  /// p "min"
alpar@964
    41
  /// \endcode
jacint@575
    42
  /// At the beginning \c g is cleared by \c g.clear(). The edge
jacint@575
    43
  /// capacities are written to \c capacity, \c s and \c t are set to
jacint@575
    44
  /// the source and the target nodes resp. and the cost of the edges
jacint@575
    45
  /// are written to \c cost.
marci@465
    46
  ///
jacint@575
    47
  /// \author Marton Makai
jacint@528
    48
  template<typename Graph, typename CapacityMap, typename CostMap>
jacint@528
    49
  void readDimacs(std::istream& is, Graph &g, CapacityMap& capacity, 
jacint@528
    50
		  typename Graph::Node &s, typename Graph::Node &t, 
jacint@528
    51
		  CostMap& cost) {
jacint@528
    52
    g.clear();
jacint@528
    53
    typename CapacityMap::ValueType _cap;
jacint@528
    54
    typename CostMap::ValueType _cost;
jacint@528
    55
    char d;
jacint@528
    56
    std::string problem;
jacint@528
    57
    char c;
jacint@528
    58
    int i, j;
jacint@528
    59
    std::string str;
jacint@528
    60
    int n, m; 
jacint@528
    61
    typename Graph::Edge e;
jacint@528
    62
    std::vector<typename Graph::Node> nodes;
jacint@528
    63
    while (is>>c) {
jacint@528
    64
      switch (c) {
jacint@528
    65
      case 'c': //comment
jacint@528
    66
	getline(is, str);
jacint@528
    67
	break;
jacint@528
    68
      case 'p': //problem definition
jacint@528
    69
	is >> problem >> n >> m;
jacint@528
    70
	getline(is, str);
jacint@528
    71
	nodes.resize(n+1);
jacint@528
    72
	for (int k=1; k<=n; ++k) nodes[k]=g.addNode();
jacint@528
    73
	break;
jacint@528
    74
      case 'n': //node definition
jacint@528
    75
	if (problem=="sp") { //shortest path problem
jacint@528
    76
	  is >> i;
jacint@528
    77
	  getline(is, str);
jacint@528
    78
	  s=nodes[i];
jacint@528
    79
	}
jacint@528
    80
	if (problem=="max" || problem=="min") { //((max) or (min cost)) flow problem
jacint@528
    81
	  is >> i >> d;
jacint@528
    82
	  getline(is, str);
jacint@528
    83
	  if (d=='s') s=nodes[i];
jacint@528
    84
	  if (d=='t') t=nodes[i];
jacint@528
    85
	}
jacint@528
    86
	break;
jacint@528
    87
      case 'a':
jacint@528
    88
	if ( problem == "max" || problem == "sp") {
jacint@528
    89
	  is >> i >> j >> _cap;
jacint@528
    90
	  getline(is, str);
jacint@528
    91
	  e=g.addEdge(nodes[i], nodes[j]);
marci@784
    92
	  //capacity.update();
jacint@528
    93
	  capacity.set(e, _cap);
jacint@575
    94
	} else {
jacint@575
    95
	  if ( problem == "min" ) {
jacint@575
    96
	    is >> i >> j >> _cap >> _cost;
jacint@575
    97
	    getline(is, str);
jacint@575
    98
	    e=g.addEdge(nodes[i], nodes[j]);
marci@784
    99
	    //capacity.update();
jacint@575
   100
	    capacity.set(e, _cap);
marci@784
   101
	    //cost.update();
jacint@575
   102
	    cost.set(e, _cost);
jacint@575
   103
	  } else {
jacint@575
   104
	    is >> i >> j;
jacint@575
   105
	    getline(is, str);
jacint@575
   106
	    g.addEdge(nodes[i], nodes[j]);
jacint@575
   107
	  }
jacint@528
   108
	}
jacint@528
   109
	break;
jacint@528
   110
      }
jacint@528
   111
    }
jacint@528
   112
  }
jacint@528
   113
jacint@528
   114
jacint@575
   115
  /// Dimacs max flow reader function.
jacint@575
   116
jacint@575
   117
  /// This function reads a max flow instance from dimacs format,
alpar@964
   118
  /// i.e. from dimacs files having a line starting with
alpar@964
   119
  /// \code
alpar@964
   120
  /// p "max"
alpar@964
   121
  /// \endcode
alpar@964
   122
  ///At the beginning \c g is cleared by \c g.clear(). The
jacint@575
   123
  /// edge capacities are written to \c capacity and \c s and \c t are
jacint@575
   124
  /// set to the source and the target nodes.
jacint@575
   125
  ///
jacint@575
   126
  /// \author Marton Makai
jacint@575
   127
  template<typename Graph, typename CapacityMap>
jacint@575
   128
  void readDimacs(std::istream& is, Graph &g, CapacityMap& capacity, 
jacint@575
   129
		  typename Graph::Node &s, typename Graph::Node &t) {
jacint@575
   130
    NullMap<typename Graph::Edge, int> n;
jacint@575
   131
    readDimacs(is, g, capacity, s, t, n);
jacint@575
   132
  }
jacint@575
   133
jacint@575
   134
jacint@575
   135
  /// Dimacs shortest path reader function.
jacint@575
   136
jacint@575
   137
  /// This function reads a shortest path instance from dimacs format,
alpar@964
   138
  /// i.e. from dimacs files having a line starting with
alpar@964
   139
  /// \code
alpar@964
   140
  /// p "sp"
alpar@964
   141
  /// \endcode
jacint@575
   142
  /// At the beginning \c g is cleared by \c g.clear(). The edge
jacint@575
   143
  /// capacities are written to \c capacity and \c s is set to the
jacint@575
   144
  /// source node.
jacint@575
   145
  ///
jacint@575
   146
  /// \author Marton Makai
jacint@575
   147
  template<typename Graph, typename CapacityMap>
jacint@575
   148
  void readDimacs(std::istream& is, Graph &g, CapacityMap& capacity, 
jacint@575
   149
		  typename Graph::Node &s) {
jacint@575
   150
    NullMap<typename Graph::Edge, int> n;
jacint@575
   151
    readDimacs(is, g, capacity, s, s, n);
jacint@575
   152
  }
jacint@575
   153
jacint@575
   154
jacint@575
   155
  /// Dimacs capacitated graph reader function.
jacint@575
   156
jacint@575
   157
  /// This function reads an edge capacitated graph instance from
jacint@575
   158
  /// dimacs format.  At the beginning \c g is cleared by \c g.clear()
jacint@575
   159
  /// and the edge capacities are written to \c capacity.
jacint@575
   160
  ///
jacint@575
   161
  /// \author Marton Makai
jacint@575
   162
  template<typename Graph, typename CapacityMap>
jacint@575
   163
  void readDimacs(std::istream& is, Graph &g, CapacityMap& capacity) {
jacint@575
   164
    typename Graph::Node u;
jacint@575
   165
    NullMap<typename Graph::Edge, int> n;
jacint@575
   166
    readDimacs(is, g, capacity, u, u, n);
jacint@575
   167
  }
jacint@575
   168
jacint@575
   169
jacint@575
   170
  /// Dimacs plain graph reader function.
jacint@575
   171
jacint@575
   172
  /// This function reads a graph without any designated nodes and
jacint@575
   173
  /// maps from dimacs format, i.e. from dimacs files having a line
alpar@964
   174
  /// starting with
alpar@964
   175
  /// \code
alpar@964
   176
  /// p "mat"
alpar@964
   177
  /// \endcode
alpar@964
   178
  /// At the beginning \c g is cleared
jacint@575
   179
  /// by \c g.clear().
jacint@575
   180
  ///
jacint@575
   181
  /// \author Marton Makai
jacint@575
   182
  template<typename Graph>
jacint@575
   183
  void readDimacs(std::istream& is, Graph &g) {
jacint@575
   184
    typename Graph::Node u;
jacint@575
   185
    NullMap<typename Graph::Edge, int> n;
jacint@575
   186
    readDimacs(is, g, n, u, u, n);
jacint@575
   187
  }
jacint@575
   188
  
jacint@575
   189
jacint@575
   190
marci@423
   191
  
jacint@528
   192
  /// write matching problem
jacint@528
   193
  template<typename Graph>
jacint@528
   194
  void writeDimacs(std::ostream& os, const Graph &g) {
jacint@528
   195
    typedef typename Graph::NodeIt NodeIt;
jacint@528
   196
    typedef typename Graph::EdgeIt EdgeIt;  
jacint@528
   197
    
jacint@528
   198
    typename Graph::template NodeMap<int> nodes(g);
jacint@528
   199
jacint@528
   200
    os << "c matching problem" << std::endl;
jacint@528
   201
jacint@528
   202
    int i=1;
marci@778
   203
    for(NodeIt v(g); v!=INVALID; ++v) {
jacint@528
   204
      nodes.set(v, i);
jacint@528
   205
      ++i;
jacint@528
   206
    }    
jacint@528
   207
    
jacint@528
   208
    os << "p mat " << g.nodeNum() << " " << g.edgeNum() << std::endl;
jacint@528
   209
marci@778
   210
    for(EdgeIt e(g); e!=INVALID; ++e) {
jacint@528
   211
      os << "a " << nodes[g.tail(e)] << " " << nodes[g.head(e)] << std::endl; 
jacint@528
   212
    }
jacint@528
   213
jacint@528
   214
  }
jacint@528
   215
jacint@575
   216
  /// @}
jacint@528
   217
alpar@921
   218
} //namespace lemon
marci@423
   219
alpar@921
   220
#endif //LEMON_DIMACS_H