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