alpar@385
|
1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*-
|
alpar@385
|
2 |
*
|
alpar@385
|
3 |
* This file is a part of LEMON, a generic C++ optimization library.
|
alpar@385
|
4 |
*
|
alpar@1092
|
5 |
* Copyright (C) 2003-2013
|
alpar@385
|
6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
|
alpar@385
|
7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES).
|
alpar@385
|
8 |
*
|
alpar@385
|
9 |
* Permission to use, modify and distribute this software is granted
|
alpar@385
|
10 |
* provided that this copyright notice appears in all copies. For
|
alpar@385
|
11 |
* precise terms see the accompanying LICENSE file.
|
alpar@385
|
12 |
*
|
alpar@385
|
13 |
* This software is provided "AS IS" with no warranty of any kind,
|
alpar@385
|
14 |
* express or implied, and with no claim as to its suitability for any
|
alpar@385
|
15 |
* purpose.
|
alpar@385
|
16 |
*
|
alpar@385
|
17 |
*/
|
alpar@385
|
18 |
|
alpar@385
|
19 |
#ifndef LEMON_DIMACS_H
|
alpar@385
|
20 |
#define LEMON_DIMACS_H
|
alpar@385
|
21 |
|
alpar@385
|
22 |
#include <iostream>
|
alpar@385
|
23 |
#include <string>
|
alpar@385
|
24 |
#include <vector>
|
alpar@561
|
25 |
#include <limits>
|
alpar@385
|
26 |
#include <lemon/maps.h>
|
alpar@387
|
27 |
#include <lemon/error.h>
|
kpeter@1157
|
28 |
|
alpar@385
|
29 |
/// \ingroup dimacs_group
|
alpar@385
|
30 |
/// \file
|
alpar@385
|
31 |
/// \brief DIMACS file format reader.
|
alpar@385
|
32 |
|
alpar@385
|
33 |
namespace lemon {
|
alpar@385
|
34 |
|
alpar@385
|
35 |
/// \addtogroup dimacs_group
|
alpar@385
|
36 |
/// @{
|
alpar@385
|
37 |
|
alpar@387
|
38 |
/// DIMACS file type descriptor.
|
alpar@387
|
39 |
struct DimacsDescriptor
|
alpar@387
|
40 |
{
|
kpeter@584
|
41 |
///\brief DIMACS file type enum
|
kpeter@584
|
42 |
///
|
kpeter@584
|
43 |
///DIMACS file type enum.
|
kpeter@584
|
44 |
enum Type {
|
kpeter@584
|
45 |
NONE, ///< Undefined type.
|
kpeter@584
|
46 |
MIN, ///< DIMACS file type for minimum cost flow problems.
|
kpeter@584
|
47 |
MAX, ///< DIMACS file type for maximum flow problems.
|
kpeter@584
|
48 |
SP, ///< DIMACS file type for shostest path problems.
|
kpeter@584
|
49 |
MAT ///< DIMACS file type for plain graphs and matching problems.
|
kpeter@584
|
50 |
};
|
alpar@387
|
51 |
///The file type
|
alpar@387
|
52 |
Type type;
|
kpeter@388
|
53 |
///The number of nodes in the graph
|
alpar@387
|
54 |
int nodeNum;
|
kpeter@388
|
55 |
///The number of edges in the graph
|
alpar@387
|
56 |
int edgeNum;
|
alpar@387
|
57 |
int lineShift;
|
kpeter@584
|
58 |
///Constructor. It sets the type to \c NONE.
|
alpar@387
|
59 |
DimacsDescriptor() : type(NONE) {}
|
alpar@387
|
60 |
};
|
alpar@387
|
61 |
|
alpar@387
|
62 |
///Discover the type of a DIMACS file
|
alpar@387
|
63 |
|
kpeter@584
|
64 |
///This function starts seeking the beginning of the given file for the
|
alpar@877
|
65 |
///problem type and size info.
|
kpeter@584
|
66 |
///The found data is returned in a special struct that can be evaluated
|
kpeter@584
|
67 |
///and passed to the appropriate reader function.
|
alpar@387
|
68 |
DimacsDescriptor dimacsType(std::istream& is)
|
alpar@387
|
69 |
{
|
alpar@387
|
70 |
DimacsDescriptor r;
|
alpar@387
|
71 |
std::string problem,str;
|
alpar@387
|
72 |
char c;
|
alpar@387
|
73 |
r.lineShift=0;
|
alpar@387
|
74 |
while (is >> c)
|
alpar@387
|
75 |
switch(c)
|
alpar@387
|
76 |
{
|
alpar@387
|
77 |
case 'p':
|
alpar@387
|
78 |
if(is >> problem >> r.nodeNum >> r.edgeNum)
|
alpar@387
|
79 |
{
|
alpar@387
|
80 |
getline(is, str);
|
alpar@387
|
81 |
r.lineShift++;
|
alpar@387
|
82 |
if(problem=="min") r.type=DimacsDescriptor::MIN;
|
alpar@387
|
83 |
else if(problem=="max") r.type=DimacsDescriptor::MAX;
|
alpar@387
|
84 |
else if(problem=="sp") r.type=DimacsDescriptor::SP;
|
alpar@387
|
85 |
else if(problem=="mat") r.type=DimacsDescriptor::MAT;
|
alpar@387
|
86 |
else throw FormatError("Unknown problem type");
|
alpar@387
|
87 |
return r;
|
alpar@387
|
88 |
}
|
alpar@387
|
89 |
else
|
alpar@387
|
90 |
{
|
alpar@387
|
91 |
throw FormatError("Missing or wrong problem type declaration.");
|
alpar@387
|
92 |
}
|
alpar@387
|
93 |
break;
|
alpar@387
|
94 |
case 'c':
|
alpar@387
|
95 |
getline(is, str);
|
alpar@387
|
96 |
r.lineShift++;
|
alpar@387
|
97 |
break;
|
alpar@387
|
98 |
default:
|
alpar@387
|
99 |
throw FormatError("Unknown DIMACS declaration.");
|
alpar@387
|
100 |
}
|
alpar@387
|
101 |
throw FormatError("Missing problem type declaration.");
|
alpar@387
|
102 |
}
|
alpar@387
|
103 |
|
alpar@387
|
104 |
|
kpeter@584
|
105 |
/// \brief DIMACS minimum cost flow reader function.
|
alpar@385
|
106 |
///
|
kpeter@388
|
107 |
/// This function reads a minimum cost flow instance from DIMACS format,
|
kpeter@388
|
108 |
/// i.e. from a DIMACS file having a line starting with
|
alpar@385
|
109 |
/// \code
|
alpar@385
|
110 |
/// p min
|
alpar@385
|
111 |
/// \endcode
|
alpar@387
|
112 |
/// At the beginning, \c g is cleared by \c g.clear(). The supply
|
alpar@561
|
113 |
/// amount of the nodes are written to the \c supply node map
|
alpar@561
|
114 |
/// (they are signed values). The lower bounds, capacities and costs
|
alpar@561
|
115 |
/// of the arcs are written to the \c lower, \c capacity and \c cost
|
alpar@561
|
116 |
/// arc maps.
|
alpar@561
|
117 |
///
|
alpar@561
|
118 |
/// If the capacity of an arc is less than the lower bound, it will
|
alpar@561
|
119 |
/// be set to "infinite" instead. The actual value of "infinite" is
|
alpar@561
|
120 |
/// contolled by the \c infty parameter. If it is 0 (the default value),
|
alpar@561
|
121 |
/// \c std::numeric_limits<Capacity>::infinity() will be used if available,
|
alpar@561
|
122 |
/// \c std::numeric_limits<Capacity>::max() otherwise. If \c infty is set to
|
alpar@561
|
123 |
/// a non-zero value, that value will be used as "infinite".
|
alpar@387
|
124 |
///
|
alpar@387
|
125 |
/// If the file type was previously evaluated by dimacsType(), then
|
kpeter@1157
|
126 |
/// the descriptor struct should be given by the \c desc parameter.
|
alpar@385
|
127 |
template <typename Digraph, typename LowerMap,
|
alpar@387
|
128 |
typename CapacityMap, typename CostMap,
|
alpar@387
|
129 |
typename SupplyMap>
|
alpar@387
|
130 |
void readDimacsMin(std::istream& is,
|
alpar@387
|
131 |
Digraph &g,
|
alpar@387
|
132 |
LowerMap& lower,
|
alpar@387
|
133 |
CapacityMap& capacity,
|
alpar@387
|
134 |
CostMap& cost,
|
alpar@387
|
135 |
SupplyMap& supply,
|
alpar@561
|
136 |
typename CapacityMap::Value infty = 0,
|
alpar@387
|
137 |
DimacsDescriptor desc=DimacsDescriptor())
|
alpar@385
|
138 |
{
|
alpar@385
|
139 |
g.clear();
|
alpar@385
|
140 |
std::vector<typename Digraph::Node> nodes;
|
alpar@385
|
141 |
typename Digraph::Arc e;
|
alpar@385
|
142 |
std::string problem, str;
|
alpar@385
|
143 |
char c;
|
alpar@385
|
144 |
int i, j;
|
alpar@387
|
145 |
if(desc.type==DimacsDescriptor::NONE) desc=dimacsType(is);
|
alpar@387
|
146 |
if(desc.type!=DimacsDescriptor::MIN)
|
alpar@387
|
147 |
throw FormatError("Problem type mismatch");
|
alpar@387
|
148 |
|
alpar@387
|
149 |
nodes.resize(desc.nodeNum + 1);
|
alpar@387
|
150 |
for (int k = 1; k <= desc.nodeNum; ++k) {
|
alpar@387
|
151 |
nodes[k] = g.addNode();
|
alpar@387
|
152 |
supply.set(nodes[k], 0);
|
alpar@387
|
153 |
}
|
alpar@387
|
154 |
|
alpar@385
|
155 |
typename SupplyMap::Value sup;
|
alpar@385
|
156 |
typename CapacityMap::Value low;
|
alpar@385
|
157 |
typename CapacityMap::Value cap;
|
alpar@385
|
158 |
typename CostMap::Value co;
|
alpar@561
|
159 |
typedef typename CapacityMap::Value Capacity;
|
alpar@561
|
160 |
if(infty==0)
|
alpar@561
|
161 |
infty = std::numeric_limits<Capacity>::has_infinity ?
|
alpar@561
|
162 |
std::numeric_limits<Capacity>::infinity() :
|
alpar@561
|
163 |
std::numeric_limits<Capacity>::max();
|
alpar@561
|
164 |
|
alpar@385
|
165 |
while (is >> c) {
|
alpar@385
|
166 |
switch (c) {
|
alpar@385
|
167 |
case 'c': // comment line
|
alpar@385
|
168 |
getline(is, str);
|
alpar@385
|
169 |
break;
|
alpar@385
|
170 |
case 'n': // node definition line
|
alpar@385
|
171 |
is >> i >> sup;
|
alpar@385
|
172 |
getline(is, str);
|
alpar@385
|
173 |
supply.set(nodes[i], sup);
|
alpar@385
|
174 |
break;
|
alpar@561
|
175 |
case 'a': // arc definition line
|
alpar@385
|
176 |
is >> i >> j >> low >> cap >> co;
|
alpar@385
|
177 |
getline(is, str);
|
alpar@385
|
178 |
e = g.addArc(nodes[i], nodes[j]);
|
alpar@385
|
179 |
lower.set(e, low);
|
alpar@561
|
180 |
if (cap >= low)
|
alpar@385
|
181 |
capacity.set(e, cap);
|
alpar@385
|
182 |
else
|
alpar@561
|
183 |
capacity.set(e, infty);
|
alpar@385
|
184 |
cost.set(e, co);
|
alpar@385
|
185 |
break;
|
alpar@385
|
186 |
}
|
alpar@385
|
187 |
}
|
alpar@385
|
188 |
}
|
alpar@385
|
189 |
|
alpar@385
|
190 |
template<typename Digraph, typename CapacityMap>
|
alpar@387
|
191 |
void _readDimacs(std::istream& is,
|
alpar@387
|
192 |
Digraph &g,
|
alpar@387
|
193 |
CapacityMap& capacity,
|
alpar@387
|
194 |
typename Digraph::Node &s,
|
alpar@387
|
195 |
typename Digraph::Node &t,
|
alpar@561
|
196 |
typename CapacityMap::Value infty = 0,
|
alpar@387
|
197 |
DimacsDescriptor desc=DimacsDescriptor()) {
|
alpar@385
|
198 |
g.clear();
|
alpar@387
|
199 |
s=t=INVALID;
|
alpar@385
|
200 |
std::vector<typename Digraph::Node> nodes;
|
alpar@385
|
201 |
typename Digraph::Arc e;
|
alpar@385
|
202 |
char c, d;
|
alpar@385
|
203 |
int i, j;
|
alpar@385
|
204 |
typename CapacityMap::Value _cap;
|
alpar@385
|
205 |
std::string str;
|
alpar@387
|
206 |
nodes.resize(desc.nodeNum + 1);
|
alpar@387
|
207 |
for (int k = 1; k <= desc.nodeNum; ++k) {
|
alpar@387
|
208 |
nodes[k] = g.addNode();
|
alpar@387
|
209 |
}
|
alpar@561
|
210 |
typedef typename CapacityMap::Value Capacity;
|
alpar@387
|
211 |
|
alpar@561
|
212 |
if(infty==0)
|
alpar@561
|
213 |
infty = std::numeric_limits<Capacity>::has_infinity ?
|
alpar@561
|
214 |
std::numeric_limits<Capacity>::infinity() :
|
alpar@561
|
215 |
std::numeric_limits<Capacity>::max();
|
alpar@877
|
216 |
|
alpar@385
|
217 |
while (is >> c) {
|
alpar@385
|
218 |
switch (c) {
|
alpar@385
|
219 |
case 'c': // comment line
|
alpar@385
|
220 |
getline(is, str);
|
alpar@385
|
221 |
break;
|
alpar@385
|
222 |
case 'n': // node definition line
|
alpar@387
|
223 |
if (desc.type==DimacsDescriptor::SP) { // shortest path problem
|
alpar@385
|
224 |
is >> i;
|
alpar@385
|
225 |
getline(is, str);
|
alpar@385
|
226 |
s = nodes[i];
|
alpar@385
|
227 |
}
|
alpar@387
|
228 |
if (desc.type==DimacsDescriptor::MAX) { // max flow problem
|
alpar@385
|
229 |
is >> i >> d;
|
alpar@385
|
230 |
getline(is, str);
|
alpar@385
|
231 |
if (d == 's') s = nodes[i];
|
alpar@385
|
232 |
if (d == 't') t = nodes[i];
|
alpar@385
|
233 |
}
|
alpar@385
|
234 |
break;
|
alpar@561
|
235 |
case 'a': // arc definition line
|
alpar@561
|
236 |
if (desc.type==DimacsDescriptor::SP) {
|
alpar@385
|
237 |
is >> i >> j >> _cap;
|
alpar@385
|
238 |
getline(is, str);
|
alpar@385
|
239 |
e = g.addArc(nodes[i], nodes[j]);
|
alpar@385
|
240 |
capacity.set(e, _cap);
|
alpar@877
|
241 |
}
|
alpar@561
|
242 |
else if (desc.type==DimacsDescriptor::MAX) {
|
alpar@561
|
243 |
is >> i >> j >> _cap;
|
alpar@561
|
244 |
getline(is, str);
|
alpar@561
|
245 |
e = g.addArc(nodes[i], nodes[j]);
|
alpar@561
|
246 |
if (_cap >= 0)
|
alpar@561
|
247 |
capacity.set(e, _cap);
|
alpar@561
|
248 |
else
|
alpar@561
|
249 |
capacity.set(e, infty);
|
alpar@561
|
250 |
}
|
alpar@561
|
251 |
else {
|
alpar@385
|
252 |
is >> i >> j;
|
alpar@385
|
253 |
getline(is, str);
|
alpar@385
|
254 |
g.addArc(nodes[i], nodes[j]);
|
alpar@385
|
255 |
}
|
alpar@385
|
256 |
break;
|
alpar@385
|
257 |
}
|
alpar@385
|
258 |
}
|
alpar@385
|
259 |
}
|
alpar@385
|
260 |
|
kpeter@584
|
261 |
/// \brief DIMACS maximum flow reader function.
|
alpar@387
|
262 |
///
|
kpeter@388
|
263 |
/// This function reads a maximum flow instance from DIMACS format,
|
kpeter@388
|
264 |
/// i.e. from a DIMACS file having a line starting with
|
alpar@387
|
265 |
/// \code
|
alpar@387
|
266 |
/// p max
|
alpar@387
|
267 |
/// \endcode
|
alpar@387
|
268 |
/// At the beginning, \c g is cleared by \c g.clear(). The arc
|
alpar@561
|
269 |
/// capacities are written to the \c capacity arc map and \c s and
|
alpar@561
|
270 |
/// \c t are set to the source and the target nodes.
|
alpar@561
|
271 |
///
|
alpar@561
|
272 |
/// If the capacity of an arc is negative, it will
|
alpar@561
|
273 |
/// be set to "infinite" instead. The actual value of "infinite" is
|
alpar@561
|
274 |
/// contolled by the \c infty parameter. If it is 0 (the default value),
|
alpar@561
|
275 |
/// \c std::numeric_limits<Capacity>::infinity() will be used if available,
|
alpar@561
|
276 |
/// \c std::numeric_limits<Capacity>::max() otherwise. If \c infty is set to
|
alpar@561
|
277 |
/// a non-zero value, that value will be used as "infinite".
|
alpar@387
|
278 |
///
|
alpar@387
|
279 |
/// If the file type was previously evaluated by dimacsType(), then
|
kpeter@1157
|
280 |
/// the descriptor struct should be given by the \c desc parameter.
|
alpar@387
|
281 |
template<typename Digraph, typename CapacityMap>
|
alpar@387
|
282 |
void readDimacsMax(std::istream& is,
|
alpar@387
|
283 |
Digraph &g,
|
alpar@387
|
284 |
CapacityMap& capacity,
|
alpar@387
|
285 |
typename Digraph::Node &s,
|
alpar@387
|
286 |
typename Digraph::Node &t,
|
alpar@561
|
287 |
typename CapacityMap::Value infty = 0,
|
alpar@387
|
288 |
DimacsDescriptor desc=DimacsDescriptor()) {
|
alpar@387
|
289 |
if(desc.type==DimacsDescriptor::NONE) desc=dimacsType(is);
|
alpar@387
|
290 |
if(desc.type!=DimacsDescriptor::MAX)
|
alpar@387
|
291 |
throw FormatError("Problem type mismatch");
|
alpar@561
|
292 |
_readDimacs(is,g,capacity,s,t,infty,desc);
|
alpar@387
|
293 |
}
|
alpar@387
|
294 |
|
kpeter@584
|
295 |
/// \brief DIMACS shortest path reader function.
|
alpar@385
|
296 |
///
|
alpar@385
|
297 |
/// This function reads a shortest path instance from DIMACS format,
|
kpeter@388
|
298 |
/// i.e. from a DIMACS file having a line starting with
|
alpar@385
|
299 |
/// \code
|
alpar@385
|
300 |
/// p sp
|
alpar@385
|
301 |
/// \endcode
|
alpar@387
|
302 |
/// At the beginning, \c g is cleared by \c g.clear(). The arc
|
alpar@561
|
303 |
/// lengths are written to the \c length arc map and \c s is set to the
|
alpar@385
|
304 |
/// source node.
|
alpar@387
|
305 |
///
|
alpar@387
|
306 |
/// If the file type was previously evaluated by dimacsType(), then
|
kpeter@1157
|
307 |
/// the descriptor struct should be given by the \c desc parameter.
|
alpar@387
|
308 |
template<typename Digraph, typename LengthMap>
|
alpar@387
|
309 |
void readDimacsSp(std::istream& is,
|
alpar@387
|
310 |
Digraph &g,
|
alpar@387
|
311 |
LengthMap& length,
|
alpar@387
|
312 |
typename Digraph::Node &s,
|
alpar@387
|
313 |
DimacsDescriptor desc=DimacsDescriptor()) {
|
alpar@386
|
314 |
typename Digraph::Node t;
|
alpar@387
|
315 |
if(desc.type==DimacsDescriptor::NONE) desc=dimacsType(is);
|
alpar@387
|
316 |
if(desc.type!=DimacsDescriptor::SP)
|
alpar@387
|
317 |
throw FormatError("Problem type mismatch");
|
alpar@561
|
318 |
_readDimacs(is, g, length, s, t, 0, desc);
|
alpar@385
|
319 |
}
|
alpar@385
|
320 |
|
kpeter@584
|
321 |
/// \brief DIMACS capacitated digraph reader function.
|
alpar@385
|
322 |
///
|
alpar@385
|
323 |
/// This function reads an arc capacitated digraph instance from
|
alpar@561
|
324 |
/// DIMACS 'max' or 'sp' format.
|
alpar@387
|
325 |
/// At the beginning, \c g is cleared by \c g.clear()
|
alpar@561
|
326 |
/// and the arc capacities/lengths are written to the \c capacity
|
alpar@561
|
327 |
/// arc map.
|
alpar@561
|
328 |
///
|
alpar@561
|
329 |
/// In case of the 'max' format, if the capacity of an arc is negative,
|
alpar@561
|
330 |
/// it will
|
alpar@561
|
331 |
/// be set to "infinite" instead. The actual value of "infinite" is
|
alpar@561
|
332 |
/// contolled by the \c infty parameter. If it is 0 (the default value),
|
alpar@561
|
333 |
/// \c std::numeric_limits<Capacity>::infinity() will be used if available,
|
alpar@561
|
334 |
/// \c std::numeric_limits<Capacity>::max() otherwise. If \c infty is set to
|
alpar@561
|
335 |
/// a non-zero value, that value will be used as "infinite".
|
alpar@387
|
336 |
///
|
alpar@387
|
337 |
/// If the file type was previously evaluated by dimacsType(), then
|
kpeter@1157
|
338 |
/// the descriptor struct should be given by the \c desc parameter.
|
alpar@385
|
339 |
template<typename Digraph, typename CapacityMap>
|
alpar@387
|
340 |
void readDimacsCap(std::istream& is,
|
alpar@387
|
341 |
Digraph &g,
|
alpar@387
|
342 |
CapacityMap& capacity,
|
alpar@561
|
343 |
typename CapacityMap::Value infty = 0,
|
alpar@387
|
344 |
DimacsDescriptor desc=DimacsDescriptor()) {
|
alpar@386
|
345 |
typename Digraph::Node u,v;
|
alpar@387
|
346 |
if(desc.type==DimacsDescriptor::NONE) desc=dimacsType(is);
|
kpeter@1156
|
347 |
if(desc.type!=DimacsDescriptor::MAX && desc.type!=DimacsDescriptor::SP)
|
alpar@387
|
348 |
throw FormatError("Problem type mismatch");
|
alpar@561
|
349 |
_readDimacs(is, g, capacity, u, v, infty, desc);
|
alpar@385
|
350 |
}
|
alpar@385
|
351 |
|
alpar@525
|
352 |
template<typename Graph>
|
alpar@525
|
353 |
typename enable_if<lemon::UndirectedTagIndicator<Graph>,void>::type
|
alpar@525
|
354 |
_addArcEdge(Graph &g, typename Graph::Node s, typename Graph::Node t,
|
alpar@525
|
355 |
dummy<0> = 0)
|
alpar@525
|
356 |
{
|
alpar@525
|
357 |
g.addEdge(s,t);
|
alpar@525
|
358 |
}
|
alpar@525
|
359 |
template<typename Graph>
|
alpar@525
|
360 |
typename disable_if<lemon::UndirectedTagIndicator<Graph>,void>::type
|
alpar@525
|
361 |
_addArcEdge(Graph &g, typename Graph::Node s, typename Graph::Node t,
|
alpar@525
|
362 |
dummy<1> = 1)
|
alpar@525
|
363 |
{
|
alpar@525
|
364 |
g.addArc(s,t);
|
alpar@525
|
365 |
}
|
alpar@877
|
366 |
|
kpeter@584
|
367 |
/// \brief DIMACS plain (di)graph reader function.
|
alpar@385
|
368 |
///
|
kpeter@584
|
369 |
/// This function reads a plain (di)graph without any designated nodes
|
alpar@877
|
370 |
/// and maps (e.g. a matching instance) from DIMACS format, i.e. from
|
kpeter@584
|
371 |
/// DIMACS files having a line starting with
|
alpar@385
|
372 |
/// \code
|
alpar@385
|
373 |
/// p mat
|
alpar@385
|
374 |
/// \endcode
|
alpar@387
|
375 |
/// At the beginning, \c g is cleared by \c g.clear().
|
alpar@387
|
376 |
///
|
alpar@387
|
377 |
/// If the file type was previously evaluated by dimacsType(), then
|
kpeter@1157
|
378 |
/// the descriptor struct should be given by the \c desc parameter.
|
alpar@525
|
379 |
template<typename Graph>
|
alpar@525
|
380 |
void readDimacsMat(std::istream& is, Graph &g,
|
alpar@525
|
381 |
DimacsDescriptor desc=DimacsDescriptor())
|
alpar@525
|
382 |
{
|
alpar@387
|
383 |
if(desc.type==DimacsDescriptor::NONE) desc=dimacsType(is);
|
alpar@387
|
384 |
if(desc.type!=DimacsDescriptor::MAT)
|
alpar@387
|
385 |
throw FormatError("Problem type mismatch");
|
alpar@525
|
386 |
|
alpar@525
|
387 |
g.clear();
|
alpar@525
|
388 |
std::vector<typename Graph::Node> nodes;
|
alpar@525
|
389 |
char c;
|
alpar@525
|
390 |
int i, j;
|
alpar@525
|
391 |
std::string str;
|
alpar@525
|
392 |
nodes.resize(desc.nodeNum + 1);
|
alpar@525
|
393 |
for (int k = 1; k <= desc.nodeNum; ++k) {
|
alpar@525
|
394 |
nodes[k] = g.addNode();
|
alpar@525
|
395 |
}
|
alpar@877
|
396 |
|
alpar@525
|
397 |
while (is >> c) {
|
alpar@525
|
398 |
switch (c) {
|
alpar@525
|
399 |
case 'c': // comment line
|
alpar@525
|
400 |
getline(is, str);
|
alpar@525
|
401 |
break;
|
alpar@525
|
402 |
case 'n': // node definition line
|
alpar@525
|
403 |
break;
|
alpar@561
|
404 |
case 'a': // arc definition line
|
alpar@525
|
405 |
is >> i >> j;
|
alpar@525
|
406 |
getline(is, str);
|
alpar@525
|
407 |
_addArcEdge(g,nodes[i], nodes[j]);
|
alpar@525
|
408 |
break;
|
alpar@525
|
409 |
}
|
alpar@525
|
410 |
}
|
alpar@385
|
411 |
}
|
alpar@385
|
412 |
|
alpar@385
|
413 |
/// DIMACS plain digraph writer function.
|
alpar@385
|
414 |
///
|
alpar@385
|
415 |
/// This function writes a digraph without any designated nodes and
|
alpar@385
|
416 |
/// maps into DIMACS format, i.e. into DIMACS file having a line
|
alpar@385
|
417 |
/// starting with
|
alpar@385
|
418 |
/// \code
|
alpar@385
|
419 |
/// p mat
|
alpar@385
|
420 |
/// \endcode
|
alpar@387
|
421 |
/// If \c comment is not empty, then it will be printed in the first line
|
alpar@387
|
422 |
/// prefixed by 'c'.
|
alpar@385
|
423 |
template<typename Digraph>
|
alpar@387
|
424 |
void writeDimacsMat(std::ostream& os, const Digraph &g,
|
alpar@387
|
425 |
std::string comment="") {
|
alpar@385
|
426 |
typedef typename Digraph::NodeIt NodeIt;
|
alpar@385
|
427 |
typedef typename Digraph::ArcIt ArcIt;
|
alpar@385
|
428 |
|
alpar@440
|
429 |
if(!comment.empty())
|
alpar@387
|
430 |
os << "c " << comment << std::endl;
|
alpar@385
|
431 |
os << "p mat " << g.nodeNum() << " " << g.arcNum() << std::endl;
|
alpar@385
|
432 |
|
alpar@385
|
433 |
typename Digraph::template NodeMap<int> nodes(g);
|
alpar@385
|
434 |
int i = 1;
|
alpar@385
|
435 |
for(NodeIt v(g); v != INVALID; ++v) {
|
alpar@385
|
436 |
nodes.set(v, i);
|
alpar@385
|
437 |
++i;
|
alpar@385
|
438 |
}
|
alpar@385
|
439 |
for(ArcIt e(g); e != INVALID; ++e) {
|
alpar@385
|
440 |
os << "a " << nodes[g.source(e)] << " " << nodes[g.target(e)]
|
alpar@385
|
441 |
<< std::endl;
|
alpar@385
|
442 |
}
|
alpar@385
|
443 |
}
|
alpar@385
|
444 |
|
alpar@385
|
445 |
/// @}
|
alpar@385
|
446 |
|
alpar@385
|
447 |
} //namespace lemon
|
alpar@385
|
448 |
|
alpar@385
|
449 |
#endif //LEMON_DIMACS_H
|