Main Page | Modules | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

dimacs.h

Go to the documentation of this file.
00001 /* -*- C++ -*-
00002  * lemon/dimacs.h - Part of LEMON, a generic C++ optimization library
00003  *
00004  * Copyright (C) 2005 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
00005  * (Egervary Research Group on Combinatorial Optimization, EGRES).
00006  *
00007  * Permission to use, modify and distribute this software is granted
00008  * provided that this copyright notice appears in all copies. For
00009  * precise terms see the accompanying LICENSE file.
00010  *
00011  * This software is provided "AS IS" with no warranty of any kind,
00012  * express or implied, and with no claim as to its suitability for any
00013  * purpose.
00014  *
00015  */
00016 
00017 #ifndef LEMON_DIMACS_H
00018 #define LEMON_DIMACS_H
00019 
00020 #include <iostream>
00021 #include <string>
00022 #include <vector>
00023 #include <lemon/maps.h>
00024 #include <lemon/invalid.h>
00025 
00029 
00030 namespace lemon {
00031 
00039 
00042 
00044 
00056   template<typename Graph, typename CapacityMap, typename CostMap>
00057   void readDimacs(std::istream& is, Graph &g, CapacityMap& capacity, 
00058                   typename Graph::Node &s, typename Graph::Node &t, 
00059                   CostMap& cost) {
00060     g.clear();
00061     typename CapacityMap::Value _cap;
00062     typename CostMap::Value _cost;
00063     char d;
00064     std::string problem;
00065     char c;
00066     int i, j;
00067     std::string str;
00068     int n, m; 
00069     typename Graph::Edge e;
00070     std::vector<typename Graph::Node> nodes;
00071     while (is>>c) {
00072       switch (c) {
00073       case 'c': //comment
00074         getline(is, str);
00075         break;
00076       case 'p': //problem definition
00077         is >> problem >> n >> m;
00078         getline(is, str);
00079         nodes.resize(n+1);
00080         for (int k=1; k<=n; ++k) nodes[k]=g.addNode();
00081         break;
00082       case 'n': //node definition
00083         if (problem=="sp") { //shortest path problem
00084           is >> i;
00085           getline(is, str);
00086           s=nodes[i];
00087         }
00088         if (problem=="max" || problem=="min") { //((max) or (min cost)) flow problem
00089           is >> i >> d;
00090           getline(is, str);
00091           if (d=='s') s=nodes[i];
00092           if (d=='t') t=nodes[i];
00093         }
00094         break;
00095       case 'a':
00096         if ( problem == "max" || problem == "sp") {
00097           is >> i >> j >> _cap;
00098           getline(is, str);
00099           e=g.addEdge(nodes[i], nodes[j]);
00100           //capacity.update();
00101           capacity.set(e, _cap);
00102         } else {
00103           if ( problem == "min" ) {
00104             is >> i >> j >> _cap >> _cost;
00105             getline(is, str);
00106             e=g.addEdge(nodes[i], nodes[j]);
00107             //capacity.update();
00108             capacity.set(e, _cap);
00109             //cost.update();
00110             cost.set(e, _cost);
00111           } else {
00112             is >> i >> j;
00113             getline(is, str);
00114             g.addEdge(nodes[i], nodes[j]);
00115           }
00116         }
00117         break;
00118       }
00119     }
00120   }
00121 
00122 
00124 
00135   template<typename Graph, typename CapacityMap>
00136   void readDimacs(std::istream& is, Graph &g, CapacityMap& capacity, 
00137                   typename Graph::Node &s, typename Graph::Node &t) {
00138     NullMap<typename Graph::Edge, int> n;
00139     readDimacs(is, g, capacity, s, t, n);
00140   }
00141 
00142 
00144 
00155   template<typename Graph, typename CapacityMap>
00156   void readDimacs(std::istream& is, Graph &g, CapacityMap& capacity, 
00157                   typename Graph::Node &s) {
00158     NullMap<typename Graph::Edge, int> n;
00159     readDimacs(is, g, capacity, s, s, n);
00160   }
00161 
00162 
00164 
00170   template<typename Graph, typename CapacityMap>
00171   void readDimacs(std::istream& is, Graph &g, CapacityMap& capacity) {
00172     typename Graph::Node u;
00173     NullMap<typename Graph::Edge, int> n;
00174     readDimacs(is, g, capacity, u, u, n);
00175   }
00176 
00177 
00179 
00190   template<typename Graph>
00191   void readDimacs(std::istream& is, Graph &g) {
00192     typename Graph::Node u;
00193     NullMap<typename Graph::Edge, int> n;
00194     readDimacs(is, g, n, u, u, n);
00195   }
00196   
00197 
00198 
00199   
00201   template<typename Graph>
00202   void writeDimacs(std::ostream& os, const Graph &g) {
00203     typedef typename Graph::NodeIt NodeIt;
00204     typedef typename Graph::EdgeIt EdgeIt;  
00205     
00206     typename Graph::template NodeMap<int> nodes(g);
00207 
00208     os << "c matching problem" << std::endl;
00209 
00210     int i=1;
00211     for(NodeIt v(g); v!=INVALID; ++v) {
00212       nodes.set(v, i);
00213       ++i;
00214     }    
00215     
00216     os << "p mat " << g.nodeNum() << " " << g.edgeNum() << std::endl;
00217 
00218     for(EdgeIt e(g); e!=INVALID; ++e) {
00219       os << "a " << nodes[g.source(e)] << " " << nodes[g.target(e)] << std::endl; 
00220     }
00221 
00222   }
00223 
00225 
00226 } //namespace lemon
00227 
00228 #endif //LEMON_DIMACS_H

Generated on Sat Aug 27 14:14:51 2005 for LEMON by  doxygen 1.4.4