/Src/Dependencies/Boost/boost/graph/read_dimacs.hpp

http://hadesmem.googlecode.com/ · C++ Header · 312 lines · 206 code · 56 blank · 50 comment · 42 complexity · 75e3bae670bda8253b7750a1a426055b MD5 · raw file

  1. //=======================================================================
  2. // Copyright 1997, 1998, 1999, 2000 University of Notre Dame.
  3. // Authors: Jeremy G. Siek, Andrew Lumsdaine, Lie-Quan Lee
  4. //
  5. // Distributed under the Boost Software License, Version 1.0. (See
  6. // accompanying file LICENSE_1_0.txt or copy at
  7. // http://www.boost.org/LICENSE_1_0.txt)
  8. //=======================================================================
  9. /*
  10. Reads maximal flow problem in extended DIMACS format.
  11. This works, but could use some polishing.
  12. */
  13. /* ----------------------------------------------------------------- */
  14. #ifndef BOOST_GRAPH_READ_DIMACS_HPP
  15. #define BOOST_GRAPH_READ_DIMACS_HPP
  16. #include <vector>
  17. #include <iostream>
  18. #include <string>
  19. #include <cstdio>
  20. #include <cstring>
  21. #include <cstdlib>
  22. #include <boost/graph/graph_traits.hpp>
  23. namespace boost {
  24. namespace detail {
  25. template <class Graph, class CapacityMap, class ReverseEdgeMap>
  26. int read_dimacs_max_flow_internal(Graph& g,
  27. CapacityMap capacity,
  28. ReverseEdgeMap reverse_edge,
  29. typename graph_traits<Graph>::vertex_descriptor& src,
  30. typename graph_traits<Graph>::vertex_descriptor& sink,
  31. std::istream& in,
  32. bool require_source_and_sink,
  33. const std::string& problem_type)
  34. {
  35. // const int MAXLINE = 100; /* max line length in the input file */
  36. const int ARC_FIELDS = 3; /* no of fields in arc line */
  37. const int NODE_FIELDS = 2; /* no of fields in node line */
  38. const int P_FIELDS = 3; /* no of fields in problem line */
  39. typedef typename graph_traits<Graph>::vertices_size_type vertices_size_type;
  40. typedef typename graph_traits<Graph>::vertex_descriptor vertex_descriptor;
  41. typedef typename graph_traits<Graph>::edge_descriptor edge_descriptor;
  42. std::vector<vertex_descriptor> verts;
  43. long m, n, /* number of edges and nodes */
  44. i, head, tail, cap;
  45. long no_lines=0, /* no of current input line */
  46. no_plines=0, /* no of problem-lines */
  47. no_nslines=0, /* no of node-source-lines */
  48. no_nklines=0, /* no of node-source-lines */
  49. no_alines=0; /* no of arc-lines */
  50. std::string in_line; /* for reading input line */
  51. char pr_type[4]; /* for reading type of the problem */
  52. char nd; /* source (s) or sink (t) */
  53. int k, /* temporary */
  54. err_no; /* no of detected error */
  55. /* -------------- error numbers & error messages ---------------- */
  56. const int EN1 = 0;
  57. const int EN2 = 1;
  58. const int EN3 = 2;
  59. const int EN4 = 3;
  60. // const int EN6 = 4;
  61. // const int EN10 = 5;
  62. // const int EN7 = 6;
  63. const int EN8 = 7;
  64. const int EN9 = 8;
  65. const int EN11 = 9;
  66. const int EN12 = 10;
  67. // const int EN13 = 11;
  68. const int EN14 = 12;
  69. const int EN16 = 13;
  70. const int EN15 = 14;
  71. const int EN17 = 15;
  72. const int EN18 = 16;
  73. const int EN21 = 17;
  74. const int EN19 = 18;
  75. const int EN20 = 19;
  76. const int EN22 = 20;
  77. static const char *err_message[] =
  78. {
  79. /* 0*/ "more than one problem line.",
  80. /* 1*/ "wrong number of parameters in the problem line.",
  81. /* 2*/ "it is not a Max Flow problem line.",
  82. /* 3*/ "bad value of a parameter in the problem line.",
  83. /* 4*/ "can't obtain enough memory to solve this problem.",
  84. /* 5*/ "more than one line with the problem name.",
  85. /* 6*/ "can't read problem name.",
  86. /* 7*/ "problem description must be before node description.",
  87. /* 8*/ "this parser doesn't support multiply sources and sinks.",
  88. /* 9*/ "wrong number of parameters in the node line.",
  89. /*10*/ "wrong value of parameters in the node line.",
  90. /*11*/ " ",
  91. /*12*/ "source and sink descriptions must be before arc descriptions.",
  92. /*13*/ "too many arcs in the input.",
  93. /*14*/ "wrong number of parameters in the arc line.",
  94. /*15*/ "wrong value of parameters in the arc line.",
  95. /*16*/ "unknown line type in the input.",
  96. /*17*/ "reading error.",
  97. /*18*/ "not enough arcs in the input.",
  98. /*19*/ "source or sink doesn't have incident arcs.",
  99. /*20*/ "can't read anything from the input file."
  100. };
  101. /* --------------------------------------------------------------- */
  102. /* The main loop:
  103. - reads the line of the input,
  104. - analyses its type,
  105. - checks correctness of parameters,
  106. - puts data to the arrays,
  107. - does service functions
  108. */
  109. while (std::getline(in, in_line)) {
  110. ++no_lines;
  111. switch (in_line[0]) {
  112. case 'c': /* skip lines with comments */
  113. case '\n': /* skip empty lines */
  114. case '\0': /* skip empty lines at the end of file */
  115. break;
  116. case 'p': /* problem description */
  117. if ( no_plines > 0 )
  118. /* more than one problem line */
  119. { err_no = EN1 ; goto error; }
  120. no_plines = 1;
  121. if (
  122. /* reading problem line: type of problem, no of nodes, no of arcs */
  123. std::sscanf ( in_line.c_str(), "%*c %3s %ld %ld", pr_type, &n, &m )
  124. != P_FIELDS
  125. )
  126. /*wrong number of parameters in the problem line*/
  127. { err_no = EN2; goto error; }
  128. if ( pr_type != problem_type )
  129. /*wrong problem type*/
  130. { err_no = EN3; goto error; }
  131. if ( n <= 0 || m <= 0 )
  132. /*wrong value of no of arcs or nodes*/
  133. { err_no = EN4; goto error; }
  134. {
  135. for (long vi = 0; vi < n; ++vi)
  136. verts.push_back(add_vertex(g));
  137. }
  138. break;
  139. case 'n': /* source(s) description */
  140. if ( no_plines == 0 )
  141. /* there was not problem line above */
  142. { err_no = EN8; goto error; }
  143. /* reading source or sink */
  144. k = std::sscanf ( in_line.c_str(),"%*c %ld %c", &i, &nd );
  145. --i; // index from 0
  146. if ( k < NODE_FIELDS )
  147. /* node line is incorrect */
  148. { err_no = EN11; goto error; }
  149. if ( i < 0 || i > n )
  150. /* wrong value of node */
  151. { err_no = EN12; goto error; }
  152. switch (nd) {
  153. case 's': /* source line */
  154. if ( no_nslines != 0)
  155. /* more than one source line */
  156. { err_no = EN9; goto error; }
  157. no_nslines = 1;
  158. src = verts[i];
  159. break;
  160. case 't': /* sink line */
  161. if ( no_nklines != 0)
  162. /* more than one sink line */
  163. { err_no = EN9; goto error; }
  164. no_nklines = 1;
  165. sink = verts[i];
  166. break;
  167. default:
  168. /* wrong type of node-line */
  169. err_no = EN12; goto error;
  170. }
  171. break;
  172. case 'a': /* arc description */
  173. if ( require_source_and_sink && (no_nslines == 0 || no_nklines == 0) )
  174. /* there was not source and sink description above */
  175. { err_no = EN14; goto error; }
  176. if ( no_alines >= m )
  177. /*too many arcs on input*/
  178. { err_no = EN16; goto error; }
  179. if (
  180. /* reading an arc description */
  181. std::sscanf ( in_line.c_str(),"%*c %ld %ld %ld",
  182. &tail, &head, &cap )
  183. != ARC_FIELDS
  184. )
  185. /* arc description is not correct */
  186. { err_no = EN15; goto error; }
  187. --tail; // index from 0, not 1
  188. --head;
  189. if ( tail < 0 || tail > n ||
  190. head < 0 || head > n
  191. )
  192. /* wrong value of nodes */
  193. { err_no = EN17; goto error; }
  194. {
  195. edge_descriptor e1, e2;
  196. bool in1, in2;
  197. boost::tie(e1, in1) = add_edge(verts[tail], verts[head], g);
  198. boost::tie(e2, in2) = add_edge(verts[head], verts[tail], g);
  199. if (!in1 || !in2) {
  200. std::cerr << "unable to add edge (" << head << "," << tail << ")"
  201. << std::endl;
  202. return -1;
  203. }
  204. capacity[e1] = cap;
  205. capacity[e2] = 0;
  206. reverse_edge[e1] = e2;
  207. reverse_edge[e2] = e1;
  208. }
  209. ++no_alines;
  210. break;
  211. default:
  212. /* unknown type of line */
  213. err_no = EN18; goto error;
  214. } /* end of switch */
  215. } /* end of input loop */
  216. /* ----- all is red or error while reading ----- */
  217. if ( in.eof() == 0 ) /* reading error */
  218. { err_no=EN21; goto error; }
  219. if ( no_lines == 0 ) /* empty input */
  220. { err_no = EN22; goto error; }
  221. if ( no_alines < m ) /* not enough arcs */
  222. { err_no = EN19; goto error; }
  223. if ( require_source_and_sink &&
  224. (out_degree(src, g) == 0 || out_degree(sink, g) == 0) )
  225. /* no arc goes out of the source */
  226. { err_no = EN20; goto error; }
  227. /* Thanks God! all is done */
  228. return (0);
  229. /* ---------------------------------- */
  230. error: /* error found reading input */
  231. std::printf ( "\nline %ld of input - %s\n",
  232. no_lines, err_message[err_no] );
  233. std::exit (1);
  234. return (0); /* to avoid warning */
  235. }
  236. /* -------------------- end of parser -------------------*/
  237. } // namespace detail
  238. template <class Graph, class CapacityMap, class ReverseEdgeMap>
  239. int read_dimacs_max_flow(Graph& g,
  240. CapacityMap capacity,
  241. ReverseEdgeMap reverse_edge,
  242. typename graph_traits<Graph>::vertex_descriptor& src,
  243. typename graph_traits<Graph>::vertex_descriptor& sink,
  244. std::istream& in = std::cin) {
  245. return detail::read_dimacs_max_flow_internal(g, capacity, reverse_edge, src, sink, in, true, "max");
  246. }
  247. template <class Graph, class CapacityMap, class ReverseEdgeMap>
  248. int read_dimacs_min_cut(Graph& g,
  249. CapacityMap capacity,
  250. ReverseEdgeMap reverse_edge,
  251. std::istream& in = std::cin) {
  252. typename graph_traits<Graph>::vertex_descriptor dummy_src, dummy_sink; // Not filled in
  253. return detail::read_dimacs_max_flow_internal(g, capacity, reverse_edge, dummy_src, dummy_sink, in, false, "cut");
  254. }
  255. } // namespace boost
  256. #endif // BOOST_GRAPH_READ_DIMACS_HPP