2 * src/lemon/dimacs.h - Part of LEMON, a generic C++ optimization library
4 * Copyright (C) 2004 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
5 * (Egervary Combinatorial Optimization Research Group, EGRES).
7 * Permission to use, modify and distribute this software is granted
8 * provided that this copyright notice appears in all copies. For
9 * precise terms see the accompanying LICENSE file.
11 * This software is provided "AS IS" with no warranty of any kind,
12 * express or implied, and with no claim as to its suitability for any
17 #ifndef LEMON_DIMACS_H
18 #define LEMON_DIMACS_H
23 #include <lemon/maps.h>
27 /// \brief Dimacs file format reader.
35 /// Dimacs min cost flow reader function.
37 /// This function reads a min cost flow instance from dimacs format,
38 /// i.e. from dimacs files having a line starting with
42 /// At the beginning \c g is cleared by \c g.clear(). The edge
43 /// capacities are written to \c capacity, \c s and \c t are set to
44 /// the source and the target nodes resp. and the cost of the edges
45 /// are written to \c cost.
47 /// \author Marton Makai
48 template<typename Graph, typename CapacityMap, typename CostMap>
49 void readDimacs(std::istream& is, Graph &g, CapacityMap& capacity,
50 typename Graph::Node &s, typename Graph::Node &t,
53 typename CapacityMap::Value _cap;
54 typename CostMap::Value _cost;
61 typename Graph::Edge e;
62 std::vector<typename Graph::Node> nodes;
68 case 'p': //problem definition
69 is >> problem >> n >> m;
72 for (int k=1; k<=n; ++k) nodes[k]=g.addNode();
74 case 'n': //node definition
75 if (problem=="sp") { //shortest path problem
80 if (problem=="max" || problem=="min") { //((max) or (min cost)) flow problem
83 if (d=='s') s=nodes[i];
84 if (d=='t') t=nodes[i];
88 if ( problem == "max" || problem == "sp") {
91 e=g.addEdge(nodes[i], nodes[j]);
93 capacity.set(e, _cap);
95 if ( problem == "min" ) {
96 is >> i >> j >> _cap >> _cost;
98 e=g.addEdge(nodes[i], nodes[j]);
100 capacity.set(e, _cap);
106 g.addEdge(nodes[i], nodes[j]);
115 /// Dimacs max flow reader function.
117 /// This function reads a max flow instance from dimacs format,
118 /// i.e. from dimacs files having a line starting with
122 ///At the beginning \c g is cleared by \c g.clear(). The
123 /// edge capacities are written to \c capacity and \c s and \c t are
124 /// set to the source and the target nodes.
126 /// \author Marton Makai
127 template<typename Graph, typename CapacityMap>
128 void readDimacs(std::istream& is, Graph &g, CapacityMap& capacity,
129 typename Graph::Node &s, typename Graph::Node &t) {
130 NullMap<typename Graph::Edge, int> n;
131 readDimacs(is, g, capacity, s, t, n);
135 /// Dimacs shortest path reader function.
137 /// This function reads a shortest path instance from dimacs format,
138 /// i.e. from dimacs files having a line starting with
142 /// At the beginning \c g is cleared by \c g.clear(). The edge
143 /// capacities are written to \c capacity and \c s is set to the
146 /// \author Marton Makai
147 template<typename Graph, typename CapacityMap>
148 void readDimacs(std::istream& is, Graph &g, CapacityMap& capacity,
149 typename Graph::Node &s) {
150 NullMap<typename Graph::Edge, int> n;
151 readDimacs(is, g, capacity, s, s, n);
155 /// Dimacs capacitated graph reader function.
157 /// This function reads an edge capacitated graph instance from
158 /// dimacs format. At the beginning \c g is cleared by \c g.clear()
159 /// and the edge capacities are written to \c capacity.
161 /// \author Marton Makai
162 template<typename Graph, typename CapacityMap>
163 void readDimacs(std::istream& is, Graph &g, CapacityMap& capacity) {
164 typename Graph::Node u;
165 NullMap<typename Graph::Edge, int> n;
166 readDimacs(is, g, capacity, u, u, n);
170 /// Dimacs plain graph reader function.
172 /// This function reads a graph without any designated nodes and
173 /// maps from dimacs format, i.e. from dimacs files having a line
178 /// At the beginning \c g is cleared
181 /// \author Marton Makai
182 template<typename Graph>
183 void readDimacs(std::istream& is, Graph &g) {
184 typename Graph::Node u;
185 NullMap<typename Graph::Edge, int> n;
186 readDimacs(is, g, n, u, u, n);
192 /// write matching problem
193 template<typename Graph>
194 void writeDimacs(std::ostream& os, const Graph &g) {
195 typedef typename Graph::NodeIt NodeIt;
196 typedef typename Graph::EdgeIt EdgeIt;
198 typename Graph::template NodeMap<int> nodes(g);
200 os << "c matching problem" << std::endl;
203 for(NodeIt v(g); v!=INVALID; ++v) {
208 os << "p mat " << g.nodeNum() << " " << g.edgeNum() << std::endl;
210 for(EdgeIt e(g); e!=INVALID; ++e) {
211 os << "a " << nodes[g.source(e)] << " " << nodes[g.target(e)] << std::endl;
220 #endif //LEMON_DIMACS_H