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