src/hugo/dimacs.h
author ladanyi
Thu, 06 May 2004 13:21:24 +0000
changeset 539 fb261e3a9a0f
parent 533 src/include/dimacs.h@04eb0d9022c8
child 542 69bde1d90c04
permissions -rw-r--r--
Rename 'include' to 'hugo' (for automake)
marci@423
     1
// -*- c++ -*-
marci@423
     2
#ifndef HUGO_DIMACS_H
marci@423
     3
#define HUGO_DIMACS_H
marci@423
     4
marci@423
     5
#include <iostream>
marci@423
     6
#include <string>
marci@423
     7
#include <vector>
jacint@528
     8
#include <maps.h>
marci@423
     9
klao@442
    10
/// \file
klao@442
    11
/// \brief Dimacs file format reader.
klao@427
    12
marci@423
    13
namespace hugo {
marci@423
    14
klao@427
    15
  /// Dimacs flow file format reader function.
marci@423
    16
marci@423
    17
  /// This function reads a flow instance from dimacs flow format.
klao@427
    18
  /// At the beginning \c g is cleared by \c g.clear().
klao@427
    19
  /// If the data coming from \c is is a max flow problem instance, then 
klao@427
    20
  /// \c s and \c t will be respectively the source and target nodes 
marci@423
    21
  /// and \c capacity will contain the edge capacities.
klao@427
    22
  /// If the data is a shortest path problem instance then \c s will be the 
klao@427
    23
  /// source node and \c capacity will contain the edge lengths.
marci@465
    24
  ///
marci@465
    25
  ///\author Marton Makai
marci@423
    26
  template<typename Graph, typename CapacityMap>
jacint@528
    27
  void readDimacsMaxFlow(std::istream& is, Graph &g, 
jacint@528
    28
			 typename Graph::Node &s, typename Graph::Node &t, CapacityMap& capacity) {
marci@423
    29
    g.clear();
marci@423
    30
    int cap;
marci@423
    31
    char d;
marci@423
    32
    std::string problem;
marci@423
    33
    char c;
marci@423
    34
    int i, j;
marci@423
    35
    std::string str;
marci@423
    36
    int n, m; 
marci@423
    37
    typename Graph::Edge e;
marci@423
    38
    std::vector<typename Graph::Node> nodes;
marci@423
    39
    while (is>>c) {
marci@423
    40
      switch (c) {
marci@423
    41
      case 'c': //comment
marci@423
    42
	getline(is, str);
marci@423
    43
	break;
marci@423
    44
      case 'p': //problem definition
marci@423
    45
	is >> problem >> n >> m;
marci@423
    46
	getline(is, str);
marci@423
    47
	nodes.resize(n+1);
marci@423
    48
	for (int k=1; k<=n; ++k) nodes[k]=g.addNode();
marci@423
    49
	break;
marci@423
    50
      case 'n': //node definition
marci@423
    51
	if (problem=="sp") { //shortest path problem
marci@423
    52
	  is >> i;
marci@423
    53
	  getline(is, str);
marci@423
    54
	  s=nodes[i];
marci@423
    55
	}
marci@423
    56
	if (problem=="max") { //max flow problem
marci@423
    57
	  is >> i >> d;
marci@423
    58
	  getline(is, str);
marci@423
    59
	  if (d=='s') s=nodes[i];
marci@423
    60
	  if (d=='t') t=nodes[i];
marci@423
    61
	}
marci@423
    62
	break;
marci@423
    63
      case 'a':
marci@423
    64
	is >> i >> j >> cap;
marci@423
    65
	getline(is, str);
marci@423
    66
	e=g.addEdge(nodes[i], nodes[j]);
marci@423
    67
	capacity.update();
marci@423
    68
	capacity.set(e, cap);
marci@423
    69
	break;
marci@423
    70
      }
marci@423
    71
    }
marci@423
    72
  }
jacint@528
    73
jacint@528
    74
  /// matching problem
jacint@528
    75
  template<typename Graph>
jacint@528
    76
  void readDimacs(std::istream& is, Graph &g) {
jacint@528
    77
    typename Graph::Node u;
jacint@528
    78
    NullMap<typename Graph::Edge, int> n;
jacint@528
    79
    readDimacs(is, g, n, u, u, n);
jacint@528
    80
    std::cout<<"igen en.";
jacint@528
    81
  }
jacint@528
    82
jacint@528
    83
  /// sg problem
jacint@528
    84
  template<typename Graph, typename CapacityMap>
jacint@528
    85
  void readDimacs(std::istream& is, Graph &g, CapacityMap& capacity) {
jacint@528
    86
    typename Graph::Node u;
jacint@528
    87
    NullMap<typename Graph::Edge, int> n;
alpar@533
    88
    readDimacs(is, g, capacity, u, u, n);
jacint@528
    89
  }
jacint@528
    90
jacint@528
    91
  /// shortest path problem
jacint@528
    92
  template<typename Graph, typename CapacityMap>
jacint@528
    93
  void readDimacs(std::istream& is, Graph &g, CapacityMap& capacity, 
jacint@528
    94
		  typename Graph::Node &s) {
jacint@528
    95
    NullMap<typename Graph::Edge, int> n;
jacint@528
    96
    readDimacs(is, g, capacity, s, s, n);
jacint@528
    97
  }
jacint@528
    98
jacint@528
    99
  /// max flow problem
jacint@528
   100
  template<typename Graph, typename CapacityMap>
jacint@528
   101
  void readDimacs(std::istream& is, Graph &g, CapacityMap& capacity, 
jacint@528
   102
		  typename Graph::Node &s, typename Graph::Node &t) {
jacint@528
   103
    NullMap<typename Graph::Edge, int> n;
jacint@528
   104
    readDimacs(is, g, capacity, s, t, n);
jacint@528
   105
  }
jacint@528
   106
jacint@528
   107
  /// min cost flow problem
jacint@528
   108
  template<typename Graph, typename CapacityMap, typename CostMap>
jacint@528
   109
  void readDimacs(std::istream& is, Graph &g, CapacityMap& capacity, 
jacint@528
   110
		  typename Graph::Node &s, typename Graph::Node &t, 
jacint@528
   111
		  CostMap& cost) {
jacint@528
   112
    g.clear();
jacint@528
   113
    typename CapacityMap::ValueType _cap;
jacint@528
   114
    typename CostMap::ValueType _cost;
jacint@528
   115
    char d;
jacint@528
   116
    std::string problem;
jacint@528
   117
    char c;
jacint@528
   118
    int i, j;
jacint@528
   119
    std::string str;
jacint@528
   120
    int n, m; 
jacint@528
   121
    typename Graph::Edge e;
jacint@528
   122
    std::vector<typename Graph::Node> nodes;
jacint@528
   123
    while (is>>c) {
jacint@528
   124
      switch (c) {
jacint@528
   125
      case 'c': //comment
jacint@528
   126
	getline(is, str);
jacint@528
   127
	break;
jacint@528
   128
      case 'p': //problem definition
jacint@528
   129
	is >> problem >> n >> m;
jacint@528
   130
	getline(is, str);
jacint@528
   131
	nodes.resize(n+1);
jacint@528
   132
	for (int k=1; k<=n; ++k) nodes[k]=g.addNode();
jacint@528
   133
	break;
jacint@528
   134
      case 'n': //node definition
jacint@528
   135
	if (problem=="sp") { //shortest path problem
jacint@528
   136
	  is >> i;
jacint@528
   137
	  getline(is, str);
jacint@528
   138
	  s=nodes[i];
jacint@528
   139
	}
jacint@528
   140
	if (problem=="max" || problem=="min") { //((max) or (min cost)) flow problem
jacint@528
   141
	  is >> i >> d;
jacint@528
   142
	  getline(is, str);
jacint@528
   143
	  if (d=='s') s=nodes[i];
jacint@528
   144
	  if (d=='t') t=nodes[i];
jacint@528
   145
	}
jacint@528
   146
	break;
jacint@528
   147
      case 'a':
jacint@528
   148
	if ( problem == "mat" ) {
jacint@528
   149
	  is >> i >> j;
jacint@528
   150
	  getline(is, str);
jacint@528
   151
	  g.addEdge(nodes[i], nodes[j]);
jacint@528
   152
	}
jacint@528
   153
	if ( problem == "max" || problem == "sp") {
jacint@528
   154
	  is >> i >> j >> _cap;
jacint@528
   155
	  getline(is, str);
jacint@528
   156
	  e=g.addEdge(nodes[i], nodes[j]);
jacint@528
   157
	  capacity.update();
jacint@528
   158
	  capacity.set(e, _cap);
jacint@528
   159
	}
jacint@528
   160
	if ( problem == "min" ) {
jacint@528
   161
	  is >> i >> j >> _cap >> _cost;
jacint@528
   162
	  getline(is, str);
jacint@528
   163
	  e=g.addEdge(nodes[i], nodes[j]);
jacint@528
   164
	  capacity.update();
jacint@528
   165
	  capacity.set(e, _cap);
jacint@528
   166
	  cost.update();
jacint@528
   167
	  cost.set(e, _cost);
jacint@528
   168
	}
jacint@528
   169
	break;
jacint@528
   170
      }
jacint@528
   171
    }
jacint@528
   172
  }
jacint@528
   173
jacint@528
   174
marci@423
   175
  
jacint@528
   176
  /// write matching problem
jacint@528
   177
  template<typename Graph>
jacint@528
   178
  void writeDimacs(std::ostream& os, const Graph &g) {
jacint@528
   179
    typedef typename Graph::NodeIt NodeIt;
jacint@528
   180
    typedef typename Graph::EdgeIt EdgeIt;  
jacint@528
   181
    
jacint@528
   182
    typename Graph::template NodeMap<int> nodes(g);
jacint@528
   183
jacint@528
   184
    os << "c matching problem" << std::endl;
jacint@528
   185
jacint@528
   186
    int i=1;
jacint@528
   187
    NodeIt v;
jacint@528
   188
    for(g.first(v); g.valid(v); g.next(v)) {
jacint@528
   189
      nodes.set(v, i);
jacint@528
   190
      ++i;
jacint@528
   191
    }    
jacint@528
   192
    
jacint@528
   193
    os << "p mat " << g.nodeNum() << " " << g.edgeNum() << std::endl;
jacint@528
   194
jacint@528
   195
    EdgeIt e;
jacint@528
   196
    for(g.first(e); g.valid(e); g.next(e)) {
jacint@528
   197
      os << "a " << nodes[g.tail(e)] << " " << nodes[g.head(e)] << std::endl; 
jacint@528
   198
    }
jacint@528
   199
jacint@528
   200
  }
jacint@528
   201
jacint@528
   202
jacint@528
   203
marci@423
   204
} //namespace hugo
marci@423
   205
marci@423
   206
#endif //HUGO_DIMACS_H