alpar@906
|
1 |
/* -*- C++ -*-
|
alpar@906
|
2 |
*
|
alpar@1956
|
3 |
* This file is a part of LEMON, a generic C++ optimization library
|
alpar@1956
|
4 |
*
|
alpar@2391
|
5 |
* Copyright (C) 2003-2007
|
alpar@1956
|
6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
|
alpar@1359
|
7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES).
|
alpar@906
|
8 |
*
|
alpar@906
|
9 |
* Permission to use, modify and distribute this software is granted
|
alpar@906
|
10 |
* provided that this copyright notice appears in all copies. For
|
alpar@906
|
11 |
* precise terms see the accompanying LICENSE file.
|
alpar@906
|
12 |
*
|
alpar@906
|
13 |
* This software is provided "AS IS" with no warranty of any kind,
|
alpar@906
|
14 |
* express or implied, and with no claim as to its suitability for any
|
alpar@906
|
15 |
* purpose.
|
alpar@906
|
16 |
*
|
alpar@906
|
17 |
*/
|
alpar@906
|
18 |
|
alpar@921
|
19 |
#ifndef LEMON_DIMACS_H
|
alpar@921
|
20 |
#define LEMON_DIMACS_H
|
marci@423
|
21 |
|
marci@423
|
22 |
#include <iostream>
|
marci@423
|
23 |
#include <string>
|
marci@423
|
24 |
#include <vector>
|
alpar@921
|
25 |
#include <lemon/maps.h>
|
deba@1993
|
26 |
#include <lemon/bits/invalid.h>
|
marci@423
|
27 |
|
alpar@1287
|
28 |
/// \ingroup dimacs_group
|
klao@442
|
29 |
/// \file
|
deba@2413
|
30 |
/// \brief DIMACS file format reader.
|
klao@427
|
31 |
|
alpar@921
|
32 |
namespace lemon {
|
marci@423
|
33 |
|
alpar@1287
|
34 |
///@defgroup dimacs_group DIMACS format
|
alpar@1287
|
35 |
///\brief Read and write files in DIMACS format
|
alpar@1287
|
36 |
///
|
alpar@1287
|
37 |
///Tools to read a graph from or write it to a file in DIMACS format
|
alpar@1287
|
38 |
///data
|
alpar@1287
|
39 |
///\ingroup io_group
|
marci@423
|
40 |
|
alpar@1287
|
41 |
/// \addtogroup dimacs_group
|
jacint@575
|
42 |
/// @{
|
deba@2413
|
43 |
|
deba@2413
|
44 |
/// DIMACS min cost flow reader function.
|
deba@2413
|
45 |
///
|
deba@2413
|
46 |
/// This function reads a min cost flow instance from DIMACS format,
|
deba@2413
|
47 |
/// i.e. from DIMACS files having a line starting with
|
deba@2413
|
48 |
/// \code
|
deba@2413
|
49 |
/// p min
|
deba@2413
|
50 |
/// \endcode
|
deba@2413
|
51 |
/// At the beginning \c g is cleared by \c g.clear(). The supply
|
deba@2413
|
52 |
/// amount of the nodes are written to \c supply (signed). The
|
deba@2413
|
53 |
/// lower bounds, capacities and costs of the edges are written to
|
deba@2413
|
54 |
/// \c lower, \c capacity and \c cost.
|
deba@2413
|
55 |
///
|
deba@2413
|
56 |
/// \author Marton Makai and Peter Kovacs
|
deba@2413
|
57 |
template <typename Graph, typename SupplyMap,
|
deba@2413
|
58 |
typename CapacityMap, typename CostMap>
|
deba@2413
|
59 |
void readDimacs( std::istream& is,
|
deba@2413
|
60 |
Graph &g,
|
deba@2413
|
61 |
SupplyMap& supply,
|
deba@2413
|
62 |
CapacityMap& lower,
|
deba@2413
|
63 |
CapacityMap& capacity,
|
deba@2413
|
64 |
CostMap& cost )
|
deba@2413
|
65 |
{
|
deba@2413
|
66 |
g.clear();
|
deba@2413
|
67 |
std::vector<typename Graph::Node> nodes;
|
deba@2413
|
68 |
typename Graph::Edge e;
|
deba@2413
|
69 |
std::string problem, str;
|
deba@2413
|
70 |
char c;
|
deba@2413
|
71 |
int n, m;
|
deba@2413
|
72 |
int i, j;
|
deba@2413
|
73 |
typename SupplyMap::Value sup;
|
deba@2413
|
74 |
typename CapacityMap::Value low;
|
deba@2413
|
75 |
typename CapacityMap::Value cap;
|
deba@2413
|
76 |
typename CostMap::Value co;
|
deba@2413
|
77 |
while (is >> c) {
|
deba@2413
|
78 |
switch (c) {
|
deba@2413
|
79 |
case 'c': // comment line
|
deba@2413
|
80 |
getline(is, str);
|
deba@2413
|
81 |
break;
|
deba@2413
|
82 |
case 'p': // problem definition line
|
deba@2413
|
83 |
is >> problem >> n >> m;
|
deba@2413
|
84 |
getline(is, str);
|
deba@2413
|
85 |
if (problem != "min") return;
|
deba@2413
|
86 |
nodes.resize(n + 1);
|
deba@2413
|
87 |
for (int k = 1; k <= n; ++k) {
|
deba@2413
|
88 |
nodes[k] = g.addNode();
|
deba@2413
|
89 |
supply[nodes[k]] = 0;
|
deba@2413
|
90 |
}
|
deba@2413
|
91 |
break;
|
deba@2413
|
92 |
case 'n': // node definition line
|
deba@2413
|
93 |
is >> i >> sup;
|
deba@2413
|
94 |
getline(is, str);
|
deba@2413
|
95 |
supply.set(nodes[i], sup);
|
deba@2413
|
96 |
break;
|
deba@2413
|
97 |
case 'a': // edge (arc) definition line
|
deba@2413
|
98 |
is >> i >> j >> low >> cap >> co;
|
deba@2413
|
99 |
getline(is, str);
|
deba@2413
|
100 |
e = g.addEdge(nodes[i], nodes[j]);
|
deba@2413
|
101 |
lower.set(e, low);
|
deba@2413
|
102 |
if (cap >= 0)
|
deba@2413
|
103 |
capacity.set(e, cap);
|
deba@2413
|
104 |
else
|
deba@2413
|
105 |
capacity.set(e, -1);
|
deba@2413
|
106 |
cost.set(e, co);
|
deba@2413
|
107 |
break;
|
deba@2413
|
108 |
}
|
deba@2413
|
109 |
}
|
deba@2413
|
110 |
}
|
jacint@575
|
111 |
|
deba@2413
|
112 |
/// DIMACS max flow reader function.
|
deba@2413
|
113 |
///
|
deba@2413
|
114 |
/// This function reads a max flow instance from DIMACS format,
|
deba@2413
|
115 |
/// i.e. from DIMACS files having a line starting with
|
deba@2413
|
116 |
/// \code
|
deba@2413
|
117 |
/// p max
|
deba@2413
|
118 |
/// \endcode
|
deba@2413
|
119 |
/// At the beginning \c g is cleared by \c g.clear(). The edge
|
deba@2413
|
120 |
/// capacities are written to \c capacity and \c s and \c t are
|
deba@2413
|
121 |
/// set to the source and the target nodes.
|
marci@465
|
122 |
///
|
jacint@575
|
123 |
/// \author Marton Makai
|
deba@2413
|
124 |
template<typename Graph, typename CapacityMap>
|
jacint@528
|
125 |
void readDimacs(std::istream& is, Graph &g, CapacityMap& capacity,
|
deba@2413
|
126 |
typename Graph::Node &s, typename Graph::Node &t) {
|
jacint@528
|
127 |
g.clear();
|
deba@2413
|
128 |
std::vector<typename Graph::Node> nodes;
|
deba@2413
|
129 |
typename Graph::Edge e;
|
deba@2413
|
130 |
std::string problem;
|
deba@2413
|
131 |
char c, d;
|
deba@2413
|
132 |
int n, m;
|
deba@2413
|
133 |
int i, j;
|
alpar@987
|
134 |
typename CapacityMap::Value _cap;
|
jacint@528
|
135 |
std::string str;
|
deba@2413
|
136 |
while (is >> c) {
|
jacint@528
|
137 |
switch (c) {
|
deba@2413
|
138 |
case 'c': // comment line
|
jacint@528
|
139 |
getline(is, str);
|
jacint@528
|
140 |
break;
|
deba@2413
|
141 |
case 'p': // problem definition line
|
jacint@528
|
142 |
is >> problem >> n >> m;
|
jacint@528
|
143 |
getline(is, str);
|
deba@2413
|
144 |
nodes.resize(n + 1);
|
deba@2413
|
145 |
for (int k = 1; k <= n; ++k)
|
deba@2413
|
146 |
nodes[k] = g.addNode();
|
jacint@528
|
147 |
break;
|
deba@2413
|
148 |
case 'n': // node definition line
|
deba@2413
|
149 |
if (problem == "sp") { // shortest path problem
|
jacint@528
|
150 |
is >> i;
|
jacint@528
|
151 |
getline(is, str);
|
deba@2413
|
152 |
s = nodes[i];
|
jacint@528
|
153 |
}
|
deba@2413
|
154 |
if (problem == "max") { // max flow problem
|
jacint@528
|
155 |
is >> i >> d;
|
jacint@528
|
156 |
getline(is, str);
|
deba@2413
|
157 |
if (d == 's') s = nodes[i];
|
deba@2413
|
158 |
if (d == 't') t = nodes[i];
|
jacint@528
|
159 |
}
|
jacint@528
|
160 |
break;
|
deba@2413
|
161 |
case 'a': // edge (arc) definition line
|
deba@2413
|
162 |
if (problem == "max" || problem == "sp") {
|
jacint@528
|
163 |
is >> i >> j >> _cap;
|
jacint@528
|
164 |
getline(is, str);
|
deba@2413
|
165 |
e = g.addEdge(nodes[i], nodes[j]);
|
jacint@528
|
166 |
capacity.set(e, _cap);
|
jacint@575
|
167 |
} else {
|
deba@2413
|
168 |
is >> i >> j;
|
deba@2413
|
169 |
getline(is, str);
|
deba@2413
|
170 |
g.addEdge(nodes[i], nodes[j]);
|
jacint@528
|
171 |
}
|
jacint@528
|
172 |
break;
|
jacint@528
|
173 |
}
|
jacint@528
|
174 |
}
|
jacint@528
|
175 |
}
|
jacint@528
|
176 |
|
deba@2413
|
177 |
/// DIMACS shortest path reader function.
|
jacint@575
|
178 |
///
|
deba@2413
|
179 |
/// This function reads a shortest path instance from DIMACS format,
|
deba@2413
|
180 |
/// i.e. from DIMACS files having a line starting with
|
deba@2413
|
181 |
/// \code
|
deba@2413
|
182 |
/// p sp
|
deba@2413
|
183 |
/// \endcode
|
jacint@575
|
184 |
/// At the beginning \c g is cleared by \c g.clear(). The edge
|
jacint@575
|
185 |
/// capacities are written to \c capacity and \c s is set to the
|
jacint@575
|
186 |
/// source node.
|
jacint@575
|
187 |
///
|
jacint@575
|
188 |
/// \author Marton Makai
|
jacint@575
|
189 |
template<typename Graph, typename CapacityMap>
|
jacint@575
|
190 |
void readDimacs(std::istream& is, Graph &g, CapacityMap& capacity,
|
jacint@575
|
191 |
typename Graph::Node &s) {
|
deba@2413
|
192 |
readDimacs(is, g, capacity, s, s);
|
jacint@575
|
193 |
}
|
jacint@575
|
194 |
|
deba@2413
|
195 |
/// DIMACS capacitated graph reader function.
|
deba@2413
|
196 |
///
|
jacint@575
|
197 |
/// This function reads an edge capacitated graph instance from
|
deba@2413
|
198 |
/// DIMACS format. At the beginning \c g is cleared by \c g.clear()
|
jacint@575
|
199 |
/// and the edge capacities are written to \c capacity.
|
jacint@575
|
200 |
///
|
jacint@575
|
201 |
/// \author Marton Makai
|
jacint@575
|
202 |
template<typename Graph, typename CapacityMap>
|
jacint@575
|
203 |
void readDimacs(std::istream& is, Graph &g, CapacityMap& capacity) {
|
jacint@575
|
204 |
typename Graph::Node u;
|
deba@2413
|
205 |
readDimacs(is, g, capacity, u, u);
|
jacint@575
|
206 |
}
|
jacint@575
|
207 |
|
deba@2413
|
208 |
/// DIMACS plain graph reader function.
|
deba@2413
|
209 |
///
|
jacint@575
|
210 |
/// This function reads a graph without any designated nodes and
|
deba@2413
|
211 |
/// maps from DIMACS format, i.e. from DIMACS files having a line
|
alpar@964
|
212 |
/// starting with
|
deba@2413
|
213 |
/// \code
|
deba@2413
|
214 |
/// p mat
|
deba@2413
|
215 |
/// \endcode
|
deba@2413
|
216 |
/// At the beginning \c g is cleared by \c g.clear().
|
jacint@575
|
217 |
///
|
jacint@575
|
218 |
/// \author Marton Makai
|
jacint@575
|
219 |
template<typename Graph>
|
jacint@575
|
220 |
void readDimacs(std::istream& is, Graph &g) {
|
jacint@575
|
221 |
typename Graph::Node u;
|
jacint@575
|
222 |
NullMap<typename Graph::Edge, int> n;
|
deba@2413
|
223 |
readDimacs(is, g, n, u, u);
|
jacint@575
|
224 |
}
|
jacint@575
|
225 |
|
deba@2413
|
226 |
/// DIMACS plain graph writer function.
|
deba@2413
|
227 |
///
|
deba@2413
|
228 |
/// This function writes a graph without any designated nodes and
|
deba@2413
|
229 |
/// maps into DIMACS format, i.e. into DIMACS file having a line
|
deba@2413
|
230 |
/// starting with
|
deba@2413
|
231 |
/// \code
|
deba@2413
|
232 |
/// p mat
|
deba@2413
|
233 |
/// \endcode
|
deba@2413
|
234 |
///
|
deba@2413
|
235 |
/// \author Marton Makai
|
jacint@528
|
236 |
template<typename Graph>
|
jacint@528
|
237 |
void writeDimacs(std::ostream& os, const Graph &g) {
|
jacint@528
|
238 |
typedef typename Graph::NodeIt NodeIt;
|
jacint@528
|
239 |
typedef typename Graph::EdgeIt EdgeIt;
|
jacint@528
|
240 |
|
deba@2413
|
241 |
os << "c matching problem" << std::endl;
|
deba@2413
|
242 |
os << "p mat " << g.nodeNum() << " " << g.edgeNum() << std::endl;
|
deba@2413
|
243 |
|
jacint@528
|
244 |
typename Graph::template NodeMap<int> nodes(g);
|
deba@2413
|
245 |
int i = 1;
|
deba@2413
|
246 |
for(NodeIt v(g); v != INVALID; ++v) {
|
jacint@528
|
247 |
nodes.set(v, i);
|
jacint@528
|
248 |
++i;
|
jacint@528
|
249 |
}
|
deba@2413
|
250 |
for(EdgeIt e(g); e != INVALID; ++e) {
|
alpar@986
|
251 |
os << "a " << nodes[g.source(e)] << " " << nodes[g.target(e)] << std::endl;
|
jacint@528
|
252 |
}
|
jacint@528
|
253 |
}
|
jacint@528
|
254 |
|
jacint@575
|
255 |
/// @}
|
jacint@528
|
256 |
|
alpar@921
|
257 |
} //namespace lemon
|
marci@423
|
258 |
|
alpar@921
|
259 |
#endif //LEMON_DIMACS_H
|