src/hugo/dimacs.h
changeset 611 83530dad618a
parent 549 5531429143bc
child 778 08a1d1e3070d
equal deleted inserted replaced
2:743ab9d8e6fc 3:5bb65cb38835
     5 #include <iostream>
     5 #include <iostream>
     6 #include <string>
     6 #include <string>
     7 #include <vector>
     7 #include <vector>
     8 #include <hugo/maps.h>
     8 #include <hugo/maps.h>
     9 
     9 
       
    10 /// \ingroup misc
    10 /// \file
    11 /// \file
    11 /// \brief Dimacs file format reader.
    12 /// \brief Dimacs file format reader.
    12 
    13 
    13 namespace hugo {
    14 namespace hugo {
    14 
    15 
    15   /// Dimacs flow file format reader function.
    16 
    16 
    17   /// \addtogroup misc
    17   /// This function reads a flow instance from dimacs flow format.
    18   /// @{
    18   /// At the beginning \c g is cleared by \c g.clear().
    19 
    19   /// If the data coming from \c is is a max flow problem instance, then 
    20   /// Dimacs min cost flow reader function.
    20   /// \c s and \c t will be respectively the source and target nodes 
    21 
    21   /// and \c capacity will contain the edge capacities.
    22   /// This function reads a min cost flow instance from dimacs format,
    22   /// If the data is a shortest path problem instance then \c s will be the 
    23   /// i.e. from dimacs files having a line starting with \c p \c "min".
    23   /// source node and \c capacity will contain the edge lengths.
    24   /// At the beginning \c g is cleared by \c g.clear(). The edge
    24   ///
    25   /// capacities are written to \c capacity, \c s and \c t are set to
    25   ///\author Marton Makai
    26   /// the source and the target nodes resp. and the cost of the edges
    26   template<typename Graph, typename CapacityMap>
    27   /// are written to \c cost.
    27   void readDimacsMaxFlow(std::istream& is, Graph &g, 
    28   ///
    28 			 typename Graph::Node &s, typename Graph::Node &t, CapacityMap& capacity) {
    29   /// \author Marton Makai
    29     g.clear();
       
    30     int cap;
       
    31     char d;
       
    32     std::string problem;
       
    33     char c;
       
    34     int i, j;
       
    35     std::string str;
       
    36     int n, m; 
       
    37     typename Graph::Edge e;
       
    38     std::vector<typename Graph::Node> nodes;
       
    39     while (is>>c) {
       
    40       switch (c) {
       
    41       case 'c': //comment
       
    42 	getline(is, str);
       
    43 	break;
       
    44       case 'p': //problem definition
       
    45 	is >> problem >> n >> m;
       
    46 	getline(is, str);
       
    47 	nodes.resize(n+1);
       
    48 	for (int k=1; k<=n; ++k) nodes[k]=g.addNode();
       
    49 	break;
       
    50       case 'n': //node definition
       
    51 	if (problem=="sp") { //shortest path problem
       
    52 	  is >> i;
       
    53 	  getline(is, str);
       
    54 	  s=nodes[i];
       
    55 	}
       
    56 	if (problem=="max") { //max flow problem
       
    57 	  is >> i >> d;
       
    58 	  getline(is, str);
       
    59 	  if (d=='s') s=nodes[i];
       
    60 	  if (d=='t') t=nodes[i];
       
    61 	}
       
    62 	break;
       
    63       case 'a':
       
    64 	is >> i >> j >> cap;
       
    65 	getline(is, str);
       
    66 	e=g.addEdge(nodes[i], nodes[j]);
       
    67 	capacity.update();
       
    68 	capacity.set(e, cap);
       
    69 	break;
       
    70       }
       
    71     }
       
    72   }
       
    73 
       
    74   /// matching problem
       
    75   template<typename Graph>
       
    76   void readDimacs(std::istream& is, Graph &g) {
       
    77     typename Graph::Node u;
       
    78     NullMap<typename Graph::Edge, int> n;
       
    79     readDimacs(is, g, n, u, u, n);
       
    80   }
       
    81 
       
    82   /// sg problem
       
    83   template<typename Graph, typename CapacityMap>
       
    84   void readDimacs(std::istream& is, Graph &g, CapacityMap& capacity) {
       
    85     typename Graph::Node u;
       
    86     NullMap<typename Graph::Edge, int> n;
       
    87     readDimacs(is, g, capacity, u, u, n);
       
    88   }
       
    89 
       
    90   /// shortest path problem
       
    91   template<typename Graph, typename CapacityMap>
       
    92   void readDimacs(std::istream& is, Graph &g, CapacityMap& capacity, 
       
    93 		  typename Graph::Node &s) {
       
    94     NullMap<typename Graph::Edge, int> n;
       
    95     readDimacs(is, g, capacity, s, s, n);
       
    96   }
       
    97 
       
    98   /// max flow problem
       
    99   template<typename Graph, typename CapacityMap>
       
   100   void readDimacs(std::istream& is, Graph &g, CapacityMap& capacity, 
       
   101 		  typename Graph::Node &s, typename Graph::Node &t) {
       
   102     NullMap<typename Graph::Edge, int> n;
       
   103     readDimacs(is, g, capacity, s, t, n);
       
   104   }
       
   105 
       
   106   /// min cost flow problem
       
   107   template<typename Graph, typename CapacityMap, typename CostMap>
    30   template<typename Graph, typename CapacityMap, typename CostMap>
   108   void readDimacs(std::istream& is, Graph &g, CapacityMap& capacity, 
    31   void readDimacs(std::istream& is, Graph &g, CapacityMap& capacity, 
   109 		  typename Graph::Node &s, typename Graph::Node &t, 
    32 		  typename Graph::Node &s, typename Graph::Node &t, 
   110 		  CostMap& cost) {
    33 		  CostMap& cost) {
   111     g.clear();
    34     g.clear();
   142 	  if (d=='s') s=nodes[i];
    65 	  if (d=='s') s=nodes[i];
   143 	  if (d=='t') t=nodes[i];
    66 	  if (d=='t') t=nodes[i];
   144 	}
    67 	}
   145 	break;
    68 	break;
   146       case 'a':
    69       case 'a':
   147 	if ( problem == "mat" ) {
       
   148 	  is >> i >> j;
       
   149 	  getline(is, str);
       
   150 	  g.addEdge(nodes[i], nodes[j]);
       
   151 	}
       
   152 	if ( problem == "max" || problem == "sp") {
    70 	if ( problem == "max" || problem == "sp") {
   153 	  is >> i >> j >> _cap;
    71 	  is >> i >> j >> _cap;
   154 	  getline(is, str);
    72 	  getline(is, str);
   155 	  e=g.addEdge(nodes[i], nodes[j]);
    73 	  e=g.addEdge(nodes[i], nodes[j]);
   156 	  capacity.update();
    74 	  capacity.update();
   157 	  capacity.set(e, _cap);
    75 	  capacity.set(e, _cap);
   158 	}
    76 	} else {
   159 	if ( problem == "min" ) {
    77 	  if ( problem == "min" ) {
   160 	  is >> i >> j >> _cap >> _cost;
    78 	    is >> i >> j >> _cap >> _cost;
   161 	  getline(is, str);
    79 	    getline(is, str);
   162 	  e=g.addEdge(nodes[i], nodes[j]);
    80 	    e=g.addEdge(nodes[i], nodes[j]);
   163 	  capacity.update();
    81 	    capacity.update();
   164 	  capacity.set(e, _cap);
    82 	    capacity.set(e, _cap);
   165 	  cost.update();
    83 	    cost.update();
   166 	  cost.set(e, _cost);
    84 	    cost.set(e, _cost);
       
    85 	  } else {
       
    86 	    is >> i >> j;
       
    87 	    getline(is, str);
       
    88 	    g.addEdge(nodes[i], nodes[j]);
       
    89 	  }
   167 	}
    90 	}
   168 	break;
    91 	break;
   169       }
    92       }
   170     }
    93     }
   171   }
    94   }
       
    95 
       
    96 
       
    97   /// Dimacs max flow reader function.
       
    98 
       
    99   /// This function reads a max flow instance from dimacs format,
       
   100   /// i.e. from dimacs files having a line starting with \c p \c
       
   101   /// "max".  At the beginning \c g is cleared by \c g.clear(). The
       
   102   /// edge capacities are written to \c capacity and \c s and \c t are
       
   103   /// set to the source and the target nodes.
       
   104   ///
       
   105   /// \author Marton Makai
       
   106   template<typename Graph, typename CapacityMap>
       
   107   void readDimacs(std::istream& is, Graph &g, CapacityMap& capacity, 
       
   108 		  typename Graph::Node &s, typename Graph::Node &t) {
       
   109     NullMap<typename Graph::Edge, int> n;
       
   110     readDimacs(is, g, capacity, s, t, n);
       
   111   }
       
   112 
       
   113 
       
   114   /// Dimacs shortest path reader function.
       
   115 
       
   116   /// This function reads a shortest path instance from dimacs format,
       
   117   /// i.e. from dimacs files having a line starting with \c p \c "sp".
       
   118   /// At the beginning \c g is cleared by \c g.clear(). The edge
       
   119   /// capacities are written to \c capacity and \c s is set to the
       
   120   /// source node.
       
   121   ///
       
   122   /// \author Marton Makai
       
   123   template<typename Graph, typename CapacityMap>
       
   124   void readDimacs(std::istream& is, Graph &g, CapacityMap& capacity, 
       
   125 		  typename Graph::Node &s) {
       
   126     NullMap<typename Graph::Edge, int> n;
       
   127     readDimacs(is, g, capacity, s, s, n);
       
   128   }
       
   129 
       
   130 
       
   131   /// Dimacs capacitated graph reader function.
       
   132 
       
   133   /// This function reads an edge capacitated graph instance from
       
   134   /// dimacs format.  At the beginning \c g is cleared by \c g.clear()
       
   135   /// and the edge capacities are written to \c capacity.
       
   136   ///
       
   137   /// \author Marton Makai
       
   138   template<typename Graph, typename CapacityMap>
       
   139   void readDimacs(std::istream& is, Graph &g, CapacityMap& capacity) {
       
   140     typename Graph::Node u;
       
   141     NullMap<typename Graph::Edge, int> n;
       
   142     readDimacs(is, g, capacity, u, u, n);
       
   143   }
       
   144 
       
   145 
       
   146   /// Dimacs plain graph reader function.
       
   147 
       
   148   /// This function reads a graph without any designated nodes and
       
   149   /// maps from dimacs format, i.e. from dimacs files having a line
       
   150   /// starting with \c p \c "mat".  At the beginning \c g is cleared
       
   151   /// by \c g.clear().
       
   152   ///
       
   153   /// \author Marton Makai
       
   154   template<typename Graph>
       
   155   void readDimacs(std::istream& is, Graph &g) {
       
   156     typename Graph::Node u;
       
   157     NullMap<typename Graph::Edge, int> n;
       
   158     readDimacs(is, g, n, u, u, n);
       
   159   }
       
   160   
   172 
   161 
   173 
   162 
   174   
   163   
   175   /// write matching problem
   164   /// write matching problem
   176   template<typename Graph>
   165   template<typename Graph>
   197     }
   186     }
   198 
   187 
   199   }
   188   }
   200 
   189 
   201 
   190 
       
   191   /// @}
   202 
   192 
   203 } //namespace hugo
   193 } //namespace hugo
   204 
   194 
   205 #endif //HUGO_DIMACS_H
   195 #endif //HUGO_DIMACS_H
       
   196 
       
   197 //  template<typename Graph, typename CapacityMap>
       
   198 //   void readDimacsMaxFlow(std::istream& is, Graph &g, 
       
   199 // 			 typename Graph::Node &s, typename Graph::Node &t, CapacityMap& capacity) {
       
   200 //     g.clear();
       
   201 //     int cap;
       
   202 //     char d;
       
   203 //     std::string problem;
       
   204 //     char c;
       
   205 //     int i, j;
       
   206 //     std::string str;
       
   207 //     int n, m; 
       
   208 //     typename Graph::Edge e;
       
   209 //     std::vector<typename Graph::Node> nodes;
       
   210 //     while (is>>c) {
       
   211 //       switch (c) {
       
   212 //       case 'c': //comment
       
   213 // 	getline(is, str);
       
   214 // 	break;
       
   215 //       case 'p': //problem definition
       
   216 // 	is >> problem >> n >> m;
       
   217 // 	getline(is, str);
       
   218 // 	nodes.resize(n+1);
       
   219 // 	for (int k=1; k<=n; ++k) nodes[k]=g.addNode();
       
   220 // 	break;
       
   221 //       case 'n': //node definition
       
   222 // 	if (problem=="sp") { //shortest path problem
       
   223 // 	  is >> i;
       
   224 // 	  getline(is, str);
       
   225 // 	  s=nodes[i];
       
   226 // 	}
       
   227 // 	if (problem=="max") { //max flow problem
       
   228 // 	  is >> i >> d;
       
   229 // 	  getline(is, str);
       
   230 // 	  if (d=='s') s=nodes[i];
       
   231 // 	  if (d=='t') t=nodes[i];
       
   232 // 	}
       
   233 // 	break;
       
   234 //       case 'a':
       
   235 // 	is >> i >> j >> cap;
       
   236 // 	getline(is, str);
       
   237 // 	e=g.addEdge(nodes[i], nodes[j]);
       
   238 // 	capacity.update();
       
   239 // 	capacity.set(e, cap);
       
   240 // 	break;
       
   241 //       }
       
   242 //     }
       
   243 //   }