aboutsummaryrefslogtreecommitdiffstats
path: root/test/monniaux/glpk-4.65/src/misc/ffalg.c
blob: 4ea2913db8158d2fda4a9d57174a272536de6e37 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
/* ffalg.c (Ford-Fulkerson algorithm) */

/***********************************************************************
*  This code is part of GLPK (GNU Linear Programming Kit).
*
*  Copyright (C) 2009-2013 Andrew Makhorin, Department for Applied
*  Informatics, Moscow Aviation Institute, Moscow, Russia. All rights
*  reserved. E-mail: <mao@gnu.org>.
*
*  GLPK is free software: you can redistribute it and/or modify it
*  under the terms of the GNU General Public License as published by
*  the Free Software Foundation, either version 3 of the License, or
*  (at your option) any later version.
*
*  GLPK is distributed in the hope that it will be useful, but WITHOUT
*  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
*  or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
*  License for more details.
*
*  You should have received a copy of the GNU General Public License
*  along with GLPK. If not, see <http://www.gnu.org/licenses/>.
***********************************************************************/

#include "env.h"
#include "ffalg.h"

/***********************************************************************
*  NAME
*
*  ffalg - Ford-Fulkerson algorithm
*
*  SYNOPSIS
*
*  #include "ffalg.h"
*  void ffalg(int nv, int na, const int tail[], const int head[],
*     int s, int t, const int cap[], int x[], char cut[]);
*
*  DESCRIPTION
*
*  The routine ffalg implements the Ford-Fulkerson algorithm to find a
*  maximal flow in the specified flow network.
*
*  INPUT PARAMETERS
*
*  nv is the number of nodes, nv >= 2.
*
*  na is the number of arcs, na >= 0.
*
*  tail[a], a = 1,...,na, is the index of tail node of arc a.
*
*  head[a], a = 1,...,na, is the index of head node of arc a.
*
*  s is the source node index, 1 <= s <= nv.
*
*  t is the sink node index, 1 <= t <= nv, t != s.
*
*  cap[a], a = 1,...,na, is the capacity of arc a, cap[a] >= 0.
*
*  NOTE: Multiple arcs are allowed, but self-loops are not allowed.
*
*  OUTPUT PARAMETERS
*
*  x[a], a = 1,...,na, is optimal value of the flow through arc a.
*
*  cut[i], i = 1,...,nv, is 1 if node i is labelled, and 0 otherwise.
*  The set of arcs, whose one endpoint is labelled and other is not,
*  defines the minimal cut corresponding to the maximal flow found.
*  If the parameter cut is NULL, the cut information are not stored.
*
*  REFERENCES
*
*  L.R.Ford, Jr., and D.R.Fulkerson, "Flows in Networks," The RAND
*  Corp., Report R-375-PR (August 1962), Chap. I "Static Maximal Flow,"
*  pp.30-33. */

