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.

4202 lines
127 KiB

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