You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

4150 lines
127 KiB

  1. %* graphs.tex *%
  2. %***********************************************************************
  3. % This code is part of GLPK (GNU Linear Programming Kit).
  4. %
  5. % Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008,
  6. % 2009, 2010, 2011, 2013 Andrew Makhorin, Department for Applied
  7. % Informatics, Moscow Aviation Institute, Moscow, Russia. All rights
  8. % reserved. E-mail: <mao@gnu.org>.
  9. %
  10. % GLPK is free software: you can redistribute it and/or modify it
  11. % under the terms of the GNU General Public License as published by
  12. % the Free Software Foundation, either version 3 of the License, or
  13. % (at your option) any later version.
  14. %
  15. % GLPK is distributed in the hope that it will be useful, but WITHOUT
  16. % ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  17. % or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
  18. % License for more details.
  19. %
  20. % You should have received a copy of the GNU General Public License
  21. % along with GLPK. If not, see <http://www.gnu.org/licenses/>.
  22. %***********************************************************************
  23. \documentclass[11pt]{report}
  24. \usepackage{amssymb}
  25. \usepackage[dvipdfm,linktocpage,colorlinks,linkcolor=blue,
  26. urlcolor=blue]{hyperref}
  27. \usepackage{indentfirst}
  28. \usepackage[all]{xy}
  29. \setlength{\textwidth}{6.5in}
  30. \setlength{\textheight}{8.5in}
  31. \setlength{\oddsidemargin}{0in}
  32. \setlength{\topmargin}{0in}
  33. \setlength{\headheight}{0in}
  34. \setlength{\headsep}{0in}
  35. \setlength{\footskip}{0.5in}
  36. \setlength{\parindent}{16pt}
  37. \setlength{\parskip}{5pt}
  38. \setlength{\topsep}{0pt}
  39. \setlength{\partopsep}{0pt}
  40. \setlength{\itemsep}{\parskip}
  41. \setlength{\parsep}{0pt}
  42. \setlength{\leftmargini}{\parindent}
  43. \renewcommand{\labelitemi}{---}
  44. \def\para#1{\noindent{\bf#1}}
  45. \def\synopsis{\para{Synopsis}}
  46. \def\description{\para{Description}}
  47. \def\returns{\para{Returns}}
  48. \renewcommand\contentsname{\sf\bfseries Contents}
  49. \renewcommand\chaptername{\sf\bfseries Chapter}
  50. \renewcommand\appendixname{\sf\bfseries Appendix}
  51. \newenvironment{retlist}
  52. { \def\arraystretch{1.5}
  53. \noindent
  54. \begin{tabular}{@{}p{1in}@{}p{5.5in}@{}}
  55. }
  56. {\end{tabular}}
  57. \begin{document}
  58. \thispagestyle{empty}
  59. \begin{center}
  60. \vspace*{1.5in}
  61. \begin{huge}
  62. \sf\bfseries GNU Linear Programming Kit
  63. \end{huge}
  64. \vspace{0.5in}
  65. \begin{LARGE}
  66. \sf Graph and Network Routines
  67. \end{LARGE}
  68. \vspace{0.5in}
  69. \begin{LARGE}
  70. \sf for GLPK Version 4.49
  71. \end{LARGE}
  72. \vspace{0.5in}
  73. \begin{Large}
  74. \sf (DRAFT, April 2013)
  75. \end{Large}
  76. \end{center}
  77. \newpage
  78. \vspace*{1in}
  79. \vfill
  80. \noindent
  81. The GLPK package is part of the GNU Project released under the aegis of
  82. GNU.
  83. \noindent
  84. Copyright \copyright{} 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007,
  85. 2008, 2009, 2010, 2011, 2013 Andrew Makhorin, Department for Applied
  86. Informatics, Moscow Aviation Institute, Moscow, Russia. All rights
  87. reserved.
  88. \noindent
  89. Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  90. 02110-1301, USA.
  91. \noindent
  92. Permission is granted to make and distribute verbatim copies of this
  93. manual provided the copyright notice and this permission notice are
  94. preserved on all copies.
  95. \noindent
  96. Permission is granted to copy and distribute modified versions of this
  97. manual under the conditions for verbatim copying, provided also that the
  98. entire resulting derived work is distributed under the terms of
  99. a permission notice identical to this one.
  100. \noindent
  101. Permission is granted to copy and distribute translations of this manual
  102. into another language, under the above conditions for modified versions.
  103. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  104. \newpage
  105. {\setlength{\parskip}{0pt}
  106. \tableofcontents
  107. }
  108. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  109. \chapter{Basic Graph API Routines}
  110. \section{Graph program object}
  111. In GLPK the base program object used to represent graphs and networks
  112. is a directed graph (digraph).
  113. Formally, {\it digraph} (or simply, {\it graph}) is a pair $G=(V,A)$,
  114. where $V$ is a set of {\it vertices}, and $A$ is a set
  115. {\it arcs}.\footnote{$A$ may be a multiset.} Each arc $a\in A$ is an
  116. ordered pair of vertices $a=(x,y)$, where $x\in V$ is called {\it tail
  117. vertex} of arc $a$, and $y\in V$ is called its {\it head vertex}.
  118. Representation of a graph in the program includes three structs defined
  119. by typedef in the header \verb|glpk.h|:
  120. \vspace*{-8pt}
  121. \begin{itemize}
  122. \item \verb|glp_graph|, which represents the graph in a whole,
  123. \item \verb|glp_vertex|, which represents a vertex of the graph, and
  124. \item \verb|glp_arc|, which represents an arc of the graph.
  125. \end{itemize}
  126. \vspace*{-8pt}
  127. All these three structs are ``semi-opaque'', i.e. the application
  128. program can directly access their fields through pointers, however,
  129. changing the fields directly is not allowed --- all changes should be
  130. performed only with appropriate GLPK API routines.
  131. \newenvironment{comment}
  132. {\addtolength{\leftskip}{16pt}\noindent}
  133. {\par\addtolength{\leftskip}{-16pt}}
  134. \para{\bf glp\_graph.} The struct \verb|glp_graph| has the following
  135. fields available to the application program:
  136. \noindent
  137. \verb|char *name;|
  138. \begin{comment}Symbolic name assigned to the graph. It is a pointer to
  139. a null terminated character string of length from 1 to 255 characters.
  140. If no name is assigned to the graph, this field contains \verb|NULL|.
  141. \end{comment}
  142. \noindent
  143. \verb|int nv;|
  144. \begin{comment}The number of vertices in the graph, $nv\geq 0$.
  145. \end{comment}
  146. \noindent
  147. \verb|int na;|
  148. \begin{comment}The number of arcs in the graph, $na\geq 0$.
  149. \end{comment}
  150. \newpage
  151. \noindent
  152. \verb|glp_vertex **v;|
  153. \begin{comment}Pointer to an array containing the list of vertices.
  154. Element $v[0]$ is not used. Element $v[i]$, $1\leq i\leq nv$, is a
  155. pointer to $i$-th vertex of the graph. Note that on adding new vertices
  156. to the graph the field $v$ may be altered due to reallocation. However,
  157. pointers $v[i]$ are not changed while corresponding vertices exist in
  158. the graph.
  159. \end{comment}
  160. \noindent
  161. \verb|int v_size;|
  162. \begin{comment}Size of vertex data blocks, in bytes,
  163. $0\leq v\_size\leq 256$. (See also the field \verb|data| in the struct
  164. \verb|glp_vertex|.)
  165. \end{comment}
  166. \noindent
  167. \verb|int a_size;|
  168. \begin{comment}Size of arc data blocks, in bytes,
  169. $0\leq v\_size\leq 256$. (See also the field \verb|data| in the struct
  170. \verb|glp_arc|.)
  171. \end{comment}
  172. \para{\bf glp\_vertex.} The struct \verb|glp_vertex| has the following
  173. fields available to the application program:
  174. \noindent
  175. \verb|int i;|
  176. \begin{comment}Ordinal number of the vertex, $1\leq i\leq nv$. Note
  177. that element $v[i]$ in the struct \verb|glp_graph| points to the vertex,
  178. whose ordinal number is $i$.
  179. \end{comment}
  180. \noindent
  181. \verb|char *name;|
  182. \begin{comment}Symbolic name assigned to the vertex. It is a pointer to
  183. a null terminated character string of length from 1 to 255 characters.
  184. If no name is assigned to the vertex, this field contains \verb|NULL|.
  185. \end{comment}
  186. \noindent
  187. \verb|void *data;|
  188. \begin{comment}Pointer to a data block associated with the vertex. This
  189. data block is automatically allocated on creating a new vertex and freed
  190. on deleting the vertex. If $v\_size=0$, the block is not allocated, and
  191. this field contains \verb|NULL|.
  192. \end{comment}
  193. \noindent
  194. \verb|void *temp;|
  195. \begin{comment}Working pointer, which may be used freely for any
  196. purposes. The application program can change this field directly.
  197. \end{comment}
  198. \noindent
  199. \verb|glp_arc *in;|
  200. \begin{comment}Pointer to the (unordered) list of incoming arcs. If the
  201. vertex has no incoming arcs, this field contains \verb|NULL|.
  202. \end{comment}
  203. \noindent
  204. \verb|glp_arc *out;|
  205. \begin{comment}Pointer to the (unordered) list of outgoing arcs. If the
  206. vertex has no outgoing arcs, this field contains \verb|NULL|.
  207. \end{comment}
  208. \para{\bf glp\_arc.} The struct \verb|glp_arc| has the following fields
  209. available to the application program:
  210. \noindent
  211. \verb|glp_vertex *tail;|
  212. \begin{comment}Pointer to a vertex, which is tail endpoint of the arc.
  213. \end{comment}
  214. \noindent
  215. \verb|glp_vertex *head;|
  216. \begin{comment}Pointer to a vertex, which is head endpoint of the arc.
  217. \end{comment}
  218. \newpage
  219. \noindent
  220. \verb|void *data;|
  221. \begin{comment}Pointer to a data block associated with the arc. This
  222. data block is automatically allocated on creating a new arc and freed on
  223. deleting the arc. If $v\_size=0$, the block is not allocated, and this
  224. field contains \verb|NULL|.
  225. \end{comment}
  226. \noindent
  227. \verb|void *temp;|
  228. \begin{comment}Working pointer, which may be used freely for any
  229. purposes. The application program can change this field directly.
  230. \end{comment}
  231. \noindent
  232. \verb|glp_arc *t_next;|
  233. \begin{comment}Pointer to another arc, which has the same tail endpoint
  234. as this one. \verb|NULL| in this field indicates the end of the list of
  235. outgoing arcs.
  236. \end{comment}
  237. \noindent
  238. \verb|glp_arc *h_next;|
  239. \begin{comment}Pointer to another arc, which has the same head endpoint
  240. as this one. \verb|NULL| in this field indicates the end of the list of
  241. incoming arcs.
  242. \end{comment}
  243. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  244. \newpage
  245. \section{Graph creating and modifying routines}
  246. \subsection{glp\_create\_graph --- create graph}
  247. \synopsis
  248. \begin{verbatim}
  249. glp_graph *glp_create_graph(int v_size, int a_size);
  250. \end{verbatim}
  251. \description
  252. The routine \verb|glp_create_graph| creates a new graph, which
  253. initially is empty, i.e. has no vertices and arcs.
  254. The parameter \verb|v_size| specifies the size of vertex data blocks,
  255. in bytes, $0\leq v\_size\leq 256$.
  256. The parameter \verb|a_size| specifies the size of arc data blocks, in
  257. bytes, $0\leq a\_size\leq 256$.
  258. \returns
  259. The routine returns a pointer to the graph object created.
  260. \subsection{glp\_set\_graph\_name --- assign (change) graph name}
  261. \synopsis
  262. \begin{verbatim}
  263. void glp_set_graph_name(glp_graph *G, const char *name);
  264. \end{verbatim}
  265. \description
  266. The routine \verb|glp_set_graph_name| assigns a symbolic name specified
  267. by the character string \verb|name| (1 to 255 chars) to the graph.
  268. If the parameter \verb|name| is \verb|NULL| or an empty string, the
  269. routine erases the existing symbolic name of the graph.
  270. \subsection{glp\_add\_vertices --- add new vertices to graph}
  271. \synopsis
  272. \begin{verbatim}
  273. int glp_add_vertices(glp_graph *G, int nadd);
  274. \end{verbatim}
  275. \description
  276. The routine \verb|glp_add_vertices| adds \verb|nadd| vertices to the
  277. specified graph. New vertices are always added to the end of the vertex
  278. list, so ordinal numbers of existing vertices remain unchanged. Note
  279. that this operation may change the field \verb|v| in the struct
  280. \verb|glp_graph| (pointer to the vertex array) due to reallocation.
  281. Being added each new vertex is isolated, i.e. has no incident arcs.
  282. If the size of vertex data blocks specified on creating the graph is
  283. non-zero, the routine also allocates a memory block of that size for
  284. each new vertex added, fills it by binary zeros, and stores a pointer
  285. to it in the field \verb|data| of the struct \verb|glp_vertex|.
  286. Otherwise, if the block size is zero, the field \verb|data| is set to
  287. \verb|NULL|.
  288. \returns
  289. The routine \verb|glp_add_vertices| returns the ordinal number of the
  290. first new vertex added to the graph.
  291. \subsection{glp\_set\_vertex\_name --- assign (change) vertex name}
  292. \synopsis
  293. \begin{verbatim}
  294. void glp_set_vertex_name(glp_graph *G, int i, const char *name);
  295. \end{verbatim}
  296. \description
  297. The routine \verb|glp_set_vertex_name| assigns a given symbolic name
  298. (1 up to 255 characters) to \verb|i|-th vertex of the specified graph.
  299. If the parameter \verb|name| is \verb|NULL| or empty string, the
  300. routine erases an existing name of \verb|i|-th vertex.
  301. \subsection{glp\_add\_arc --- add new arc to graph}
  302. \synopsis
  303. \begin{verbatim}
  304. glp_arc *glp_add_arc(glp_graph *G, int i, int j);
  305. \end{verbatim}
  306. \description
  307. The routine \verb|glp_add_arc| adds one new arc to the specified graph.
  308. The parameters \verb|i| and \verb|j| specify the ordinal numbers of,
  309. resp., tail and head endpoints (vertices) of the arc. Note that
  310. self-loops and multiple arcs are allowed.
  311. If the size of arc data blocks specified on creating the graph is
  312. non-zero, the routine also allocates a memory block of that size, fills
  313. it by binary zeros, and stores a pointer to it in the field \verb|data|
  314. of the struct \verb|glp_arc|. Otherwise, if the block size is zero, the
  315. field \verb|data| is set to \verb|NULL|.
  316. \subsection{glp\_del\_vertices --- delete vertices from graph}
  317. \synopsis
  318. \begin{verbatim}
  319. void glp_del_vertices(glp_graph *G, int ndel, const int num[]);
  320. \end{verbatim}
  321. \description
  322. The routine \verb|glp_del_vertices| deletes vertices along with all
  323. incident arcs from the specified graph. Ordinal numbers of vertices to
  324. be deleted should be placed in locations \verb|num[1]|, \dots,
  325. \verb|num[ndel]|, \verb|ndel| $>0$.
  326. Note that deleting vertices involves changing ordinal numbers of other
  327. vertices remaining in the graph. New ordinal numbers of the remaining
  328. vertices are assigned under the assumption that the original order of
  329. vertices is not changed.
  330. \newpage
  331. \subsection{glp\_del\_arc --- delete arc from graph}
  332. \synopsis
  333. \begin{verbatim}
  334. void glp_del_arc(glp_graph *G, glp_arc *a);
  335. \end{verbatim}
  336. \description
  337. The routine \verb|glp_del_arc| deletes an arc from the specified graph.
  338. The arc to be deleted must exist.
  339. \subsection{glp\_erase\_graph --- erase graph content}
  340. \synopsis
  341. \begin{verbatim}
  342. void glp_erase_graph(glp_graph *G, int v_size, int a_size);
  343. \end{verbatim}
  344. \description
  345. The routine \verb|glp_erase_graph| erases the content of the specified
  346. graph. The effect of this operation is the same as if the graph would
  347. be deleted with the routine \verb|glp_delete_graph| and then created
  348. anew with the routine \verb|glp_create_graph|, with exception that the
  349. pointer to the graph remains valid.
  350. The parameters \verb|v_size| and \verb|a_size| have the same meaning as
  351. for \verb|glp_create_graph|.
  352. \subsection{glp\_delete\_graph --- delete graph}
  353. \synopsis
  354. \begin{verbatim}
  355. void glp_delete_graph(glp_graph *G);
  356. \end{verbatim}
  357. \description
  358. The routine \verb|glp_delete_graph| deletes the specified graph and
  359. frees all the memory allocated to this program object.
  360. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  361. \newpage
  362. \section{Graph searching routines}
  363. \subsection{glp\_create\_v\_index --- create vertex name index}
  364. \synopsis
  365. \begin{verbatim}
  366. void glp_create_v_index(glp_graph *G);
  367. \end{verbatim}
  368. \description
  369. The routine \verb|glp_create_v_index| creates the name index for the
  370. specified graph. The name index is an auxiliary data structure, which
  371. is intended to quickly (i.e. for logarithmic time) find vertices by
  372. their names.
  373. This routine can be called at any time. If the name index already
  374. exists, the routine does nothing.
  375. \subsection{glp\_find\_vertex --- find vertex by its name}
  376. \synopsis
  377. \begin{verbatim}
  378. int glp_find_vertex(glp_graph *G, const char *name);
  379. \end{verbatim}
  380. \returns
  381. The routine \verb|glp_find_vertex| returns the ordinal number of
  382. a vertex, which is assigned (by the routine \verb|glp_set_vertex_name|)
  383. the specified symbolic \verb|name|. If no such vertex exists, the
  384. routine returns 0.
  385. \subsection{glp\_delete\_v\_index --- delete vertex name index}
  386. \synopsis
  387. \begin{verbatim}
  388. void glp_delete_v_index(glp_graph *G);
  389. \end{verbatim}
  390. \description
  391. The routine \verb|glp_delete_v_index| deletes the name index previously
  392. created by the routine \verb|glp_create_v_index| and frees the memory
  393. allocated to this auxiliary data structure.
  394. This routine can be called at any time. If the name index does not
  395. exist, the routine does nothing.
  396. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  397. \newpage
  398. \section{Graph reading/writing routines}
  399. \subsection{glp\_read\_graph --- read graph from plain text file}
  400. \synopsis
  401. \begin{verbatim}
  402. int glp_read_graph(glp_graph *G, const char *fname);
  403. \end{verbatim}
  404. \description
  405. The routine \verb|glp_read_graph| reads a graph from a plain text file,
  406. whose name is specified by the parameter \verb|fname|. Note that before
  407. reading data the current content of the graph object is completely
  408. erased with the routine \verb|glp_erase_graph|.
  409. For the file format see description of the routine
  410. \verb|glp_write_graph|.
  411. \returns
  412. If the operation was successful, the routine returns zero. Otherwise
  413. it prints an error message and returns non-zero.
  414. \subsection{glp\_write\_graph --- write graph to plain text file}
  415. \synopsis
  416. \begin{verbatim}
  417. int glp_write_graph(glp_graph *G, const char *fname);
  418. \end{verbatim}
  419. \description
  420. The routine \verb|glp_write_graph| writes the graph to a plain text
  421. file, whose name is specified by the parameter \verb|fname|.
  422. \returns
  423. If the operation was successful, the routine returns zero. Otherwise
  424. it prints an error message and returns non-zero.
  425. \para{File format}
  426. The file created by the routine \verb|glp_write_graph| is a plain text
  427. file, which contains the following information:
  428. \begin{verbatim}
  429. nv na
  430. i[1] j[1]
  431. i[2] j[2]
  432. . . .
  433. i[na] j[na]
  434. \end{verbatim}
  435. \noindent
  436. where: \verb|nv| is the number of vertices (nodes); \verb|na| is the
  437. number of arcs; \verb|i[k]|, $k=1,\dots,na$, is the index of tail
  438. vertex of arc $k$; \verb|j[k]|, $k=1,\dots,na$, is the index of head
  439. vertex of arc $k$.
  440. \newpage
  441. \subsection{glp\_read\_ccdata --- read graph from text file in DIMACS
  442. clique/coloring\\format}
  443. \synopsis
  444. \begin{verbatim}
  445. int glp_read_ccdata(glp_graph *G, int v_wgt, const char *fname);
  446. \end{verbatim}
  447. \description
  448. The routine {\tt glp\_read\_ccdata} reads a graph from a text file in
  449. DIMACS clique/coloring format. (Though this format is originally
  450. designed to represent data for the minimal vertex coloring and maximal
  451. clique problems, it may be used to represent general undirected and
  452. directed graphs, because the routine allows reading self-loops and
  453. multiple edges/arcs keeping the order of vertices specified for each
  454. edge/arc of the graph.)
  455. The parameter {\tt G} specifies the graph object to be read in. Note
  456. that before reading data the current content of the graph object is
  457. completely erased with the routine {\tt glp\_erase\_graph}.
  458. The parameter {\tt v\_wgt} specifies an offset of the field of type
  459. {\tt double} in the vertex data block, to which the routine stores the
  460. vertex weight. If {\tt v\_wgt} $<0$, the vertex weights are not stored.
  461. The character string {\tt fname} specifies the name of a text file to
  462. be read in. (If the file name ends with the suffix `\verb|.gz|', the
  463. file is assumed to be compressed, in which case the routine
  464. decompresses it ``on the fly''.)
  465. \returns
  466. If the operation was successful, the routine returns zero. Otherwise,
  467. it prints an error message and returns non-zero.
  468. \para{DIMACS clique/coloring format\footnote{This material is
  469. based on the paper ``Clique and Coloring Problems Graph Format'', which
  470. is publically available at \url{http://dimacs.rutgers.edu/Challenges}.}}
  471. The DIMACS input file is a plain ASCII text file. It contains
  472. {\it lines} of several types described below. A line is terminated with
  473. an end-of-line character. Fields in each line are separated by at least
  474. one blank space. Each line begins with a one-character designator to
  475. identify the line type.
  476. Note that DIMACS requires all numerical quantities to be integers in
  477. the range $[-2^{31},2^{31}-1]$ while GLPK allows the quantities to be
  478. floating-point numbers.
  479. \para{Comment lines.} Comment lines give human-readable information
  480. about the file and are ignored by programs. Comment lines can appear
  481. anywhere in the file. Each comment line begins with a lower-case
  482. character \verb|c|.
  483. \begin{verbatim}
  484. c This is a comment line
  485. \end{verbatim}
  486. \para{Problem line.} There is one problem line per data file.
  487. The problem line must appear before any node or edge descriptor lines.
  488. It has the following format:
  489. \begin{verbatim}
  490. p edge NODES EDGES
  491. \end{verbatim}
  492. \noindent
  493. The lower-case letter \verb|p| signifies that this is a problem line.
  494. The four-character problem designator \verb|edge| identifies the file
  495. as containing data for the minimal vertex coloring or maximal clique
  496. problem. The \verb|NODES| field contains an integer value specifying
  497. the number of vertices in the graph. The \verb|EDGES| field contains an
  498. integer value specifying the number of edges (arcs) in the graph.
  499. \para{Vertex descriptors.} These lines give the weight assigned to
  500. a vertex of the graph. There is one vertex descriptor line for each
  501. vertex, with the following format. Vertices without a descriptor take
  502. on a default value of 1.
  503. \begin{verbatim}
  504. n ID VALUE
  505. \end{verbatim}
  506. \noindent
  507. The lower-case character \verb|n| signifies that this is a vertex
  508. descriptor line. The \verb|ID| field gives a vertex identification
  509. number, an integer between 1 and $n$, where $n$ is the number of
  510. vertices in the graph. The \verb|VALUE| field gives a vertex weight,
  511. which can either positive or negative (or zero).
  512. \para{Edge descriptors.} There is one edge descriptor line for each
  513. edge (arc) of the graph, each with the following format:
  514. \begin{verbatim}
  515. e I J
  516. \end{verbatim}
  517. \noindent
  518. The lower-case character \verb|e| signifies that this is an edge
  519. descriptor line. For an edge (arc) $(i,j)$ the fields \verb|I| and
  520. \verb|J| specify its endpoints.
  521. \para{Example.} The following example of an undirected graph:
  522. \bigskip
  523. \noindent\hfil
  524. \xymatrix %@C=32pt
  525. {&{v_1}\ar@{-}[ldd]\ar@{-}[dd]\ar@{-}[rd]\ar@{-}[rr]&&{v_2}\ar@{-}[ld]
  526. \ar@{-}[dd]\ar@{-}[rdd]&\\
  527. &&{v_7}\ar@{-}[ld]\ar@{-}[rd]&&\\
  528. {v_6}\ar@{-}[r]\ar@{-}[rdd]&{v_{10}}\ar@{-}[rr]\ar@{-}[rd]\ar@{-}[dd]&&
  529. {v_8}\ar@{-}[ld]\ar@{-}[dd]\ar@{-}[r]&{v_3}\ar@{-}[ldd]\\
  530. &&{v_9}\ar@{-}[ld]\ar@{-}[rd]&&\\
  531. &{v_5}\ar@{-}[rr]&&{v_4}&\\
  532. }
  533. \bigskip
  534. \noindent
  535. might be coded in DIMACS clique/coloring format as follows:
  536. \begin{footnotesize}
  537. \begin{verbatim}
  538. c sample.col
  539. c
  540. c This is an example of the vertex coloring problem data
  541. c in DIMACS format.
  542. c
  543. p edge 10 21
  544. c
  545. e 1 2
  546. e 1 6
  547. e 1 7
  548. e 1 10
  549. e 2 3
  550. e 2 7
  551. e 2 8
  552. e 3 4
  553. e 3 8
  554. e 4 5
  555. e 4 8
  556. e 4 9
  557. e 5 6
  558. e 5 9
  559. e 5 10
  560. e 6 10
  561. e 7 8
  562. e 7 10
  563. e 8 9
  564. e 8 10
  565. e 9 10
  566. c
  567. c eof
  568. \end{verbatim}
  569. \end{footnotesize}
  570. \subsection{glp\_write\_ccdata --- write graph to text file in DIMACS
  571. clique/coloring\\format}
  572. \synopsis
  573. \begin{verbatim}
  574. int glp_write_ccdata(glp_graph *G, int v_wgt, const char *fname);
  575. \end{verbatim}
  576. \description
  577. The routine {\tt glp\_write\_ccdata} writes the graph object specified
  578. by the parameter {\tt G} to a text file in DIMACS clique/coloring
  579. format. (Though this format is originally designed to represent data
  580. for the minimal vertex coloring and maximal clique problems, it may be
  581. used to represent general undirected and directed graphs, because the
  582. routine allows writing self-loops and multiple edges/arcs keeping the
  583. order of vertices specified for each edge/arc of the graph.)
  584. The parameter {\tt v\_wgt} specifies an offset of the field of type
  585. {\tt double} in the vertex data block, which contains the vertex
  586. weight. If {\tt v\_wgt} $<0$, it is assumed that the weight of each
  587. vertex is 1.
  588. The character string {\tt fname} specifies a name of the text file to
  589. be written out. (If the file name ends with suffix `\verb|.gz|', the
  590. file is assumed to be compressed, in which case the routine performs
  591. automatic compression on writing it.)
  592. \returns
  593. If the operation was successful, the routine returns zero. Otherwise,
  594. it prints an error message and returns non-zero.
  595. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  596. \newpage
  597. \section{Graph analysis routines}
  598. \subsection{glp\_weak\_comp --- find all weakly connected components of
  599. graph}
  600. \synopsis
  601. \begin{verbatim}
  602. int glp_weak_comp(glp_graph *G, int v_num);
  603. \end{verbatim}
  604. \description
  605. The routine \verb|glp_weak_comp| finds all weakly connected components
  606. of the specified graph.
  607. The parameter \verb|v_num| specifies an offset of the field of type
  608. \verb|int| in the vertex data block, to which the routine stores the
  609. number of a weakly connected component containing that vertex. If
  610. \verb|v_num| $<0$, no component numbers are stored.
  611. The components are numbered in arbitrary order from 1 to \verb|nc|,
  612. where \verb|nc| is the total number of components found,
  613. $0\leq$ \verb|nc| $\leq|V|$.
  614. \returns
  615. The routine returns \verb|nc|, the total number of components found.
  616. \subsection{glp\_strong\_comp --- find all strongly connected
  617. components of graph}
  618. \synopsis
  619. \begin{verbatim}
  620. int glp_strong_comp(glp_graph *G, int v_num);
  621. \end{verbatim}
  622. \description
  623. The routine \verb|glp_strong_comp| finds all strongly connected
  624. components of the specified graph.
  625. The parameter \verb|v_num| specifies an offset of the field of type
  626. \verb|int| in the vertex data block, to which the routine stores the
  627. number of a strongly connected component containing that vertex. If
  628. \verb|v_num| $<0$, no component numbers are stored.
  629. The components are numbered in arbitrary order from 1 to \verb|nc|,
  630. where \verb|nc| is the total number of components found,
  631. $0\leq$ \verb|nc| $\leq|V|$. However, the component numbering has the
  632. property that for every arc $(i\rightarrow j)$ in the graph the
  633. condition $num(i)\geq num(j)$ holds.
  634. \returns
  635. The routine returns \verb|nc|, the total number of components found.
  636. \para{References}
  637. I.~S.~Duff, J.~K.~Reid, Algorithm 529: Permutations to block triangular
  638. form, ACM Trans. on Math. Softw. 4 (1978), 189-92.
  639. \newpage
  640. \para{Example}
  641. The following program reads a graph from a plain text file
  642. `\verb|graph.txt|' and finds all its strongly connected components.
  643. \begin{footnotesize}
  644. \begin{verbatim}
  645. #include <stddef.h>
  646. #include <stdio.h>
  647. #include <stdlib.h>
  648. #include <glpk.h>
  649. typedef struct { int num; } v_data;
  650. #define vertex(v) ((v_data *)((v)->data))
  651. int main(void)
  652. { glp_graph *G;
  653. int i, nc;
  654. G = glp_create_graph(sizeof(v_data), 0);
  655. glp_read_graph(G, "graph.txt");
  656. nc = glp_strong_comp(G, offsetof(v_data, num));
  657. printf("nc = %d\n", nc);
  658. for (i = 1; i <= G->nv; i++)
  659. printf("num[%d] = %d\n", i, vertex(G->v[i])->num);
  660. glp_delete_graph(G);
  661. return 0;
  662. }
  663. \end{verbatim}
  664. \end{footnotesize}
  665. \noindent
  666. If the file `\verb|graph.txt|' contains the following graph:
  667. \medskip
  668. \noindent\hfil
  669. \xymatrix
  670. {1\ar[r]&2\ar[r]&3\ar[r]\ar[dd]&4\ar[dd]\\
  671. 5\ar[u]&6\ar[l]\\
  672. 7\ar[u]&&8\ar[lu]\ar[ll]\ar[r]&9\ar[r]&10\ar[r]\ar[d]&11\ar[d]\\
  673. 12\ar[u]\ar[rru]\ar@/_/[rr]&&13\ar[ll]\ar[u]\ar[rr]&&14\ar[lu]&15\ar[l]
  674. \\
  675. }
  676. \medskip\medskip
  677. \noindent
  678. the program output may look like follows:
  679. \begin{footnotesize}
  680. \begin{verbatim}
  681. Reading graph from `graph.txt'...
  682. Graph has 15 vertices and 30 arcs
  683. 31 lines were read
  684. nc = 4
  685. num[1] = 3
  686. num[2] = 3
  687. num[3] = 3
  688. num[4] = 2
  689. num[5] = 3
  690. num[6] = 3
  691. num[7] = 3
  692. num[8] = 3
  693. num[9] = 1
  694. num[10] = 1
  695. num[11] = 1
  696. num[12] = 4
  697. num[13] = 4
  698. num[14] = 1
  699. num[15] = 1
  700. \end{verbatim}
  701. \end{footnotesize}
  702. \subsection{glp\_top\_sort --- topological sorting of acyclic digraph}
  703. \synopsis
  704. \begin{verbatim}
  705. int glp_top_sort(glp_graph *G, int v_num);
  706. \end{verbatim}
  707. \description
  708. The routine \verb|glp_top_sort| performs topological sorting of
  709. vertices of the specified acyclic digraph.
  710. The parameter \verb|v_num| specifies an offset of the field of type
  711. \verb|int| in the vertex data block, to which the routine stores the
  712. vertex number assigned. If \verb|v_num| $<0$, vertex numbers are not
  713. stored.
  714. The vertices are numbered from 1 to $n$, where $n$ is the total number
  715. of vertices in the graph. The vertex numbering has the property that
  716. for every arc $(i\rightarrow j)$ in the graph the condition
  717. $num(i)<num(j)$ holds. Special case $num(i)=0$ means that vertex $i$ is
  718. not assigned a number, because the graph is {\it not} acyclic.
  719. \returns
  720. If the graph is acyclic and therefore all the vertices have been
  721. assigned numbers, the routine \verb|glp_top_sort| returns zero.
  722. Otherwise, if the graph is not acyclic, the routine returns the number
  723. of vertices which have not been numbered, i.e. for which $num(i)=0$.
  724. \para{Example}
  725. The following program reads a digraph from a plain text file
  726. `\verb|graph.txt|' and performs topological sorting of its vertices.
  727. \begin{footnotesize}
  728. \begin{verbatim}
  729. #include <stddef.h>
  730. #include <stdio.h>
  731. #include <stdlib.h>
  732. #include <glpk.h>
  733. typedef struct { int num; } v_data;
  734. #define vertex(v) ((v_data *)((v)->data))
  735. int main(void)
  736. { glp_graph *G;
  737. int i, cnt;
  738. G = glp_create_graph(sizeof(v_data), 0);
  739. glp_read_graph(G, "graph.txt");
  740. cnt = glp_top_sort(G, offsetof(v_data, num));
  741. printf("cnt = %d\n", cnt);
  742. for (i = 1; i <= G->nv; i++)
  743. printf("num[%d] = %d\n", i, vertex(G->v[i])->num);
  744. glp_delete_graph(G);
  745. return 0;
  746. }
  747. \end{verbatim}
  748. \end{footnotesize}
  749. \noindent
  750. If the file `\verb|graph.txt|' contains the following graph:
  751. \medskip
  752. \noindent\hfil
  753. \xymatrix @=20pt
  754. {
  755. 1\ar[rrr]&&&2\ar[r]\ar[rddd]&3\ar[rd]&&&&\\
  756. &&&4\ar[ru]&&5\ar[r]&6\ar[rd]\ar[dd]&&\\
  757. 7\ar[r]&8\ar[r]&9\ar[ruu]\ar[ru]\ar[r]\ar[rd]&10\ar[rr]\ar[rru]&&
  758. 11\ar[ru]&&12\ar[r]&13\\
  759. &&&14\ar[r]&15\ar[rrru]\ar[rr]&&16\ar[rru]\ar[rr]&&17\\
  760. }
  761. \medskip\medskip
  762. \noindent
  763. the program output may look like follows:
  764. \begin{footnotesize}
  765. \begin{verbatim}
  766. Reading graph from `graph.txt'...
  767. Graph has 17 vertices and 23 arcs
  768. 24 lines were read
  769. cnt = 0
  770. num[1] = 8
  771. num[2] = 9
  772. num[3] = 10
  773. num[4] = 4
  774. num[5] = 11
  775. num[6] = 12
  776. num[7] = 1
  777. num[8] = 2
  778. num[9] = 3
  779. num[10] = 5
  780. num[11] = 6
  781. num[12] = 14
  782. num[13] = 16
  783. num[14] = 7
  784. num[15] = 13
  785. num[16] = 15
  786. num[17] = 17
  787. \end{verbatim}
  788. \end{footnotesize}
  789. \noindent
  790. The output corresponds to the following vertex numbering:
  791. \medskip
  792. \noindent\hfil
  793. \xymatrix @=20pt
  794. {
  795. 8\ar[rrr]&&&9\ar[r]\ar[rddd]&10\ar[rd]&&&&\\
  796. &&&4\ar[ru]&&11\ar[r]&12\ar[rd]\ar[dd]&&\\
  797. 1\ar[r]&2\ar[r]&3\ar[ruu]\ar[ru]\ar[r]\ar[rd]&5\ar[rr]\ar[rru]&&
  798. 6\ar[ru]&&14\ar[r]&16\\
  799. &&&7\ar[r]&13\ar[rrru]\ar[rr]&&15\ar[rru]\ar[rr]&&17\\
  800. }
  801. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  802. \chapter{Network optimization API routines}
  803. \section{Minimum cost flow problem}
  804. \subsection{Background}
  805. The {\it minimum cost flow problem} (MCFP) is stated as follows. Let
  806. there be given a directed graph (flow network) $G=(V,A)$, where $V$ is
  807. a set of vertices (nodes), and $A\subseteq V\times V$ is a set of arcs.
  808. Let for each node $i\in V$ there be given a quantity $b_i$ having the
  809. following meaning:
  810. if $b_i>0$, then $|b_i|$ is a {\it supply} at node $i$, which shows
  811. how many flow units are {\it generated} at node $i$ (or, equivalently,
  812. entering the network through node $i$ from outside);
  813. if $b_i<0$, then $|b_i|$ is a {\it demand} at node $i$, which shows how
  814. many flow units are {\it lost} at node $i$ (or, equivalently, leaving
  815. the network through node $i$ to outside);
  816. if $b_i=0$, then $i$ is a {\it transshipment} node, at which the flow
  817. is conserved, i.e. neither generated nor lost.
  818. Let also for each arc $a=(i,j)\in A$ there be given the following three
  819. quantities:
  820. $l_{ij}$, a (non-negative) lower bound to the flow through arc $(i,j)$;
  821. $u_{ij}$, an upper bound to the flow through arc $(i,j)$, which is the
  822. {\it arc capacity};
  823. $c_{ij}$, a per-unit cost of the flow through arc $(i,j)$.
  824. The problem is to find flows $x_{ij}$ through every arc of the network,
  825. which satisfy the specified bounds and the conservation constraints at
  826. all nodes, and minimize the total flow cost. Here the conservation
  827. constraint at a node means that the total flow entering this node
  828. through its incoming arcs plus the supply at this node must be equal to
  829. the total flow leaving this node through its outgoing arcs plus the
  830. demand at this node.
  831. An example of the minimum cost flow problem is shown on Fig.~1.
  832. \newpage
  833. \noindent\hfil
  834. \xymatrix @C=48pt
  835. {_{20}\ar@{~>}[d]&
  836. v_2\ar[r]|{_{0,10,\$2}}\ar[dd]|{_{0,9,\$3}}&
  837. v_3\ar[dd]|{_{2,12,\$1}}\ar[r]|{_{0,18,\$0}}&
  838. v_8\ar[rd]|{_{0,20,\$9}}&\\
  839. v_1\ar[ru]|{_{0,14,\$0}}\ar[rd]|{_{0,23,\$0}}&&&
  840. v_6\ar[d]|{_{0,7,\$0}}\ar[u]|{_{4,8,\$0}}&
  841. v_9\ar@{~>}[d]\\
  842. &v_4\ar[r]|{_{0,26,\$0}}&
  843. v_5\ar[luu]|{_{0,11,\$1}}\ar[ru]|{_{0,25,\$5}}\ar[r]|{_{0,4,\$7}}&
  844. v_7\ar[ru]|{_{0,15,\$3}}&_{20}\\
  845. }
  846. \noindent\hfil
  847. \begin{tabular}{ccc}
  848. \xymatrix @C=48pt{v_i\ar[r]|{\ l,u,\$c\ }&v_j\\}&
  849. \xymatrix{\hbox{\footnotesize supply}\ar@{~>}[r]&v_i\\}&
  850. \xymatrix{v_i\ar@{~>}[r]&\hbox{\footnotesize demand}\\}\\
  851. \end{tabular}
  852. \noindent\hfil
  853. Fig.~1. An example of the minimum cost flow problem.
  854. \medskip
  855. The minimum cost flow problem can be naturally formulated as the
  856. following LP problem:
  857. \noindent
  858. \hspace{1in}minimize
  859. $$z=\sum_{(i,j)\in A}c_{ij}x_{ij}\eqno(1)$$
  860. \hspace{1in}subject to
  861. $$\sum_{(i,j)\in A}x_{ij}-\sum_{(j,i)\in A}x_{ji}=b_i\ \ \ \hbox
  862. {for all}\ i\in V\eqno(2)$$
  863. $$l_{ij}\leq x_{ij}\leq u_{ij}\ \ \ \hbox{for all}\ (i,j)\in A
  864. \eqno(3)$$
  865. \subsection{glp\_read\_mincost --- read minimum cost flow problem data
  866. in DIMACS\\format}
  867. \synopsis
  868. \begin{verbatim}
  869. int glp_read_mincost(glp_graph *G, int v_rhs, int a_low, int a_cap,
  870. int a_cost, const char *fname);
  871. \end{verbatim}
  872. \description
  873. The routine \verb|glp_read_mincost| reads the minimum cost flow problem
  874. data from a text file in DIMACS format.
  875. The parameter \verb|G| specifies the graph object, to which the problem
  876. data have to be stored. Note that before reading data the current
  877. content of the graph object is completely erased with the routine
  878. \verb|glp_erase_graph|.
  879. The parameter \verb|v_rhs| specifies an offset of the field of type
  880. \verb|double| in the vertex data block, to which the routine stores
  881. $b_i$, the supply/demand value. If \verb|v_rhs| $<0$, the value is not
  882. stored.
  883. The parameter \verb|a_low| specifies an offset of the field of type
  884. \verb|double| in the arc data block, to which the routine stores
  885. $l_{ij}$, the lower bound to the arc flow. If \verb|a_low| $<0$, the
  886. lower bound is not stored.
  887. The parameter \verb|a_cap| specifies an offset of the field of type
  888. \verb|double| in the arc data block, to which the routine stores
  889. $u_{ij}$, the upper bound to the arc flow (the arc capacity). If
  890. \verb|a_cap| $<0$, the upper bound is not stored.
  891. The parameter \verb|a_cost| specifies an offset of the field of type
  892. \verb|double| in the arc data block, to which the routine stores
  893. $c_{ij}$, the per-unit cost of the arc flow. If \verb|a_cost| $<0$, the
  894. cost is not stored.
  895. The character string \verb|fname| specifies the name of a text file to
  896. be read in. (If the file name name ends with the suffix `\verb|.gz|',
  897. the file is assumed to be compressed, in which case the routine
  898. decompresses it ``on the fly''.)
  899. \returns
  900. If the operation was successful, the routine returns zero. Otherwise,
  901. it prints an error message and returns non-zero.
  902. \para{Example}
  903. \begin{footnotesize}
  904. \begin{verbatim}
  905. typedef struct
  906. { /* vertex data block */
  907. ...
  908. double rhs;
  909. ...
  910. } v_data;
  911. typedef struct
  912. { /* arc data block */
  913. ...
  914. double low, cap, cost;
  915. ...
  916. } a_data;
  917. int main(void)
  918. { glp_graph *G;
  919. int ret;
  920. G = glp_create_graph(sizeof(v_data), sizeof(a_data));
  921. ret = glp_read_mincost(G, offsetof(v_data, rhs),
  922. offsetof(a_data, low), offsetof(a_data, cap),
  923. offsetof(a_data, cost), "sample.min");
  924. if (ret != 0) goto ...
  925. ...
  926. }
  927. \end{verbatim}
  928. \end{footnotesize}
  929. \para{DIMACS minimum cost flow problem format\footnote{This
  930. material is based on the paper ``The First DIMACS International
  931. Algorithm Implementation Challenge: Problem Definitions and
  932. Specifications'', which is publically available at
  933. \url{http://dimacs.rutgers.edu/Challenges}.}}
  934. \label{subsecmincost}
  935. The DIMACS input file is a plain ASCII text file. It contains
  936. {\it lines} of several types described below. A line is terminated with
  937. an end-of-line character. Fields in each line are separated by at least
  938. one blank space. Each line begins with a one-character designator to
  939. identify the line type.
  940. Note that DIMACS requires all numerical quantities to be integers in
  941. the range $[-2^{31},\ 2^{31}-1]$ while GLPK allows the quantities to be
  942. floating-point numbers.
  943. \para{Comment lines.} Comment lines give human-readable information
  944. about the file and are ignored by programs. Comment lines can appear
  945. anywhere in the file. Each comment line begins with a lower-case
  946. character \verb|c|.
  947. \begin{verbatim}
  948. c This is a comment line
  949. \end{verbatim}
  950. \newpage
  951. \para{Problem line.} There is one problem line per data file. The
  952. problem line must appear before any node or arc descriptor lines. It
  953. has the following format:
  954. \begin{verbatim}
  955. p min NODES ARCS
  956. \end{verbatim}
  957. \noindent
  958. The lower-case character \verb|p| signifies that this is a problem line.
  959. The three-character problem designator \verb|min| identifies the file as
  960. containing specification information for the minimum cost flow problem.
  961. The \verb|NODES| field contains an integer value specifying the number
  962. of nodes in the network. The \verb|ARCS| field contains an integer value
  963. specifying the number of arcs in the network.
  964. \para{Node descriptors.} All node descriptor lines must appear before
  965. all arc descriptor lines. The node descriptor lines describe supply and
  966. demand nodes, but not transshipment nodes. That is, only nodes with
  967. non-zero node supply/demand values appear. There is one node descriptor
  968. line for each such node, with the following format:
  969. \begin{verbatim}
  970. n ID FLOW
  971. \end{verbatim}
  972. \noindent
  973. The lower-case character \verb|n| signifies that this is a node
  974. descriptor line. The \verb|ID| field gives a node identification
  975. number, an integer between 1 and \verb|NODES|. The \verb|FLOW| field
  976. gives the amount of supply (if positive) or demand (if negative) at
  977. node \verb|ID|.
  978. \para{Arc descriptors.} There is one arc descriptor line for each arc
  979. in the network. Arc descriptor lines are of the following format:
  980. \begin{verbatim}
  981. a SRC DST LOW CAP COST
  982. \end{verbatim}
  983. \noindent
  984. The lower-case character \verb|a| signifies that this is an arc
  985. descriptor line. For a directed arc $(i,j)$ the \verb|SRC| field gives
  986. the identification number $i$ for the tail endpoint, and the \verb|DST|
  987. field gives the identification number $j$ for the head endpoint.
  988. Identification numbers are integers between 1 and \verb|NODES|. The
  989. \verb|LOW| field specifies the minimum amount of flow that can be sent
  990. along arc $(i,j)$, and the \verb|CAP| field gives the maximum amount of
  991. flow that can be sent along arc $(i,j)$ in a feasible flow. The
  992. \verb|COST| field contains the per-unit cost of flow sent along arc
  993. $(i,j)$.
  994. \para{Example.} Below here is an example of the data file in DIMACS
  995. format corresponding to the minimum cost flow problem shown on Fig~1.
  996. \begin{footnotesize}
  997. \begin{verbatim}
  998. c sample.min
  999. c
  1000. c This is an example of the minimum cost flow problem data
  1001. c in DIMACS format.
  1002. c
  1003. p min 9 14
  1004. c
  1005. n 1 20
  1006. n 9 -20
  1007. c
  1008. a 1 2 0 14 0
  1009. a 1 4 0 23 0
  1010. a 2 3 0 10 2
  1011. a 2 4 0 9 3
  1012. a 3 5 2 12 1
  1013. a 3 8 0 18 0
  1014. a 4 5 0 26 0
  1015. a 5 2 0 11 1
  1016. a 5 6 0 25 5
  1017. a 5 7 0 4 7
  1018. a 6 7 0 7 0
  1019. a 6 8 4 8 0
  1020. a 7 9 0 15 3
  1021. a 8 9 0 20 9
  1022. c
  1023. c eof
  1024. \end{verbatim}
  1025. \end{footnotesize}
  1026. \subsection{glp\_write\_mincost --- write minimum cost flow problem
  1027. data in DIMACS\\format}
  1028. \synopsis
  1029. \begin{verbatim}
  1030. int glp_write_mincost(glp_graph *G, int v_rhs, int a_low, int a_cap,
  1031. int a_cost, const char *fname);
  1032. \end{verbatim}
  1033. \description
  1034. The routine \verb|glp_write_mincost| writes the minimum cost flow
  1035. problem data to a text file in DIMACS format.
  1036. The parameter \verb|G| is the graph (network) program object, which
  1037. specifies the minimum cost flow problem instance.
  1038. The parameter \verb|v_rhs| specifies an offset of the field of type
  1039. \verb|double| in the vertex data block, which contains $b_i$, the
  1040. supply/demand value. If \verb|v_rhs| $<0$, it is assumed that $b_i=0$
  1041. for all nodes.
  1042. The parameter \verb|a_low| specifies an offset of the field of type
  1043. \verb|double| in the arc data block, which contains $l_{ij}$, the lower
  1044. bound to the arc flow. If \verb|a_low| $<0$, it is assumed that
  1045. $l_{ij}=0$ for all arcs.
  1046. The parameter \verb|a_cap| specifies an offset of the field of type
  1047. \verb|double| in the arc data block, which contains $u_{ij}$, the upper
  1048. bound to the arc flow (the arc capacity). If the upper bound is
  1049. specified as \verb|DBL_MAX|, it is assumed that $u_{ij}=\infty$, i.e.
  1050. the arc is uncapacitated. If \verb|a_cap| $<0$, it is assumed that
  1051. $u_{ij}=1$ for all arcs.
  1052. The parameter \verb|a_cost| specifies an offset of the field of type
  1053. \verb|double| in the arc data block, which contains $c_{ij}$, the
  1054. per-unit cost of the arc flow. If \verb|a_cost| $<0$, it is assumed
  1055. that $c_{ij}=0$ for all arcs.
  1056. The character string \verb|fname| specifies a name of the text file to
  1057. be written out. (If the file name ends with suffix `\verb|.gz|', the
  1058. file is assumed to be compressed, in which case the routine performs
  1059. automatic compression on writing it.)
  1060. \returns
  1061. If the operation was successful, the routine returns zero. Otherwise,
  1062. it prints an error message and returns non-zero.
  1063. \newpage
  1064. \subsection{glp\_mincost\_lp --- convert minimum cost flow problem
  1065. to LP}
  1066. \synopsis
  1067. \begin{verbatim}
  1068. void glp_mincost_lp(glp_prob *P, glp_graph *G, int names, int v_rhs,
  1069. int a_low, int a_cap, int a_cost);
  1070. \end{verbatim}
  1071. \description
  1072. The routine \verb|glp_mincost_lp| builds LP problem (1)---(3), which
  1073. corresponds to the specified minimum cost flow problem.
  1074. The parameter \verb|P| is the resultant LP problem object to be built.
  1075. Note that on entry its current content is erased with the routine
  1076. \verb|glp_erase_prob|.
  1077. The parameter \verb|G| is the graph (network) program object, which
  1078. specifies the minimum cost flow problem instance.
  1079. The parameter \verb|names| is a flag. If it is \verb|GLP_ON|, the
  1080. routine uses symbolic names of the graph object components to assign
  1081. symbolic names to the LP problem object components. If the flag is
  1082. \verb|GLP_OFF|, no symbolic names are assigned.
  1083. The parameter \verb|v_rhs| specifies an offset of the field of type
  1084. \verb|double| in the vertex data block, which contains $b_i$, the
  1085. supply/demand value. If \verb|v_rhs| $<0$, it is assumed that $b_i=0$
  1086. for all nodes.
  1087. The parameter \verb|a_low| specifies an offset of the field of type
  1088. \verb|double| in the arc data block, which contains $l_{ij}$, the lower
  1089. bound to the arc flow. If \verb|a_low| $<0$, it is assumed that
  1090. $l_{ij}=0$ for all arcs.
  1091. The parameter \verb|a_cap| specifies an offset of the field of type
  1092. \verb|double| in the arc data block, which contains $u_{ij}$, the upper
  1093. bound to the arc flow (the arc capacity). If the upper bound is
  1094. specified as \verb|DBL_MAX|, it is assumed that $u_{ij}=\infty$, i.e.
  1095. the arc is uncapacitated. If \verb|a_cap| $<0$, it is assumed that
  1096. $u_{ij}=1$ for all arcs.
  1097. The parameter \verb|a_cost| specifies an offset of the field of type
  1098. \verb|double| in the arc data block, which contains $c_{ij}$, the
  1099. per-unit cost of the arc flow. If \verb|a_cost| $<0$, it is assumed that
  1100. $c_{ij}=0$ for all arcs.
  1101. \para{Example}
  1102. The example program below reads the minimum cost problem instance in
  1103. DIMACS format from file `\verb|sample.min|', converts the instance to
  1104. LP, and then writes the resultant LP in CPLEX format to file
  1105. `\verb|mincost.lp|'.
  1106. \begin{footnotesize}
  1107. \begin{verbatim}
  1108. #include <stddef.h>
  1109. #include <glpk.h>
  1110. typedef struct { double rhs; } v_data;
  1111. typedef struct { double low, cap, cost; } a_data;
  1112. int main(void)
  1113. { glp_graph *G;
  1114. glp_prob *P;
  1115. G = glp_create_graph(sizeof(v_data), sizeof(a_data));
  1116. glp_read_mincost(G, offsetof(v_data, rhs),
  1117. offsetof(a_data, low), offsetof(a_data, cap),
  1118. offsetof(a_data, cost), "sample.min");
  1119. P = glp_create_prob();
  1120. glp_mincost_lp(P, G, GLP_ON, offsetof(v_data, rhs),
  1121. offsetof(a_data, low), offsetof(a_data, cap),
  1122. offsetof(a_data, cost));
  1123. glp_delete_graph(G);
  1124. glp_write_lp(P, NULL, "mincost.lp");
  1125. glp_delete_prob(P);
  1126. return 0;
  1127. }
  1128. \end{verbatim}
  1129. \end{footnotesize}
  1130. If `\verb|sample.min|' is the example data file from the subsection
  1131. describing \verb|glp_read_mincost|, file `\verb|mincost.lp|' may look
  1132. like follows:
  1133. \begin{footnotesize}
  1134. \begin{verbatim}
  1135. Minimize
  1136. obj: + 3 x(2,4) + 2 x(2,3) + x(3,5) + 7 x(5,7) + 5 x(5,6)
  1137. + x(5,2) + 3 x(7,9) + 9 x(8,9)
  1138. Subject To
  1139. r_1: + x(1,2) + x(1,4) = 20
  1140. r_2: - x(5,2) + x(2,3) + x(2,4) - x(1,2) = 0
  1141. r_3: + x(3,5) + x(3,8) - x(2,3) = 0
  1142. r_4: + x(4,5) - x(2,4) - x(1,4) = 0
  1143. r_5: + x(5,2) + x(5,6) + x(5,7) - x(4,5) - x(3,5) = 0
  1144. r_6: + x(6,7) + x(6,8) - x(5,6) = 0
  1145. r_7: + x(7,9) - x(6,7) - x(5,7) = 0
  1146. r_8: + x(8,9) - x(6,8) - x(3,8) = 0
  1147. r_9: - x(8,9) - x(7,9) = -20
  1148. Bounds
  1149. 0 <= x(1,4) <= 23
  1150. 0 <= x(1,2) <= 14
  1151. 0 <= x(2,4) <= 9
  1152. 0 <= x(2,3) <= 10
  1153. 0 <= x(3,8) <= 18
  1154. 2 <= x(3,5) <= 12
  1155. 0 <= x(4,5) <= 26
  1156. 0 <= x(5,7) <= 4
  1157. 0 <= x(5,6) <= 25
  1158. 0 <= x(5,2) <= 11
  1159. 4 <= x(6,8) <= 8
  1160. 0 <= x(6,7) <= 7
  1161. 0 <= x(7,9) <= 15
  1162. 0 <= x(8,9) <= 20
  1163. End
  1164. \end{verbatim}
  1165. \end{footnotesize}
  1166. \newpage
  1167. \subsection{glp\_mincost\_okalg --- solve minimum cost flow problem
  1168. with out-of-kilter\\algorithm}
  1169. \synopsis
  1170. \begin{verbatim}
  1171. int glp_mincost_okalg(glp_graph *G, int v_rhs, int a_low, int a_cap,
  1172. int a_cost, double *sol, int a_x, int v_pi);
  1173. \end{verbatim}
  1174. \description
  1175. The routine \verb|glp_mincost_okalg| finds optimal solution to the
  1176. minimum cost flow problem with the out-of-kilter
  1177. algorithm.\footnote{GLPK implementation of the out-of-kilter algorithm
  1178. is based on the following book: L.~R.~Ford,~Jr., and D.~R.~Fulkerson,
  1179. ``Flows in Networks,'' The RAND Corp., Report R-375-PR (August 1962),
  1180. Chap. III ``Minimal Cost Flow Problems,'' pp.~113-26.} Note that this
  1181. routine requires all the problem data to be integer-valued.
  1182. The parameter \verb|G| is a graph (network) program object which
  1183. specifies the minimum cost flow problem instance to be solved.
  1184. The parameter \verb|v_rhs| specifies an offset of the field of type
  1185. \verb|double| in the vertex data block, which contains $b_i$, the
  1186. supply/demand value. This value must be integer in the range
  1187. [$-$\verb|INT_MAX|, $+$\verb|INT_MAX|]. If \verb|v_rhs| $<0$, it is
  1188. assumed that $b_i=0$ for all nodes.
  1189. The parameter \verb|a_low| specifies an offset of the field of type
  1190. \verb|double| in the arc data block, which contains $l_{ij}$, the lower
  1191. bound to the arc flow. This bound must be integer in the range
  1192. [$0$, \verb|INT_MAX|]. If \verb|a_low| $<0$, it is assumed that
  1193. $l_{ij}=0$ for all arcs.
  1194. The parameter \verb|a_cap| specifies an offset of the field of type
  1195. \verb|double| in the arc data block, which contains $u_{ij}$, the upper
  1196. bound to the arc flow (the arc capacity). This bound must be integer in
  1197. the range [$l_{ij}$, \verb|INT_MAX|]. If \verb|a_cap| $<0$, it is
  1198. assumed that $u_{ij}=1$ for all arcs.
  1199. The parameter \verb|a_cost| specifies an offset of the field of type
  1200. \verb|double| in the arc data block, which contains $c_{ij}$, the
  1201. per-unit cost of the arc flow. This value must be integer in the range
  1202. [$-$\verb|INT_MAX|, $+$\verb|INT_MAX|]. If \verb|a_cost| $<0$, it is
  1203. assumed that $c_{ij}=0$ for all arcs.
  1204. The parameter \verb|sol| specifies a location, to which the routine
  1205. stores the objective value (that is, the total cost) found. If
  1206. \verb|sol| is NULL, the objective value is not stored.
  1207. The parameter \verb|a_x| specifies an offset of the field of type
  1208. \verb|double| in the arc data block, to which the routine stores
  1209. $x_{ij}$, the arc flow found. If \verb|a_x| $<0$, the arc flow value is
  1210. not stored.
  1211. The parameter \verb|v_pi| specifies an offset of the field of type
  1212. \verb|double| in the vertex data block, to which the routine stores
  1213. $\pi_i$, the node potential, which is the Lagrange multiplier for the
  1214. corresponding flow conservation equality constraint (see (2) in
  1215. Subsection ``Background''). If necessary, the application program may
  1216. use the node potentials to compute $\lambda_{ij}$, reduced costs of the
  1217. arc flows $x_{ij}$, which are the Lagrange multipliers for the arc flow
  1218. bound constraints (see (3) ibid.), using the following formula:
  1219. $$\lambda_{ij}=c_{ij}-(\pi_i-\pi_j),$$
  1220. where $c_{ij}$ is the per-unit cost for arc $(i,j)$.
  1221. \newpage
  1222. Note that all solution components (the objective value, arc flows, and
  1223. node potentials) computed by the routine are always integer-valued.
  1224. \returns
  1225. \begin{retlist}
  1226. 0 & Optimal solution found.\\
  1227. \verb|GLP_ENOPFS| & No (primal) feasible solution exists.\\
  1228. \verb|GLP_EDATA| & Unable to start the search, because some problem
  1229. data are either not integer-valued or out of range. This code is also
  1230. returned if the total supply, which is the sum of $b_i$ over all source
  1231. nodes (nodes with $b_i>0$), exceeds \verb|INT_MAX|.\\
  1232. \verb|GLP_ERANGE| & The search was prematurely terminated because of
  1233. integer overflow.\\
  1234. \verb|GLP_EFAIL| & An error has been detected in the program logic.
  1235. (If this code is returned for your problem instance, please report to
  1236. \verb|<bug-glpk@gnu.org>|.)\\
  1237. \end{retlist}
  1238. \para{Comments}
  1239. By design the out-of-kilter algorithm is applicable only to networks,
  1240. where $b_i=0$ for {\it all} nodes, i.e. actually this algorithm finds a
  1241. minimal cost {\it circulation}. Due to this requirement the routine
  1242. \verb|glp_mincost_okalg| converts the original network to a network
  1243. suitable for the out-of-kilter algorithm in the following
  1244. way:\footnote{The conversion is performed internally and does not change
  1245. the original network program object passed to the routine.}
  1246. 1) it adds two auxiliary nodes $s$ and $t$;
  1247. 2) for each original node $i$ with $b_i>0$ the routine adds auxiliary
  1248. supply arc $(s\rightarrow i)$, flow $x_{si}$ through which is costless
  1249. ($c_{si}=0$) and fixed to $+b_i$ ($l_{si}=u_{si}=+b_i$);
  1250. 3) for each original node $i$ with $b_i<0$ the routine adds auxiliary
  1251. demand arc $(i\rightarrow t)$, flow $x_{it}$ through which is costless
  1252. ($c_{it}=0$) and fixed to $-b_i$ ($l_{it}=u_{it}=-b_i$);
  1253. 4) finally, the routine adds auxiliary feedback arc $(t\rightarrow s)$,
  1254. flow $x_{ts}$ through which is also costless ($c_{ts}=0$) and fixed to
  1255. $F$ ($l_{ts}=u_{ts}=F$), where $\displaystyle F=\sum_{b_i>0}b_i$ is the
  1256. total supply.
  1257. \para{Example}
  1258. The example program below reads the minimum cost problem instance in
  1259. DIMACS format from file `\verb|sample.min|', solves it by using the
  1260. routine \verb|glp_mincost_okalg|, and writes the solution found on the
  1261. standard output.
  1262. \begin{footnotesize}
  1263. \begin{verbatim}
  1264. #include <stddef.h>
  1265. #include <stdio.h>
  1266. #include <stdlib.h>
  1267. #include <glpk.h>
  1268. typedef struct { double rhs, pi; } v_data;
  1269. typedef struct { double low, cap, cost, x; } a_data;
  1270. #define node(v) ((v_data *)((v)->data))
  1271. #define arc(a) ((a_data *)((a)->data))
  1272. int main(void)
  1273. { glp_graph *G;
  1274. glp_vertex *v, *w;
  1275. glp_arc *a;
  1276. int i, ret;
  1277. double sol;
  1278. G = glp_create_graph(sizeof(v_data), sizeof(a_data));
  1279. glp_read_mincost(G, offsetof(v_data, rhs),
  1280. offsetof(a_data, low), offsetof(a_data, cap),
  1281. offsetof(a_data, cost), "sample.min");
  1282. ret = glp_mincost_okalg(G, offsetof(v_data, rhs),
  1283. offsetof(a_data, low), offsetof(a_data, cap),
  1284. offsetof(a_data, cost), &sol, offsetof(a_data, x),
  1285. offsetof(v_data, pi));
  1286. printf("ret = %d; sol = %5g\n", ret, sol);
  1287. for (i = 1; i <= G->nv; i++)
  1288. { v = G->v[i];
  1289. printf("node %d: pi = %5g\n", i, node(v)->pi);
  1290. for (a = v->out; a != NULL; a = a->t_next)
  1291. { w = a->head;
  1292. printf("arc %d->%d: x = %5g; lambda = %5g\n",
  1293. v->i, w->i, arc(a)->x,
  1294. arc(a)->cost - (node(v)->pi - node(w)->pi));
  1295. }
  1296. }
  1297. glp_delete_graph(G);
  1298. return 0;
  1299. }
  1300. \end{verbatim}
  1301. \end{footnotesize}
  1302. If `\verb|sample.min|' is the example data file from the subsection
  1303. describing \verb|glp_read_mincost|, the output may look like follows:
  1304. \begin{footnotesize}
  1305. \begin{verbatim}
  1306. Reading min-cost flow problem data from `sample.min'...
  1307. Flow network has 9 nodes and 14 arcs
  1308. 24 lines were read
  1309. ret = 0; sol = 213
  1310. node 1: pi = -12
  1311. arc 1->4: x = 13; lambda = 0
  1312. arc 1->2: x = 7; lambda = 0
  1313. node 2: pi = -12
  1314. arc 2->4: x = 0; lambda = 3
  1315. arc 2->3: x = 7; lambda = 0
  1316. node 3: pi = -14
  1317. arc 3->8: x = 5; lambda = 0
  1318. arc 3->5: x = 2; lambda = 3
  1319. node 4: pi = -12
  1320. arc 4->5: x = 13; lambda = 0
  1321. node 5: pi = -12
  1322. arc 5->7: x = 4; lambda = -1
  1323. arc 5->6: x = 11; lambda = 0
  1324. arc 5->2: x = 0; lambda = 1
  1325. node 6: pi = -17
  1326. arc 6->8: x = 4; lambda = 3
  1327. arc 6->7: x = 7; lambda = -3
  1328. node 7: pi = -20
  1329. arc 7->9: x = 11; lambda = 0
  1330. node 8: pi = -14
  1331. arc 8->9: x = 9; lambda = 0
  1332. node 9: pi = -23
  1333. \end{verbatim}
  1334. \end{footnotesize}
  1335. \subsection{glp\_mincost\_relax4 --- solve minimum cost flow problem
  1336. with relaxation\\method of Bertsekas and Tseng (RELAX-IV)}
  1337. \synopsis
  1338. \begin{verbatim}
  1339. int glp_mincost_relax4(glp_graph *G, int v_rhs, int a_low, int a_cap,
  1340. int a_cost, int crash, double *sol, int a_x, int a_rc);
  1341. \end{verbatim}
  1342. \description
  1343. The routine \verb|glp_mincost_relax4| finds optimal solution to the
  1344. minimum cost flow problem with the relaxation method RELAX-IV developed
  1345. by Bertsekas and Tseng.\footnote{GLPK implementation of this method is
  1346. based on a C translation of the original Fortran code {\tt RELAX4}
  1347. written by Dimitri P. Bertsekas and Paul Tseng, with a contribution by
  1348. Jonathan Eckstein in the phase II initialization.} This method is one
  1349. of most efficient methods for network optimization.
  1350. Note that this routine requires all the problem data to be
  1351. integer-valued.
  1352. The parameter \verb|G| is a graph (network) program object which
  1353. specifies the minimum cost flow problem instance to be solved.
  1354. The parameter \verb|v_rhs| specifies an offset of the field of type
  1355. \verb|double| in the vertex data block, which contains $b_i$, the
  1356. supply/demand value. This value must be integer in the range
  1357. [$-$\verb|INT_MAX|/4, $+$\verb|INT_MAX|/4]. If \verb|v_rhs| $<0$, it is
  1358. assumed that $b_i=0$ for all nodes.
  1359. The parameter \verb|a_low| specifies an offset of the field of type
  1360. \verb|double| in the arc data block, which contains $l_{ij}$, the lower
  1361. bound to the arc flow. This bound must be integer in the range
  1362. {\linebreak} [$0$, \verb|INT_MAX|/4]. If \verb|a_low| $<0$, it is
  1363. assumed that $l_{ij}=0$ for all arcs.
  1364. The parameter \verb|a_cap| specifies an offset of the field of type
  1365. \verb|double| in the arc data block, which contains $u_{ij}$, the upper
  1366. bound to the arc flow (the arc capacity). This bound must be integer in
  1367. the range [$l_{ij}$, \verb|INT_MAX|/4]. If \verb|a_cap| $<0$, it is
  1368. assumed that $u_{ij}=1$ for all arcs.
  1369. The parameter \verb|a_cost| specifies an offset of the field of type
  1370. \verb|double| in the arc data block, which contains $c_{ij}$, the
  1371. per-unit cost of the arc flow. This value must be integer in the range
  1372. [$-$\verb|INT_MAX|/4, $+$\verb|INT_MAX|/4]. If \verb|a_cost| $<0$, it
  1373. is assumed that $c_{ij}=0$ for all arcs.
  1374. The parameter \verb|crash| is option specifying initialization method:
  1375. 0 --- default initialization is used;
  1376. 1 --- auction initialization is used.
  1377. \noindent
  1378. If \verb|crash| = 1, initialization is performed with a special crash
  1379. procedure based on an auction/shorest path method. This option is
  1380. recommended for difficult problems where the default initialization
  1381. results in long running times.
  1382. The parameter \verb|sol| specifies a location, to which the routine
  1383. stores the objective value (that is, the total cost) found. If
  1384. \verb|sol| is NULL, the objective value is not stored.
  1385. \newpage
  1386. The parameter \verb|a_x| specifies an offset of the field of type
  1387. \verb|double| in the arc data block, to which the routine stores
  1388. $x_{ij}$, the arc flow found. If \verb|a_x| $<0$, the arc flow value is
  1389. not stored.
  1390. The parameter \verb|a_rc| specifies an offset of the field of type
  1391. \verb|double| in the arc data block, to which the routine stores
  1392. the reduced cost for corresponding arc flow (see (3) in Subsection
  1393. ``Background''). If \verb|a_rc| $<0$, the reduced cost is not stored.
  1394. Note that all solution components (the objective value, arc flows, and
  1395. node potentials) computed by the routine are always integer-valued.
  1396. \returns
  1397. \begin{retlist}
  1398. 0 & Optimal solution found.\\
  1399. \verb|GLP_ENOPFS| & No (primal) feasible solution exists.\\
  1400. \verb|GLP_EDATA| & Unable to start the search, because some problem
  1401. data are either not integer-valued or out of range.\\
  1402. \verb|GLP_ERANGE| & Unable to start the search because of integer
  1403. overflow.\\
  1404. \end{retlist}
  1405. \para{Example}
  1406. The example program below reads the minimum cost problem instance in
  1407. DIMACS format from file `\verb|sample.min|', solves it by using the
  1408. routine \verb|glp_mincost_relax4|, and writes the solution found on the
  1409. standard output.
  1410. \begin{footnotesize}
  1411. \begin{verbatim}
  1412. #include <stddef.h>
  1413. #include <stdio.h>
  1414. #include <stdlib.h>
  1415. #include <glpk.h>
  1416. typedef struct { double rhs; } v_data;
  1417. typedef struct { double low, cap, cost, x, rc; } a_data;
  1418. #define node(v) ((v_data *)((v)->data))
  1419. #define arc(a) ((a_data *)((a)->data))
  1420. int main(void)
  1421. { glp_graph *G;
  1422. glp_vertex *v, *w;
  1423. glp_arc *a;
  1424. int i, ret;
  1425. double sol;
  1426. G = glp_create_graph(sizeof(v_data), sizeof(a_data));
  1427. glp_read_mincost(G, offsetof(v_data, rhs),
  1428. offsetof(a_data, low), offsetof(a_data, cap),
  1429. offsetof(a_data, cost), "sample.min");
  1430. ret = glp_mincost_relax4(G, offsetof(v_data, rhs),
  1431. offsetof(a_data, low), offsetof(a_data, cap),
  1432. offsetof(a_data, cost), 0, &sol, offsetof(a_data, x),
  1433. offsetof(a_data, rc));
  1434. printf("ret = %d; sol = %5g\n", ret, sol);
  1435. for (i = 1; i <= G->nv; i++)
  1436. { v = G->v[i];
  1437. for (a = v->out; a != NULL; a = a->t_next)
  1438. { w = a->head;
  1439. printf("arc %d->%d: x = %5g; rc = %5g\n",
  1440. v->i, w->i, arc(a)->x, arc(a)->rc);
  1441. }
  1442. }
  1443. glp_delete_graph(G);
  1444. return 0;
  1445. }
  1446. \end{verbatim}
  1447. \end{footnotesize}
  1448. If `\verb|sample.min|' is the example data file from the subsection
  1449. describing \verb|glp_read_mincost|, the output may look like follows:
  1450. \begin{footnotesize}
  1451. \begin{verbatim}
  1452. Reading min-cost flow problem data from `sample.min'...
  1453. Flow network has 9 nodes and 14 arcs
  1454. 24 lines were read
  1455. ret = 0; sol = 213
  1456. arc 1->4: x = 13; rc = 0
  1457. arc 1->2: x = 7; rc = 0
  1458. arc 2->4: x = 0; rc = 3
  1459. arc 2->3: x = 7; rc = 0
  1460. arc 3->8: x = 5; rc = 0
  1461. arc 3->5: x = 2; rc = 3
  1462. arc 4->5: x = 13; rc = 0
  1463. arc 5->7: x = 4; rc = -1
  1464. arc 5->6: x = 11; rc = 0
  1465. arc 5->2: x = 0; rc = 1
  1466. arc 6->8: x = 4; rc = 3
  1467. arc 6->7: x = 7; rc = -3
  1468. arc 7->9: x = 11; rc = 0
  1469. arc 8->9: x = 9; rc = 0
  1470. \end{verbatim}
  1471. \end{footnotesize}
  1472. \subsection{glp\_netgen --- Klingman's network problem generator}
  1473. \synopsis
  1474. \begin{verbatim}
  1475. int glp_netgen(glp_graph *G, int v_rhs, int a_cap, int a_cost,
  1476. const int parm[1+15]);
  1477. \end{verbatim}
  1478. \description
  1479. The routine \verb|glp_netgen| is a GLPK version of the network problem
  1480. generator developed by Dr.~Darwin~Klingman.\footnote{D.~Klingman,
  1481. A.~Napier, and J.~Stutz. NETGEN: A program for generating large scale
  1482. capacitated assignment, transportation, and minimum cost flow networks.
  1483. Management Science 20 (1974), 814-20.} It can create capacitated and
  1484. uncapacitated minimum cost flow (or transshipment), transportation, and
  1485. assignment problems.
  1486. The parameter \verb|G| specifies the graph object, to which the
  1487. generated problem data have to be stored. Note that on entry the graph
  1488. object is erased with the routine \verb|glp_erase_graph|.
  1489. The parameter \verb|v_rhs| specifies an offset of the field of type
  1490. \verb|double| in the vertex data block, to which the routine stores the
  1491. supply or demand value. If \verb|v_rhs| $<0$, the value is not stored.
  1492. The parameter \verb|a_cap| specifies an offset of the field of type
  1493. \verb|double| in the arc data block, to which the routine stores the
  1494. arc capacity. If \verb|a_cap| $<0$, the capacity is not stored.
  1495. \newpage
  1496. The parameter \verb|a_cost| specifies an offset of the field of type
  1497. \verb|double| in the arc data block, to which the routine stores the
  1498. per-unit cost if the arc flow. If \verb|a_cost| $<0$, the cost is not
  1499. stored.
  1500. The array \verb|parm| contains description of the network to be
  1501. generated:
  1502. \begin{tabular}{@{}lll@{}}
  1503. \verb|parm[0] |& &not used\\
  1504. \verb|parm[1] |&\verb|iseed |&8-digit positive random number seed\\
  1505. \verb|parm[2] |&\verb|nprob |&8-digit problem id number\\
  1506. \verb|parm[3] |&\verb|nodes |&total number of nodes\\
  1507. \verb|parm[4] |&\verb|nsorc |&total number of source nodes
  1508. (including transshipment nodes)\\
  1509. \verb|parm[5] |&\verb|nsink |&total number of sink nodes
  1510. (including transshipment nodes)\\
  1511. \verb|parm[6] |&\verb|iarcs |&number of arc\\
  1512. \verb|parm[7] |&\verb|mincst|&minimum cost for arcs\\
  1513. \verb|parm[8] |&\verb|maxcst|&maximum cost for arcs\\
  1514. \verb|parm[9] |&\verb|itsup |&total supply\\
  1515. \verb|parm[10]|&\verb|ntsorc|&number of transshipment source nodes\\
  1516. \verb|parm[11]|&\verb|ntsink|&number of transshipment sink nodes\\
  1517. \verb|parm[12]|&\verb|iphic |&percentage of skeleton arcs to be given
  1518. the maximum cost\\
  1519. \verb|parm[13]|&\verb|ipcap |&percentage of arcs to be capacitated\\
  1520. \verb|parm[14]|&\verb|mincap|&minimum upper bound for capacitated arcs\\
  1521. \verb|parm[15]|&\verb|maxcap|&maximum upper bound for capacitated arcs\\
  1522. \end{tabular}
  1523. \returns
  1524. If the instance was successfully generated, the routine
  1525. \verb|glp_netgen| returns zero; otherwise, if specified parameters are
  1526. inconsistent, the routine returns a non-zero error code.
  1527. \para{Notes}
  1528. 1. The routine generates a transportation problem if:
  1529. $${\tt nsorc}+{\tt nsink}={\tt nodes},
  1530. \ {\tt ntsorc}=0,\ \mbox{and}\ {\tt ntsink}=0.$$
  1531. 2. The routine generates an assignment problem if the requirements for
  1532. a transportation problem are met and:
  1533. $${\tt nsorc}={\tt nsink}\ \mbox{and}\ {\tt itsup}={\tt nsorc}.$$
  1534. 3. The routine always generates connected graphs. So, if the number of
  1535. requested arcs has been reached and the generated instance is not fully
  1536. connected, the routine generates a few remaining arcs to ensure
  1537. connectedness. Thus, the actual number of arcs generated by the routine
  1538. may be greater than the requested number of arcs.
  1539. \subsection{glp\_netgen\_prob --- Klingman's standard network problem
  1540. instance}
  1541. \synopsis
  1542. \begin{verbatim}
  1543. void glp_netgen_prob(int nprob, int parm[1+15]);
  1544. \end{verbatim}
  1545. \description
  1546. The routine \verb|glp_netgen_prob| provides the set of parameters for
  1547. Klingman's network problem generator (see the routine
  1548. \verb|glp_netgen|), which describe a standard network problem instance.
  1549. The parameter \verb|nprob| ($101\leq$ \verb|nprob| $\leq 150$)
  1550. specifies the problem instance number.
  1551. The array \verb|parm| contains description of the network, provided by
  1552. the routine. (For detailed description of these parameters see comments
  1553. to the routine \verb|glp_netgen|.)
  1554. \para{Problem characteristics}
  1555. The table below shows characteristics of Klingman's standard network
  1556. problem instances.
  1557. $$
  1558. \begin{array}{crrr}
  1559. {\rm Problem} & {\rm Nodes} & {\rm Arcs} & {\rm Optimum} \\
  1560. \hline
  1561. 101 & 5000 & 25336 & 6191726 \\
  1562. 102 & 5000 & 25387 & 72337144 \\
  1563. 103 & 5000 & 25355 & 218947553 \\
  1564. 104 & 5000 & 25344 & -19100371 \\
  1565. 105 & 5000 & 25332 & 31192578 \\
  1566. 106 & 5000 & 12870 & 4314276 \\
  1567. 107 & 5000 & 37832 & 7393769 \\
  1568. 108 & 5000 & 50309 & 8405738 \\
  1569. 109 & 5000 & 75299 & 9190300 \\
  1570. 110 & 5000 & 12825 & 8975048 \\
  1571. 111 & 5000 & 37828 & 4747532 \\
  1572. 112 & 5000 & 50325 & 4012671 \\
  1573. 113 & 5000 & 75318 & 2979725 \\
  1574. 114 & 5000 & 26514 & 5821181 \\
  1575. 115 & 5000 & 25962 & 6353310 \\
  1576. 116 & 5000 & 25304 & 5915426 \\
  1577. 117 & 5000 & 12816 & 4420560 \\
  1578. 118 & 5000 & 37797 & 7045842 \\
  1579. 119 & 5000 & 50301 & 7724179 \\
  1580. 120 & 5000 & 75330 & 8455200 \\
  1581. 121 & 5000 & 25000 & 66366360 \\
  1582. 122 & 5000 & 25000 & 30997529 \\
  1583. 123 & 5000 & 25000 & 23388777 \\
  1584. 124 & 5000 & 25000 & 17803443 \\
  1585. 125 & 5000 & 25000 & 14119622 \\
  1586. \end{array}
  1587. \hspace{.5in}
  1588. \begin{array}{crrr}
  1589. {\rm Problem} & {\rm Nodes} & {\rm Arcs} & {\rm Optimum} \\
  1590. \hline
  1591. 126 & 5000 & 12500 & 18802218 \\
  1592. 127 & 5000 & 37500 & 27674647 \\
  1593. 128 & 5000 & 50000 & 30906194 \\
  1594. 129 & 5000 & 75000 & 40905209 \\
  1595. 130 & 5000 & 12500 & 38939608 \\
  1596. 131 & 5000 & 37500 & 16752978 \\
  1597. 132 & 5000 & 50000 & 13302951 \\
  1598. 133 & 5000 & 75000 & 9830268 \\
  1599. 134 & 1000 & 25000 & 3804874 \\
  1600. 135 & 2500 & 25000 & 11729616 \\
  1601. 136 & 7500 & 25000 & 33318101 \\
  1602. 137 & 10000 & 25000 & 46426030 \\
  1603. 138 & 5000 & 25000 & 60710879 \\
  1604. 139 & 5000 & 25000 & 32729682 \\
  1605. 140 & 5000 & 25000 & 27183831 \\
  1606. 141 & 5000 & 25000 & 19963286 \\
  1607. 142 & 5000 & 25000 & 20243457 \\
  1608. 143 & 5000 & 25000 & 18586777 \\
  1609. 144 & 5000 & 25000 & 2504591 \\
  1610. 145 & 5000 & 25000 & 215956138 \\
  1611. 146 & 5000 & 25000 & 2253113811 \\
  1612. 147 & 5000 & 25000 & -427908373 \\
  1613. 148 & 5000 & 25000 & -92965318 \\
  1614. 149 & 5000 & 25000 & 86051224 \\
  1615. 150 & 5000 & 25000 & 619314919 \\
  1616. \end{array}
  1617. $$
  1618. \subsection{glp\_gridgen --- grid-like network problem generator}
  1619. \synopsis
  1620. \begin{verbatim}
  1621. int glp_gridgen(glp_graph *G, int v_rhs, int a_cap, int a_cost,
  1622. const int parm[1+14]);
  1623. \end{verbatim}
  1624. \description
  1625. The routine \verb|glp_gridgen| is a GLPK version of the grid-like
  1626. network problem generator developed by Yusin Lee and Jim
  1627. Orlin.\footnote{Y.~Lee and J.~Orlin. GRIDGEN generator., 1991. The
  1628. original code is publically available from
  1629. \url{ftp://dimacs.rutgers.edu/pub/netflow/generators/network/gridgen}.}
  1630. \newpage
  1631. The parameter \verb|G| specifies the graph object, to which the
  1632. generated problem data have to be stored. Note that on entry the graph
  1633. object is erased with the routine \verb|glp_erase_graph|.
  1634. The parameter \verb|v_rhs| specifies an offset of the field of type
  1635. \verb|double| in the vertex data block, to which the routine stores the
  1636. supply or demand value. If \verb|v_rhs| $<0$, the value is not stored.
  1637. The parameter \verb|a_cap| specifies an offset of the field of type
  1638. \verb|double| in the arc data block, to which the routine stores the
  1639. arc capacity. If \verb|a_cap| $<0$, the capacity is not stored.
  1640. The parameter \verb|a_cost| specifies an offset of the field of type
  1641. \verb|double| in the arc data block, to which the routine stores the
  1642. per-unit cost if the arc flow. If \verb|a_cost| $<0$, the cost is not
  1643. stored.
  1644. The array \verb|parm| contains parameters of the network to be
  1645. generated:
  1646. \begin{tabular}{@{}ll@{}}
  1647. \verb|parm[0] |&not used\\
  1648. \verb|parm[1] |&two-ways arcs indicator:\\
  1649. &1 --- if links in both direction should be generated\\
  1650. &0 --- otherwise\\
  1651. \verb|parm[2] |&random number seed (a positive integer)\\
  1652. \verb|parm[3] |&number of nodes (the number of nodes generated might
  1653. be slightly different to\\&make the network a grid)\\
  1654. \verb|parm[4] |&grid width\\
  1655. \verb|parm[5] |&number of sources\\
  1656. \verb|parm[6] |&number of sinks\\
  1657. \verb|parm[7] |&average degree\\
  1658. \verb|parm[8] |&total flow\\
  1659. \verb|parm[9] |&distribution of arc costs:
  1660. 1 --- uniform, 2 --- exponential\\
  1661. \verb|parm[10]|&lower bound for arc cost (uniform),
  1662. $100\lambda$ (exponential)\\
  1663. \verb|parm[11]|&upper bound for arc cost (uniform),
  1664. not used (exponential)\\
  1665. \verb|parm[12]|&distribution of arc capacities:
  1666. 1 --- uniform, 2 --- exponential\\
  1667. \verb|parm[13]|&lower bound for arc capacity (uniform),
  1668. $100\lambda$ (exponential)\\
  1669. \verb|parm[14]|&upper bound for arc capacity (uniform),
  1670. not used (exponential)\\
  1671. \end{tabular}
  1672. \returns
  1673. If the instance was successfully generated, the routine
  1674. \verb|glp_gridgen| returns zero; otherwise, if specified parameters are
  1675. inconsistent, the routine returns a non-zero error code.
  1676. \para{Comments\footnote{This material is based on comments
  1677. to the original version of GRIDGEN.}}
  1678. This network generator generates a grid-like network plus a super node.
  1679. In additional to the arcs connecting the nodes in the grid, there is an
  1680. arc from each supply node to the super node and from the super node to
  1681. each demand node to guarantee feasiblity. These arcs have very high
  1682. costs and very big capacities.
  1683. The idea of this network generator is as follows: First, a grid of
  1684. $n_1\times n_2$ is generated. For example, $5\times 3$. The nodes are
  1685. numbered as 1 to 15, and the supernode is numbered as
  1686. $n_1\times n_2+1$. Then arcs between adjacent nodes are generated.
  1687. For these arcs, the user is allowed to specify either to generate
  1688. two-way arcs or one-way arcs. If two-way arcs are to be generated, two
  1689. arcs, one in each direction, will be generated between each adjacent
  1690. node pairs. Otherwise, only one arc will be generated. If this is the
  1691. case, the arcs will be generated in alterntive directions as shown
  1692. below.
  1693. \medskip
  1694. \noindent\hfil
  1695. \xymatrix
  1696. {1\ar[r]\ar[d]&2\ar[r]&3\ar[r]\ar[d]&4\ar[r]&5\ar[d]\\
  1697. 6\ar[d]&7\ar[l]\ar[u]&8\ar[l]\ar[d]&9\ar[l]\ar[u]&10\ar[l]\ar[d]\\
  1698. 11\ar[r]&12\ar[r]\ar[u]&13\ar[r]&14\ar[r]\ar[u]&15\\
  1699. }
  1700. \medskip
  1701. Then the arcs between the super node and the source/sink nodes are
  1702. added as mentioned before. If the number of arcs still doesn't reach
  1703. the requirement, additional arcs will be added by uniformly picking
  1704. random node pairs. There is no checking to prevent multiple arcs
  1705. between any pair of nodes. However, there will be no self-arcs (arcs
  1706. that poins back to its tail node) in the network.
  1707. The source and sink nodes are selected uniformly in the network, and
  1708. the imbalances of each source/sink node are also assigned by uniform
  1709. distribution.
  1710. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  1711. \newpage
  1712. \section{Maximum flow problem}
  1713. \subsection{Background}
  1714. The {\it maximum flow problem} (MAXFLOW) is stated as follows. Let
  1715. there be given a directed graph (flow network) $G=(V,A)$, where $V$ is
  1716. a set of vertices (nodes), and $A\subseteq V\times V$ is a set of arcs.
  1717. Let also for each arc $a=(i,j)\in A$ there be given its capacity
  1718. $u_{ij}$. The problem is, for given {\it source} node $s\in V$ and
  1719. {\it sink} node $t\in V$, to find flows $x_{ij}$ through every arc of
  1720. the network, which satisfy the specified arc capacities and the
  1721. conservation constraints at all nodes, and maximize the total flow $F$
  1722. through the network from $s$ to $t$. Here the conservation constraint
  1723. at a node means that the total flow entering this node through its
  1724. incoming arcs (plus $F$, if it is the source node) must be equal to the
  1725. total flow leaving this node through its outgoing arcs (plus $F$, if it
  1726. is the sink node). An example of the maximum flow problem,
  1727. where $s=v_1$ and $t=v_9$, is shown on Fig.~2.
  1728. \medskip
  1729. \noindent\hfil
  1730. \xymatrix @C=48pt
  1731. {_{F}\ar@{~>}[d]&
  1732. v_2\ar[r]|{_{10}}\ar[dd]|{_{9}}&
  1733. v_3\ar[dd]|{_{12}}\ar[r]|{_{18}}&
  1734. v_8\ar[rd]|{_{20}}&\\
  1735. v_1\ar[ru]|{_{14}}\ar[rd]|{_{23}}&&&
  1736. v_6\ar[d]|{_{7}}\ar[u]|{_{8}}&
  1737. v_9\ar@{~>}[d]\\
  1738. &v_4\ar[r]|{_{26}}&
  1739. v_5\ar[luu]|{_{11}}\ar[ru]|{_{25}}\ar[r]|{_{4}}&
  1740. v_7\ar[ru]|{_{15}}&_{F}\\
  1741. }
  1742. \medskip
  1743. \noindent\hfil
  1744. Fig.~2. An example of the maximum flow problem.
  1745. \medskip
  1746. The maximum flow problem can be naturally formulated as the following
  1747. LP problem:
  1748. \noindent
  1749. \hspace{1in}maximize
  1750. $$F\eqno(4)$$
  1751. \hspace{1in}subject to
  1752. $$\sum_{(i,j)\in A}x_{ij}-\sum_{(j,i)\in A}x_{ji}=\left\{
  1753. \begin{array}{@{\ }rl}
  1754. +F,&\hbox{for}\ i=s\\
  1755. 0,&\hbox{for all}\ i\in V\backslash\{s,t\}\\
  1756. -F,&\hbox{for}\ i=t\\
  1757. \end{array}
  1758. \right.\eqno(5)
  1759. $$
  1760. $$0\leq x_{ij}\leq u_{ij}\ \ \ \hbox{for all}\ (i,j)\in A
  1761. \eqno(6)$$
  1762. \noindent
  1763. where $F\geq 0$ is an additional variable playing the role of the
  1764. objective.
  1765. Another LP formulation of the maximum flow problem, which does not
  1766. include the variable $F$, is the following:
  1767. \noindent
  1768. \hspace{1in}maximize
  1769. $$z=\sum_{(s,j)\in A}x_{sj}-\sum_{(j,s)\in A}x_{js}\ (=F)\eqno(7)$$
  1770. \hspace{1in}subject to
  1771. $$\sum_{(i,j)\in A}x_{ij}-\sum_{(j,i)\in A}x_{ji}\left\{
  1772. \begin{array}{@{\ }rl}
  1773. \geq 0,&\hbox{for}\ i=s\\
  1774. =0,&\hbox{for all}\ i\in V\backslash\{s,t\}\\
  1775. \leq 0,&\hbox{for}\ i=t\\
  1776. \end{array}
  1777. \right.\eqno(8)
  1778. $$
  1779. $$0\leq x_{ij}\leq u_{ij}\ \ \ \hbox{for all}\ (i,j)\in A
  1780. \eqno(9)$$
  1781. \subsection{glp\_read\_maxflow --- read maximum flow problem data in
  1782. DIMACS\\format}
  1783. \synopsis
  1784. \begin{verbatim}
  1785. int glp_read_maxflow(glp_graph *G, int *s, int *t, int a_cap,
  1786. const char *fname);
  1787. \end{verbatim}
  1788. \description
  1789. The routine \verb|glp_read_maxflow| reads the maximum flow problem
  1790. data from a text file in DIMACS format.
  1791. The parameter \verb|G| specifies the graph object, to which the problem
  1792. data have to be stored. Note that before reading data the current
  1793. content of the graph object is completely erased with the routine
  1794. \verb|glp_erase_graph|.
  1795. The pointer \verb|s| specifies a location, to which the routine stores
  1796. the ordinal number of the source node. If \verb|s| is \verb|NULL|, the
  1797. source node number is not stored.
  1798. The pointer \verb|t| specifies a location, to which the routine stores
  1799. the ordinal number of the sink node. If \verb|t| is \verb|NULL|, the
  1800. sink node number is not stored.
  1801. The parameter \verb|a_cap| specifies an offset of the field of type
  1802. \verb|double| in the arc data block, to which the routine stores
  1803. $u_{ij}$, the arc capacity. If \verb|a_cap| $<0$, the arc capacity is
  1804. not stored.
  1805. The character string \verb|fname| specifies the name of a text file to
  1806. be read in. (If the file name name ends with the suffix `\verb|.gz|',
  1807. the file is assumed to be compressed, in which case the routine
  1808. decompresses it ``on the fly''.)
  1809. \returns
  1810. If the operation was successful, the routine returns zero. Otherwise,
  1811. it prints an error message and returns non-zero.
  1812. \para{Example}
  1813. \begin{footnotesize}
  1814. \begin{verbatim}
  1815. typedef struct
  1816. { /* arc data block */
  1817. ...
  1818. double cap;
  1819. ...
  1820. } a_data;
  1821. int main(void)
  1822. { glp_graph *G;
  1823. int s, t, ret;
  1824. G = glp_create_graph(..., sizeof(a_data));
  1825. ret = glp_read_maxflow(G, &s, &t, offsetof(a_data, cap),
  1826. "sample.max");
  1827. if (ret != 0) goto ...
  1828. ...
  1829. }
  1830. \end{verbatim}
  1831. \end{footnotesize}
  1832. \newpage
  1833. \para{DIMACS maximum flow problem format\footnote{This material is
  1834. based on the paper ``The First DIMACS International Algorithm
  1835. Implementation Challenge: Problem Definitions and Specifications'',
  1836. which is publically available at
  1837. \url{http://dimacs.rutgers.edu/Challenges/}.}}
  1838. \label{subsecmaxflow}
  1839. The DIMACS input file is a plain ASCII text file. It contains
  1840. {\it lines} of several types described below. A line is terminated with
  1841. an end-of-line character. Fields in each line are separated by at least
  1842. one blank space. Each line begins with a one-character designator to
  1843. identify the line type.
  1844. Note that DIMACS requires all numerical quantities to be integers in
  1845. the range $[-2^{31},\ 2^{31}-1]$ while GLPK allows the quantities to be
  1846. floating-point numbers.
  1847. \para{Comment lines.} Comment lines give human-readable information
  1848. about the file and are ignored by programs. Comment lines can appear
  1849. anywhere in the file. Each comment line begins with a lower-case
  1850. character \verb|c|.
  1851. \begin{verbatim}
  1852. c This is a comment line
  1853. \end{verbatim}
  1854. \para{Problem line.} There is one problem line per data file. The
  1855. problem line must appear before any node or arc descriptor lines.
  1856. It has the following format:
  1857. \begin{verbatim}
  1858. p max NODES ARCS
  1859. \end{verbatim}
  1860. \noindent
  1861. The lower-case character \verb|p| signifies that this is a problem line.
  1862. The three-character problem designator \verb|max| identifies the file as
  1863. containing specification information for the maximum flow problem. The
  1864. \verb|NODES| field contains an integer value specifying the number of
  1865. nodes in the network. The \verb|ARCS| field contains an integer value
  1866. specifying the number of arcs in the network.
  1867. \para{Node descriptors.} Two node descriptor lines for the source and
  1868. sink nodes must appear before all arc descriptor lines. They may appear
  1869. in either order, each with the following format:
  1870. \begin{verbatim}
  1871. n ID WHICH
  1872. \end{verbatim}
  1873. \noindent
  1874. The lower-case character \verb|n| signifies that this a node descriptor
  1875. line. The \verb|ID| field gives a node identification number,
  1876. an integer between 1 and \verb|NODES|. The \verb|WHICH| field gives
  1877. either a lower-case \verb|s| or \verb|t|, designating the source and
  1878. sink, respectively.
  1879. \para{Arc descriptors.} There is one arc descriptor line for each arc
  1880. in the network. Arc descriptor lines are of the following format:
  1881. \begin{verbatim}
  1882. a SRC DST CAP
  1883. \end{verbatim}
  1884. \noindent
  1885. The lower-case character \verb|a| signifies that this is an arc
  1886. descriptor line. For a directed arc $(i,j)$ the \verb|SRC| field gives
  1887. the identification number $i$ for the tail endpoint, and the \verb|DST|
  1888. field gives the identification number $j$ for the head endpoint.
  1889. Identification numbers are integers between 1 and \verb|NODES|. The
  1890. \verb|CAP| field gives the arc capacity, i.e. maximum amount of flow
  1891. that can be sent along arc $(i,j)$ in a feasible flow.
  1892. \para{Example.} Below here is an example of the data file in DIMACS
  1893. format corresponding to the maximum flow problem shown on Fig~2.
  1894. \begin{footnotesize}
  1895. \begin{verbatim}
  1896. c sample.max
  1897. c
  1898. c This is an example of the maximum flow problem data
  1899. c in DIMACS format.
  1900. c
  1901. p max 9 14
  1902. c
  1903. n 1 s
  1904. n 9 t
  1905. c
  1906. a 1 2 14
  1907. a 1 4 23
  1908. a 2 3 10
  1909. a 2 4 9
  1910. a 3 5 12
  1911. a 3 8 18
  1912. a 4 5 26
  1913. a 5 2 11
  1914. a 5 6 25
  1915. a 5 7 4
  1916. a 6 7 7
  1917. a 6 8 8
  1918. a 7 9 15
  1919. a 8 9 20
  1920. c
  1921. c eof
  1922. \end{verbatim}
  1923. \end{footnotesize}
  1924. \subsection{glp\_write\_maxflow --- write maximum flow problem data in
  1925. DIMACS\\format}
  1926. \synopsis
  1927. \begin{verbatim}
  1928. int glp_write_maxflow(glp_graph *G, int s, int t, int a_cap,
  1929. const char *fname);
  1930. \end{verbatim}
  1931. \description
  1932. The routine \verb|glp_write_maxflow| writes the maximum flow problem
  1933. data to a text file in DIMACS format.
  1934. The parameter \verb|G| is the graph (network) program object, which
  1935. specifies the maximum flow problem instance.
  1936. The parameter \verb|s| specifies the ordinal number of the source node.
  1937. The parameter \verb|t| specifies the ordinal number of the sink node.
  1938. The parameter \verb|a_cap| specifies an offset of the field of type
  1939. \verb|double| in the arc data block, which contains $u_{ij}$, the upper
  1940. bound to the arc flow (the arc capacity). If the upper bound is
  1941. specified as \verb|DBL_MAX|, it is assumed that $u_{ij}=\infty$, i.e.
  1942. the arc is uncapacitated. If \verb|a_cap| $<0$, it is assumed that
  1943. $u_{ij}=1$ for all arcs.
  1944. The character string \verb|fname| specifies a name of the text file to
  1945. be written out. (If the file name ends with suffix `\verb|.gz|', the
  1946. file is assumed to be compressed, in which case the routine performs
  1947. automatic compression on writing it.)
  1948. \returns
  1949. If the operation was successful, the routine returns zero. Otherwise,
  1950. it prints an error message and returns non-zero.
  1951. \newpage
  1952. \subsection{glp\_maxflow\_lp --- convert maximum flow problem to LP}
  1953. \synopsis
  1954. \begin{verbatim}
  1955. void glp_maxflow_lp(glp_prob *P, glp_graph *G, int names, int s, int t,
  1956. int a_cap);
  1957. \end{verbatim}
  1958. \description
  1959. The routine \verb|glp_maxflow_lp| builds LP problem (7)---(9), which
  1960. corresponds to the specified maximum flow problem.
  1961. The parameter \verb|P| is the resultant LP problem object to be built.
  1962. Note that on entry its current content is erased with the routine
  1963. \verb|glp_erase_prob|.
  1964. The parameter \verb|G| is the graph (network) program object, which
  1965. specifies the maximum flow problem instance.
  1966. The parameter \verb|names| is a flag. If it is \verb|GLP_ON|, the
  1967. routine uses symbolic names of the graph object components to assign
  1968. symbolic names to the LP problem object components. If the flag is
  1969. \verb|GLP_OFF|, no symbolic names are assigned.
  1970. The parameter \verb|s| specifies the ordinal number of the source node.
  1971. The parameter \verb|t| specifies the ordinal number of the sink node.
  1972. The parameter \verb|a_cap| specifies an offset of the field of type
  1973. \verb|double| in the arc data block, which contains $u_{ij}$, the upper
  1974. bound to the arc flow (the arc capacity). If the upper bound is
  1975. specified as \verb|DBL_MAX|, it is assumed that $u_{ij}=\infty$, i.e.
  1976. the arc is uncapacitated. If \verb|a_cap| $<0$, it is assumed that
  1977. $u_{ij}=1$ for all arcs.
  1978. \para{Example}
  1979. The example program below reads the maximum flow problem in DIMACS
  1980. format from file `\verb|sample.max|', converts the instance to LP, and
  1981. then writes the resultant LP in CPLEX format to file
  1982. `\verb|maxflow.lp|'.
  1983. \begin{footnotesize}
  1984. \begin{verbatim}
  1985. #include <stddef.h>
  1986. #include <glpk.h>
  1987. int main(void)
  1988. { glp_graph *G;
  1989. glp_prob *P;
  1990. int s, t;
  1991. G = glp_create_graph(0, sizeof(double));
  1992. glp_read_maxflow(G, &s, &t, 0, "sample.max");
  1993. P = glp_create_prob();
  1994. glp_maxflow_lp(P, G, GLP_ON, s, t, 0);
  1995. glp_delete_graph(G);
  1996. glp_write_lp(P, NULL, "maxflow.lp");
  1997. glp_delete_prob(P);
  1998. return 0;
  1999. }
  2000. \end{verbatim}
  2001. \end{footnotesize}
  2002. If `\verb|sample.max|' is the example data file from the previous
  2003. subsection, the output `\verb|maxflow.lp|' may look like follows:
  2004. \newpage
  2005. \begin{footnotesize}
  2006. \begin{verbatim}
  2007. Maximize
  2008. obj: + x(1,4) + x(1,2)
  2009. Subject To
  2010. r_1: + x(1,2) + x(1,4) >= 0
  2011. r_2: - x(5,2) + x(2,3) + x(2,4) - x(1,2) = 0
  2012. r_3: + x(3,5) + x(3,8) - x(2,3) = 0
  2013. r_4: + x(4,5) - x(2,4) - x(1,4) = 0
  2014. r_5: + x(5,2) + x(5,6) + x(5,7) - x(4,5) - x(3,5) = 0
  2015. r_6: + x(6,7) + x(6,8) - x(5,6) = 0
  2016. r_7: + x(7,9) - x(6,7) - x(5,7) = 0
  2017. r_8: + x(8,9) - x(6,8) - x(3,8) = 0
  2018. r_9: - x(8,9) - x(7,9) <= 0
  2019. Bounds
  2020. 0 <= x(1,4) <= 23
  2021. 0 <= x(1,2) <= 14
  2022. 0 <= x(2,4) <= 9
  2023. 0 <= x(2,3) <= 10
  2024. 0 <= x(3,8) <= 18
  2025. 0 <= x(3,5) <= 12
  2026. 0 <= x(4,5) <= 26
  2027. 0 <= x(5,7) <= 4
  2028. 0 <= x(5,6) <= 25
  2029. 0 <= x(5,2) <= 11
  2030. 0 <= x(6,8) <= 8
  2031. 0 <= x(6,7) <= 7
  2032. 0 <= x(7,9) <= 15
  2033. 0 <= x(8,9) <= 20
  2034. End
  2035. \end{verbatim}
  2036. \end{footnotesize}
  2037. \subsection{glp\_maxflow\_ffalg --- solve maximum flow problem with
  2038. Ford-Fulkerson\\algorithm}
  2039. \synopsis
  2040. \begin{verbatim}
  2041. int glp_maxflow_ffalg(glp_graph *G, int s, int t, int a_cap, double *sol,
  2042. int a_x, int v_cut);
  2043. \end{verbatim}
  2044. \description
  2045. The routine \verb|glp_mincost_ffalg| finds optimal solution to the
  2046. maximum flow problem with the Ford-Fulkerson algorithm.\footnote{GLPK
  2047. implementation of the Ford-Fulkerson algorithm is based on the
  2048. following book: L.~R.~Ford,~Jr., and D.~R.~Fulkerson, ``Flows in
  2049. Networks,'' The RAND Corp., Report R-375-PR (August 1962), Chap. I
  2050. ``Static Maximal Flow,'' pp.~30-33.} Note that this routine requires
  2051. all the problem data to be integer-valued.
  2052. The parameter \verb|G| is a graph (network) program object which
  2053. specifies the maximum flow problem instance to be solved.
  2054. The parameter $s$ specifies the ordinal number of the source node.
  2055. \newpage
  2056. The parameter $t$ specifies the ordinal number of the sink node.
  2057. The parameter \verb|a_cap| specifies an offset of the field of type
  2058. \verb|double| in the arc data block, which contains $u_{ij}$, the upper
  2059. bound to the arc flow (the arc capacity). This bound must be integer in
  2060. the range [0, \verb|INT_MAX|]. If \verb|a_cap| $<0$, it is assumed that
  2061. $u_{ij}=1$ for all arcs.
  2062. The parameter \verb|sol| specifies a location, to which the routine
  2063. stores the objective value (that is, the total flow from $s$ to $t$)
  2064. found. If \verb|sol| is NULL, the objective value is not stored.
  2065. The parameter \verb|a_x| specifies an offset of the field of type
  2066. \verb|double| in the arc data block, to which the routine stores
  2067. $x_{ij}$, the arc flow found. If \verb|a_x| $<0$, the arc flow values
  2068. are not stored.
  2069. The parameter \verb|v_cut| specifies an offset of the field of type
  2070. \verb|int| in the vertex data block, to which the routine stores node
  2071. flags corresponding to the optimal solution found: if the node flag is
  2072. 1, the node is labelled, and if the node flag is 0, the node is
  2073. unlabelled. The calling program may use these node flags to determine
  2074. the {\it minimal cut}, which is a subset of arcs whose one endpoint is
  2075. labelled and other is not. If \verb|v_cut| $<0$, the node flags are not
  2076. stored.
  2077. Note that all solution components (the objective value and arc flows)
  2078. computed by the routine are always integer-valued.
  2079. \returns
  2080. \begin{retlist}
  2081. 0 & Optimal solution found.\\
  2082. \verb|GLP_EDATA| & Unable to start the search, because some problem
  2083. data are either not integer-valued or out of range.\\
  2084. \end{retlist}
  2085. \para{Example}
  2086. The example program shown below reads the maximum flow problem instance
  2087. in DIMACS format from file `\verb|sample.max|', solves it using the
  2088. routine \verb|glp_maxflow_ffalg|, and write the solution found to the
  2089. standard output.
  2090. \begin{footnotesize}
  2091. \begin{verbatim}
  2092. #include <stddef.h>
  2093. #include <stdio.h>
  2094. #include <stdlib.h>
  2095. #include <glpk.h>
  2096. typedef struct { int cut; } v_data;
  2097. typedef struct { double cap, x; } a_data;
  2098. #define node(v) ((v_data *)((v)->data))
  2099. #define arc(a) ((a_data *)((a)->data))
  2100. int main(void)
  2101. { glp_graph *G;
  2102. glp_vertex *v, *w;
  2103. glp_arc *a;
  2104. int i, s, t, ret;
  2105. double sol;
  2106. G = glp_create_graph(sizeof(v_data), sizeof(a_data));
  2107. glp_read_maxflow(G, &s, &t, offsetof(a_data, cap),
  2108. "sample.max");
  2109. ret = glp_maxflow_ffalg(G, s, t, offsetof(a_data, cap),
  2110. &sol, offsetof(a_data, x), offsetof(v_data, cut));
  2111. printf("ret = %d; sol = %5g\n", ret, sol);
  2112. for (i = 1; i <= G->nv; i++)
  2113. { v = G->v[i];
  2114. for (a = v->out; a != NULL; a = a->t_next)
  2115. { w = a->head;
  2116. printf("x[%d->%d] = %5g (%d)\n", v->i, w->i,
  2117. arc(a)->x, node(v)->cut ^ node(w)->cut);
  2118. }
  2119. }
  2120. glp_delete_graph(G);
  2121. return 0;
  2122. }
  2123. \end{verbatim}
  2124. \end{footnotesize}
  2125. If `\verb|sample.max|' is the example data file from the subsection
  2126. describing \verb|glp_read_maxflow|, the output may look like follows:
  2127. \begin{footnotesize}
  2128. \begin{verbatim}
  2129. Reading maximum flow problem data from `sample.max'...
  2130. Flow network has 9 nodes and 14 arcs
  2131. 24 lines were read
  2132. ret = 0; sol = 29
  2133. x[1->4] = 19 (0)
  2134. x[1->2] = 10 (0)
  2135. x[2->4] = 0 (0)
  2136. x[2->3] = 10 (1)
  2137. x[3->8] = 10 (0)
  2138. x[3->5] = 0 (1)
  2139. x[4->5] = 19 (0)
  2140. x[5->7] = 4 (1)
  2141. x[5->6] = 15 (0)
  2142. x[5->2] = 0 (0)
  2143. x[6->8] = 8 (1)
  2144. x[6->7] = 7 (1)
  2145. x[7->9] = 11 (0)
  2146. x[8->9] = 18 (0)
  2147. \end{verbatim}
  2148. \end{footnotesize}
  2149. \subsection{glp\_rmfgen --- Goldfarb's maximum flow problem generator}
  2150. \synopsis
  2151. \begin{verbatim}
  2152. int glp_rmfgen(glp_graph *G, int *s, int *t, int a_cap, const int parm[1+5]);
  2153. \end{verbatim}
  2154. \description
  2155. The routine \verb|glp_rmfgen| is a GLPK version of the maximum flow
  2156. problem generator developed by D.~Goldfarb and
  2157. M.~Grigoriadis.\footnote{D.~Goldfarb and M.~D.~Grigoriadis,
  2158. ``A computational comparison of the Dinic and network simplex methods
  2159. for maximum flow.'' Annals of Op. Res. 13 (1988),
  2160. pp.~83-123.}$^{,}$\footnote{U.~Derigs and W.~Meier, ``Implementing
  2161. Goldberg's max-flow algorithm: A computational investigation.''
  2162. Zeitschrift f\"ur Operations Research 33 (1989),
  2163. pp.~383-403.}$^{,}$\footnote{The original code of RMFGEN implemented by
  2164. Tamas Badics is publically available from
  2165. \url{ftp://dimacs.rutgers.edu/pub/netflow/generators/network/genrmf}.}
  2166. The parameter \verb|G| specifies the graph object, to which the
  2167. generated problem data have to be stored. Note that on entry the graph
  2168. object is erased with the routine \verb|glp_erase_graph|.
  2169. \newpage
  2170. The pointers \verb|s| and \verb|t| specify locations, to which the
  2171. routine stores the source and sink node numbers, respectively. If
  2172. \verb|s| or \verb|t| is \verb|NULL|, corresponding node number is not
  2173. stored.
  2174. The parameter \verb|a_cap| specifies an offset of the field of type
  2175. \verb|double| in the arc data block, to which the routine stores the arc
  2176. capacity. If \verb|a_cap| $<0$, the capacity is not stored.
  2177. The array \verb|parm| contains description of the network to be
  2178. generated:
  2179. \begin{tabular}{@{}lll@{}}
  2180. \verb|parm[0]|& &not used\\
  2181. \verb|parm[1]|&\verb|seed|&random number seed (a positive integer)\\
  2182. \verb|parm[2]|&\verb|a |&frame size\\
  2183. \verb|parm[3]|&\verb|b |&depth\\
  2184. \verb|parm[4]|&\verb|c1 |&minimal arc capacity\\
  2185. \verb|parm[5]|&\verb|c2 |&maximal arc capacity\\
  2186. \end{tabular}
  2187. \returns
  2188. If the instance was successfully generated, the routine
  2189. \verb|glp_netgen| returns zero; otherwise, if specified parameters are
  2190. inconsistent, the routine returns a non-zero error code.
  2191. \para{Comments\footnote{This material is based on comments to the
  2192. original version of RMFGEN.}}
  2193. The generated network is as follows. It has $b$ pieces of frames of
  2194. size $a\times a$. (So alltogether the number of vertices is
  2195. $a\times a\times b$.)
  2196. In each frame all the vertices are connected with their neighbours
  2197. (forth and back). In addition the vertices of a frame are connected
  2198. one to one with the vertices of next frame using a random permutation
  2199. of those vertices.
  2200. The source is the lower left vertex of the first frame, the sink is
  2201. the upper right vertex of the $b$-th frame.
  2202. \begin{verbatim}
  2203. t
  2204. +-------+
  2205. | .|
  2206. | . |
  2207. / | / |
  2208. +-------+/ -+ b
  2209. | | |/.
  2210. a | -v- |/
  2211. | | |/
  2212. +-------+ 1
  2213. s a
  2214. \end{verbatim}
  2215. The capacities are randomly chosen integers from the range of
  2216. $[c_1,c_2]$ in the case of interconnecting edges, and $c_2\cdot a^2$
  2217. for the in-frame edges.
  2218. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  2219. \newpage
  2220. \section{Assignment problem}
  2221. \subsection{Background}
  2222. Let there be given an undirected bipartite graph $G=(R\cup S,E)$, where
  2223. $R$ and $S$ are disjoint sets of vertices (nodes), and
  2224. $E\subseteq R\times S$ is a set of edges. Let also for each edge
  2225. $e=(i,j)\in E$ there be given its cost $c_{ij}$. A {\it matching}
  2226. (which in case of bipartite graph is also called {\it assignment})
  2227. $M\subseteq E$ in $G$ is a set of pairwise non-adjacent edges, that is,
  2228. no two edges in $M$ share a common vertex. A matching, which matches
  2229. all vertices of the graph, is called a {\it perfect matching}.
  2230. Obviously, a perfect matching in bipartite graph $G=(R\cup S,E)$
  2231. defines some bijection $R\leftrightarrow S$.
  2232. The {\it assignment problem} has two different variants. In the first
  2233. variant the problem is to find matching (assignment) $M$, which
  2234. maximizes the sum:
  2235. $$\sum_{(i,j)\in M}c_{ij}\eqno(10)$$
  2236. (so this variant is also called the {\it maximum weighted bipartite
  2237. matching problem} or, if all $c_{ij}=1$, the {\it maximum cardinality
  2238. bipartite matching problem}). In the second, classic variant the
  2239. problem is to find {\it perfect} matching (assignment) $M$, which
  2240. minimizes or maximizes the sum (10).
  2241. An example of the assignment problem, which is the maximum weighted
  2242. bipartite matching problem, is shown on Fig. 3.
  2243. The maximum weighted bipartite matching problem can be naturally
  2244. formulated as the following LP problem:
  2245. \noindent
  2246. \hspace{1in}maximize
  2247. $$z=\sum_{(i,j)\in E}c_{ij}x_{ij}\eqno(11)$$
  2248. \hspace{1in}subject to
  2249. $$\sum_{(i,j)\in E}x_{ij}\leq 1\ \ \ \hbox{for all}\ i\in R\eqno(12)$$
  2250. $$\sum_{(i,j)\in E}x_{ij}\leq 1\ \ \ \hbox{for all}\ j\in S\eqno(13)$$
  2251. $$\ \ \ \ \ \ \ \ 0\leq x_{ij}\leq 1\ \ \ \hbox{for all}\ (i,j)\in E
  2252. \eqno(14)$$
  2253. \noindent
  2254. where $x_{ij}=1$ means that $(i,j)\in M$, and $x_{ij}=0$ means that
  2255. $(i,j)\notin M$.\footnote{The constraint matrix of LP formulation
  2256. (11)---(14) is totally unimodular, due to which $x_{ij}\in\{0,1\}$ for
  2257. any basic solution.}
  2258. \newpage
  2259. \noindent\hfil
  2260. \xymatrix @C=48pt
  2261. {v_1\ar@{-}[rr]|{_{13}}\ar@{-}[rrd]|{_{21}}\ar@{-}[rrddd]|(.2){_{20}}&&
  2262. v_9\\
  2263. v_2\ar@{-}[rr]|{_{12}}\ar@{-}[rrdd]|(.3){_{8}}
  2264. \ar@{-}[rrddd]|(.4){_{26}}&&v_{10}\\
  2265. v_3\ar@{-}[rr]|(.2){_{22}}\ar@{-}[rrdd]|(.3){_{11}}&&v_{11}\\
  2266. v_4\ar@{-}[rruuu]|(.6){_{12}}\ar@{-}[rr]|(.2){_{36}}
  2267. \ar@{-}[rrdd]|(.7){_{25}}&&v_{12}\\
  2268. v_5\ar@{-}[rruu]|(.42){_{41}}\ar@{-}[rru]|(.4){_{40}}
  2269. \ar@{-}[rr]|(.75){_{11}}\ar@{-}[rrd]|(.6){_{4}}\ar@{-}[rrdd]|{_{8}}
  2270. \ar@{-}[rrddd]|{_{35}}\ar@{-}[rrdddd]|{_{32}}&&v_{13}\\
  2271. v_6\ar@{-}[rruuuuu]|(.7){_{13}}&&v_{14}\\
  2272. v_7\ar@{-}[rruuuuu]|(.15){_{19}}&&v_{15}\\
  2273. v_8\ar@{-}[rruuuuuu]|(.25){_{39}}\ar@{-}[rruuuuu]|(.65){_{15}}&&
  2274. v_{16}\\
  2275. &&v_{17}\\
  2276. }
  2277. \medskip
  2278. \noindent\hfil
  2279. Fig.~3. An example of the assignment problem.
  2280. \medskip
  2281. Similarly, the perfect assignment problem can be naturally formulated
  2282. as the following LP problem:
  2283. \noindent
  2284. \hspace{1in}minimize (or maximize)
  2285. $$z=\sum_{(i,j)\in E}c_{ij}x_{ij}\eqno(15)$$
  2286. \hspace{1in}subject to
  2287. $$\sum_{(i,j)\in E}x_{ij}=1\ \ \ \hbox{for all}\ i\in R\eqno(16)$$
  2288. $$\sum_{(i,j)\in E}x_{ij}=1\ \ \ \hbox{for all}\ j\in S\eqno(17)$$
  2289. $$\ \ \ \ \ \ \ \ 0\leq x_{ij}\leq 1\ \ \ \hbox{for all}\ (i,j)\in E
  2290. \eqno(18)$$
  2291. \noindent
  2292. where variables $x_{ij}$ have the same meaning as for (11)---(14)
  2293. above.
  2294. In GLPK an undirected bipartite graph $G=(R\cup S,E)$ is represented as
  2295. directed graph $\overline{G}=(V,A)$, where $V=R\cup S$ and
  2296. $A=\{(i,j):(i,j)\in E\}$, i.e. every edge $(i,j)\in E$ in $G$
  2297. corresponds to arc $(i\rightarrow j)\in A$ in $\overline{G}$.
  2298. \subsection{glp\_read\_asnprob --- read assignment problem data in
  2299. DIMACS format}
  2300. \synopsis
  2301. \begin{verbatim}
  2302. int glp_read_asnprob(glp_graph *G, int v_set, int a_cost, const char *fname);
  2303. \end{verbatim}
  2304. \description
  2305. The routine \verb|glp_read_asnprob| reads the assignment problem data
  2306. from a text file in DIMACS format.
  2307. The parameter \verb|G| specifies the graph object, to which the problem
  2308. data have to be stored. Note that before reading data the current
  2309. content of the graph object is completely erased with the routine
  2310. \verb|glp_erase_graph|.
  2311. The parameter \verb|v_set| specifies an offset of the field of type
  2312. \verb|int| in the vertex data block, to which the routine stores the
  2313. node set indicator:
  2314. 0 --- the node is in set $R$;
  2315. 1 --- the node is in set $S$.
  2316. \noindent
  2317. If \verb|v_set| $<0$, the node set indicator is not stored.
  2318. The parameter \verb|a_cost| specifies an offset of the field of type
  2319. \verb|double| in the arc data block, to which the routine stores the
  2320. edge cost $c_{ij}$. If \verb|a_cost| $<0$, the edge cost is not stored.
  2321. The character string \verb|fname| specifies the name of a text file to
  2322. be read in. (If the file name name ends with the suffix `\verb|.gz|',
  2323. the file is assumed to be compressed, in which case the routine
  2324. decompresses it ``on the fly''.)
  2325. \returns
  2326. If the operation was successful, the routine returns zero. Otherwise,
  2327. it prints an error message and returns non-zero.
  2328. \para{Example.} Below here is an example program that read the
  2329. assignment problem data in DIMACS format from a text file
  2330. `\verb|sample.asn|'.
  2331. \begin{footnotesize}
  2332. \begin{verbatim}
  2333. typedef struct
  2334. { /* vertex data block */
  2335. ...
  2336. int set;
  2337. ...
  2338. } v_data;
  2339. typedef struct
  2340. { /* arc data block */
  2341. ...
  2342. double cost;
  2343. ...
  2344. } a_data;
  2345. int main(void)
  2346. { glp_graph *G;
  2347. int ret;
  2348. G = glp_create_graph(sizeof(v_data), sizeof(a_data));
  2349. ret = glp_read_asnprob(G, offsetof(v_data, set),
  2350. offsetof(a_data, cost), "sample.asn");
  2351. if (ret != 0) goto ...
  2352. ...
  2353. }
  2354. \end{verbatim}
  2355. \end{footnotesize}
  2356. \para{DIMACS assignment problem format\footnote{This material is based
  2357. on the paper ``The First DIMACS International Algorithm Implementation
  2358. Challenge: Problem Definitions and Specifications'', which is
  2359. publically available at \url{http://dimacs.rutgers.edu/Challenges/}.}}
  2360. \label{subsecasnprob}
  2361. The DIMACS input file is a plain ASCII text file. It contains
  2362. {\it lines} of several types described below. A line is terminated with
  2363. an end-of-line character. Fields in each line are separated by at least
  2364. one blank space. Each line begins with a one-character designator to
  2365. identify the line type.
  2366. Note that DIMACS requires all numerical quantities to be integers in
  2367. the range $[-2^{31},\ 2^{31}-1]$ while GLPK allows the quantities to be
  2368. floating-point numbers.
  2369. \para{Comment lines.} Comment lines give human-readable information
  2370. about the file and are ignored by programs. Comment lines can appear
  2371. anywhere in the file. Each comment line begins with a lower-case
  2372. character \verb|c|.
  2373. \begin{verbatim}
  2374. c This is a comment line
  2375. \end{verbatim}
  2376. \para{Problem line.} There is one problem line per data file. The
  2377. problem line must appear before any node or arc descriptor lines. It
  2378. has the following format:
  2379. \begin{verbatim}
  2380. p asn NODES EDGES
  2381. \end{verbatim}
  2382. \noindent
  2383. The lower-case character \verb|p| signifies that this is a problem line.
  2384. The three-character problem designator \verb|asn| identifies the file as
  2385. containing specification information for the assignment problem.
  2386. The \verb|NODES| field contains an integer value specifying the total
  2387. number of nodes in the graph (i.e. in both sets $R$ and $S$). The
  2388. \verb|EDGES| field contains an integer value specifying the number of
  2389. edges in the graph.
  2390. \para{Node descriptors.} All node descriptor lines must appear before
  2391. all edge descriptor lines. The node descriptor lines lists the nodes in
  2392. set $R$ only, and all other nodes are assumed to be in set $S$. There
  2393. is one node descriptor line for each such node, with the following
  2394. format:
  2395. \begin{verbatim}
  2396. n ID
  2397. \end{verbatim}
  2398. \noindent
  2399. The lower-case character \verb|n| signifies that this is a node
  2400. descriptor line. The \verb|ID| field gives a node identification number,
  2401. an integer between 1 and \verb|NODES|.
  2402. \para{Edge descriptors.} There is one edge descriptor line for each
  2403. edge in the graph. Edge descriptor lines are of the following format:
  2404. \begin{verbatim}
  2405. a SRC DST COST
  2406. \end{verbatim}
  2407. \noindent
  2408. The lower-case character \verb|a| signifies that this is an edge
  2409. descriptor line. For each edge $(i,j)$, where $i\in R$ and $j\in S$,
  2410. the \verb|SRC| field gives the identification number of vertex $i$, and
  2411. the \verb|DST| field gives the identification number of vertex $j$.
  2412. Identification numbers are integers between 1 and \verb|NODES|. The
  2413. \verb|COST| field contains the cost of edge $(i,j)$.
  2414. \para{Example.} Below here is an example of the data file in DIMACS
  2415. format corresponding to the assignment problem shown on Fig~3.
  2416. \begin{footnotesize}
  2417. \begin{verbatim}
  2418. c sample.asn
  2419. c
  2420. c This is an example of the assignment problem data
  2421. c in DIMACS format.
  2422. c
  2423. p asn 17 22
  2424. c
  2425. n 1
  2426. n 2
  2427. n 3
  2428. n 4
  2429. n 5
  2430. n 6
  2431. n 7
  2432. n 8
  2433. c
  2434. a 1 9 13
  2435. a 1 10 21
  2436. a 1 12 20
  2437. a 2 10 12
  2438. a 2 12 8
  2439. a 2 13 26
  2440. a 3 11 22
  2441. a 3 13 11
  2442. a 4 9 12
  2443. a 4 12 36
  2444. a 4 14 25
  2445. a 5 11 41
  2446. a 5 12 40
  2447. a 5 13 11
  2448. a 5 14 4
  2449. a 5 15 8
  2450. a 5 16 35
  2451. a 5 17 32
  2452. a 6 9 13
  2453. a 7 10 19
  2454. a 8 10 39
  2455. a 8 11 15
  2456. c
  2457. c eof
  2458. \end{verbatim}
  2459. \end{footnotesize}
  2460. \newpage
  2461. \subsection{glp\_write\_asnprob --- write assignment problem data in
  2462. DIMACS format}
  2463. \synopsis
  2464. \begin{verbatim}
  2465. int glp_write_asnprob(glp_graph *G, int v_set, int a_cost, const char *fname);
  2466. \end{verbatim}
  2467. \description
  2468. The routine \verb|glp_write_asnprob| writes the assignment problem data
  2469. to a text file in DIMACS format.
  2470. The parameter \verb|G| is the graph program object, which specifies the
  2471. assignment problem instance.
  2472. The parameter \verb|v_set| specifies an offset of the field of type
  2473. \verb|int| in the vertex data block, which contains the node set
  2474. indicator:
  2475. 0 --- the node is in set $R$;
  2476. 1 --- the node is in set $S$.
  2477. \noindent
  2478. If \verb|v_set| $<0$, it is assumed that a node having no incoming arcs
  2479. is in set $R$, and a node having no outgoing arcs is in set $S$.
  2480. The parameter \verb|a_cost| specifies an offset of the field of type
  2481. \verb|double| in the arc data block, which contains $c_{ij}$, the edge
  2482. cost. If \verb|a_cost| $<0$, it is assumed that $c_{ij}=1$ for all
  2483. edges.
  2484. The character string \verb|fname| specifies a name of the text file to
  2485. be written out. (If the file name ends with suffix `\verb|.gz|', the
  2486. file is assumed to be compressed, in which case the routine performs
  2487. automatic compression on writing it.)
  2488. \para{Note}
  2489. The routine \verb|glp_write_asnprob| does not check that the specified
  2490. graph object correctly represents a bipartite graph. To make sure that
  2491. the problem data are correct, use the routine \verb|glp_check_asnprob|.
  2492. \returns
  2493. If the operation was successful, the routine returns zero. Otherwise,
  2494. it prints an error message and returns non-zero.
  2495. \vspace*{-4pt}
  2496. \subsection{glp\_check\_asnprob --- check correctness of assignment
  2497. problem data}
  2498. \synopsis
  2499. \begin{verbatim}
  2500. int glp_check_asnprob(glp_graph *G, int v_set);
  2501. \end{verbatim}
  2502. \description
  2503. The routine \verb|glp_check_asnprob| checks that the specified graph
  2504. object \verb|G| correctly represents a bipartite graph.
  2505. The parameter \verb|v_set| specifies an offset of the field of type
  2506. \verb|int| in the vertex data block, which contains the node set
  2507. indicator:
  2508. 0 --- the node is in set $R$;
  2509. 1 --- the node is in set $S$.
  2510. \noindent
  2511. If \verb|v_set| $<0$, it is assumed that a node having no incoming arcs
  2512. is in set $R$, and a node having no outgoing arcs is in set $S$.
  2513. \returns
  2514. 0 --- the data are correct;
  2515. 1 --- the set indicator of some node is 0, however, that node has one
  2516. or more incoming arcs;
  2517. 2 --- the set indicator of some node is 1, however, that node has one
  2518. or more outgoing arcs;
  2519. 3 --- the set indicator of some node is invalid (neither 0 nor 1);
  2520. 4 --- some node has both incoming and outgoing arcs.
  2521. \subsection{glp\_asnprob\_lp --- convert assignment problem to LP}
  2522. \synopsis
  2523. \begin{verbatim}
  2524. int glp_asnprob_lp(glp_prob *P, int form, glp_graph *G, int names, int v_set,
  2525. int a_cost);
  2526. \end{verbatim}
  2527. \description
  2528. The routine \verb|glp_asnprob_lp| builds LP problem, which corresponds
  2529. to the specified assignment problem.
  2530. The parameter \verb|P| is the resultant LP problem object to be built.
  2531. Note that on entry its current content is erased with the routine
  2532. \verb|glp_erase_prob|.
  2533. The parameter \verb|form| defines which LP formulation should be used:
  2534. \verb|GLP_ASN_MIN| --- perfect matching (15)---(18), minimization;
  2535. \verb|GLP_ASN_MAX| --- perfect matching (15)---(18), maximization;
  2536. \verb|GLP_ASN_MMP| --- maximum weighted matching (11)---(14).
  2537. The parameter \verb|G| is the graph program object, which specifies the
  2538. assignment problem instance.
  2539. The parameter \verb|names| is a flag. If it is \verb|GLP_ON|, the
  2540. routine uses symbolic names of the graph object components to assign
  2541. symbolic names to the LP problem object components. If the \verb|flag|
  2542. is \verb|GLP_OFF|, no symbolic names are assigned.
  2543. The parameter \verb|v_set| specifies an offset of the field of type
  2544. \verb|int| in the vertex data block, which contains the node set
  2545. indicator:
  2546. 0 --- the node is in set $R$;
  2547. 1 --- the node is in set $S$.
  2548. \noindent
  2549. If \verb|v_set| $<0$, it is assumed that a node having no incoming arcs
  2550. is in set $R$, and a node having no outgoing arcs is in set $S$.
  2551. The parameter \verb|a_cost| specifies an offset of the field of type
  2552. \verb|double| in the arc data block, which contains $c_{ij}$, the edge
  2553. cost. If \verb|a_cost| $<0$, it is assumed that $c_{ij}=1$ for all
  2554. edges.
  2555. \newpage
  2556. \returns
  2557. If the LP problem has been successfully built, the routine
  2558. \verb|glp_asnprob_lp| returns zero, otherwise, non-zero (see the
  2559. routine \verb|glp_check_asnprob|).
  2560. \para{Example}
  2561. The example program below reads the assignment problem instance in
  2562. DIMACS format from file `\verb|sample.asn|', converts the instance to
  2563. LP (11)---(14), and writes the resultant LP in CPLEX format to file
  2564. `\verb|matching.lp|'.
  2565. \begin{footnotesize}
  2566. \begin{verbatim}
  2567. #include <stddef.h>
  2568. #include <glpk.h>
  2569. typedef struct { int set; } v_data;
  2570. typedef struct { double cost; } a_data;
  2571. int main(void)
  2572. { glp_graph *G;
  2573. glp_prob *P;
  2574. G = glp_create_graph(sizeof(v_data), sizeof(a_data));
  2575. glp_read_asnprob(G, offsetof(v_data, set),
  2576. offsetof(a_data, cost), "sample.asn");
  2577. P = glp_create_prob();
  2578. glp_asnprob_lp(P, GLP_ASN_MMP, G, GLP_ON,
  2579. offsetof(v_data, set), offsetof(a_data, cost));
  2580. glp_delete_graph(G);
  2581. glp_write_lp(P, NULL, "matching.lp");
  2582. glp_delete_prob(P);
  2583. return 0;
  2584. }
  2585. \end{verbatim}
  2586. \end{footnotesize}
  2587. If `\verb|sample.asn|' is the example data file from the subsection
  2588. describing \verb|glp_read_asnprob|, file `\verb|matching.lp|' may look
  2589. like follows:
  2590. \begin{footnotesize}
  2591. \begin{verbatim}
  2592. Maximize
  2593. obj: + 20 x(1,12) + 21 x(1,10) + 13 x(1,9) + 26 x(2,13) + 8 x(2,12)
  2594. + 12 x(2,10) + 11 x(3,13) + 22 x(3,11) + 25 x(4,14) + 36 x(4,12)
  2595. + 12 x(4,9) + 32 x(5,17) + 35 x(5,16) + 8 x(5,15) + 4 x(5,14)
  2596. + 11 x(5,13) + 40 x(5,12) + 41 x(5,11) + 13 x(6,9) + 19 x(7,10)
  2597. + 15 x(8,11) + 39 x(8,10)
  2598. Subject To
  2599. r_1: + x(1,9) + x(1,10) + x(1,12) <= 1
  2600. r_2: + x(2,10) + x(2,12) + x(2,13) <= 1
  2601. r_3: + x(3,11) + x(3,13) <= 1
  2602. r_4: + x(4,9) + x(4,12) + x(4,14) <= 1
  2603. r_5: + x(5,11) + x(5,12) + x(5,13) + x(5,14) + x(5,15) + x(5,16)
  2604. + x(5,17) <= 1
  2605. r_6: + x(6,9) <= 1
  2606. r_7: + x(7,10) <= 1
  2607. r_8: + x(8,10) + x(8,11) <= 1
  2608. r_9: + x(6,9) + x(4,9) + x(1,9) <= 1
  2609. r_10: + x(8,10) + x(7,10) + x(2,10) + x(1,10) <= 1
  2610. r_11: + x(8,11) + x(5,11) + x(3,11) <= 1
  2611. r_12: + x(5,12) + x(4,12) + x(2,12) + x(1,12) <= 1
  2612. r_13: + x(5,13) + x(3,13) + x(2,13) <= 1
  2613. r_14: + x(5,14) + x(4,14) <= 1
  2614. r_15: + x(5,15) <= 1
  2615. r_16: + x(5,16) <= 1
  2616. r_17: + x(5,17) <= 1
  2617. Bounds
  2618. 0 <= x(1,12) <= 1
  2619. 0 <= x(1,10) <= 1
  2620. 0 <= x(1,9) <= 1
  2621. 0 <= x(2,13) <= 1
  2622. 0 <= x(2,12) <= 1
  2623. 0 <= x(2,10) <= 1
  2624. 0 <= x(3,13) <= 1
  2625. 0 <= x(3,11) <= 1
  2626. 0 <= x(4,14) <= 1
  2627. 0 <= x(4,12) <= 1
  2628. 0 <= x(4,9) <= 1
  2629. 0 <= x(5,17) <= 1
  2630. 0 <= x(5,16) <= 1
  2631. 0 <= x(5,15) <= 1
  2632. 0 <= x(5,14) <= 1
  2633. 0 <= x(5,13) <= 1
  2634. 0 <= x(5,12) <= 1
  2635. 0 <= x(5,11) <= 1
  2636. 0 <= x(6,9) <= 1
  2637. 0 <= x(7,10) <= 1
  2638. 0 <= x(8,11) <= 1
  2639. 0 <= x(8,10) <= 1
  2640. End
  2641. \end{verbatim}
  2642. \end{footnotesize}
  2643. \subsection{glp\_asnprob\_okalg --- solve assignment problem with
  2644. out-of-kilter\\algorithm}
  2645. \synopsis
  2646. \begin{verbatim}
  2647. int glp_asnprob_okalg(int form, glp_graph *G, int v_set, int a_cost,
  2648. double *sol, int a_x);
  2649. \end{verbatim}
  2650. \description
  2651. The routine \verb|glp_mincost_okalg| finds optimal solution to the
  2652. assignment problem with the out-of-kilter
  2653. algorithm.\footnote{GLPK implementation of the out-of-kilter algorithm
  2654. is based on the following book: L.~R.~Ford,~Jr., and D.~R.~Fulkerson,
  2655. ``Flows in Networks,'' The RAND Corp., Report R-375-PR (August 1962),
  2656. Chap. III ``Minimal Cost Flow Problems,'' pp.~113-26.} Note that this
  2657. routine requires all the problem data to be integer-valued.
  2658. The parameter \verb|form| defines which LP formulation should be used:
  2659. \verb|GLP_ASN_MIN| --- perfect matching (15)---(18), minimization;
  2660. \verb|GLP_ASN_MAX| --- perfect matching (15)---(18), maximization;
  2661. \verb|GLP_ASN_MMP| --- maximum weighted matching (11)---(14).
  2662. \newpage
  2663. The parameter \verb|G| is the graph program object, which specifies the
  2664. assignment problem instance.
  2665. The parameter \verb|v_set| specifies an offset of the field of type
  2666. \verb|int| in the vertex data block, which contains the node set
  2667. indicator:
  2668. 0 --- the node is in set $R$;
  2669. 1 --- the node is in set $S$.
  2670. \noindent
  2671. If \verb|v_set| $<0$, it is assumed that a node having no incoming arcs
  2672. is in set $R$, and a node having no outgoing arcs is in set $S$.
  2673. The parameter \verb|a_cost| specifies an offset of the field of type
  2674. \verb|double| in the arc data block, which contains $c_{ij}$, the edge
  2675. cost. This value must be integer in the range [\verb|-INT_MAX|,
  2676. \verb|+INT_MAX|]. If \verb|a_cost| $<0$, it is assumed that $c_{ij}=1$
  2677. for all edges.
  2678. The parameter \verb|sol| specifies a location, to which the routine
  2679. stores the objective value (that is, the total cost) found.
  2680. If \verb|sol| is \verb|NULL|, the objective value is not stored.
  2681. The parameter \verb|a_x| specifies an offset of the field of type
  2682. \verb|int| in the arc data block, to which the routine stores $x_{ij}$.
  2683. If \verb|a_x| $<0$, this value is not stored.
  2684. \returns
  2685. \begin{retlist}
  2686. 0 & Optimal solution found.\\
  2687. \verb|GLP_ENOPFS| & No (primal) feasible solution exists.\\
  2688. \verb|GLP_EDATA| & Unable to start the search, because the assignment
  2689. problem data are either incorrect (this error is detected by the
  2690. routine \verb|glp_check_asnprob|), not integer-valued or out of range.\\
  2691. \verb|GLP_ERANGE| & The search was prematurely terminated because of
  2692. integer overflow.\\
  2693. \verb|GLP_EFAIL| & An error has been detected in the program logic.
  2694. (If this code is returned for your problem instance, please report to
  2695. \verb|<bug-glpk@gnu.org>|.)\\
  2696. \end{retlist}
  2697. \para{Comments}
  2698. Since the out-of-kilter algorithm is designed to find a minimal cost
  2699. circulation, the routine \verb|glp_asnprob_okalg| converts the original
  2700. graph to a network suitable for this algorithm in the following
  2701. way:\footnote{The conversion is performed internally and does not
  2702. change the original graph program object passed to the routine.}
  2703. 1) it replaces each edge $(i,j)$ by arc $(i\rightarrow j)$,
  2704. flow $x_{ij}$ through which has zero lower bound ($l_{ij}=0$), unity
  2705. upper bound ($u_{ij}=1$), and per-unit cost $+c_{ij}$ (in case of
  2706. \verb|GLP_ASN_MIN|), or $-c_{ij}$ (in case of \verb|GLP_ASN_MAX| and
  2707. \verb|GLP_ASN_MMP|);
  2708. 2) then it adds one auxiliary feedback node $k$;
  2709. 3) for each original node $i\in R$ the routine adds auxiliary supply
  2710. arc $(k\rightarrow i)$, flow $x_{ki}$ through which is costless
  2711. ($c_{ki}=0$) and either fixed at 1 ($l_{ki}=u_{ki}=1$, in case of
  2712. \verb|GLP_ASN_MIN| and \verb|GLP_ASN_MAX|) or has zero lower bound and
  2713. unity upper bound ($l_{ij}=0$, $u_{ij}=1$, in case of
  2714. \verb|GLP_ASN_MMP|);
  2715. \newpage
  2716. 4) similarly, for each original node $j\in S$ the routine adds
  2717. auxiliary demand arc $(j\rightarrow k)$, flow $x_{jk}$ through which is
  2718. costless ($c_{jk}=0$) and either fixed at 1 ($l_{jk}=u_{jk}=1$, in case
  2719. of \verb|GLP_ASN_MIN| and \verb|GLP_ASN_MAX|) or has zero lower bound
  2720. and unity upper bound ($l_{jk}=0$, $u_{jk}=1$, in case of
  2721. \verb|GLP_ASN_MMP|).
  2722. \para{Example}
  2723. The example program shown below reads the assignment problem instance
  2724. in DIMACS format from file `\verb|sample.asn|', solves it by using the
  2725. routine \verb|glp_asnprob_okalg|, and writes the solution found to the
  2726. standard output.
  2727. \begin{footnotesize}
  2728. \begin{verbatim}
  2729. #include <stddef.h>
  2730. #include <stdio.h>
  2731. #include <stdlib.h>
  2732. #include <glpk.h>
  2733. typedef struct { int set; } v_data;
  2734. typedef struct { double cost; int x; } e_data;
  2735. #define node(v) ((v_data *)((v)->data))
  2736. #define edge(e) ((e_data *)((e)->data))
  2737. int main(void)
  2738. { glp_graph *G;
  2739. glp_vertex *v;
  2740. glp_arc *e;
  2741. int i, ret;
  2742. double sol;
  2743. G = glp_create_graph(sizeof(v_data), sizeof(e_data));
  2744. glp_read_asnprob(G, offsetof(v_data, set),
  2745. offsetof(e_data, cost), "sample.asn");
  2746. ret = glp_asnprob_okalg(GLP_ASN_MMP, G,
  2747. offsetof(v_data, set), offsetof(e_data, cost), &sol,
  2748. offsetof(e_data, x));
  2749. printf("ret = %d; sol = %5g\n", ret, sol);
  2750. for (i = 1; i <= G->nv; i++)
  2751. { v = G->v[i];
  2752. for (e = v->out; e != NULL; e = e->t_next)
  2753. printf("edge %2d %2d: x = %d; c = %g\n",
  2754. e->tail->i, e->head->i, edge(e)->x, edge(e)->cost);
  2755. }
  2756. glp_delete_graph(G);
  2757. return 0;
  2758. }
  2759. \end{verbatim}
  2760. \end{footnotesize}
  2761. If `\verb|sample.asn|' is the example data file from the subsection
  2762. describing \verb|glp_read_asnprob|, the output may look like follows:
  2763. \begin{footnotesize}
  2764. \begin{verbatim}
  2765. Reading assignment problem data from `sample.asn'...
  2766. Assignment problem has 8 + 9 = 17 nodes and 22 arcs
  2767. 38 lines were read
  2768. ret = 0; sol = 180
  2769. edge 1 12: x = 1; c = 20
  2770. edge 1 10: x = 0; c = 21
  2771. edge 1 9: x = 0; c = 13
  2772. edge 2 13: x = 1; c = 26
  2773. edge 2 12: x = 0; c = 8
  2774. edge 2 10: x = 0; c = 12
  2775. edge 3 13: x = 0; c = 11
  2776. edge 3 11: x = 1; c = 22
  2777. edge 4 14: x = 1; c = 25
  2778. edge 4 12: x = 0; c = 36
  2779. edge 4 9: x = 0; c = 12
  2780. edge 5 17: x = 0; c = 32
  2781. edge 5 16: x = 1; c = 35
  2782. edge 5 15: x = 0; c = 8
  2783. edge 5 14: x = 0; c = 4
  2784. edge 5 13: x = 0; c = 11
  2785. edge 5 12: x = 0; c = 40
  2786. edge 5 11: x = 0; c = 41
  2787. edge 6 9: x = 1; c = 13
  2788. edge 7 10: x = 0; c = 19
  2789. edge 8 11: x = 0; c = 15
  2790. edge 8 10: x = 1; c = 39
  2791. \end{verbatim}
  2792. \end{footnotesize}
  2793. \subsection{glp\_asnprob\_hall --- find bipartite matching of maximum
  2794. cardinality}
  2795. \synopsis
  2796. \begin{verbatim}
  2797. int glp_asnprob_hall(glp_graph *G, int v_set, int a_x);
  2798. \end{verbatim}
  2799. \description
  2800. The routine \verb|glp_asnprob_hall| finds a matching of maximal
  2801. cardinality in the specified bipartite graph. It uses a version of the
  2802. Fortran routine \verb|MC21A| developed by
  2803. I.~S.~Duff\footnote{I.~S.~Duff, Algorithm 575: Permutations for
  2804. zero-free diagonal, ACM Trans. on Math. Softw. 7 (1981),\linebreak
  2805. pp.~387-390.}, which implements Hall's algorithm.\footnote{M.~Hall,
  2806. ``An Algorithm for Distinct Representatives,'' Am. Math. Monthly 63
  2807. (1956), pp.~716-717.}
  2808. The parameter \verb|G| is a pointer to the graph program object.
  2809. The parameter \verb|v_set| specifies an offset of the field of type
  2810. \verb|int| in the vertex data block, which contains the node set
  2811. indicator:
  2812. 0 --- the node is in set $R$;
  2813. 1 --- the node is in set $S$.
  2814. \noindent
  2815. If \verb|v_set| $<0$, it is assumed that a node having no incoming arcs
  2816. is in set $R$, and a node having no outgoing arcs is in set $S$.
  2817. The parameter \verb|a_x| specifies an offset of the field of type
  2818. \verb|int| in the arc data block, to which the routine stores $x_{ij}$.
  2819. If \verb|a_x| $<0$, this value is not stored.
  2820. \returns
  2821. The routine \verb|glp_asnprob_hall| returns the cardinality of the
  2822. matching found. However, if the specified graph is incorrect (as
  2823. detected by the routine \verb|glp_check_asnprob|), this routine returns
  2824. a negative value.
  2825. \newpage
  2826. \para{Comments}
  2827. The same solution may be obtained with the routine
  2828. \verb|glp_asnprob_okalg| (for LP formulation \verb|GLP_ASN_MMP| and
  2829. all edge costs equal to 1). However, the routine
  2830. \verb|glp_asnprob_hall| is much faster.
  2831. \para{Example}
  2832. The example program shown below reads the assignment problem instance
  2833. in DIMACS format from file `\verb|sample.asn|', finds a bipartite
  2834. matching of maximal cardinality by using the routine
  2835. \verb|glp_asnprob_hall|, and writes the solution found to the standard
  2836. output.
  2837. \begin{footnotesize}
  2838. \begin{verbatim}
  2839. #include <stddef.h>
  2840. #include <stdio.h>
  2841. #include <stdlib.h>
  2842. #include <glpk.h>
  2843. typedef struct { int set; } v_data;
  2844. typedef struct { int x; } e_data;
  2845. #define node(v) ((v_data *)((v)->data))
  2846. #define edge(e) ((e_data *)((e)->data))
  2847. int main(void)
  2848. { glp_graph *G;
  2849. glp_vertex *v;
  2850. glp_arc *e;
  2851. int i, card;
  2852. G = glp_create_graph(sizeof(v_data), sizeof(e_data));
  2853. glp_read_asnprob(G, offsetof(v_data, set), -1,
  2854. "sample.asn");
  2855. card = glp_asnprob_hall(G, offsetof(v_data, set),
  2856. offsetof(e_data, x));
  2857. printf("card = %d\n", card);
  2858. for (i = 1; i <= G->nv; i++)
  2859. { v = G->v[i];
  2860. for (e = v->out; e != NULL; e = e->t_next)
  2861. printf("edge %2d %2d: x = %d\n",
  2862. e->tail->i, e->head->i, edge(e)->x);
  2863. }
  2864. glp_delete_graph(G);
  2865. return 0;
  2866. }
  2867. \end{verbatim}
  2868. \end{footnotesize}
  2869. If `\verb|sample.asn|' is the example data file from the subsection
  2870. describing \verb|glp_read_asnprob|, the output may look like follows:
  2871. \begin{footnotesize}
  2872. \begin{verbatim}
  2873. Reading assignment problem data from `sample.asn'...
  2874. Assignment problem has 8 + 9 = 17 nodes and 22 arcs
  2875. 38 lines were read
  2876. card = 7
  2877. edge 1 12: x = 1
  2878. edge 1 10: x = 0
  2879. edge 1 9: x = 0
  2880. edge 2 13: x = 1
  2881. edge 2 12: x = 0
  2882. edge 2 10: x = 0
  2883. edge 3 13: x = 0
  2884. edge 3 11: x = 1
  2885. edge 4 14: x = 1
  2886. edge 4 12: x = 0
  2887. edge 4 9: x = 0
  2888. edge 5 17: x = 1
  2889. edge 5 16: x = 0
  2890. edge 5 15: x = 0
  2891. edge 5 14: x = 0
  2892. edge 5 13: x = 0
  2893. edge 5 12: x = 0
  2894. edge 5 11: x = 0
  2895. edge 6 9: x = 1
  2896. edge 7 10: x = 1
  2897. edge 8 11: x = 0
  2898. edge 8 10: x = 0
  2899. \end{verbatim}
  2900. \end{footnotesize}
  2901. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  2902. \newpage
  2903. \section{Critical path problem}
  2904. \subsection{Background}
  2905. The {\it critical path problem} (CPP) is stated as follows. Let there
  2906. be given a project $J$, which is a set of jobs (tasks, activities,
  2907. etc.). Performing each job $i\in J$ requires time $t_i\geq 0$. Besides,
  2908. over the set $J$ there is given a precedence relation
  2909. $R\subseteq J\times J$, where $(i,j)\in R$ means that job $i$
  2910. immediately precedes job $j$, i.e. performing job $j$ cannot start
  2911. until job $i$ has been completely performed. The problem is to find
  2912. starting times $x_i$ for each job $i\in J$, which satisfy to the
  2913. precedence relation and minimize the total duration (makespan) of the
  2914. project.
  2915. The following is an example of the critical path problem:
  2916. \bigskip
  2917. \begin{center}
  2918. \begin{tabular}{|c|l|c|c|}
  2919. \hline
  2920. Job&Desription&Time&Predecessors\\
  2921. \hline
  2922. A&Excavate&3&---\\
  2923. B&Lay foundation&4&A\\
  2924. C&Rough plumbing&3&B\\
  2925. D&Frame&10&B\\
  2926. E&Finish exterior&8&D\\
  2927. F&Install HVAC&4&D\\
  2928. G&Rough electric&6&D\\
  2929. H&Sheet rock&8&C, E, F, G\\
  2930. I&Install cabinets&5&H\\
  2931. J&Paint&5&H\\
  2932. K&Final plumbing&4&I\\
  2933. L&Final electric&2&J\\
  2934. M&Install flooring&4&K, L\\
  2935. \hline
  2936. \end{tabular}
  2937. \end{center}
  2938. \bigskip
  2939. Obviously, the project along with the precedence relation can be
  2940. represented as a directed graph $G=(J,R)$ called {\it project network},
  2941. where each node $i\in J$ corresponds to a job, and arc
  2942. $(i\rightarrow j)\in R$ means that job $i$ immediately precedes job
  2943. $j$.\footnote{There exists another network representation of the
  2944. critical path problem, where jobs correspond to arcs while nodes
  2945. correspond to events introduced to express the precedence relation.
  2946. That representation, however, is much less convenient than the one,
  2947. where jobs are represented as nodes of the network.} The project network
  2948. for the example above is shown on Fig.~4.
  2949. May note that the project network must be acyclic; otherwise, it would
  2950. be impossible to satisfy to the precedence relation for any job that
  2951. belongs to a cycle.
  2952. \newpage
  2953. \hspace*{.5in}
  2954. \xymatrix
  2955. {&&&C|3\ar[rd]&&I|5\ar[r]&K|4\ar[rd]&\\
  2956. A|3\ar[r]&B|4\ar[rru]\ar[rd]&&E|8\ar[r]&H|8\ar[ru]\ar[rd]&&&M|4\\
  2957. &&D|10\ar[ru]\ar[r]\ar[rd]&F|4\ar[ru]&&J|5\ar[r]&L|2\ar[ru]&\\
  2958. &&&G|6\ar[ruu]&&&&\\
  2959. }
  2960. \medskip
  2961. \noindent\hfil
  2962. Fig.~4. An example of the project network.
  2963. \medskip
  2964. The critical path problem can be naturally formulated as the following
  2965. LP problem:
  2966. \medskip
  2967. \noindent
  2968. \hspace{.5in}minimize
  2969. $$z\eqno(19)$$
  2970. \hspace{.5in}subject to
  2971. $$x_i+t_i\leq z\ \ \ \hbox{for all}\ i\in J\ \ \ \ \eqno(20)$$
  2972. $$x_i+t_i\leq x_j\ \ \ \hbox{for all}\ (i,j)\in R\eqno(21)$$
  2973. $$x_i\geq 0\ \ \ \ \ \ \ \hbox{for all}\ i\in J\ \ \eqno(22)$$
  2974. The inequality constraints (21), which are active in the optimal
  2975. solution, define so called {\it critical path} having the following
  2976. property: the minimal project duration $z$ can be decreased only by
  2977. decreasing the times $t_j$ for jobs on the critical path, and delaying
  2978. any critical job delays the entire project.
  2979. \subsection{glp\_cpp --- solve critical path problem}
  2980. \synopsis
  2981. \begin{verbatim}
  2982. double glp_cpp(glp_graph *G, int v_t, int v_es, int v_ls);
  2983. \end{verbatim}
  2984. \description
  2985. The routine \verb|glp_cpp| solves the critical path problem represented
  2986. in the form of the project network.
  2987. The parameter \verb|G| is a pointer to the graph object, which
  2988. specifies the project network. This graph must be acyclic. Multiple
  2989. arcs are allowed being considered as single arcs.
  2990. The parameter \verb|v_t| specifies an offset of the field of type
  2991. \verb|double| in the vertex data block, which contains time $t_i\geq 0$
  2992. needed to perform corresponding job $j\in J$. If \verb|v_t| $<0$, it is
  2993. assumed that $t_i=1$ for all jobs.
  2994. The parameter \verb|v_es| specifies an offset of the field of type
  2995. \verb|double| in the vertex data block, to which the routine stores
  2996. the {\it earliest start time} for corresponding job. If \verb|v_es|
  2997. $<0$, this time is not stored.
  2998. \newpage
  2999. The parameter \verb|v_ls| specifies an offset of the field of type
  3000. \verb|double| in the vertex data block, to which the routine stores
  3001. the {\it latest start time} for corresponding job. If \verb|v_ls|
  3002. $<0$, this time is not stored.
  3003. The difference between the latest and earliest start times of some job
  3004. is called its {\it time reserve}. Delaying a job within its time
  3005. reserve does not affect the project duration, so if the time reserve is
  3006. zero, the corresponding job is critical.
  3007. \para{Returns}
  3008. The routine \verb|glp_cpp| returns the minimal project duration, i.e.
  3009. minimal time needed to perform all jobs in the project.
  3010. \para{Example}
  3011. The example program below solves the critical path problem shown on
  3012. Fig.~4 by using the routine \verb|glp_cpp| and writes the solution
  3013. found on the standard output.
  3014. \begin{footnotesize}
  3015. \begin{verbatim}
  3016. #include <stddef.h>
  3017. #include <stdio.h>
  3018. #include <stdlib.h>
  3019. #include <glpk.h>
  3020. typedef struct { double t, es, ls; } v_data;
  3021. #define node(v) ((v_data *)((v)->data))
  3022. int main(void)
  3023. { glp_graph *G;
  3024. int i;
  3025. double t, es, ef, ls, lf, total;
  3026. G = glp_create_graph(sizeof(v_data), 0);
  3027. glp_add_vertices(G, 13);
  3028. node(G->v[1])->t = 3; /* A: Excavate */
  3029. node(G->v[2])->t = 4; /* B: Lay foundation */
  3030. node(G->v[3])->t = 3; /* C: Rough plumbing */
  3031. node(G->v[4])->t = 10; /* D: Frame */
  3032. node(G->v[5])->t = 8; /* E: Finish exterior */
  3033. node(G->v[6])->t = 4; /* F: Install HVAC */
  3034. node(G->v[7])->t = 6; /* G: Rough elecrtic */
  3035. node(G->v[8])->t = 8; /* H: Sheet rock */
  3036. node(G->v[9])->t = 5; /* I: Install cabinets */
  3037. node(G->v[10])->t = 5; /* J: Paint */
  3038. node(G->v[11])->t = 4; /* K: Final plumbing */
  3039. node(G->v[12])->t = 2; /* L: Final electric */
  3040. node(G->v[13])->t = 4; /* M: Install flooring */
  3041. glp_add_arc(G, 1, 2); /* A precedes B */
  3042. glp_add_arc(G, 2, 3); /* B precedes C */
  3043. glp_add_arc(G, 2, 4); /* B precedes D */
  3044. glp_add_arc(G, 4, 5); /* D precedes E */
  3045. glp_add_arc(G, 4, 6); /* D precedes F */
  3046. glp_add_arc(G, 4, 7); /* D precedes G */
  3047. glp_add_arc(G, 3, 8); /* C precedes H */
  3048. glp_add_arc(G, 5, 8); /* E precedes H */
  3049. glp_add_arc(G, 6, 8); /* F precedes H */
  3050. glp_add_arc(G, 7, 8); /* G precedes H */
  3051. glp_add_arc(G, 8, 9); /* H precedes I */
  3052. glp_add_arc(G, 8, 10); /* H precedes J */
  3053. glp_add_arc(G, 9, 11); /* I precedes K */
  3054. glp_add_arc(G, 10, 12); /* J precedes L */
  3055. glp_add_arc(G, 11, 13); /* K precedes M */
  3056. glp_add_arc(G, 12, 13); /* L precedes M */
  3057. total = glp_cpp(G, offsetof(v_data, t), offsetof(v_data, es),
  3058. offsetof(v_data, ls));
  3059. printf("Minimal project duration is %.2f\n\n", total);
  3060. printf("Job Time ES EF LS LF\n");
  3061. printf("--- ------ ------ ------ ------ ------\n");
  3062. for (i = 1; i <= G->nv; i++)
  3063. { t = node(G->v[i])->t;
  3064. es = node(G->v[i])->es;
  3065. ef = es + node(G->v[i])->t;
  3066. ls = node(G->v[i])->ls;
  3067. lf = ls + node(G->v[i])->t;
  3068. printf("%3d %6.2f %s %6.2f %6.2f %6.2f %6.2f\n",
  3069. i, t, ls - es < 0.001 ? "*" : " ", es, ef, ls, lf);
  3070. }
  3071. glp_delete_graph(G);
  3072. return 0;
  3073. }
  3074. \end{verbatim}
  3075. \end{footnotesize}
  3076. The output from the example program shown below includes job number,
  3077. the time needed to perform a job, earliest start time (\verb|ES|),
  3078. earliest finish time (\verb|EF|), latest start time (\verb|LS|), and
  3079. latest finish time (\verb|LF|) for each job in the project. Critical
  3080. jobs are marked by asterisks.
  3081. \begin{footnotesize}
  3082. \begin{verbatim}
  3083. Minimal project duration is 46.00
  3084. Job Time ES EF LS LF
  3085. --- ------ ------ ------ ------ ------
  3086. 1 3.00 * 0.00 3.00 0.00 3.00
  3087. 2 4.00 * 3.00 7.00 3.00 7.00
  3088. 3 3.00 7.00 10.00 22.00 25.00
  3089. 4 10.00 * 7.00 17.00 7.00 17.00
  3090. 5 8.00 * 17.00 25.00 17.00 25.00
  3091. 6 4.00 17.00 21.00 21.00 25.00
  3092. 7 6.00 17.00 23.00 19.00 25.00
  3093. 8 8.00 * 25.00 33.00 25.00 33.00
  3094. 9 5.00 * 33.00 38.00 33.00 38.00
  3095. 10 5.00 33.00 38.00 35.00 40.00
  3096. 11 4.00 * 38.00 42.00 38.00 42.00
  3097. 12 2.00 38.00 40.00 40.00 42.00
  3098. 13 4.00 * 42.00 46.00 42.00 46.00
  3099. \end{verbatim}
  3100. \end{footnotesize}
  3101. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  3102. \chapter{Graph Optimization API Routines}
  3103. \section{Maximum clique problem}
  3104. \subsection{Background}
  3105. The {\it maximum clique problem (MCP)} is a classic combinatorial
  3106. optimization problem. Given an undirected graph $G=(V,E)$, where $V$ is
  3107. a set of vertices, and $E$ is a set of edges, this problem is to find
  3108. the largest {\it clique} $C\subseteq G$, i.e. the largest induced
  3109. complete subgraph. A generalization of this problem is the {\it maximum
  3110. weight clique problem (MWCP)}, which is to find a clique $C\subseteq G$
  3111. of the largest weight $\displaystyle\sum_{v\in C}w(v)\rightarrow\max$,
  3112. where $w(v)$ is a weight of vertex $v\in V$.
  3113. An example of the maximum weight clique problem is shown on Fig.~5.
  3114. \begin{figure}
  3115. \noindent\hfil
  3116. \begin{tabular}{c}
  3117. {\xymatrix %@C=16pt
  3118. {&&&{v_1}\ar@{-}[lllddd]\ar@{-}[llddddd]\ar@{-}[dddddd]
  3119. \ar@{-}[rrrddd]&&&\\
  3120. &{v_2}\ar@{-}[rrrr]\ar@{-}[rrrrdddd]\ar@{-}[rrddddd]\ar@{-}[dddd]&&&&
  3121. {v_3}\ar@{-}[llllldd]\ar@{-}[lllldddd]\ar@{-}[dddd]&\\
  3122. &&&&&&\\
  3123. {v_4}\ar@{-}[rrrrrr]\ar@{-}[rrrddd]&&&&&&{v_5}\ar@{-}[lllddd]
  3124. \ar@{-}[ldd]\\
  3125. &&&&&&\\
  3126. &{v_6}\ar@{-}[rrrr]&&&&{v_7}&\\
  3127. &&&{v_8}&&&\\
  3128. }}
  3129. \end{tabular}
  3130. \begin{tabular}{r@{\ }c@{\ }l}
  3131. $w(v_1)$&=&3\\$w(v_2)$&=&4\\$w(v_3)$&=&8\\$w(v_4)$&=&1\\
  3132. $w(v_5)$&=&5\\$w(v_6)$&=&2\\$w(v_7)$&=&1\\$w(v_8)$&=&3\\
  3133. \end{tabular}
  3134. \bigskip
  3135. \begin{center}
  3136. Fig.~5. An example of the maximum weight clique problem.
  3137. \end{center}
  3138. \end{figure}
  3139. \subsection{glp\_wclique\_exact --- find maximum weight clique with
  3140. exact algorithm}
  3141. \synopsis
  3142. \begin{verbatim}
  3143. int glp_wclique_exact(glp_graph *G, int v_wgt, double *sol, int v_set);
  3144. \end{verbatim}
  3145. \description
  3146. The routine {\tt glp\_wclique\_exact} finds a maximum weight clique in
  3147. the specified undirected graph with the exact algorithm developed by
  3148. Patric \"Osterg{\aa}rd.\footnote{P.~R.~J.~\"Osterg{\aa}rd, A new
  3149. algorithm for the maximum-weight clique problem, Nordic J. of
  3150. Computing, Vol.~8, No.~4, 2001, pp.~424--36.}
  3151. The parameter {\tt G} is the program object, which specifies
  3152. an undirected graph. Each arc $(x\rightarrow y)$ in {\tt G} is
  3153. considered as edge $(x,y)$, self-loops are ignored, and multiple edges,
  3154. if present, are replaced (internally) by simple edges.
  3155. The parameter {\tt v\_wgt} specifies an offset of the field of type
  3156. {\tt double} in the vertex data block, which contains a weight of
  3157. corresponding vertex. Vertex weights must be integer-valued in the
  3158. range $[0,$ {\tt INT\_MAX}$]$. If {\tt v\_wgt} $<0$, it is assumed that
  3159. all vertices of the graph have the weight 1.
  3160. \newpage
  3161. The parameter {\tt sol} specifies a location, to which the routine
  3162. stores the weight of the clique found (the clique weight is the sum
  3163. of weights of all vertices included in the clique.) If {\tt sol} is
  3164. {\tt NULL}, the solution is not stored.
  3165. The parameter {\tt v\_set} specifies an offset of the field of type
  3166. {\tt int} in the vertex data block, to which the routines stores a
  3167. vertex flag: 1 means that the corresponding vertex is included in the
  3168. clique found, and 0 otherwise. If {\tt v\_set} $<0$, vertex flags are
  3169. not stored.
  3170. \returns
  3171. \begin{retlist}
  3172. 0 & Optimal solution found.\\
  3173. \verb|GLP_EDATA| & Unable to start the search, because some vertex
  3174. weights are either not integer-valued or out of range. This code is
  3175. also returned if the sum of weights of all vertices exceeds
  3176. {\tt INT\_MAX}. \\
  3177. \end{retlist}
  3178. \para{Notes}
  3179. 1. The routine {\it glp\_wclique\_exact} finds exact solution. Since
  3180. both MCP and MWCP problems are NP-complete, the algorithm may require
  3181. exponential time in worst cases.
  3182. 2. Internally the specified graph is converted to an adjacency matrix
  3183. in {\it dense} format. This requires about $|V|^2/16$ bytes of memory,
  3184. where $|V|$ is the number of vertices in the graph.
  3185. \para{Example}
  3186. The example program shown below reads a MWCP instance in DIMACS
  3187. clique/coloring format from file `\verb|sample.clq|', finds the clique
  3188. of largest weight, and writes the solution found on the standard
  3189. output.
  3190. \newpage
  3191. \begin{footnotesize}
  3192. \begin{verbatim}
  3193. #include <stddef.h>
  3194. #include <stdio.h>
  3195. #include <stdlib.h>
  3196. #include <glpk.h>
  3197. typedef struct { double wgt; int set; } v_data;
  3198. #define vertex(v) ((v_data *)((v)->data))
  3199. int main(void)
  3200. { glp_graph *G;
  3201. glp_vertex *v;
  3202. int i, ret;
  3203. double sol;
  3204. G = glp_create_graph(sizeof(v_data), 0);
  3205. glp_read_ccdata(G, offsetof(v_data, wgt), "sample.clq");
  3206. ret = glp_wclique_exact(G, offsetof(v_data, wgt), &sol,
  3207. offsetof(v_data, set));
  3208. printf("ret = %d; sol = %g\n", ret, sol);
  3209. for (i = 1; i <= G->nv; i++)
  3210. { v = G->v[i];
  3211. printf("vertex %d: weight = %g, flag = %d\n",
  3212. i, vertex(v)->wgt, vertex(v)->set);
  3213. }
  3214. glp_delete_graph(G);
  3215. return 0;
  3216. }
  3217. \end{verbatim}
  3218. \end{footnotesize}
  3219. For the example shown on Fig.~5 the data file may look like follows:
  3220. \begin{footnotesize}
  3221. \begin{verbatim}
  3222. c sample.clq
  3223. c
  3224. c This is an example of the maximum weight clique
  3225. c problem in DIMACS clique/coloring format.
  3226. c
  3227. p edge 8 16
  3228. n 1 3
  3229. n 2 4
  3230. n 3 8
  3231. n 5 5
  3232. n 6 2
  3233. n 8 3
  3234. e 1 4
  3235. e 1 5
  3236. e 1 6
  3237. e 1 8
  3238. e 2 3
  3239. e 2 6
  3240. e 2 7
  3241. e 2 8
  3242. e 3 4
  3243. e 3 6
  3244. e 3 7
  3245. e 4 5
  3246. e 4 8
  3247. e 5 7
  3248. e 5 8
  3249. e 6 7
  3250. c
  3251. c eof
  3252. \end{verbatim}
  3253. \end{footnotesize}
  3254. The corresponding output from the example program is the following:
  3255. \begin{footnotesize}
  3256. \begin{verbatim}
  3257. Reading graph from `sample.clq'...
  3258. Graph has 8 vertices and 16 edges
  3259. 28 lines were read
  3260. ret = 0; sol = 15
  3261. vertex 1: weight = 3, flag = 0
  3262. vertex 2: weight = 4, flag = 1
  3263. vertex 3: weight = 8, flag = 1
  3264. vertex 4: weight = 1, flag = 0
  3265. vertex 5: weight = 5, flag = 0
  3266. vertex 6: weight = 2, flag = 1
  3267. vertex 7: weight = 1, flag = 1
  3268. vertex 8: weight = 3, flag = 0
  3269. \end{verbatim}
  3270. \end{footnotesize}
  3271. \end{document}