void ffalg(int nv, int na, const int tail[], const int head[],
      int s, int t, const int cap[], int x[], char cut[])
{     int a, delta, i, j, k, pos1, pos2, temp,
         *ptr, *arc, *link, *list;
      /* sanity checks */
      xassert(nv >= 2);
      xassert(na >= 0);
      xassert(1 <= s && s <= nv);
      xassert(1 <= t && t <= nv);
      xassert(s != t);
      for (a = 1; a <= na; a++)
      {  i = tail[a], j = head[a];
         xassert(1 <= i && i <= nv);
         xassert(1 <= j && j <= nv);
         xassert(i != j);
         xassert(cap[a] >= 0);
      }
      /* allocate working arrays */
      ptr = xcalloc(1+nv+1, sizeof(int));
      arc = xcalloc(1+na+na, sizeof(int));
      link = xcalloc(1+nv, sizeof(int));
      list = xcalloc(1+nv, sizeof(int));
      /* ptr[i] := (degree of node i) */
      for (i = 1; i <= nv; i++)
         ptr[i] = 0;
      for (a = 1; a <= na; a++)
      {  ptr[tail[a]]++;
         ptr[head[a]]++;
      }
      /* initialize arc pointers */
      ptr[1]++;
      for (i = 1; i < nv; i++)
         ptr[i+1] += ptr[i];
      ptr[nv+1] = ptr[nv];
      /* build arc lists */
      for (a = 1; a <= na; a++)
      {  arc[--ptr[tail[a]]] = a;
         arc[--ptr[head[a]]] = a;
      }
      xassert(ptr[1] == 1);
      xassert(ptr[nv+1] == na+na+1);
      /* now the indices of arcs incident to node i are stored in
       * locations arc[ptr[i]], arc[ptr[i]+1], ..., arc[ptr[i+1]-1] */
      /* initialize arc flows */
      for (a = 1; a <= na; a++)
         x[a] = 0;
loop: /* main loop starts here */
      /* build augmenting tree rooted at s */
      /* link[i] = 0 means that node i is not labelled yet;
       * link[i] = a means that arc a immediately precedes node i */
      /* initially node s is labelled as the root */
      for (i = 1; i <= nv; i++)
         link[i] = 0;
      link[s] = -1, list[1] = s, pos1 = pos2 = 1;
      /* breadth first search */
      while (pos1 <= pos2)
      {  /* dequeue node i */
         i = list[pos1++];
         /* consider all arcs incident to node i */
         for (k = ptr[i]; k < ptr[i+1]; k++)
         {  a = arc[k];
            if (tail[a] == i)
            {  /* a = i->j is a forward arc from s to t */
               j = head[a];
               /* if node j has been labelled, skip the arc */
               if (link[j] != 0) continue;
               /* if the arc does not allow increasing the flow through
                * it, skip the arc */
               if (x[a] == cap[a]) continue;
            }
            else if (head[a] == i)
            {  /* a = i<-j is a backward arc from s to t */
               j = tail[a];
               /* if node j has been labelled, skip the arc */
               if (link[j] != 0) continue;
               /* if the arc does not allow decreasing the flow through
                * it, skip the arc */
               if (x[a] == 0) continue;
            }
            else
               xassert(a != a);
            /* label node j and enqueue it */
            link[j] = a, list[++pos2] = j;
            /* check for breakthrough */
            if (j == t) goto brkt;
         }
      }
      /* NONBREAKTHROUGH */
      /* no augmenting path exists; current flow is maximal */
      /* store minimal cut information, if necessary */
      if (cut != NULL)
      {  for (i = 1; i <= nv; i++)
            cut[i] = (char)(link[i] != 0);
      }
      goto done;
brkt: /* BREAKTHROUGH */
      /* walk through arcs of the augmenting path (s, ..., t) found in
       * the reverse order and determine maximal change of the flow */
      delta = 0;
      for (j = t; j != s; j = i)
      {  /* arc a immediately precedes node j in the path */
         a = link[j];
         if (head[a] == j)
         {  /* a = i->j is a forward arc of the cycle */
            i = tail[a];
            /* x[a] may be increased until its upper bound */
            temp = cap[a] - x[a];
         }
         else if (tail[a] == j)
         {  /* a = i<-j is a backward arc of the cycle */
            i = head[a];
            /* x[a] may be decreased until its lower bound */
            temp = x[a];
         }
         else
            xassert(a != a);
         if (delta == 0 || delta > temp) delta = temp;
      }
      xassert(delta > 0);
      /* increase the flow along the path */
      for (j = t; j != s; j = i)
      {  /* arc a immediately precedes node j in the path */
         a = link[j];
         if (head[a] == j)
         {  /* a = i->j is a forward arc of the cycle */
            i = tail[a];
            x[a] += delta;
         }
         else if (tail[a] == j)
         {  /* a = i<-j is a backward arc of the cycle */
            i = head[a];
            x[a] -= delta;
         }
         else
            xassert(a != a);
      }
      goto loop;
done: /* free working arrays */
      xfree(ptr);
      xfree(arc);
      xfree(link);
      xfree(list);
      return;
}

/* eof */