alpar@906: /* -*- C++ -*- alpar@906: * alpar@1956: * This file is a part of LEMON, a generic C++ optimization library alpar@1956: * alpar@2391: * Copyright (C) 2003-2007 alpar@1956: * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport alpar@1359: * (Egervary Research Group on Combinatorial Optimization, EGRES). alpar@906: * alpar@906: * Permission to use, modify and distribute this software is granted alpar@906: * provided that this copyright notice appears in all copies. For alpar@906: * precise terms see the accompanying LICENSE file. alpar@906: * alpar@906: * This software is provided "AS IS" with no warranty of any kind, alpar@906: * express or implied, and with no claim as to its suitability for any alpar@906: * purpose. alpar@906: * alpar@906: */ alpar@906: alpar@921: #ifndef LEMON_DIMACS_H alpar@921: #define LEMON_DIMACS_H marci@423: marci@423: #include marci@423: #include marci@423: #include alpar@921: #include deba@1993: #include marci@423: alpar@1287: /// \ingroup dimacs_group klao@442: /// \file deba@2413: /// \brief DIMACS file format reader. klao@427: alpar@921: namespace lemon { marci@423: alpar@1287: ///@defgroup dimacs_group DIMACS format alpar@1287: ///\brief Read and write files in DIMACS format alpar@1287: /// alpar@1287: ///Tools to read a graph from or write it to a file in DIMACS format alpar@1287: ///data alpar@1287: ///\ingroup io_group marci@423: alpar@1287: /// \addtogroup dimacs_group jacint@575: /// @{ deba@2413: deba@2413: /// DIMACS min cost flow reader function. deba@2413: /// deba@2413: /// This function reads a min cost flow instance from DIMACS format, deba@2413: /// i.e. from DIMACS files having a line starting with deba@2413: /// \code deba@2413: /// p min deba@2413: /// \endcode deba@2413: /// At the beginning \c g is cleared by \c g.clear(). The supply deba@2413: /// amount of the nodes are written to \c supply (signed). The deba@2413: /// lower bounds, capacities and costs of the edges are written to deba@2413: /// \c lower, \c capacity and \c cost. deba@2413: /// deba@2413: /// \author Marton Makai and Peter Kovacs deba@2413: template deba@2413: void readDimacs( std::istream& is, deba@2413: Graph &g, deba@2413: SupplyMap& supply, deba@2413: CapacityMap& lower, deba@2413: CapacityMap& capacity, deba@2413: CostMap& cost ) deba@2413: { deba@2413: g.clear(); deba@2413: std::vector nodes; deba@2413: typename Graph::Edge e; deba@2413: std::string problem, str; deba@2413: char c; deba@2413: int n, m; deba@2413: int i, j; deba@2413: typename SupplyMap::Value sup; deba@2413: typename CapacityMap::Value low; deba@2413: typename CapacityMap::Value cap; deba@2413: typename CostMap::Value co; deba@2413: while (is >> c) { deba@2413: switch (c) { deba@2413: case 'c': // comment line deba@2413: getline(is, str); deba@2413: break; deba@2413: case 'p': // problem definition line deba@2413: is >> problem >> n >> m; deba@2413: getline(is, str); deba@2413: if (problem != "min") return; deba@2413: nodes.resize(n + 1); deba@2413: for (int k = 1; k <= n; ++k) { deba@2413: nodes[k] = g.addNode(); deba@2413: supply[nodes[k]] = 0; deba@2413: } deba@2413: break; deba@2413: case 'n': // node definition line deba@2413: is >> i >> sup; deba@2413: getline(is, str); deba@2413: supply.set(nodes[i], sup); deba@2413: break; deba@2413: case 'a': // edge (arc) definition line deba@2413: is >> i >> j >> low >> cap >> co; deba@2413: getline(is, str); deba@2413: e = g.addEdge(nodes[i], nodes[j]); deba@2413: lower.set(e, low); deba@2413: if (cap >= 0) deba@2413: capacity.set(e, cap); deba@2413: else deba@2413: capacity.set(e, -1); deba@2413: cost.set(e, co); deba@2413: break; deba@2413: } deba@2413: } deba@2413: } jacint@575: deba@2413: /// DIMACS max flow reader function. deba@2413: /// deba@2413: /// This function reads a max flow instance from DIMACS format, deba@2413: /// i.e. from DIMACS files having a line starting with deba@2413: /// \code deba@2413: /// p max deba@2413: /// \endcode deba@2413: /// At the beginning \c g is cleared by \c g.clear(). The edge deba@2413: /// capacities are written to \c capacity and \c s and \c t are deba@2413: /// set to the source and the target nodes. marci@465: /// jacint@575: /// \author Marton Makai deba@2413: template jacint@528: void readDimacs(std::istream& is, Graph &g, CapacityMap& capacity, deba@2413: typename Graph::Node &s, typename Graph::Node &t) { jacint@528: g.clear(); deba@2413: std::vector nodes; deba@2413: typename Graph::Edge e; deba@2413: std::string problem; deba@2413: char c, d; deba@2413: int n, m; deba@2413: int i, j; alpar@987: typename CapacityMap::Value _cap; jacint@528: std::string str; deba@2413: while (is >> c) { jacint@528: switch (c) { deba@2413: case 'c': // comment line jacint@528: getline(is, str); jacint@528: break; deba@2413: case 'p': // problem definition line jacint@528: is >> problem >> n >> m; jacint@528: getline(is, str); deba@2413: nodes.resize(n + 1); deba@2413: for (int k = 1; k <= n; ++k) deba@2413: nodes[k] = g.addNode(); jacint@528: break; deba@2413: case 'n': // node definition line deba@2413: if (problem == "sp") { // shortest path problem jacint@528: is >> i; jacint@528: getline(is, str); deba@2413: s = nodes[i]; jacint@528: } deba@2413: if (problem == "max") { // max flow problem jacint@528: is >> i >> d; jacint@528: getline(is, str); deba@2413: if (d == 's') s = nodes[i]; deba@2413: if (d == 't') t = nodes[i]; jacint@528: } jacint@528: break; deba@2413: case 'a': // edge (arc) definition line deba@2413: if (problem == "max" || problem == "sp") { jacint@528: is >> i >> j >> _cap; jacint@528: getline(is, str); deba@2413: e = g.addEdge(nodes[i], nodes[j]); jacint@528: capacity.set(e, _cap); jacint@575: } else { deba@2413: is >> i >> j; deba@2413: getline(is, str); deba@2413: g.addEdge(nodes[i], nodes[j]); jacint@528: } jacint@528: break; jacint@528: } jacint@528: } jacint@528: } jacint@528: deba@2413: /// DIMACS shortest path reader function. jacint@575: /// deba@2413: /// This function reads a shortest path instance from DIMACS format, deba@2413: /// i.e. from DIMACS files having a line starting with deba@2413: /// \code deba@2413: /// p sp deba@2413: /// \endcode jacint@575: /// At the beginning \c g is cleared by \c g.clear(). The edge jacint@575: /// capacities are written to \c capacity and \c s is set to the jacint@575: /// source node. jacint@575: /// jacint@575: /// \author Marton Makai jacint@575: template jacint@575: void readDimacs(std::istream& is, Graph &g, CapacityMap& capacity, jacint@575: typename Graph::Node &s) { deba@2413: readDimacs(is, g, capacity, s, s); jacint@575: } jacint@575: deba@2413: /// DIMACS capacitated graph reader function. deba@2413: /// jacint@575: /// This function reads an edge capacitated graph instance from deba@2413: /// DIMACS format. At the beginning \c g is cleared by \c g.clear() jacint@575: /// and the edge capacities are written to \c capacity. jacint@575: /// jacint@575: /// \author Marton Makai jacint@575: template jacint@575: void readDimacs(std::istream& is, Graph &g, CapacityMap& capacity) { jacint@575: typename Graph::Node u; deba@2413: readDimacs(is, g, capacity, u, u); jacint@575: } jacint@575: deba@2413: /// DIMACS plain graph reader function. deba@2413: /// jacint@575: /// This function reads a graph without any designated nodes and deba@2413: /// maps from DIMACS format, i.e. from DIMACS files having a line alpar@964: /// starting with deba@2413: /// \code deba@2413: /// p mat deba@2413: /// \endcode deba@2413: /// At the beginning \c g is cleared by \c g.clear(). jacint@575: /// jacint@575: /// \author Marton Makai jacint@575: template jacint@575: void readDimacs(std::istream& is, Graph &g) { jacint@575: typename Graph::Node u; jacint@575: NullMap n; deba@2413: readDimacs(is, g, n, u, u); jacint@575: } jacint@575: deba@2413: /// DIMACS plain graph writer function. deba@2413: /// deba@2413: /// This function writes a graph without any designated nodes and deba@2413: /// maps into DIMACS format, i.e. into DIMACS file having a line deba@2413: /// starting with deba@2413: /// \code deba@2413: /// p mat deba@2413: /// \endcode deba@2413: /// deba@2413: /// \author Marton Makai jacint@528: template jacint@528: void writeDimacs(std::ostream& os, const Graph &g) { jacint@528: typedef typename Graph::NodeIt NodeIt; jacint@528: typedef typename Graph::EdgeIt EdgeIt; jacint@528: deba@2413: os << "c matching problem" << std::endl; deba@2413: os << "p mat " << g.nodeNum() << " " << g.edgeNum() << std::endl; deba@2413: jacint@528: typename Graph::template NodeMap nodes(g); deba@2413: int i = 1; deba@2413: for(NodeIt v(g); v != INVALID; ++v) { jacint@528: nodes.set(v, i); jacint@528: ++i; jacint@528: } deba@2413: for(EdgeIt e(g); e != INVALID; ++e) { alpar@986: os << "a " << nodes[g.source(e)] << " " << nodes[g.target(e)] << std::endl; jacint@528: } jacint@528: } jacint@528: jacint@575: /// @} jacint@528: alpar@921: } //namespace lemon marci@423: alpar@921: #endif //LEMON_DIMACS_H