glpnet07.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. /* glpnet07.c (Ford-Fulkerson algorithm) */
  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 Andrew Makhorin, Department for Applied Informatics,
  7. * Moscow Aviation Institute, Moscow, Russia. All rights reserved.
  8. * 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. #include "glpenv.h"
  24. #include "glpnet.h"
  25. /***********************************************************************
  26. * NAME
  27. *
  28. * ffalg - Ford-Fulkerson algorithm
  29. *
  30. * SYNOPSIS
  31. *
  32. * #include "glpnet.h"
  33. * void ffalg(int nv, int na, const int tail[], const int head[],
  34. * int s, int t, const int cap[], int x[], char cut[]);
  35. *
  36. * DESCRIPTION
  37. *
  38. * The routine ffalg implements the Ford-Fulkerson algorithm to find a
  39. * maximal flow in the specified flow network.
  40. *
  41. * INPUT PARAMETERS
  42. *
  43. * nv is the number of nodes, nv >= 2.
  44. *
  45. * na is the number of arcs, na >= 0.
  46. *
  47. * tail[a], a = 1,...,na, is the index of tail node of arc a.
  48. *
  49. * head[a], a = 1,...,na, is the index of head node of arc a.
  50. *
  51. * s is the source node index, 1 <= s <= nv.
  52. *
  53. * t is the sink node index, 1 <= t <= nv, t != s.
  54. *
  55. * cap[a], a = 1,...,na, is the capacity of arc a, cap[a] >= 0.
  56. *
  57. * NOTE: Multiple arcs are allowed, but self-loops are not allowed.
  58. *
  59. * OUTPUT PARAMETERS
  60. *
  61. * x[a], a = 1,...,na, is optimal value of the flow through arc a.
  62. *
  63. * cut[i], i = 1,...,nv, is 1 if node i is labelled, and 0 otherwise.
  64. * The set of arcs, whose one endpoint is labelled and other is not,
  65. * defines the minimal cut corresponding to the maximal flow found.
  66. * If the parameter cut is NULL, the cut information are not stored.
  67. *
  68. * REFERENCES
  69. *
  70. * L.R.Ford, Jr., and D.R.Fulkerson, "Flows in Networks," The RAND
  71. * Corp., Report R-375-PR (August 1962), Chap. I "Static Maximal Flow,"
  72. * pp.30-33. */
  73. void ffalg(int nv, int na, const int tail[], const int head[],
  74. int s, int t, const int cap[], int x[], char cut[])
  75. { int a, delta, i, j, k, pos1, pos2, temp,
  76. *ptr, *arc, *link, *list;
  77. /* sanity checks */
  78. xassert(nv >= 2);
  79. xassert(na >= 0);
  80. xassert(1 <= s && s <= nv);
  81. xassert(1 <= t && t <= nv);
  82. xassert(s != t);
  83. for (a = 1; a <= na; a++)
  84. { i = tail[a], j = head[a];
  85. xassert(1 <= i && i <= nv);
  86. xassert(1 <= j && j <= nv);
  87. xassert(i != j);
  88. xassert(cap[a] >= 0);
  89. }
  90. /* allocate working arrays */
  91. ptr = xcalloc(1+nv+1, sizeof(int));
  92. arc = xcalloc(1+na+na, sizeof(int));
  93. link = xcalloc(1+nv, sizeof(int));
  94. list = xcalloc(1+nv, sizeof(int));
  95. /* ptr[i] := (degree of node i) */
  96. for (i = 1; i <= nv; i++)
  97. ptr[i] = 0;
  98. for (a = 1; a <= na; a++)
  99. { ptr[tail[a]]++;
  100. ptr[head[a]]++;
  101. }
  102. /* initialize arc pointers */
  103. ptr[1]++;
  104. for (i = 1; i < nv; i++)
  105. ptr[i+1] += ptr[i];
  106. ptr[nv+1] = ptr[nv];
  107. /* build arc lists */
  108. for (a = 1; a <= na; a++)
  109. { arc[--ptr[tail[a]]] = a;
  110. arc[--ptr[head[a]]] = a;
  111. }
  112. xassert(ptr[1] == 1);
  113. xassert(ptr[nv+1] == na+na+1);
  114. /* now the indices of arcs incident to node i are stored in
  115. locations arc[ptr[i]], arc[ptr[i]+1], ..., arc[ptr[i+1]-1] */
  116. /* initialize arc flows */
  117. for (a = 1; a <= na; a++)
  118. x[a] = 0;
  119. loop: /* main loop starts here */
  120. /* build augmenting tree rooted at s */
  121. /* link[i] = 0 means that node i is not labelled yet;
  122. link[i] = a means that arc a immediately precedes node i */
  123. /* initially node s is labelled as the root */
  124. for (i = 1; i <= nv; i++)
  125. link[i] = 0;
  126. link[s] = -1, list[1] = s, pos1 = pos2 = 1;
  127. /* breadth first search */
  128. while (pos1 <= pos2)
  129. { /* dequeue node i */
  130. i = list[pos1++];
  131. /* consider all arcs incident to node i */
  132. for (k = ptr[i]; k < ptr[i+1]; k++)
  133. { a = arc[k];
  134. if (tail[a] == i)
  135. { /* a = i->j is a forward arc from s to t */
  136. j = head[a];
  137. /* if node j has been labelled, skip the arc */
  138. if (link[j] != 0) continue;
  139. /* if the arc does not allow increasing the flow through
  140. it, skip the arc */
  141. if (x[a] == cap[a]) continue;
  142. }
  143. else if (head[a] == i)
  144. { /* a = i<-j is a backward arc from s to t */
  145. j = tail[a];
  146. /* if node j has been labelled, skip the arc */
  147. if (link[j] != 0) continue;
  148. /* if the arc does not allow decreasing the flow through
  149. it, skip the arc */
  150. if (x[a] == 0) continue;
  151. }
  152. else
  153. xassert(a != a);
  154. /* label node j and enqueue it */
  155. link[j] = a, list[++pos2] = j;
  156. /* check for breakthrough */
  157. if (j == t) goto brkt;
  158. }
  159. }
  160. /* NONBREAKTHROUGH */
  161. /* no augmenting path exists; current flow is maximal */
  162. /* store minimal cut information, if necessary */
  163. if (cut != NULL)
  164. { for (i = 1; i <= nv; i++)
  165. cut[i] = (char)(link[i] != 0);
  166. }
  167. goto done;
  168. brkt: /* BREAKTHROUGH */
  169. /* walk through arcs of the augmenting path (s, ..., t) found in
  170. the reverse order and determine maximal change of the flow */
  171. delta = 0;
  172. for (j = t; j != s; j = i)
  173. { /* arc a immediately precedes node j in the path */
  174. a = link[j];
  175. if (head[a] == j)
  176. { /* a = i->j is a forward arc of the cycle */
  177. i = tail[a];
  178. /* x[a] may be increased until its upper bound */
  179. temp = cap[a] - x[a];
  180. }
  181. else if (tail[a] == j)
  182. { /* a = i<-j is a backward arc of the cycle */
  183. i = head[a];
  184. /* x[a] may be decreased until its lower bound */
  185. temp = x[a];
  186. }
  187. else
  188. xassert(a != a);
  189. if (delta == 0 || delta > temp) delta = temp;
  190. }
  191. xassert(delta > 0);
  192. /* increase the flow along the path */
  193. for (j = t; j != s; j = i)
  194. { /* arc a immediately precedes node j in the path */
  195. a = link[j];
  196. if (head[a] == j)
  197. { /* a = i->j is a forward arc of the cycle */
  198. i = tail[a];
  199. x[a] += delta;
  200. }
  201. else if (tail[a] == j)
  202. { /* a = i<-j is a backward arc of the cycle */
  203. i = head[a];
  204. x[a] -= delta;
  205. }
  206. else
  207. xassert(a != a);
  208. }
  209. goto loop;
  210. done: /* free working arrays */
  211. xfree(ptr);
  212. xfree(arc);
  213. xfree(link);
  214. xfree(list);
  215. return;
  216. }
  217. /* eof */