You are on page 1of 42

81

Chapter H

NETWORK FLOWS
Annals of Operations Research 13(1988)83-123 83

A COMPUTATIONAL COMPARISON OF THE DINIC AND NETWORK


S I M P L E X M E T H O D S F O R M A X I M U M FLOW*

Donald GOLDFARB
Department of Industrial Engineering and Operations Research, Columbia University,
New York, N Y 10027, USA
and
Michael D. GRIGORIADIS
Department of Computer Seience, Rutgers University,New Brunswiek, NJ 08903, USA

Abstract

We study the implementation of two fundamentally different algorithms for solving


the maximum flow problem: Dinic's method and the network simplex method. For
the former, we present the design of a storage-efficient implementation. For the latter,
we develop a "steepest-edge" pivot selection criterion that is easy to include in an
existing network simplex implementation. We compare the computational efficiency
of these two methods on a personal computer with a set of generated problems of up
to 4 600 nodes and 27 000 arcs.

1. Introduction

The maximum flow problem is defined as follows. Let N = (s, t, V, A, c) denote


a network over a directed graph (V,A) having n = IVI nodes and m = IAI arcs, a
positive-valued capacity function c on the arcs, and two special nodes s, t E V, referred
to as the source and sink, respectively. A (feasible) flow is a nonnegative-valued
function f on the arcs which satisfies the following capacity and flow conservation
constraints:

f(v, w) <~ c(v, w) for all arcs (v, w) E A, (1.I)

I(o, w) -- Y f(w,v) forallnodes v E V - { s , t } , (1.2)


w ~ FA(u) w ~ BA(o)

where FA (u) and BA(u) are the forward and backward adjacency lists of node o,
respectively. The value Ill of flow f is the net flow out of the source (equivalently,
into the sink):

Ill = if[, f ( s , o ) - ~, f(o,s)= Z f(v,t)- ~. f(t,v). (1.3)


o E FA(s) o E BA(s) o ~ BA(t) o E FA(t)

*This research was supported in part by the National Science Foundation under Grant Nos. MCS-
8113503 and DMS-8512277.

© J.C. Baltzer AG, Scientific Publishing Company


84 D. Goldfarb , M.D. Grigoriadis, Dinic and network simplex methods for max flow

A flow f * is maximum if If*l ~ Ill for all flows f. A cut (X, X ) is a partition of V
such that s E X, t E X, X A .~ = 0, and X t.) X = IF. The capacity of a cut (X, X) is
c (X, X) =___Eo~ x, ,, ~ y.__c(v, w), the flow across the cut is f ( X , X ) = Eo ~ x, w ~ ~ f(v, w),
and f ( X , X ) <<. c(X, X). The max-flow min-cut theorem [9] states that the value of
a maximum flow is equal to the capacity of a minimum cut.
The problem was first introduced in 1956 by Ford and Fulkerson, who also
devised an augmenting path method for its solution [9]. Their algorithm runs in
pseudopolynomial time for integer data. The method may converge incorrectly in the
presence of irrational arc capacities, but this is of little concern in practice. Edmonds
and Karp [7], and independently Dinic [6], observed that this algorithm, suitably
modified to push flow only along shortest augmenting paths, runs in strongly poly-
nomial time, assuming O(1) time for addition and comparison of reals. We list the
theoretical improvements that followed [9] in table 1, where Cmax denotes the
maximum arc capacity.
Table 1
Maximum flow algorithms and their complexities

Algorithm Authors Time

A-1 Ford and Fulkerson [9,10] O(rnCmax)

B-1 Edmonds and Karp [7] O(nrn ~)


B-2 Dinic [6] O(n2rn)
B-3 Galil and Naamad [12], Shiloach [30] O(nm (log n) 2)
B-4 Sleator and Tarjan [32,33] O(nm log n)

Karzanov [24], MaUaotra,Pramodh Kumar


C-1 and Maheshwari(MPM) [27], Shiloach and O(n 3)
Vishkin [31], Tarjan [35]
C-2 Cherkasky [2] O(n2m 1/2 )
C-3 Galil [11] O(nS/3rn 2#)
C-4 Goldbergand Tarjan [13] O(nm log(n2/rn))

Edmonds and Karp's modification (B-l)identifies shortest augmenting paths


one at a time. Dinic's modification (B-2) creates an O(n) sequence of structured
acyclic graphs (layered networks) and solves the maximum flow problem for each one.
This is equivalent to identifying all shortest augmenting paths (of the same length)
with respect to the current flow in the network and augmenting the flow on all of
them simultaneously. Since in the worst case each such subproblem may saturate only
one arc, the running time of this class of algorithms can only be improved by de-
creasing the time per arc saturation. This is done by algorithms B-3 and B-4 for sparse
graphs, but only at the expense of having to deal with more complicated data structures.
Karzanov [24] introduced the concept of a preflow and proposed a method
that in the worst case saturates one node at a time. A preflow is a flow for which
D. Goldfarb , M.D. Grigoriadis, Dinic and network simplex methods for max flow 85

the amount entering a node may exceed the amount leaving it, i.e. (1.2) holds with
"<<." instead of "= ". Algorithms C- 1,2 and 3 use prefiows. Those in C- 1 are the fastest
algorithms for dense graphs, the simplest of which appears to be Tarjan's wave method
[35]. The MPM [27] rendition of Karzanov's idea of preflows is very popular, but the
performance of its practical implementations is not entirely satisfactory. Tarjan
observes that the method tends to route flow through "narrow bottlenecks", and it
thus causes too many augmentations [34]. Algorithms C-2 and 3 use substantially
more complicated data structures to decrease the time per node saturation for sparse
graphs.
For a more detailed discussion of maximum flow algorithms, the reader is
referred to the books by Ford and Fulkerson [10], Lawler [26], Even [8],
Papadimitriou and Steiglitz [29], and Tarjan [34].
Algorithms B-1 through B-4 and C-1 through C-3 augment the flow along
shortest augmenting paths. Goldberg and Tarjan's recent algorithm (C-4) [13] uses
Karzanov's idea of a preflow, but it augments the flow along paths that are only
"estimated" to be the shortest. A straightforward implementation of this algorithm
is much simpler than any of those in C-l, and it runs in O(n 3) time provided that
nodes with excess flow are scanned in first-in first-out order. A more complicated
variant of it uses the Sleator-Tarjan dynamic tree data structure [33] and runs in
O(nmlog(n2/m)) time. This bound subsumes all others in table 1, uniformly for all
densities. Computational experimentation is needed to assess whether this will also
be the case in practice.
Despite its O(n2m)running time, experimental evidence [5,14,17] suggests
that even simple implementations of Dinic's method are more efficient than most
other methods listed in table 1. There are two reasons for this. First, a large constant
multiplier to the time complexity is created due to the complicated data structures
used by the other methods, and second, the instances that can arise in practice today
are not sufficiently large to exhibit the asymptotic behavior of these algorithms.
Therefore, a well-implemented, tested and documented Dinic code can be a valuable
practical tool, in addition to serving experimental and educational needs. In sects. 2
and 3, we review this method and design a storage-efficient implementation that.is
well suited for solving large sparse instances on small computers.
Next, we compare the Dinic method with the network simplex method,
rather than supplement the computational comparisons mentioned earlier. In sect. 4,
we review the network simplex method and develop a partial-pricing variant of the
"steepest-edge" criterion for the maximum flow problem, as originally implemented
in the code RNET [18,20]. We state the data structures used by this code, and we
show how this strategy can be implemented in other existing network simplex codes.
In sect. 5, we present our computational results for a set of randomly generated,
sparse and structured network flow problems, obtained on a small computer. The
network simplex method requires substantially less storage than Dinic's method
and runs surprisingly faster for these problems. However, our results indicate that
86 D. Goldfarb, M.D. Grigoriadis, Dinic and network simplex methods for max flow

Dinic's method will eventually overtake the network simplex method for problems
that are too large to be stored in the main memory of today's personal computers.

2. Implementation of the Dinic method

The Edmonds-Karp [7] and Dinic [6] improvements of the Ford-Fulkerson


augmenting path method differ only in the manner in which they select shortest
augmenting s - t paths*. Both methods start with the null flow, and perform flow
augmentations in groups, called stages. Each stage k starts with an existing flow fk
in N, and pushes as much additional flow as possible along all shortest flow augment-
ing paths of the same length, say, dk(t ). Let this additional flow be 8k. The new flow
fk + 8k, called maximal or blocking flow, clearly saturates all flow augmenting paths
of length dk(t ) in N. The next stage starts with fk÷ 1 = fk + ~k, and augments this
flow along shortest augmenting s- t paths of some length d k + 1(t), and so on. It is
easy to show that dk ÷ 1(t) > dg(t), and that there are at most n - 1 stages (see e.g.
[34], p. 104). Edmonds and Karp use breadth first search (BFS) from the source to
find each flow augmenting path in O(m) time. Since there are at most m such paths
per stage, their algorithm runs in O(m:) time per stage and O(nm 2) time overall.
Dinic introduces an O(m)-time preprocessing step in order to reduce the time
per stage to O(nm); the resulting algorithm runs in O(n2m) time. The preprocessing
step consists of a BFS from the source in the residual network R k = (s, t, V, A k, ck )
with respect to the flow fk" This network has an arc (o, w ) E Ak of capacity
ck(o, W) = c(v, w) -- fk(o, w) if (U, W) E A and fk(o, W) < c(u, w) or if (W, O) E A and
fk(w, v) > 0. The BFS defines a distance labeling dk on the nodes (with dg(s) = 0),
which in turn defines the layered network L k = (s, t, V, A L, ~ ) in O(m) time. L k has
an arc (o, w) E AL C Ag of capacity cL(v, w) = Ck(O, w) if and only if dk(W ) = dk(v ) + 1.
The set of s-t paths in L k contains the set of shortest augmenting paths in R k. Thus, a
maximum flow in L k is maximal for N. We will discuss how to compute this flow in
later paragraphs.
We wish to arrange BFS so that it defines L k implicitly by the distance labeling
d or, preferably, by two Boolean labelings L A r c F and LArcB on the arcs, conformal
to the forward and backward adjacency lists FA(o) and BA(o), v = 1 . . . . , n. The
following procedure defines LArcF(o, w) = open if w E FA(v), d(w) = d ( o ) + 1 and
f(o, w) < c(o, w), and closed otherwise; LArcB(w, v) = open if w E BA(v),
d(w) = d(v) + 1 and f(o, w ) > 0, and closed otherwise:

*The length o f a flow a u g m e n t i n g path is the n u m b e r o f arcs it contains. F o r each successive pair
o f nodes o and w o n such a p a t h , there is either a forward arc (v, w) with f(o, w) < c(v, w) or a
backward arc with f(w, u) > 0.
D. Goldfarb, M.D. Grigoriadis,Dinic and network simplex methods for max flow 87

Procedure DNBFS(N, f ) return(d, LArcF, LArcB)


Lists: d, Queue, LArcF, LArcB;
LArcF := LArcB := open, d := oo, d(s) := 0, Queue := {s};
while Queue --/=0 do:
v := POP(Queue);
for w E F A ( o ) n o t visited do:
if d(w) <~ d(o) or f(o, w) = c(o, w) then LArcF(v, w) := closed
else if d(w) ~ d(v) + 1 then APPEND(w, u, Queue) endif;
endif;
endfor;
for w E BA(v) not visited do:
if d(w) <~ d(u) or f(w, v) > 0 then LArcB(w, u) := closed;
else if d(w) 4: d(o) + 1 then APPEND(w, o, Queue) endif;
endif;
endfor;
endwhile;
end DNBFS;

The procedure uses the original data of N, and computes the augmenting
capacity of each arc of R k when it is needed. It visits each node u E V - {s } in first-in
first-out order, and (implicitly) installs in L k forward arcs (u, w) with positive augment-
ing capacity leading to new nodes w, and backward arcs (w, o) with positive flow
from new nodes w. Queue is maintained by the functions POP(Queue) and
APPEND (w, o, Queue). Each invocation of these takes O (1)time. The former removes
the node found at the head of the queue and returns it as node o. The latter appends w,
the neighbor of o, at the tail of the queue and assigns d(w) := d(u) + 1. Thus, the
distance of a node from the source is defined only once, whenever the node is first
reached in BFS order; nodes that have not been reached carry the label "oo" The
query "o E Queue?" is thus answered in O(1) time and no extra space is required.
We now return to the task of computing a maximum flow in L k. Dinic suggests
a procedure that works for an acyclic network Lt¢: use DFS to fred an s- t path in
Lk, augment the flow along this path, delete every saturated arc on the path, and
repeat by starting from the end node of the deleted arc closest to the source. If DFS
reaches a node w along an arc (v, w) and w has no outgoing arcs, then w and all arcs
into w are deleted and DFS resumes from o. Procedure DNDFS implements this
approach.
88 D. GoIdfarb, M.D. Grigoriadis, Dinic and network simplex methods for max flow

Procedure DNDFS(N, f, LArcF, LArcB) return(f)


Lists : P(paren t), D (direction), F( flow increment);
F:=O,o:=s;
repeat:
if 3 w E FA(o) such that LArcF(v, w) = open then
forward: Let w be such a neighbor o f o
F(w) := min{F(o), c(o, w ) - f ( o , w));
P(w), D ( w ) : = v, forward; u := w;
push: if o = t then o, F : = DNPUSH(F) endif;
else
if 3 w E BA(o) such that LArcB(w, o) = open then
backward: Let w be such a neighbor of u
F(w) := min IF(o), f(w, o) ) ;
P(w), D(w) := u, backward, o := w;
else
back-up: if o :~ s then
w := v, v := P(v);
if D(w) = forward then LArcF(v, w) := closed
else LArcB(w, v ) : = closed endif;
endif;
endif;
endif;
until v = s and there are no open arcs from s;
end DNDFS;

The distance labeling d is not used by DNDFS since the arcs of L k were
previously selected and recorded b y DNBFS in LarcF and LarcB. DNDFS records
a flow augmenting path by the pair of node-labelings P(parent) and D(direction),
and uses a node-labeling F to record flow increments. Space can be saved by storing
the lists LarcF, LarcB and D as the sign bits of the otherwise positive-valued lists
FA, BA and P, respectively. The actual flow augmentation is done by DNPUSH
as follows.

Procedure DNPUSH(P, D, F) return(k, F )


Lists: P(parent), D (direction), F( flow increment);
w, ~ := t, F(t);
repeat:
u := .P(w), F(w) := F(w) - 6 ;
if D (w) = backward then
f(w, v) : = f(w, o)- 5 ;
iff(w, o) = 0 then LArcB(o, w):= closed, k : = o endif;
D. Goldfarb, M.D. Grigoriadis,Dinic and network simplex methods for max flow 89

else
f(o,w):=f(o,w)+
if f(v, w) = c(o, w) then LArcF(v, w) := closed, k := v endif;
endif;
until o = s;
end DNPUSH;

Clearly, DNPUSH takes O(n) time. Does DNDFS run in O(nm) time? We
shall assume that the repeat loop of DNDFS scans FA (u) backward, and that a pointer
to the first arc from the end of FA(o) that is open is maintained. The statement
"if 3 w E FA(o) . . ." searches FA(v) backward, starting from the arc given by this
pointer. The search continues until either an open arc is found (in which case the
pointer is moved up to this arc), or none is found (in which case the pointer is set
to null, indicating that there are no open arcs from v). The search of BA(o) is imple-
mented in the same manner. This works because no arc can be opened once it is
closed; the lists FA(v) and BA(o) are reviewed only once for each o E V. Further-
more, each sequence of at most n - 1 forward or backward steps results in a push or
in a backup step. In either case, an arc is closed. Consequently, the time complexity
is O(nm) for DNDFS, and O(n2m) for the overall procedure DNSUB defined
below. The pointers require 2m space.

Procedure D N S UB (N )
f:=0;
repeat
d, LArcF, LArcB := DNBFS(N, f);
if d(t) < oo then f : = DNDFS(d, LarcF, LArcB), endif;
until d ( t ) = oo ;
report d,/'as d*, f*;
end DNS UB;

The distance labeling produced by the last invocation of DNBFS defines a


minimum cut (X, .~) of capacity tf*l by X = {v E V t d*(v) < oo} and so that Xis
of minimum size among all cuts of capacity If * l .
We have designed DNSUB with the goal of minimizing its space complexity
while retaining its worst-case running time of O(n2m). Instead of storing the L k
implicitly as we have done, one may create them explicitly in DNBFS. The space
would be nearly doubled, but as a result, an appropriately coded version of DNDFS
would run faster than ours, particularly for dense networks. The value of this advantage
is questionable: Dinic's method is not appropriate for dense problems and the actual
number of stages is usually very smaU in practice (see sect. 4), although one can
construct worst-case instances that do require n - 1 stages (Zadeh [36] ).
90 D. Goldfarb, M.D. Grigoriadis, Dinic and network simplex methods for max flow

3. Data structures

We have assumed that the network N is given as an ordered list of n (forward)


adjacency lists FA(v), v E V = {I . . . . , n}, and a capacity function c on the
arcs, in the same order. These data are usually arranged in an (n + 1)-long pointer
list F A P T R , an m-long list of nodes F A D J, and an m-long list of capacities C.
Thus, for each v E V, the adjacency FA(v) is the list of nodes F A D J ( F A P T R ( v ) )
, . . . , F A D J ( F A P T R (o + 1) - I), and C ( F A P T R (v)) . . . . . C ( F A P T R (v + 1) - 1)
are the capacities of arcs (o, w), w E FA(v). We assume that F A P T R ( n + 1) = m + 1.
This is the input data structure required by our implementation of subroutine
DNSUB.
If N is given in arbitrarily ordered arc lists, e.g. the m-long lists From, To and
Cap, the above input data structure can be easily created in O(m) time: Compute
the out-degrees (in one pass of the list From), define F A P T R (in one pass of the
out-degrees), and define F A D J and C (in another pass of the lists From, To and
Cap). A subroutine called D N F W D performs this particular task in our code.
We also require the following internal data structures. We use an m-long
list F to record the arc flows, in forward adjacency order. We store the backward
adjacency lists BA(o), o = 1 , . . . , n in an m-long list of nodes B A D J, using an (n + 1)-
long pointer list B A P T R . We set B A P T R ( n + 1) = m + 1. Thus, for each o E V,
BA(v) is the list of nodes B A D J ( B A P T R ( o ) ) . . . . . B A D J ( B A P T R ( o + 1) - t).
Rather than replicating the data of the arcs {(v, w ) I w E B A ( v ) } for o E V, we
access them indirectly through the m-long backward-to-forward mapping list
BTOF, i.e. C(BTOF(BAPTR(u)) . . . . . C ( B T O F ( B A P T R ( o + 1) - 1), and
F(BTOF(BAPTR(o)) ..... F ( B T O F ( B A P T R ( v + 1) - 1) are the capacities and
flows for these arcs.
The lists B A P T R , B A D J and B TOF can be constructed from the lists F A P T R
and F A D J in O(m) time: Compute the in-degrees (in one pass of F A D J, aided by
F A P T R ) , define B A P T R (in one pass of the in-degrees), and then define B A D J
and B T O F (in another pass of F A D J and of F A P T R ) . In our implementation, we
do this at the beginning of subroutine DNSUB. A mapping function FTOB, the
inverse of BTOF, can be similarly defined, but we do not need it for our purposes
here.
These lists require 5m + 3n space. In addition, we need 2m + 2n space for the
two pointer lists used in DFS, the distance and flow-increment labelings (which can
share the same space), and the list Queue. This brings the total space required by
D N S U B to 7m + 5n. We will refine this estimate in sect. 5.

4. A steepest-edge network simplex method

The maximum flow problem can be formulated as a minimum-cost network


flow problem in two ways. In the first, a circulation arc (t, s) with unit cost p (t, s) = - 1,
D. Goldfarb, M.D. Grigoriadis, Dinic and network simplex methods for max flow 91

and in the second, a bypass arc (s, t) with unit cost p (s, t) = 1 is appended to the
network. The extra arc has infinite capacity, and all original arcs have zero unit cost.
The so-augmented problem is a linear program of the form:

lf*l = min { l f l = p f l X f = O , 0 <~f<~ c}, (4.1)


:

where the matrix A is the n x m node-arc incidence matrix of rank n - 1 and n < m
(m includes the appended arc). For each arc ] = (v, w) there is a column ai of the
matrix A having aoi = 1 and a w i = - 1 as its only nonzero elements. The m-vectors
c, p, and f are the capacities, costs and flows, respectively, and the null flow is feasible.
We shall use the first formulation, the second one is analogous.
The correspondence between the algebra and geometry of this linear program
and simple graph-theoretic concepts provides the basis for implementing the simplex
method efficiently. These properties and their use in devising an implementation are
discussed in [18]. Here, we state some of them, specifically for the maximum flow
problem, and we derive a steepest-edge variant o f the network simplex method.
The polytope P = { l i A r = O, 0 <~ f <~ c} is the set of flows. The flow value
Ifl is the flow f(t, s) in the circulation arc. A basis B for the matrix ( - e s, A) is a set
of arcs that forms a (spanning) basis tree T of N, rooted at the source. The column
- e s represents an artificial arc (having zero cost and infinite capacity) from a fictitious

:C~,,,,) =
c(~,,,)
root
s~ (, ,~(~,) = 0
//"* f(~, ~) = 0

ce./'/'/--
e~
origin
p(,,,) = -1, oCt, s) = ~o

f ( t , s) - I/I

Fig. 1. Basis tree T a n d candidate arcs.

node, called the origin, to the source. A vertex of P [a basic feasible solution of (4.1)]
is a flow such that arcs not in T are either saturated or have zero flow. Every feasible
tree T contains the artificial arc at zero flow, and the circulation arc. Removing the
circulation arc decomposes T into two subtrees Ts and Tt, rooted at s and t,
respectively (see fig. 1). We denote the set of nodes of Ts by X and those of Tt by X.
92 D. Goldfarb, M.D. Grigoriadis, Dinic and network simplex methods for max flow

Now consider a path VOP(o) in T from some node v to the origin node. The
column % of B-1, corresponding to node u, is an encoding of this path as follows:

1 if arc i E VOP(v) in Tis directed toward the origin

~iv =
- 1 if arc i E VOP(o) in T is directed away from the origin

0 otherwise.

We see that the value of the Lagrange multiplier u (o) for the vth row of A f = 0 is the
cost of the path VOP(v), which is zero if v E X and - 1 otherwise. The vector (u, rr),
where lr(v, w) = u(v) - u(w) is the value of the dual variable for the constraint
f(v, w) <~ c (v, w), is feasible for the dual of problem (4.1). The vector u records the cut
(X, .~) whose capacity is the sum of the capacities of nontree arcs directed from nodes
of X to nodes of X. By duality, such a cut is of minimum capacity and the flow in the
circulation arc is maximum if and only if these two values are equal. (This also follows
from the max-flow min-cut theorem [9] .) Otherwise, there is at least one nontree arc
(v, w), o E X, w ~ X or an arc (w, v) w E X', v E X of residual capacity c(v, w). We refer
to the set of such arcs as candidate arcs (see fig. 1). The (unique) s-t path in T that
a candidate arc defines may or may not be flow augmenting. In the next few para-
graphs, we will discuss the manner in which one candidate arc is selected for a possible
augmentation, a task that is usually referred to as pricing.
Let B be a basis and, for notational convenience, let ( - e s, A) = (BIR)
= ( - e s, al . . . . . an- i lan . . . . . am) be a partition of A into n - 1 basic and m - n + 1
nonbasic columns o f A. Similarly, let the vectors p, c, and f be conformally parti-
tioned to (PB, PR ), ( CB, CR) and ( fB, fR ), respectively, and let Yl = B-l ai = % - aw"
This n-vector is an encoding of the path FAP(w, v), from w to v in T, where

-1 if tree arc i E FAP(w, v) has the same direction as FAP(w, v)


Y//=
1 if tree arc i E FAP(w, v) has direction opposite to FAP(w, o).

Now consider a general linear program of the form (4.1). Let B be a feasible non-
degenerate basis for A, and let (fB, fR) be the corresponding vertex of P. For each
j E { n . . . . . m}, the m-vector ( - y j , ej), where ei denotes the ( j - n + 1)-th column
of I m - n . 1, is a direction along an edge of the polytope P from its vertex at (fB, fR)
to an adjacent vertex at (fB, f n ) + * ( - Y p ei) for some ~. > 0 (see e.g. Best and
Ritter [1] ). In the original simplex method, the direction taken is the one that corre-
sponds to the largest reduced cost (-Yk, ek)P, found by

zk =inonbasicmin{zj I z. = S l ( p l - p B y l ) < 01, (4.2)


D. Goldfarb, M.D. Grigoriadis, Dinic and network simplex methods for max flow 93

where S/ = - 1 i f / i s nonbasic at its upper bound and Sj = 1 otherwise. The vertex


(fB, fR) is optimal if and only if { } = ~ in (4.2). Geometrically, this operation selects
an edge direction whose orthogonal projection on - p is as large as possible among all
edges of P that emanate from (fB, fR) and that make acute angles* with -p. We see
that (4.2) does not generally produce a "most downhill" direction with respect to
p since ( - y / , ej) has not been normalized by its length. Nevertheless, variants of
this criterion have been found to perform well in practice and have been in use in
large-scale LP codes since the early 1960's.
For the maximum flow problem, the selection (4.2) takes O ( I ) time per non-
tree arc but it is inherently arbitrary: all candidate arcs have the identical reduced
cost zj = Sj(u(w) - u(v)) = -1, j = (v, w) E A \ T . There are two other drawbacks.
First, such an arbitrary selection will most likely result in a trivial augmentation.
Second, even if the selected arc leads to an augmentation, there is no guarantee that
the corresponding augmenting path will be short. The longer the path, the greater
the chance that the flow augmentation will be small and that there will be ties (which
turn into degeneracies in the next iteration). Either one of these would tend to in-
crease the total number of iterations.
In the steepest-edge simplex method, we select k so that the neighboring
vertex of P, obtained by a nondegenerate pivot, is along an edge of P that is most
downhill with respect to the gradient of the objective function. That is, we want to
move from (fB, fR) along a direction (-Yk, etc) that makes the smallest acute angle
0k with - p or, equivalently, so that cos0i > 0 is maximized over L Since p is constant,
it suffices to normalize the vector (-Yi' ei) by its length, and to use in (4.2) the
normalized reduced cost:

zj = S j ( p j - pB yj)/(1 + yj yj)ll2. (4.3)

For general linear programs, it has been empirically found that this criterion, or an
appropriate relaxation of it, produces a larger portion of nondegenerate pivots and
less iterations, overall, than pricing strategies that use pure reduced costs [16,21,25,37].
However, the amount of work to achieve this is prohibitive unless updating formulae,
such as those proposed by Goldfarb and Reid [16], are used to maintain the values
of the squares of the norms of the edge direction vectors at each iteration.
For the special case of minimum-cost network flow problems, the product
yj y~ for each j = (o, w) E A \ T is easier to compute since it is precisely t FAP(w, o)1.
However, the O(n) time required per nontree arc is still too expensive in relation to
the O(1) time for computing its reduced cost and to the O(n) time for a pivot.
Experience indicates that such is the case even when F A P lengths are updated rather
than recomputed at each iteration (see e.g. [17,18,28] ). We note that the cycle identi-
fied by (4.2) with the z~ defined by (4.3) is a minimum cycle mean (Karp [23] ) over
all elementary cycles corresponding to the current basis tree.
*Recall that cos 0 = ab/( [a I" [b I) for any two nonzero vectors a, b E IRm.
94 D. Goldfarb, M.D. Gtqgoriadis,Dinic and network simplex methods for max flow

The situation for maximum flow problems is markedly different. A pivot is


less expensive by a constant factor and IFAP(w, v)l = Depth(u) + Depth(w) is
computed in O(1) time per nontree arc. Depth is a labeling on the nodes that records
the length o f the path between the root s and any other node in T. Depth is routinely
maintained by most network simplex codes for other purposes. The criterion ( 4 . 2 ) -
(4.3) simply selects a nor~tree arc that creates a shortest s-t path from the set of
candidate arcs having (positive) augmenting capacity (the type depicted in fig. 1)
as follows:

[FAP(wg,uk)[ = rnin tDepth(v)+ Depth(w) I u(v)-u(w)= S], j - (v, w)}.


(v, w) ~ A\ r (4.4)

The work necessary to make this choice is still too expensive in relation to that required
to implement the pivot, i.e. determining the arc to leave the tree, reordering the basis
tree, and updating the values of the flows and dual variables. The effort is substantially
reduced by employing partial pricing strategies. Here, we use the strategy implemented
by the MAXFLO option of the code RNET: scan a small, arbitrarily-selected
fraction o f the nontree arcs with positive reduced cost, and among these, select an
arc (uk, wk) with the shortest FAP(w k, uk) to enter the basis tree [18,20]. Much
more elaborate strategies are possible, but this ad hoc procedure provides a basis for
a fair comparison.
A nondegenerate pivot step corresponds to a flow augmentation along the s- t
path determined by the entering arc (ug, wk) and its FAP(wk, uk). This path is flow
augmenting if and only if both subpaths, s to uk in Ts and wk to t in Tt, are flow
augmenting. An augmentation results in at least one of the following cases: (1) One
or more forward arcs become saturated, (2) one or more backward arcs have their
flows reduced to zero, or (3) the entering arc becomes saturated if it is a forward arc,
or its saturating flow is reduced to zero if it is a backward arc. If arc capacities are
integer, it follows that there can be at most If*l ~< nCmax augmentations or non-
degenerate pivots.
Unfortunately, even when appropriate anticycling rules are used, there can be
at most n - 2 consecutive degenerate pivots preceding each nondegenerate pivot step.
Such is the case for the method of keeping flows basic, proposed by Johnson [22],
and for the network simplex method with strong feasibility rules studied by Cunning-
ham [3 ]. For the maximum flow problem, a basis tree is strongly feasible if all arcs
in Ts [ Tt] having zero flow are directed away from the source [sink], and if saturated
arcs in Ts [Tt] are directed toward the source [sink], respectively. Each basis tree can
be made to have this property by initially constructing a strongly feasible tree for
f = 0, and by resolving ties in the choice of the leaving arc at each subsequent iteration
in favor of the one closest to the source. In the worst case, such a network simplex
method requires O(n2Cmax) pivots and runs in O(n2mCmax) time for integer data,
regardless of the pricing strategy used to select the entering arc.
D. Goldfarb, M.D. Grigoriadis, Dinic and network simplex methods for max flow 95

For the purpose of the present experiments, we used the M A X F L O option


of R N E T "as is", except that we started with a basis tree for f = 0 obtained via
B F S from the source, and we removed the Cost array and all references to it from
the code. The code R N E T was designed to solve minimum-cost network flow prob-
lems. It does not employ strong feasibility rules because earlier experimentation had
shown that they do not generally result in fewer iterations as one might expect (see
e.g. [4], p. 206), and because cycling seldom occurs in practice. There is also some
evidence that the average time per pivot may be slightly higher with strong feasibility
rules rather than without them [18]. As a derivative o f R N E T , R N E T F also does
not use these rules. There is a need to establish whether strong feasibility of basis
trees, combined with the proposed steepest-edge pricing strategy, may be computa-
tionally advantageous for the maximum flow problem. We leave this to a future study.
The input data are given to R N E T F in the three m-long lists From, To and
Cap. No order is assumed. Internal data structures consist of the m-long Boolean
list S (arc status: nontree saturated or otherwise), and the eight n-long lists: Parent,
Depth (from root), Preorder Sequence, Inverse Preorder Sequence, Arc Index (of
tree arc joining F(v) and o), and F (tree arc direction; F(v) = 1 if Arc Index(o) has
flow direction from F(o) to v, and - 1 otherwise), Flow (in basic tree arcs), and U
(dual variables). The lists S and r' are stored as the sign bits of the otherwise positive-
valued lists Cap and Parent, respectively. These are described in more detail in [18].
The total space for R N E T F is 3m for input data, and 7m for internal arrays (also
see sect. 5). This may be reduced to 2m + 8n if the input data structure adopted for
D N S U B were to be used for R N E T F .

5. C o m p u t a t i o n a l results

We have seen that all-integer implementations of D N S U B and R N E T F


require 7m + 5n and 3m + 7n (or 2m + 8n) words of space, respectively. The actual
array storage, and for some computers the running time as well, depends upon limita-
tions placed upon the size of the instances to be solved, i.e. n, m and Cmax. Let bn
and bm be 2 or 4, depending upon whether n and m are less than 2 ss or 231, re-
spectively. Similarly, let bc be 2 or 4, depending upon whether Cmax is less than 2 Is
or less than 231 , respectively. Then the total space used by these implementations is:

sD = ( b + 4b m + bnc)n + ( 2 b + bc + bnc + bmc)m bytes for D N S U B ,

sR = ( 4 b n + b m + b c + 2) n + (2bn + b ) m bytes for R N E T F ,

where bnc = max{bn, bc} and brnc = max{bin, be}. For any feasible set of sizes and
arrangements, 1.1 ~< So/SR ~< 2. The space sR can be reduced further by at least
bn(m - n) + 2n bytes by dropping some incidental functions of R N E T , by using
96 D. Goldfarb, M.D. Grigoriadis, Dinic and n e t w o r k simplex m e t h o d s f o r m a x f l o w

,-- t
(si,,k)
b ufraFnes~
n o d e s / f r a?m~e. .) ~ ~ _ _ _ ."

. . . /.."

.'"

at
nodl~

ares
a 2 b a c k w a r d ares
°/*
¢g
(source)
n ode8

Fig. 2. S t r u c t u r e o f RMFGEN-generated p r o b l e m s .

the adjacency list input structure of D N S U B , by combining the Boolean array U with
another positive-valued list, and by minor reprogramming. We have not undertaken
these tasks.
For the testing reported in this paper,we generated both D N S U B and R N E T F
for b n = 2, b m = b c = 4, and we diminished both programs to the (smaller) sizes
dictated by D N S U B , i.e. 4700 nodes and 27 000 arcs. For this case, SD/sR ~ 1.78.
The selection bm = 2 would have been sufficient for our test problems; we used
b m = 4 in order to better assess the performance of the codes for realistic situations
where m might exceed 32 767.
We used the program R M F G E N [19] to generate our test cases. This program
accepts values for four parameters a, b, c~ and c 2 ( > cl), and creates networks with
the structure depicted in fig. 2. The graph can be visualized as having b frames, each
of which has a 2 nodes at the lattice points of the square of side a. Each frame is a
symmetric subgraph: each node in a frame is connected to its neighbors on the lattice
by a pair of oppositely directed arcs. There are 4 a b ( a - 1 ) s u c h "in-frame" arcs with
capacity equal to c2a 2 . There are a 2 arcs from the nodes of one frame to a pseudo-
random permutation of the nodes of the next (one to one), with pseudorandom
integer capacities in the range [c~, c2 ]. Similarly, there are a 2 arcs from a frame to
its preceding one. This portion of the graph is not symmetric. The generated graphs
have n = a2b nodes and m = 6a2b - 4ab - 2a 2 arcs, source node s = 1 (at the lower
left of the first frame in fig. 2 ) and sink node t = a2b (at the upper right of
the last frame in fig. 2). Nodes have in- and out-degrees of 3, 4, 5 or 6. Since
m / n = 6 - 4/a - 2/b ~ 6 for sufficiently large a and b, these form a relatively dense
D. Goldfarb, M.D. Grigoriadis, Dinic and network simplex methods for max ]'low 97

set of *'sparse" instances. Their solution involves flow augmenting paths whose length
is at least b. The minimum cut is located between two consecutive frames and it
contains a 2 arcs.
We generated ten replications of each problem defined by the R M F G E N
parameters a and b listed in table 2, with different seeds for the pseudorandom
number generator but with [cl, c2 ] = [ 1, 1000] for all of them. We executed R N E T F
with parameter values Po = 1.0, ff = 1.5 and with a pricing sample size ofmin{35, m}
arcs [18]. The running times we report are averages of ten replications per problem,
and do not include I/O operations for either code.
We used an IBM Personal Computer AT (Intel 80826 processor operating at
8 MHz, and 640 K bytes of 150 ns main memory) to develop D N S U B and to run
our experiments. Both codes were compiled with the two-pass Microsoft FORTRAN
77 V3.31 compiler, linked with the Microsoft 8086 Object Linker V3.04, and executed
under the IBM Personal Computer DOS V3.00 operating system. The blank cells in
table 2 correspond to problems that cannot be stored in the 640 K main memory of
an IBM/AT computer, e.g. the cell at the lower right would be a problem of 147 456
nodes and 867 841 arcs, and would require 17.1 Mbytes of array storage for D N S U B
and 9.6 Mbytes for R N E T F .
At first sight, the results of table 2 indicate that R N E T F is more efficient
than D N S U B : it solves all problems in 5 120 seconds versus 6 703 for D N S U B , and
it requires less space than DNSUB. Furthermore, our problem set is sufficiently
diverse, it has a large enough range of arc capacities and a large Cmax . We see, however,
that D N S U B runs appreciably faster for "cell" (a, b) = (12, 32) of table 2, and
slightly faster for cells (16, 16) and (8, 32). In fig. 3, we plot tn/t D, the ratio of
the average D N S U B to R N E T F running times, as a function of n. This plot shows
that R N E T F ' s advantage in running times diminishes with increasing problem size.
The question naturally arises whether this trend might accelerate for larger
problems. This can occur to the extent of a constant factor for an R N E T F equipped
with strong feasibility rules, for integer data and for fixed Cmax. For our specific
implementations, the question can be addressed with additional large-scale computa-
tional testing, but our results suggest that such an acceleration is possible for some
problems. If it occurs, it would be due to an excessive number of pivots or time per
pivot, or both. Let tg/t D = (nR/nD)(rR/rD) , where nR, rR and nD, r D denote the
number of R N E T F pivots and time per pivot, and the number of D N S U B augmenta-
tions and time per augmentation, respectively. We have plotted these ratios in figs. 4
and 5. We observe from fig. 4 that ng/n D can be approximated by a nondecreasing
concave function; fig. 5 shows that the ratio rR/r D deteriorates for large n. Thus, the
relative efficiency of R N E T F diminishes due to the time it spends to select and
implement a pivot, rather than a disproportionate increase in the number of pivots.
Although a better pricing strategy than the ad hoc procedure used by R N E T F could
lead to some improvements, these results indicate that the drop in efficiency is mainly
Table 2 "4)
CO
Test results with set of RMFGENproblems

average
Side of square Number of frames, b (see notes in caption) times
DNSU8
~ame, a 2 4 8 16 32 64 RNETF

2 8; 24 16: 56 32; 120 64; 248 128; 504 256: 1016


4 4 0.0 5 8 0.1 6 13 0.2 9 21 0.6 11 31 1.4 18 54 4.7 7.0
2 0.0 7 0~ 16 0.1 26 0.3 37 0A 73 1.9 2.9

3 18; 66 36: 150 72; 318 144: 654 288; 1326 576; 2670
6 9 0.1 10 22 0A 11 35 0.9 15 59 2.4 19 96 6.1 31 175 19.7 29.6
8 0.1 32 0.2 65 0.5 97 1.1 158 2.9 308 9.8 14.6

4 32; 128 64: 288 128; 6O8 256; 1248 512: 2528 1024: 5088
9 17 0.3 14 41 1.0 17 73 2.4 25 141 7.3 35 254 21.0 57 481 68.3 100.3
21 0.1 80 0.4 169 1.3 293 3.9 500 11.7 950 40.8 58.2

72; 312 144; 696 288; 1464 576; 3000 1152; 6072 2304; 12216
16 45 1.2 22 97 3~ 31 203 10~ 40 379 27.5 60 733 83.9 83 1300 230.2 357,0 t~
69 0.3 233 1.4 495 5.3 884 17.1 1682 61.6 2885 189.5 275.2

8 128; 576 256; 1280 512; 2688 1024; 5504 2048; 11136 4096; 22400
22 86 3.1 35 190 10.4 40 371 24.8 50 709 62.5 78 1389 196.2 120 2631 604.8 901.8
144 0.7 494 3.8 987 13.8 1812 46.7 3416 170.1 6353 607.3 842.4

12 288; 1344 576; 2976 1152; 6240 2304; 12768 4608; 25824
36 216 11A 53 442 37.0 68 927 98.6 91 1836 261.3 121 3534 702~ llll.l
432 2.7 1265 14.3 2657 60.5 5158 231.9 9520 848.1 1157.5

16 512; 2432 1024; 5376 2048; 11264 4096; 23040


50 432 29.7 68 807 86.3 91 1688 235.7 121 3431 634.6 986.3
797 6.8 2292 35.2 5066 161.8 9994 680.0 883.8

24 1152; 5568 2304:12288 4608; 25728


80 1025 108.2 106 1929 304.6 138 3974 816.2 1229.0
2063 29.1 5663 152.0 12669 750.7 931.8

32 2048: 9984 4096; 22016


108 1905 265.3 149 3573 775.3 1040.6
4055 86.5 10827 453.1 539.6

48 4608; 22656
168 4530 940.0 940.0
10273 414.4 414.4

Z average times
DNSUB: 1359.5 1218.7 1189,4 996.2 I011,2 927.7 6702.7
RNETF: 540.7 660,4 994.0 981.0 I095.0 849.3 5120,4

Numbers in cells are: n; m (first row). Average pivots and R N E T F time t R (third row).
Average stages, augmentations and DNSUB time rD (second row). Times arc IBM/AT seconds.
D. Goldfarb, M.D. Grigoriadis, Dinic and network simplex methods for max flow 99

OJ
I I I I

Legend:
X = 2 frames
= q 4rames
= 8 Statues
0 = IS ~rSmes
= 32 frames
o
oJ = 6~ ~rames

I--

I,L
l--
b.l
Z
ZC

I I I I
0 iO 20 ~]O qo 50
NODES ~I0 ~

Fig. 3, Ratio o f R N E T F to D N S U B run times for test problems.

due to the pivot step. This step is more expensive for longer FAPs because larger
subtrees have to be reordered. Figure 5 shows this to be the case: the pivot time
increases faster for problems with more frames rather than for problems with more
nodes per frame.
As a practical matter, such arguments assume that the growth of nR/n D is not
caused by a disproportionate change in the "mix" of degenerate and nondegenerate
pivots. This was indeed the case for our problems. Degenerate pivots accounted for
50, 35, 23, 18, 16 and 15 percent of all pivots for problems with n i> 500 and with
b = 2, 4, 8, 16, 32 and 64 frames, respectively. These percentages remained fairly
constant for alt problems larger that 1000 nodes.
Regardless of its relative efficiency, R N E T F runs much faster than its worst
case. This is true for D N S U B as well: we see from fig. 6 that the number of stages
is at most 0.07n (much less than n - I) and the number of augmentations per stage
is at most 30 (much less than m) for all problems larger than 1000 nodes. The product
o
100 ~-

~-
m

~N
~-
/ 0 = 32 f r a m e s
i [] = 6~ f r a m e s

I................. I I
10 20 30 qO 50
NODE5 ~I02
Fig. 4. Ratio o f R N E T F pivots to D N S U B augmentations.
I I I I
Legend:
X -- 2 frames

+i
O_
~x
G

[]
=
=
B frames
16 f r a m e s
= 32 f r a m e s
-- 6~ f r a . +
~
,.e~-/
- -
/
~/" ~]

Z TM° _

Ic

I I I I
I0 20 30 YO 50
NODES ~I0 ~
Fig. 5. Ratio o f R N E T F p i v o t t o D N S U B augmentation times.
D. Goldfarb , M.D. Grigoriadis, Dinic and network simplex methods for max flow 101

#
6 • I t. . . . . . . . . . . . . . . . . . I t

Legend:

X = 2 frame~
i + = t~ f r a m e s
-- 8 fr~tmes

l~] = 6~ fr~tmes _

o_ o_

; °
I I I I o
0 I0 20 30 YO 50
NOOE5 ~I0 z

Fig. 6. Averagestages per node and augmentations per stage.

of the two sets of curves gives corresponding curves for augmentations, all of which
are bounded above by approximately 0.9n. This is substantially smaller than the
worst case of (n - 1)m ~ 6n 2 augmentations for the type of problems we have
considered.

References

[ 1] M.J.Best and K. Ritter, Linear Programming:Active Set Analysis and Computer Programs
(Prentice-HaU, EnglewoodCliffs, New Jersey, 1985).
[2] R.V.Cherkasky, Algorithms of construction of maximum flow in networks with complexity
O(V2x/E) operations, Mathematical Methods of Solution of Economical Problems 7(1977)
112 (in Russian).
[3] W.H.Cunningham, A network simplex method, Math. Progr. 1(1976)105.
[4] W.H.Cunningham, Theoretical properties of the network simplex method, Math. of Oper.
Res. 4(1979)196.
102 D. Goldfarb, M.D. Grigoriadis, Dinic and network simplex methods for max flow

[5] T-Y. Cheung, Computational comparison of eight methods for the maximum network flow
problem, ACM Trans. on Math. Software 6(1980)1.
[6] E.A. Dinic, Algorithms for solution of a problem of maximum flow in networks with
power estimation, Soy. Math. Doklady 11(1970)1277.
[7] J. Edmonds and R.M. Karp, Theoretical improvements in algorithmic efficiency for network
flow problems, J. Assoc. Computing Machinery 19(1972)248.
[8] S. Even, GraphAlgorithms (Computer Science Press, Potomac, Maryland, 1979).
[9] L.R. Ford, Jr. and D.R. Fulkerson, Maximal flow through a network, Can. J. Math. 8(1956)
399.
[10] L.R. Ford, Jr. and D.R. Fulkerson, F/ows in Networl~ (Princeton University Press, New
Jersey, 1962).
[1t] Z. Galil, An O(VS#E~#) algorithm for the maximum flow problem, Acta Informatica
14(1980)221.
[12] Z. Galll and A. Naamad', An O(EVlog 2 V) algorithm for the maximum flow problem,
J. Comput. Syst. Sci. 21(1980)203.
[13] A.V. Goldberg and R.E. Tarjan, A new approach to the maximum flow problem, 18th
Annual ACM Symp. on Theory of Computing (1986) pp. 136 - 146.
[14] F. Glover, D. Klingman, J. Mote and D. Whitman, An extended abstract of an indepth
algorithmic and computational study for maximum flow problems, Discr. Appl. Math.
2(1980)251.
[15] D. Goldfarb, Steepest edge simplex algorithms for network flow problems, Technical
Report RC 6649, T.J. Watson Research Laboratory, IBM Corporation, Yorktown Heights,
New York (1977).
[16] D. Goldfarb and J.K. Reid, A practicable steepest-edge simplex algorithm, Math. Progr.
12(1977)361.
[17] D. Goldfarb and M.D. Grigoriadis, An efficient steepest edge algorithm for the maximum
flow problem, paper presented at the Xth Int. Symp. on Math. Progr., Montreal, Canada
(1979).
[18] M.D. Grigoriadis, An efficient implementation of the network simplex method, Math.
Progr. Study 26(1986)83.
[19] M.D. Grigoriadis, RMFGEN - A generator for a class of maximum flow networks (in
preparation).
[20] M.D. Grigoriadis and T. Hsu, RNET, SIGMAP Bulletin (1978).
[21] P.M.J. Harris, Pivot selection methods of the Devex LP code, Math. Progr. 5(1973)1.
[22] E. Johnson, Networks and basic solutions, Oper. Res. 14(1966)619.
[23] R.M. Karp, A characterization of the minimum cycle mean in a digraph, Discr. Math. 23
(1978)309.
[24] A.V. Karzanov, Determining the maximal flow in a network by the method of preflows,
Soy. Math. Doklady 15(1974)434.
[25] H.W. Kuhn and R.E. Quandt, An experimental study of the simplex method, in: Proc.
of Symposia in Applied Math., Vol. XV, ed. Metropolis et al. (Amer. Math. Assoc., 1963).
[26] E.L. Lawler, Combinatorial Optimization: Networks and Matroids (Holt, Rinehart and
Winston, New York, 1976).
[27] V.M. Malhotra, M. Pramodh Kumar and S.N. Maheshwari, An O(IVI a) algorithm for finding
maximum flows in networks, Inf. Proc. Lett. 7(1978)277.
[28] J.M. Mulvey, Pivot strategies for primal-simplex network codes, J. Assoc. Computing Mach.
25(1978)266.
[29] C. Papadimitriou and K. Steiglitz, Combinatorial Opnmization: Algorithms and Complex-
ity (Prentice-Hall, Englewood Cliffs, New Jersey, 1982).
D. Goldfarb , M.D. Grigoriadis, Dinic and network simplex methods for max flow 103

[30] Y. Shiloach, An O(nIlog2I) maximum flow algorithm, Technical Report StanoCS-78-802,


Computer Science Department, Stanford University, California (1978).
[31] Y. Shiloach and U. Vishkin, An O(n21ogn) parallel max-flow algorithm, J. Algorithms 3
(1982)128.
[32] D.D. Sleator, An O(nm logn) algorithm for maximum network flows, Technical Report
Stan-CS-80-831, Computer Science Department, Stanford University, California (1978).
[33] D.D. Sleator and R.E. Tarjan, A data structure for dynamic trees, J. Computer and System
Sci. 24(1983)362.
[34] R.E. Tarjan, Data Structures and Network Algorithms (Society for Ludustfial and Applied
Mathematics, Philadelphia, Pennsylvania, 1983).
[35] R.E. Tarjan, A simple version of Karzanov's blocking flow algorithm, Oper. Res. Lett.
2(1984)265.
[36] N. Zadeh, More pathological examples for network flow problems, Math. Progr. 5(1973)
217.
[37] P. Wolfe and L. Cutler, Experiments in linear programming, in: Recent Advances in Mathe-
matical Programming, ed. Graves and Wolfe (McGraw-Hill, t963).
D. Goldfarb , M.D. Grigoriadis, Dinic and network simplex methods for max flow 105

Appendix

******* SAMPLE CALLING PROGRAM FOR SUBROUTINE DNSUB *******


*** (MAXIMUM NETWORK FLOW PROBLEM) g**

*** T H E P R O G R A M IS B A S E D ON T H E P A P E R **t
*** D. G O L D F A R B , M.D. G R I G O R I A D I S "A C O M P U T A T I O N A L ***
*** C O M P A R I S O N OF T H E D I N I C A N D N E T W O R K S I M P L E X **t
*** M E T H O D S F O R M A X I M U M FLOW", ***
*** A N N A L S O F O P E R A T I O N S RESEARCH, THIS V O L U M E ***

*** A L L T H E S U B R O U T I N E S A R E W R I T T E N IN A M E R I C A N ***
*** S T A N D A R D F O R T R A N A N D A R E A C C E P T E D BY T H E ***
*** PFORT VERIFIER. **t

*** Q U E S T I O N S A N D C O M M E N T S S H O U L D BE D I R E C T E D TO ***
*** D. G O L D F A R B ***
*** C O L U M B I A U N I V E R S I T Y , N E W YORK, NY 10028, U.S.A. ***
*** M. G R I G O R I A D I S ***
*** R U T G E R S U N I V E R S I T Y , N E W B R U N S W I C K , NJ 08903, ***
*** U.S.A. ***
************************************************************

C D I M E N S I O N T H E S E TO AT L E A S T MAX N U M B E R OF N O D E S + 2:
INTEGER DNLIST(4700>~DNDIST(4700)
INTEGER DNFAPT<~700),DNPTRF(4700),DNBAPT(4700),
* DNFLAB(4700),DNPTRB(4700)
C D I M E N S I O N T H E S E TO AT L E A S T MAX N U M B E R OF A R C S + I:
INTEGER DNFADJ(27000),DNFROM(27000),DNTO(27000),DNBADJ(27000)
INTEGER DNCAP(27000)~DNGCAP(27000),DNFLOW(27000),DNBTOF(27000)
C
COMMON /DNOI/DNFAPT/DNO2/DNFADJ/DN03/DNCAP/DN04/DNFROM
COMMON /DN05/DNFLOW/DN06/DNGCAP/DN08/DNPTRF/DN09/DNLIST
COMMON /DNIO/DNBAPT/DNII/DNFLAB/DNI2/DNPTRB
C
INTEGER DNNODE,NODPI2,DNSRCE,DNSINK, I2,112,K2,J2~IHEAD2~
ITAIL2~QHEAD2~QTAIL2,KP2,KSAT2,DNNOP2,LAYMAX,K2DST,K2DPI,MAXD2
INTEGER DNARC,DNFVA,DNAUG,DNLFVA,DNLAUG~DNSTGE~DNIBIG
COMMON /DN00/DNARC,DNFVA~DNAUG~DNLFVA,DNLAUG,DNSTGE,
DNELTM~DNIBIG,DNNODE~DNSRCE,DNSINK,DNNOP2
EQUIVALENCE (DNFLOW(1),DNTO(I>), <DNFLAB<I)~DNDIST(I>),
(DNFROM(I>~DNBADJ(1)), (DNGCAP<I),DNBTOF<I))
C
C INITIALIZE:
C
C M A X I M U M P R O B L E M S I Z E B A S E D U P O N S I Z E S OF A R R A Y S AS D I M E N S I O N E D :
MAXNOD=4700
MAXNOD=MAXNOD-I
MAXARC=27000
C DEFINE INPUT~OUTPUT AND REPORT FILES
LINP=5
LOUT=6
LREP=8
C READ INPUT DATA FOR NEXT PROBLEM
C T Y P I C A L INPUT F I L E
106 D. Goldfarb, M.D. Grigoriadis, Dinic and network simplex methods for max flow

C R E C O R D I: N O D E S ~ N A R C S ~ I S O U , ISNK
C RECORDS 2~...,NARCS+I:FROMNODE,TONODE,ARCCAPACITY
C (ARCAPACITY=O IMPLIES INFINITE)
READ(LINP,20I)NODES~NARCS~ISOU, ISNK
201 FORMAT(4IIO)
IF(MAXNOD.GE.NODES) G O T O 100
WRITE(LOUT,203)
203 FORMAT(2X,14HTO0 MANY NODES)
STOP
100 I F ( M A X A R C . G E . N A R C S ) G O T O 110
WRITE(LOUT~205)
205 FORMAT(2X~I3HTO0 MANY ARCS)
STOP
110 IMAXS=O
IMAXT=O
D O 10 a = I ~ N A R C S
READ<LINP,20I)DNFROM<J)~DNTO<J),DNGCAP(J)
IF(DNFROM<J).EQ. ISOU)IMAXS=IMAXS+DNGCAP(J)
IF(DNTO(J).EQ. ISNK)IMAXT=IMAXT+DNGCAP(d)
I0 CONTINUE
IMAXC=IMAXT
IF(IMAXS.LT.IMAXT) IMAXC=IMAXS
D O II J = I , N A R C S
ii IF(DNGCAP<J).GT. IMAXC)DNGCAP(J)=IMAXC
C C R E A T E I N P U T D A T A IN F W D A D a A C E N C I E S AS R E Q U I R E D BY D I N S U B :
CALL DNFWD(NODES~NARCS~IRETN)
IF(IRETN.EQ.O) GO TO 30
WRITE(LOUT~33)
33 FORMAT(IX~I7H INPUT DATA ERROR)
STOP
30 CONTINUE
C SOLVE PROBLEM:
C A L L DNSUB(NODES,NARCS, ISOU, ISNK,MAXFLO,NUMAUG,NPHASE,
N N C U T , N A C U T , E T I M E , IRETN)
I F ( I R E T N . EQ.O) G O T O 4 0
WRITE(LOUT,33)
STOP
40 CONTINUE
C WRITE SOLUTION STATS TO SCREEN:
W R I T E ( L O U T , 1007)
NODES,NARCS,MAXFLO,NUMAUG,NPHASE,NNCUT,NACUT,ETIME
1007 FORMAT(21H NODES ARCS
42HMAXFLO NUMAUG STAGES NNCUT NACUT ELAPTIM
~/ I6, I7, I14,417,F8.2)
C CREATE OUTPUT REPORT:
W R I T E ( L O U T , 1003)
1003 FORMAT(37H WANT SOLUTION REPORTS (O=YES, 1=NO))
READ(LINP,20I)IYESNO
IF(IYESNO.NE.O)STOP
WRITE(LOUT, 1OI2)LREP
1012 F O R M A T ( / 4 3 H S O L U T I O N R E P O R T F O R P R O B L E M D A T A IN F I L E ,I5)
CALL DNOUT(LREP)
9999 STOP
END
SUBROUTINE GETIME(T)
REAL T
C THE USER INSTALLATION PROVIDES A TIMING ROUTINE HERE, SAY SECOND~
C T H A T R E T U R N S T H E C U R R E N T T I M E IN V A R I A B L E T~ IN S E C O N D S .
C A L L S E C O N D (T)
RETURN
D. Goldfarb , M.D. Grigoriadis, Dinic and network simplex methods for max flow 107

END
SUBROUTINE SECOND(T)
T=O. 0
RETURN
END
SUBROUTINE D N S U B (NODES, N A R C S , I S O U R C , I S I N K , M A X F L O ,
* NUMAUG, NUMSTG, NNCUT, NACUT, ELTIM, IRETN)
C
C~:*~.~***~(~**START DNSUB STANDARD USER XFACE DECLARATION BLOCK~**~.~
C
C U S E R M U S T I N C L U D E T H I S B L O C K IN H I S / H E R CALI_ING P R O G R A M .
C
C THESE DECLARATIONS ACCOUNT FOR THE ENTIRE ARRAY STORAGE USED BY
C THE DNSUB SUBROUTINES, I N C L U D I N G W H A T IS N E C E S S A R Y TO STORE THE
C I N P U T D A T A . IT A M O U N T S T O 6 * N O D E S + 5 * A R C S W O R D S ~ B U T IT M A Y B E
C STATED MORE PRECISELY AS FOLLOWS:
C
C I) F O R S O L V I N G P R O B L E M S WITH UP TO 32,765 NODES AND 2~147,483~647
C ARCS, AND WITH FLOW VALUES OF UP TO 2~147~483~647~ IT S U F F I C E S
C TO DECLARE ARRAYS AS SHOWN UNDER 'STDARRANGEMENT' IN T H E
C TABLE BELOW. THIS ARRANGEMENT IS T H E O N E U S E D IN T H E R E L E A S E
C V E R S I O N O F T H E D N S U B SUBF~OUTINES.
C
C 2) IN O R D E R T O H A N D L E P R O B L E M S W I T H M O R E T H A N 32.,765 N O D E S ~ A L L
C INTEGER*2 DECLARATIONS BELOW MUST BE CHANGED TO INTEGER~4 AND
C T H E DNSI]B S U B R O U T I N E S AND USER CALLING PROGRAMS M U S T BE C O M P I L E D
C AND LINKED. (SEE A L T E R N A T E i BELOWo)
C
C 3) F O R SOLVIIqG O N L Y P R O B L E M S WITH AT MOST 32,765 NODES~ 32,767
C ARCS~ AND WITH FLOWS NOT EXCEEDING E,2,767~ A L L I N T E G E R * 4
C DECLARATIONS BELOW MAY BE CHANGED T O INTEGERS.2, A N D T H E D N S U B
C SUBROUTINES AND USER CALLING PROGRAMS MUST BE COMPILED AND
C LINKED. (SEE A L T E R N A T E 2 BELOW.) IT IS A L S O N E C E S S A R Y TO
C ACTIVATE TIdE S T A T E M E N T D I q I B I G = 3 2 7 6 7 in s u b r o u t i n e d n s u b , and t o
C include ~JmpliciL" I N T E G E R * 2 (i-n)' s t a t e m e n t s in e a c h p r o g r a m
C unit~ THIS ARRANGEMENT, WHICH USES THE LEAST AMOUNT OF MEMORY
C A N D IS TIdE F A S T E S T w h e n e x e c u t e d on 1 6 - B I T P R O C E S S O R S , IS O N L Y
C USEFUL FOR A SET OF VERY SPECIAL APPLICATIONS. ITS general
C t~se is n o t r e c o m m e n d e d ° OTHER ALTERNATES ARE ALSO POSSIBLE.
C
C
C
C, STD ARRANGEMENT ALTERNATE I ALTERNATE 2
C (not r e c o m m e n d e d )
C
C ARRAY LENGTH TYPE EQV'D TYPE TYPE EQV'~D T Y P E TYPE EQV~D TYPE
C ....................
C DNFAPT N 4 4 2
C DNFADJ A 2 4 2
C DNCAP A 4 4 2
C DNBAPT N 4 4 2
C DNBAD,] A 2 DNFROM 2 4 DNFROM 4 2 DNFROM 2
C DNFL.OW A 4 DNTO 2 4 DNTO 4 2 DNTO 2
C DNBTOF A 4. DNGCAF' 4 4 DNGCAP 4 2 DNGCAP 2
C DNPTRF N 4 4 2
C DNPTRB N 4 4 2
C DNLIST N 2 4 2
C DIqFLAB N 4 DNDIST 2 4 DNDIST 4 2 DNDIST 2
C ........................................................................
C
108 D. Goldfarb, M.D. Grigoriadis, Dinic and network simplex methods for max flow

C TOTAL BYTES: 22~N + 16~A 24~(N + 2 0 ~ A 121N + ],3~A


C (N=NODES, A=ARCS)
C ..........................................................................

C
C DIMENSION THESE TO AT LEAST MAX NUMBER O F N O D E S + 2:
INTEGER DNLIST (4700) , DNDIST (4700)
INTEGER D N F A P T ( 4 7 0 0 ) ~ D N P T R F ,:4700 ) , D N B A P T ~4700) ,
DNFLAB (4700) , DNPTRB (4700)
C DIMENSION THESE TO AT LEAST MAX NUMBER OF ARCS + ~ :
INTEGER DNFADJ (27000) ,DNFROM(27000> ~DNTO(27000) ~DNBAD3 (27000>
INTEGER DiqCAP<27000> ~DNGCAP(27000) ~DNFLOW(27000) ~DNBTOF~27000)

COMMON /DNO1/DNFAPT/DNO2/DNFADJ/DN03/DNCAP/DN04/DNFROM
COMMON /DN05/DNFLOW/DN06/DNGCAP/DN08/DNPTRF/DN09/DNLIST
COMMON /DN~O/DNBAPT/DNII/DNFLAB/DNI2/DNPTRB

INTEGER D N N O D E , N O D P 1 2 . D N S R C E , D N S I N K , I2~ I I2, K2.,J2~ I H E A D 2 ~


ITAIL2~ QHEAD2~ QTAIL2~ KP2~ KSAT2~ DNNOP2~ LAYMAX ~ K2DST, K2DPI, MAXD2
INTEGER D N A R C , D N F V A ~D N A U G ~ D N L F V A ~ D N L A U G ~ D N S T G E ~ D N I B I G
COMMON /DNO0/DNARC,DNFVA~DIqAUG~DNLFVA~DNLAUG~DNSTGE
~ D N E L T M , Dlq I B I G, D N N O D E , D N S R C E , D N S INK, D N N O P 2
EQUIVALENCE (DNFLOW(1),DNTO(1)), (DNFLAB(1),DNDIST(1)),
~ ( D N F R O M ( i ) , D N B A D J ( 1 ~ ), ( D N G C A P ( 1 ) ~ D N B T O F ( 1 ~
C
(Z~(:~:~t~(~END DNSUB STANDARD USER XFACE DECLARATION BLOCF:II~:~:~(:~

C
C CALLING CONDITIONS:
C USER CALLABLE WITH INPUT DATA AS FOLLOWS.
C INPUT:
C SCALARS (IN C A L L I N G SEQUENCE; ALL INTEGERI4):
C NODES: NUMBER OF NODES (INCLUDING SOURCE AND SINK>
C NARCS: NUMBER OF ARCS
C ISOURC: NODE NUMBER FOR SOURCE
C ISINK: NODE NUMBER FOR SINK
C
C ARRAYS (IN C O M M O N ) :
C DNFAPT: (NODES+I)-LONG INTEGERI4 POINTER ARRAY FOR FORWARD ADJA-
C CENCY LISTS (I.E. T H E F O R W A R D ADJACENCY LIST OF A NODE I
C IS T H E S E T O F A R C S
C {(I,DNFADJ(J)) :J=DNFAPT(I)~.~.~DNFAPT(I+I)-I }
C NOTE: MUST HAVE DNFAPT(NODES+I) = NARCS+I~
C
C DNFADJ : NARCS-LONG INTEGER~2 ARRAY GIVING THE LIST OF NODES IN
C THE FORWARD ADJACENCY LIST DNFADJ(J> F O R E A C H N O D E J~
C AS DESCRIBED ABOVE.
C
C DNCAP: NARCS-LONG INTEGER~4 ARRAY GIVING THE ARC CAPACITIES~ IN
C THE ORDER PRESCRIBED BY DNFAPT<.) AND DNFADJ(.). ALL ARC
C CAPACITIES MUST BE GIVEN ~S POSITIVE INTEGERS.
C
C NOTE: FOR TRANSFORMING UNORDERED ARC LISTS TO THE ABOVE INPUT
C DATA STRUCTURE~USE SUBROUTINE DNFWD BEFORE CALLING DNSUB
C
C OUTPUT :
C
C SCALARS (IN C A L L I N G SEQUENCE):
C MAXFLO: VALUE OF MAXIMUM FLOW (INTEGER~4).
C NUMAUG: NUMBER OF FLOW AUGMENTATIONS (INTEGER~4).
C NUMSTG: NUMBER OF STAGES (LAYERED NETWORKS CREATED; INTEGER~4).
D. Goldfarb , M.D. Grigoriadis, Dinic and network simplex methods for max flow 109

C NNCUT: NUMBER OF NODES ON source SIDE OF FINAL CUT (INTEGER*4)


C NACUT: NUMBER OF SATURATED A R C S IN T H E F I N A L C U T ( I N T E G E R * 4 )
C ELTIM: EXECUTION T I M E , IN S E C O N D S (SEE N O T E 3 B E L O W ) (REAL*4).
C IRETN: NONZERO IF T H E R E A R E E R R O R S IN I N P U T D A T A .
C
C ARRAYS (IN C O M M O N : ) :
C D N F A P T : TIdE O R I G I N A L DNFAPT(.) W I T H S O M E OF I T S E L E M E N T S
C N E G A T E D T O M A R K T H E N O D E S T H A T A R E O N TIdE S I N K S I D E O F
C TIdE M I N I M U M CUT, I.E. D N F A P T ( I ) < O I M P L I E S T H A T N O D E I IS
C O N T H E S I N K S I D E O F CUT, E L S E , IT IS O N T H E s o u r c e S I D E .
C
C DNFADJ: THE ORIGINAL DNFADJ(. ) WITH SOME OF ITS ELEMENTS IqEGATED
C TO MARK THOSE SATURATED A R C S T H A T A R E IN T H E M I N C U T
C FOUND BY THE ALGORITHM, I.E. DNFADJ(1)<O IMPLIES THAT
C T H E I - T H A R C IN F O R W A R D A D J A C E N Y O R D E R IS IN T H E CUT;
C T H E FOLI_OWING C O D E S E G M E N T PRINTS THESE ARCS:
C
C DO 2 K = I , N O D E S
C I B E G = I A B S ( D N F A P T (K))
C I E N D = I A B S ( D N F A P T (K+I) ) -I
C DO 1 I=IBEG, IEND
C IF(DNFADJ(I).LT.O)PRINT THE ARC
C 1 CONTINUE
C 2 CONTINUE
C
C (SEE N O T E 1 B E L O W ) .
C
C DNCAP: THE ORIGINAL CAPACITIES, UNALTERED.
C
C DNFLOW: NARCS-LONG INTEGER*4 ARRAY WHICH GIVES FLOWS ON ARCS, IN
C FORWARD ADJACENCY ORDER.
C
C DNBAPT: (NODES+I)-LONG INTEGER*4 POIiqTER A R R A Y F O R B A C K W A R D
C ADJACENCY LISTS (I.E. T H E F O R W A R D A D J A C E N C Y LIST OF
C N O D E I IS T H E S E T O F A R C S
C {(DNBADJ(J),J): J=DNBAPT(I),...,DNBAPT(I+I)-I }.
C NOTE: WE MUST HAVE DNBAPT(NODES+I) = NARCS+I.
C
C DNBADJ: NARCS-LONG INTEGER*2 ARRAY GIVING THE BACKWARD ADJACENCY
C L I S T O F E A C H N O D E J, O N E A F T E R T H E O T H E R , A S D E S C R I B E D
C ABOVE (ALSO SEE NOTE 2 BELOW).
C
C DNBTOF: NARCS-LONG INTEGER*4 ARRAY GIVING THE BACKWARD ADJACENCY
C TO FORWARD ADJACENCY MAPPING, I.E. T H E J - T H A R C
C IN T H E B A C K W A R D ADJACENCY O R D E R IS T H E D N B T O F ( J ) - T H
C A R C IN T H E F O R W A R D A D J A C E N C Y ORDER.
C
************************************************************************
C
C INTERNAL SCRATCH ARRAYS (IN C O M M O N ) :
C
C DNLIST: (NODES+I)-LONG INTEGER*2 ARRAY USED AS FOLLOWS:
C i) A S T H E Q U E U E IN B F S S E A R C H F O R C O N S T R U C T I N G THE
C LAYERED GRAPH (SUBR. D N B F S ) .
C 2) A S T H E " P A R E N T " A R R A Y IN D F S S E A R C H O F L A Y E R E D
C GRAPH (SUBR: D N D F S , D N P U S H ) .
C 3) A S S C R A T C H IN C O N S T R U C T I N G FWD ADJACENCIES
C (SUBR DNFWD), AND BWD ADJACENCIES (SUBR. D N S U B ) .
C
C DNFLAB: NODES-LONG INTEGER*4 A R R A Y U S E D IN D F S S E A R C H O F
110 D. Goldfarb, M.D. Grigor&dis, Dinic and network simplex methods for max flow

C LAYERED GRAPH T O S T O R E T H E FLOW LABEl_ F O R EACH NODE


C (SEE A R R A Y D N D I S T ( ) "~.
C
C DNDIST: NODES-LONG I N T E G E R * 2 A R R A Y U S E D IN C O N S T R U C T I N G THE
E L A Y E R E D G R A P H B Y BFS, S T O R I N G T H E D I S T A N C E OF EACH
C NODE FROM THE SOURCE; DNDIST<ISOURC)=O (EQUIVALENCED
C TO THE INTEGER*4 ARRAY DNFLAB() )..
C
C DNPTRF: NODES-LONG INTEGER*4 STATUS AND POINTER ARRAY, f o E .
E DNPTRF<K)=O IF T H E R E A R E N O O P E N A R C S L E A V I N G N O D E K
C IN T H E R E F ' R E S E N T A T I O N O F T H E L A Y E R E D
C NETWORK.
[" >0 T H E A R C INDEX, IN ~FA' O R D E R , T H A T
C CORRESPONDS TO T H E L A S T A R C IN TIdE
C FWD ADJACENCY O F K W H I C H IS S C A N N E D
C BACKWARD.
C
E DNPTRB: NODES-LONG I N T E G E R * 4 S T A T U S A N D P[}INTER A R R A Y , I.E.
E DNPTRB(K)=O IF T H E R E A R E N O O P E N A R C S E N T E R I N G NODE K
C IN T H E R E P R E S E N T A T I O N O F TIdE L A Y E R E D
E NETWORK.
>0 T H E A R C I N D E X (IN ' B A ~ O R D E R ) O F T H E F I R S T
A R C IN T H E B W D A D J A C E N C Y OF K TO BE
C SCANNED IN L I F O O R D E R .
C
*************************************************************************
E
C NOTES: 1 DNFADJ(J) IS I N T E R N A L L Y N E G A T E D T O M A R K T H E J - T H A R C (IN
C FORWARD ADJACENCY O R D E R ) A S ' C L O S E D ~. B E F O R E R E T U R N I N G
C TO THE CALLING PGM, T H E S E M A R K I N G S ARE DISCARDED, AND SOME
C ELEIdENTS O F D N F A D J ( . ) ARE NEGATED TO R E F L E C T THE OUTPUT
C SPECIFICATION DESCRIBED ABOVE.
C
C 2 DNBADJ(J) IS IIqTERNAI_I_Y N E G A T E D TO M A R K T H E J - T H A R C
C (Ilq B A C K W A R D ADJACENCY ORDER) AS ~CLOSED'.
C
C 3 ]'HE U S E R M U S T F'ROVIDE A TIM].NG S U B R O U T I N E THAT PETURNS
C T H E C U R R E N T T I M E IN V A R I A B L E ~ T ", IN S E C O N D S . SUCH
C A SUBROUTINE IS I N S T A L L A T I O N - D E P E N D E N T A N D IS N O T G I V E N H E R E .
C IF T H I S C A N N O T BE D O N E , P R O V I D E THE FOLLOWING SUBROUTINE AND
C FORGET THE EXECUTION TIMING INFORMATION:
C
C SUBROUTINE GETIME(T)
C REAL T
C T=0.0
C RETURN
C END
C
************************************************************************
C
C SUBROUTINES CALLED: DNBFS, DNDFS, DNCUT, GETIME,DNCLEA
C
************************************************************************

NAMES COLLECTIVELY RESERVED BY ALL DNSUB SUBROUTINES:

DN00, DNOI, DN02, DN03, ON04, ON05, DN06, DN08. ON09, DNIO, DNII,
DNI2, D N A R C , D N A U G , D N B A D J , D N B A P T , D N B F S , D N B T O F , D N C A P , D N C L E A ,
DNCUT, DNDFS, DNDIST, DNFADJ, DNPUSH, DNFAPT, DNFLAB, DNFLOW,
DNFROM, DNFVA, DNFWD, DNGCAP, DNIBIG, DNLAUG, DNI_FVA, DNLIST,
D. Goldfarb , M.D. Grigoriadis, Dinic and network simplex methods for max flow 111

C DNNODE~ DNNOP2~ DNOUT, DNPTRB~ DNPTRF~ DNSINK~ DNSRCE~ DNSTGE~


C DNSUB~ DNELTM~ DNTO.
C
C ~ ~ ~ ~ ~ ~ ~ ~ ~ ~
C INITIALIZATION:

CALL GETIME(TIMBEG)
IRETN=O
DNIBIG=2147483647
C DNIBIG=32767
DNARC=NARCS
DNNODE=NODES
DNNOP2=DNNODE+2
DNSRCE=ISOURC
DNSINK=ISINK
DO 200 I4=I~DNARC
200 DNFLOW(I4)=O
C ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~
C
USING THE FORWARD ADJACENCY ARRAYS DNFAPT(.) AND DNFADJ(.) (IN
COMMON)~ CREATE THE BACKWARD ADJACENCY ARRAYS DNBAPT(.)~ DNBAD3(.)
AND THE BACKWARD TO FORWARD ADJACENCY MAPPING DNBTOF(.). DNLIST(.) IS
USED HERE AS A SCRATCH ARRAY.

NODP12=DNNODE+1
DO 210 I2=I~NODPI2
DNLIST(I2)=O
210 DNBAPT(12)=O
C T E M P O R A R I L Y S T O R E IN B N B A P T ( . ) N U M B E R O F A R C S I N T O E A C H N O D E :
DO 230 I2=I,DNNODE
IBEG4=DNFAPT(I2)
IEND4=DNFAPT(I2+I)-I
DO 220 I4=IBEG4~IEND4
JJDN=DNFADJ(I4)
220 DNBAPT(JJDN)=DNBAPT(JJDN)+I
230 CONTINUE
C CONSTRUCT DNBAPT(.):
IHPI4=DNBAPT(1)
DNBAPT(1)=I
DO 240 I2=I~DNNODE
IHPSV4=IHPI4+DNBAPT(I2)
IHPI4=DNBAPT(I2+I)
240 DNBAPT(I2+I)=IHPSV4
C CONSTRUCT DNBADJ(.) AND DNBTOF(.):
DO 260 I2=I~DNNODE
IBEG4=DNFAPT(I2)
IEND4=DNFAPT(I2+I)-I
DO 250 I4=IBEG4~IEND4
IHEAD2=DNFADJ(I4)
IHPUT4=DNBAPT(IHEAD2)+DNLIST(IHEAD2)
IF(IHPUT4.LE.0)GOTOI900
DNBADO(IHPUT4)=I2
DNBTOF(IHPUT4)=I4
250 DNLIST(IHEAD2)=DNLIST(IHEAD2)+I
260 CONTINUE
C
C ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~
C
DNFVA=O
DNAUG=O
112 D. Goldfarb, M.D. Grigoriadis, Dinic and network simplex methods for max flow

DNSTGE=I
C
C STAGE LOOP:
300 CONTINUE
C FORM NEW LAYERED NETWORK W . R . T . CURRENT FEASIBLE FLOW.
CALL DNBFS(LAYMAX)
IF(LAYMAX.ST.O)GOT01500
C COMPUTE A MAXIMAL FLOW IN THIS LAYERED NET:
CALL DNDFS
C UPDATEFLOW VALUE:
DNFVA=DNFVA+DNLFVA
DNAUG=DNAUG+DNLAUG
DNSTGE=DNSTSE+I
C ERASEMARKINGS THAT DEFINE LAYERED NETWORK:
CALL DNCLEA
GOT0300
C i i ~ ~ ~ ~ ~ ~ ~ ~ ~ i ~
C
C CURRENT FLOW IS MAXIMUM:
1500 MAXFLO=DNFVA
NUMAUG=DNAUG
NUMSTS=DNSTGE
CALL DNCLEA
C MARK EXECUTION TIME:
CALL GETIME(TIMEND)
DNELTM=TIMEND-TIMBEG
ELTIM=DNELTM
C
C MARK THE MIN CUT-SET IN DNFAPT<.) AND DNFAD3(.):
CALL DNCUT(NNCUT,NACUT)
C
RETURN
C
1900 IRETN=I
RETURN
END
SUBROUTINE DNBFS~LAYMAX)
C
C
C D I M E N S I O N T H E S E TO AT L E A S T MAX N U M B E R OF N O D E S + 2:
INTEGER DNLIST(4700~DNDIST(4700)
INTEGER DNFAPT(4700)~DNPTRF(4700)~DNBAPT(4700)~
DNFLAB(4700)~DNPTRB(4700)
C DIMENSION THESE TO AT LEAST MAX NUMBER OF ARCS + I :
INTEGER DNFADJ<27000>,DNFROM(27000),DNTO<27000),DNBADJ(27000)
INTEGER DNCAP(27000)~DNGCAP(27000),DNFLOW(27000),DNBTOF<27000)
C
COMMON /DNOI/DNFAPT/DNO2/DNFADJ/DNO3/DNCAP/DNO4/DNFROM
COMMON /DNO5/DNFLOW/DNO6/DNGCAP/DNOS/DNPTRF/DNO9/DNLIST
COMMON /DNIO/DNBAPT/DNll/DNFLAB/DNI2/DNPTRB
C
INTEGER DNNODE~NODPI2,DNSRCE~DNSINK,. I2,. I I ~ K ~ , ~ IHEAD2~
ITAIL2~QHEAD2,QTAIL2~KP2~KSAT2,DNNOP2,LAYMAX,K2DST~K2DPI,MAXD2
INTEGER DNARC, D N F V A , D N A U G ~ D N L F V A , D N L A U G ~ D N S T G E ~ D N I B I G
COMMON /DNOO/DNARC~DNFVA~DNAUG~DNLFVA, DNLAUG,DNSTGE~
DNELTM~DNIBIG~DNNODE,DNSRCE~DNSINK~DNNOP2
EQUIVALENCE (DNFLOW(I>~DNTO(I>), (DNFLAB(1)~DNDIST(1))~
<DNFROM(1),DNBADJ(1)), (DNGCAP(1),DNBTOF(1))
C
C ~ i ~ i ~ ~ ~ ~ ~ i ~ ~ ~ ~ i ~
D. Goldfarb, M.D. Grigoriadis, Dinic and network simplex methods for max flow 113

C
C CALLING CONDITIONS:
C INTERNAL CALL FROM DNSUB ONLY.
C INPUT ARRAYS:
C DNFAPT, DNBAPT~ DNFADJ, DNBADJ~ DNBTOF, DNCAP, DNFLOW
C SCRATCH ARRAYS:
C DNLIST
C OUTPUT:
C IN C A L L I N G S E Q U E N C E <INTEGER*2):
C LAYMAX: 0 (LAYERED NET COMPLETE W I T H S I N K IN L A S T L A Y E R . >
C 1 ( L A S T L A Y E R IS E M P T Y ; C U R R E N T F L O W IN O R I G I N A L
C NETWORK IS M A X I M U M . >
C IN C O M M O N (ARRAYS>:
C DNPTRF, DNPTRB, DNDIST

C
C THIS SUBROUTINE CONSTRUCTS THE LAYERED NETWORK OF THE RESIDUAL
C NETWORK W.R.T. THE CURRENT FLOW. THE RESIDUAL NETWORK IS
C INFERRED, AND THE LAYERED NETWORK IS ONLY RECORDED BY TEMPORARY
C MARKERSON THE ORIGINAL GRAPH. THE SUBROUTINE IMPLEMENTS A
C BREADTH-FIRSTSEARCH ON THE RESIDUAL NETWORK, I . E . NODES ARE
C PROCESSEDAS IN A QUEUE. SUCH A SEARCH PARTITIONS THE NODES INTO
C A SET OF "LAYERS'~ ACCORDING TO THEIR CARDNALITY DISTANCE FROM
C THE s o u r c e . THE DNDIST(.> ARRAY IS USED TO RECORD THESE DISTANCES~
C AND THUS DEFINES THE LAYERS (NOTE: DNDIST(ISOURC)=O).
C
C IN SELECTING ARCS OF THE RESIDUAL NEEWORK THAT ARE DIRECTED FROM
C ONE LAYER TO THE NEXT, WE SHALL MARK THE ARCS IN THE ORIGINAL
C N E T W O R K A S ~ O P E N ~ O R ' C L O S E D ~. A ~ C L O S E D ~ A R C (V,W> IN T H E
C LAYERED NETWORK IS E I T H E R A N A R C I = ( V , W > T H A T IS S A T U R A T E D IN T H E
C ORIGINAL NETWORK, OR AN ARC I=(W,V) OF THE ORIGINAL NETWORK WITH
C Z E R O FLOW. IN T H E F O R M E R C A S E , T H E A B S E N C E OF T H I S A R C F R O M T H E
C LAYERED NETWORK IS M A R K E D B Y N E G A T I N G DNFADJ(I), A N D IN T H E L A T T E R
C CASE, BY NEGATING DNBADJ(I). AN 'OPEN ~ ARC I=(V,W) FOR THE LAYERED
C NETWORK EITHER HAS POSITIVE RESIDUAL CAPACITY, O R A R C (W,V) H A S
C POSITIVE FLOW, IN T H E O R I G I N A L NETWORK. IF A N O D E V H A S N O O P E N
C A R C S IN T H E L A Y E R E D N E T W O R K T H A T A R E IN T H E F O R W A R D A D J A C E N C Y LIST
C O F V IN T H E O R I G I N A L NETWORK, THEN WE SET DNPTRF(V>=O; A N D IF IT H A S
C N O O P E N A R C S T H A T A R E IN T H E B A C K W A R D ADJACENCY L I S T O F V IN T H E
C ORIGINAL NETWORK, WE SET DNPTRB(V)=O.
C
C IN T H I S B F S S E A R C H W E O N L Y U S E O P E N A R C S T H A T L E A D U S T O N E W
C NODES, OR TO NODES THAT HAVE ALREADY BEEN PLACED IN T H E N E X T
C LAYER. INITIALLY, ]'HE Q U E U E C O N T A I N S ONLY THE source. AS THE
C VERTICES ARE POPPED FROM THE QUEUE AND SCANNED~ NEW NODES ARE
C INJECTED INTO THE QUEUE. EVENTUALLY~ E I T H E R T H E S I N K IS R E A C H E D ,
C OR SOME VERTICES ARE NOT REACHABLE WITH OPEN ARCS FROM THE
C CURRENT LAYER. IN T H E F O R M E R C A S E T H E L A Y E R E D N E T W O R K F O R T H E
C C U R R E N T S T A G E IS C O M P L E T E ~ AND THUS A FLOW AUGMENTATION IS
C POSSIBLE. IN TIdE L A T T E R C A S E , T H E C U R R E N T FLOW ON THE ORIGINAL
C NETWORK IS M A X I M U M , AND THE RUN TERMINATES.
C
C THE QUEUE IS M A I N T A I N E D IN T H E LIST DNLIST(.) WITH TWO POINTERS,
C AS SHOWN:
C
C ARRAY ~ NODES TO BE SCANNED
C DNLIST(.): ... ~ I
C

C ~
114 D. Goldfarb , M.D. Grigoriadis, Dinic and network simplex methods for max flow

C QHEAD2 QTAIL2
C ~ ~ i ~ i ~ ~ i ~ ~ i ~ i ~ ~ ~ i ~
C
C INITIALIZE:
DO
12=I,DNNODE I0
DNPTRF(12)=O
DNPTRB(I2>=O
I0 DNDIST(I2)=DNNOP2
DNDIST(DNSRCE)=O
C PUT source INTO QUEUE:
QHEAD2=O
QTAIL2=I
DNLIST(1)=DNSRCE
MAXD2=DNNODE-I
C
C SCAN EACH NODE IN Q U E U E :
C
C
C
100 IF (QHEAD2. EQ. Q T A I L 2 ) G O T 0 2 0 0 0
QHEAD2=QHEAD2+I
C POP N O D E IN F R O N T O F Q U E U E :
K2=DNLIST(QHEAD2)
K2DST=DNDIST(K2)
IF(K2DST.GE.MAXD2)GOTOI00
K2DPI=K2DST+I

S C A N N O D E K2, I.E. S E A R C H O V E R F W D A D J A C E N C Y O F K 2 A N D F O R A R C S
(K2,J2> S U C H T H A T J 2 IS U N S C A N N E D A N D J 2 IS N O T IN T H E Q U E U E A N D
A R C (K2,J2) H A S P O S I T I V E R E S I D U A L C A P A C I T Y IN O R I G I N A L N E T W O R K .
'J2 UNSCANNED' IS C H E C K E D B Y T H E C O N D I T I O N : 'DNDIST(J2)>=DNDIST(K2)'
'J2 N O T IN Q U E U E ' IS C H E C K E D B Y T H E C O N D I T I O N
'DNDIST(J2)=DNDIST(K2)+I F O R A S C A N N E D J 2 ~.

IBEG4=DNFAPT(K2)
IEND4=DNFAPT(K2+I)-I
IF(IBEG4.GT. IEND4)GOT01000
DO 400 14=IBEG4, IEND4
J2=DNFADJ(I4)
IF(DNDIST(J2).LE.K2DST)GOT0300
IF(DNCAP(I4).LE.DNFLOW(I4))GOT0300
C ( I M P L I C I T L Y ) M A R K A R C (K2, J2) A S O P E N IN L A Y E R E D N E T W O R K ; A L S O ,
C S A V E I T S I N D E X IN D N P T R F ( . ) :
DNPTRF(K2)=I4
IF(J2.NE.DNSINK)GOT0200
C J2=SINK:
MAXD2=K2DPI
DNDIST(J2)=MAXD2
GOT0400
C A P P E N D N O D E J 2 T O T H E Q U E U E , IF N O T A L R E A D Y T H E R E :
200 IF<DNDIST<J2).EQ.K2DPI)GOTO40Q
DNDIST(J2)=K2DPI
QTAIL2=QTAIL2+I
DNLIST(QTAIL2)=J2
GOT0400
C (EXPLICITLY) M A R K A R C (K2,J2) A S C L O S E D IN L A Y E R E D N E T W O R K :
300 DNFADJ(14)=-DNFADJ(I4)
400 CONTINUE
C
C. . . . . . . . . . . . . . .
D. Go ldfarb , M.D. Grigoriadis, Dinic and network simplex methods for max flow 115

C
C IF S I N K W A G R E A C H E D , D O N ' T N E E D T O S C A N B A C K W A R D A R C S I N T O ITS L A Y E R :
1000 IF(DNDIST(DNSINK).NE.DNNOP2)GOTOI00
C
C E L S E , C O N T I N U E S C A N O V E R F O R W A R D A R C S O U T O F K 2 IN T H E R E S I D U A L
C NETWORK (WHICH ARE BACKWARD ARCS OF THE ORIGINAL NETWORK WITH
C P O S I T I V E F L O W ) . T H I S C O D E S E G M E N T IS A N A L O G O U S T O T H E O N E A B O V E :
C
IBEG4=DNBAPT(K2)
IEND4=DNBAPT(K2+I)-I
IF(IBEG4.GT.IEND4)GOTOI00
DO 1400 I4=IBEG4, IEND4
J2=DNBADJ(I4)
IF(DNDIST(J2).LE.K2DST)GOTOI300
JJDN=DNBTOF(I4)
IF(DNFLOW(JJDN).LE.0)GOEOI300
DNPTRB(K2)=I4
IF(DNDIST(J2).EQ.K2DPI)GOTOI400
DNDIST(J2)=K2DPI
QTAIL2=QTAIL2+I
DNLIST(QTAIL2)=J2
GOTOI400
1300 DNBADJ(I4)=-DNBADJ(I4)
1400 CONTINUE
GOTOI00

C
2000 LAYER2=LAYER2+I
LAYPI2=LAYER2+I
C H E R E ~ A L L N O D E S IN Q U E U E H A V E B E E N P R O C E S S E D .
C IF T H E S I N K W A S R E A C H E D ~ T H E L A Y E R E D N E T W O R K IS C O M P L E T E :
LAYMAX=O
IF(DNDIST(DNSINK).NE.DNNOP2)RETURN
C E L S E , T H E C U R R E N T F L O W IS M A X I M U M :
LAYMAX=I
RETURN
C
END
SUBROUTINE DNDFS
C
C
C D I M E N S I O N T H E S E T O A T L E A S T M A X N U M B E R O F N O D E S + 2:
INTEGER DNLIST(4700)~DNDIST(4700)
INTEGER
DNFAPT(4700),DNF'TRF(4700),DNBAPT(4700)~
DNFLAB(4700),DNPTRB(4700)
C DIMENSION THESE TO AT LEAST MAX NUMBER OF ARCS + i :
INTEGER DNFADJ(27000)~DNFROM(27000)~DNTO(27000)~DNBADJ(27000)
INTEGER DNCAP(27000),DNGCAP(27000)~DNFLOW(27000),DNBTOF(27000)
C
COMMON /DNOI/DNFAPT/DNO2/DNFADJ/DNO3/DNCAP/DNO4/DNFROM
COMMON /DN05/DNFLOW/DN06/DNGCAP/DNOG/DNPTRF/DNO9/DNLIST
COMMON /DNIO/DNBAPT/DNII/DNFLAB/DNI2/DNPTRB
C
INTEGER DNNODE,NODP12~DNSRCE,DNSINK~ I=~ I I 2 , K 2 , J2~ IHEAD2,
ITAIL2, QHEAD2,QTAIL2, KP2, KSAT2,DNNOP2,LAYMAX,K2DST,K2DPI, MAXD2
INTEGER DNARC,DNFVA,DNAUG,DNLFVA,DNLAUG,DNSTGE,DNIBIG
COMMON /DNOO/DNARC, DNFVA,DNAUG,DNLFVA,DNLAUG,DNSTGE,
DNELTM~DNIBIG,DNNODE,DNSRCE,DNSINK~DNNOP2
EQUIVALENCE (DNFLOW(1),DNTO(1)), (DNFLAB(i),DNDIST(1)),
116 D. Goldfarb, M.D. Grigoriadis, Dinic and network simplex methods for max flow

* (DNFROM(1),DNBADO(1)), (DNGCAP(1),DNBTOF(1))
C
************************************************************************
C
C T H I S S U B R O U T I N E F I N D S A M A X I M A L F L O W IN T H E L A Y E R E D N E T W O R K . IT
C USES A DEPTH-FIRST SEARCH STARTING FROM THE SOURCE AND RECORDS
C THE DFS TREE BY THE PREDECESSOR ARRAY DNLIST(.) AND FLOW LABEL
C A R R A Y D N F L A B ( . ) . L A Y E R E D N E T W O R K A R C S T H A T A R E F O R W A R D IN T H E
C ORIGINAL NETWORK ARE SEARCHED BEFORE BACKWARD ARCS. THE
C F O R W A R D A N D B A C K W A R D A D J A C E N C I E S O F A N O D E K 2 A R E S C A N N E D IN L I F O
C ORDER, STARTING FROM THE LAST ARC THAT WAS VISITED DURING A
C P R E V I O U S S C A N O F K2. T H U S , W I T H T H E A I D O F T H E T W O P O I N T E R L I S T S
C DNPTRF(.) AND DNPTRB(.), THE ADJACENCY LISTS OF THE ORIGINAL
C NETWORK ARE SCANNED ONLY ONCE BY THIS SUBROUTINE. DNPTRF(K2>=O
C IMPLIES THAT NODE K2 HAS NO OUTGOING UNSCANNED ARCS; DNPTRB(K2)=O
C IMPLIES THAT NODE K2 HAS NO INCOMING UNSCANNED ARCS; A ~CLOSED ~
C N O D E IS D E T E C T E D BY T H E C O N D I T I O N D N P T R F ( K 2 ) = D N P T R B ( K 2 ) = O .
C
************************************************************************
C
C CALLING CONDITIONS:
C I N T E R N A L C A L L F R O M D N S U B ONLY.
C INPUT ARRAYS:
C DNPTRB, DNPTR, DNBTOF, DNCAP, DNFLOW, DNBADJ. DNFADJ
C OUTPUT ARRAYS:
C DNPTRF, DNPTRB, DNLIST, DNFLAB.
C***********************************************************************
C
C SUBROUTINES CALLED: DNPUSH
C

C
K2=DNSRCE
DNLFVA=O
DNLAUG=O
DNFLAB(K2)=DNIBIG
C
C . . . . . . . . . . .

C
C S C A N N O D E K2:
C
I00 IF(DNPTRF(K2).EQ.O)GOTOI000
C F I N D A N O P E N A R C F R O M K 2 T O S O M E N O D E J2: S C A N T H E
C F O R W A R D A D J A C E N C Y L I S T O F K2, S T A R T I N G F R O M DNPTRF(K2) AND
C PROCEEDING BACKWARD ON DNFADJ() TOWARD DNFAPT(K2):
14=DNPTRF(K2)
IPT4=DNFAPT(K2)
300 J2=DNFADJ(I4)
I F ( J 2 . GE.O)GOT0600
400 14=14-i
IF(14.GE. IPT4)GOT0300
C N O O P E N A R C S O U T O F K2; M A R K K 2 A S C L O S E D :
DNPTRF(K2)=O
SOTOI000
C A N O P E N A R C F O U N D ; M O V E P O I N T E R D N P T R F < ) T O T H I S ARC;
C E X T E N D D F S T R E E T O N O D E J2:
600 DNPTRF(K2)=I4
DNFLAB(J2>=DNFLAB<K2)
IF<DNCAP(14)-DNFLOW<I4).LT.DNFLAB(J2))
D N F L A B <O2) = D N C A P ( 1 4 ) - D N F L O W (14)
D. Goldfarb , M.D. Grigoriadis, Dinic and network simplex methods for max flow 117

DNLIST(J2)=K2
K2=J2
C IF N O D E K 2 ( F O R M E R L Y J2) IS T H E S I N K , A U G M E N T FLOW; GET NEW K2:
IF(K2.EQ.DNSINK)CALL DNPUSH(K2)
GOTO100
C
C
C
1000 IF(DNPTRB(K2).EQ.0)GOT02000
C SCAN BACKWARD A R C S I N T O K 2 T O F I N D A N O P E N ARC:
I4=DNPTRB(K2)
IPT4=DNBAPT(K2>
1300 J2=DNBADJ(I4)
IF(J2.GE.0)GOTOI600
I4=I4-I
IF(I4.GE. IPT4)GOTOI300
C N O O P E N A R C I N T O K2:
DNPTRB(K2)=O
GOT02000
C A N O P E N A R C F O U N D ; E X T E N D D F S T R E E T O N O D E J2:
1600 DNPTRB(K2)=I4
DNFLAB(J2)=DNFLAB(K2)
JJDN=DNBTOF(I4)
IFL4=DNFLOW(JJDN)
IF(IFL4.LT.DNPLAB(J2))DNFLAB(J2)=IFL4
DNLIST(J2)=-K2
K2=J2
GOTOI00
C

C
C K 2 IS " C L O S E D ' N O D E ; IF IT IS T H E s o u r c e , T H E N W E H A V E A M A X I M A L
C F L O W IN L A Y E R E D N E T W O R K :
2000 IF(K2.EQ.DNSRCE)RETURN
C E L S E , B A C K U P O N E N O D E F R O M K 2 IN D F S T R E E :
KP2=DNLIST(K2)
K2=KP2
IF(K2.LT.O)K2=-K2
IF(KP2.GE.0)SOT02200
I4=DNPTRB(K2)
DNBADJ(I4)=-DNBADJ(I4)
GOTOI00
2200 1 4 = D N P T R F (K2)
D N F A D J (I4) = - D N F A D J (14)
GOTO 100
C
C
END
SUBROUTINE D N P U S H (K'2)
C
C
C DIMENSION T H E S E T O A T L E A S T M A X N U M B E R O F N O D E S + 2:
INTEGER D N L I S T (4700), D N D I S T (4700)
INTEGER D N F A P T (4700) , D N P T R F (4700) , D N B A P T (4700) ,
D N F L A B (4700) , DNF'TRB (470(:))
C DIMENSION T H E S E T O A T L E A S T M A X N U M B E R O F A R C S + I:
INTEGER DNFADJ(27t300) ,DNFROM(27000),DNTO(27000),DNBADJ(27000)
I N T E G E R D N C A P ( 2 7 0 0 0 ) , D N G C A P (27000) ~ D N F L O W ( 2 7 0 0 0 ) , D N B T O F ( 2 7 0 0 0 )
C
COMMON /DNO 1/DNFAPT/DN02/DNFADJ/DN03/DNCAP/DN04/DNFROM
118 D. Goldfarb, M.D. Grigoriadis, Dinic and network simplex methods for max flow

COMMON /DN05/DNFLOW/DN06/DNSCAP/DN08/DNPTRF/DN09/DNLIST
COMMON /DNIO/DNBAPT/DNII/DNFLAB/DNI2/DNPTRB

INTEGER DNNODE~NODPI2, DNSRCE~DNSINK, 12,112~K2,J2, IHEAD2,


ITAIL2~QHEAD2~QTAIL2,KP2,KSAT2~DNNOP2~LAYMAX~K2DST,K2DPI,MAXD2
INTEGER DNARC,DNFVA,DNAUG,DNLFVA,DNLAUG, DNSTGE,DNIBIG
COMMON /DN00/DNARC,DNFVA,DNAUS~DNLFVA, DNLAUG, DNSTGE,
DNELTM, DNIBIG,DNNODE~DNSRCE~DNSINK~DNNOP2
EQUIVALENCE (DNFLOW(I>,DNTO(1)>, (DNFLAB(1)~DNDIST(1))~
(DNFROM(1)~DNBADJ(1)), (DNGCAP(1)~DNBTOF(1))
C

C
C AUGMENT FLOW ALONG FLOW AUGMENTING P A T H D E F I N E D S U B R O U T I N E D N D F S ~ I.E.
C USING THE PREDECESSOR ARRAY DNLIST(.), START FROM THE SINK AND
C TRAVERSE TO THE SOURCE ARC FLOWS AND FLOW LABELS BY AN AMOUNT EQUAL
C TO DNFLAB(DNSINK>. MARK SATURATED FORWARD ( F R O M S T O T) A R C S ~ A N D
C BACKWARD ARCS HAVING ZERO FLOW, AS CLOSED.
C

C
C CALLING CONDITIONS:
C INTERNAL CALL FROM DNDFS ONLY.
C INPUT ARRAYS:
C DNFLAB,DNLIST~DNPTRB, DNPTRF,DNBTOF,DNCAP~DNFLOW,DNBADJ~DNFADJ
C OUTPUT ARRAYS:
C DNFLAB,DNFLOW,DNBADJ,DNFADJ
C ~ i ~ i ~ ~ * ~ ~ i ~ ~ i ~ ~ * ~ ~ i * ~ i ~
C
KSAT2=O
J2=DNSINK
INCRE4=DNFLAB(J2)
DNLFVA=DNLFVA+INCRE4
DNLAUG=DNLAUG+I
C
i00 K2=J2
J 2 = D N L I S T (K2)
D N F L A B (K2) = D N F L A B (K2) - I N C R E 4
IF (J2. GT. O) G O T 0 2 0 0
C
C DECREASE FLOW O N B A C K W A R D A R C (K2,J2) :
J2=-J2
I 4 = D N P T R B (J2)
I I 4 = D N B T O F (I4)
D N F L O W ( I I4) = D N F L O W ( I I 4 ) - I N C R E 4
IF ( D N F L O W ( I I4) .NE. 0) G O T O I 0 0
C FLOW IS ZERO; M A R K A R C A S ' C L O S E D ' IN L A Y E R E D NETWORK:
D N B A D J (I4) = - D N B A D J (I4)
KSAT2=J2
GOTO 100
C
C INCREASE FLOW ON FORWARD ARC (J2,K2):
200 1 4 = D N P T R F (J 2)
D N F L O W (14) = D N F L O W (I4) + I N C R E 4
IF ( D N C A P ( 14). N E . D N F L O W (I4)) G O T 0 3 0 0
C ARC IS N O W S A T U R A T E D ; M A R K IS A S ' C L O S E D " IN L A Y E R E D NET
D N F A D J (I4) = - D N F A D J ( I4>
KSAT2=J2
300 IF (J2. NE. D N S R C E ) G O T O I 0 0
C
D. Goldfarb, M.D. Grigoriadis, Dinic and network simplex methods for max flow 119

C RETURN TO RESUME SEARCH FROM K 2 (NODE CLOSEST TO s o u r c e OF


C THE ARC CLOSED LAST)
I F (KSAT2. EQ. O) STOP
K2=KSAT2
RETURN
END
SUBROUTINE DNFWD(NODES,NARCS, IRETN)
C
C
C DIMENSION THESE TO AT LEAST MAX NUMBER OF NODES + 2:
INTEGER DNLIST ( 4 7 0 0 ) , DNDIST ( 4 7 0 0 )
INTEGER DNFAPT( 4 7 0 0 ) , DNPTRF ( 4 7 0 0 ) , DNBAPT ( 4 7 0 0 ) ,
* DNFLAB ( 4 7 0 0 ) , DNPTRB ( 4 7 0 0 )
C DIMENSION THESE TO AT LEAST MAX NUMBER OF ARCS 4- 1:
INTEGER D N F A D J ( 2 7 0 0 0 ) , D N F R O M ( 2 7 0 0 0 ) , D N T O ( 2 7 0 0 0 ) , D N B A D J ( 2 7 0 0 0 )
INTEGER D N C A P ( 2 7 0 0 0 ) , D N G C A P ( 2 7 0 0 0 ) , D N F L O W ( 2 7 0 0 ¢ ' ) ) , D N B T O F ( 2 7 0 0 0 )
C
COMMON / D N O 1 / D N F A P T / D N O 2 / D N F A D J / D N O 3 / D N C A P / D N O 4 / D N F R O M
COMMON / D N O 5 / D N F L O W / D N O 6 / D N G C A P / D N O 8 / D N P T R F / D N O 9 / D N L I S T
COMMON /Dlqt.:')/DNBAPT/DNI 1 / D N F L A B / D N 1 2 / D N P T R B
C
INTEGER DNNODE, NODP12, DNSRCE, D N S I N K , I 2 , I I 2 , K 2 , J 2 , I H E A D 2 ,
* I TA I L 2 , QHEAD2, QTA I L 2 , KP2, KSAT2, DNNOP2, LAYMAX, K2DST, K2DP1, MAXD2
IIqTEGER DNARC, DNFVA, DNAUG, DNLFVA, DNLAUG, DNSTGE, D N I B I G
COMMON / D N 0 0 / D N A R C , D N F V A , D N A U G , D N L F V A , D N L A U G , D N S T G E ,
* D N E L T M , D N I B I G , D N N O D E , D N S R C E , DNS~IqK., D N N O P 2
EQUIVALENCE (DNFLOW(1),DNTO(1)), (DNFLAB(1),DNDIST(1)),
* (DNFROM(I.),DNBADJ(1)), (DNGCAP(t),DNBTOF(1))
C
**************************************************************************
C
(] C A L L I N G CONDITIONS:
C USER CALLABLE; BEFORE C A L L I N G DNSUB.
C INPUT:
C IN CALI_ING SEQLIENCE (ALL I N T E G E R * 4 ) :
C NODES= NUMBER OF NODES, INCLUDING source AND SINK
C NARCS= N U M B E R O F ARCS.
C
C IN C O M M O N , P R O V I D E T H R E E A R C L I S T S W I T H A R C S IN A N Y O R D E R :
C DNFROM= THE LIST OF TAILS. (INTEGER*2)
C DNTO= THE LIST OF HEADS. (INTEGER*2)
C D N G C A P = 'THE L I S T O F C A P A C I T I E S . (INTEGER*4).
C
C SCRATCH ARRAY (IN C O M M O N ) :
C DIqL I S T
C OUTPUT :
C I N CALLING SEQUENCE ( I N T E G E R * 4 ) :
C IRETIq=O IqO ERRORS; =1 ERROR DETECTED, CHECK INPUT.
C II\l C O M M O N , A R R A Y S :
C DNFAPT,DI\IFADJ,DNCAP (I.E. TIdE F N D A D J A C E N C Y INPUT DATA STRUCTURE
C "THAT IS R E Q U I R E D A S I N P U T TO S U B R O U T I N E DNSUB).
C
******************************************************************************
C
C I]SING TIdE THREE ARC L I S T S D N F R O M ( . ) , D N T Q ( . ) , DNGCAF'(.), THIS
[" SUBROUTINE CONSTRUCTS THE FORWARD ADJACENCY ARRAYS D N F A P T ( . ) ,
(] DNFADJ ( . ) . AND D I q C A P ( . ) , AS THE INF'UT REQUIRED BY SUBROUTINE DNSUB.
C
*******************************************************************************
C
120 D. Goldfarb, M.D. Grigoriadis, Dinic and network simplex methods for max flow

C INITIALIZE:
IRETN=O
NODPI=NODES+I
DO 100 I 2 = I ~ N O D P I
DNFAPT(I2)=O
100 DNLIST(I2)=O
C T E M P O R A R I L Y S T O R E IN D N F A P T ( . ) N U M B E R OF A R C S OUT OF E A C H NODE:
DO 200 I 4 = I ~ N A R C S
,]JDN=DNFROM(I4)
200 DNFAPT(JJDN)=DNFAPT(~JDN)+I
C CONSTRUCT DNFAPT(.):
ITPI4=DNFAPT(1)
DNFAPT(1)=I
DO 300 I 2 = I ~ N O D E S
ITPSV4=ITPI4+DNFAPT(I2)
ITPI4=DNFAPT(I2+I)
300 DNFAPT(I2+I>=ITPSV4
C CONSTRUCT DNFADJ(.) AND DNCAP(.):
DO 400 I 4 = I ~ N A R C S
ITAIL2=DNFROM(14)
ITPUT4=DNFAPT(ITAIL2)+DNLIST(ITAIL2)
IF(ITPUT4.LE.0)GOT0500
DNFADJ(ITPUT4)=DNTO(14)
DNLIST(ITAIL2>=DNLIST(ITAIL2)+I
DNCAP(ITPUT4)=DNGCAP(I4>
400 CONTINUE
RETURN
50¢) IRETN=I
RETURN
END
SUBROUTINE DNCLEA
C
C
C D I M E N S I O N T H E S E TO AT L E A S T MAX N U M B E R OF N O D E S + 2:
INTEGER DNLIST(4700>~DNDIST(4700>
INTEGER DNFAPT(4700),DNPTRF(4700),DNBAPT(4700)~
DNFLAB(4700)~DNPTRB(4700)
C D I M E N S I O N T H E S E TO AT L E A S T MAX N U M B E R OF A R C S + i:
I N T E G E R D N F A D J ( 2 7 0 0 0 ) ~ D N F R O M ( 2 7 0 0 0 ) ~ D N T O ( 2 7 0 0 0 ) ~DNBADJ (27000)
INTEGER DNCAP(27000),DNGCAP(27000),DNFLOW(27000) ~DNBTOF(27000)
C
COMMON /DNOI/DNFAPT/DN02/DNFADO/DNO3/DNCAP/DNO4/DNFROM
COMMON /DN05/DNFLOW/DNO6/DNGCAP/DNO8/DNPTRF/DN09/DNLIST
COMMON /DNIO/DNBAPT/DNI1/DNFLAB/DNI2/DNPTRB
C
INTEGER DNNODE~NODPI2~DNSRCE, DNSINK~I2~II2~K2~J2~IHEAD2~
ITAIL2,QHEAD2,QTAIL2~KP2~KSAT2~DNNOP2~LAYMAX,K2DST~K2DPI,MAXD2
INTEGER DNARC, D N F V A ~ D N A U G , D N L F V A ~ D N L A U G , D N S T G E ~ D N 2 ~ I G
COMMON /DN00/DNARC,DNFVA~DNAUG~DNLFVA~DNLAUG~DNSTGE,
DNELTM~DNIBIG~DNNODE~DNSRCE~DNSINK~DNNOP2
EQUIVALENCE (DNFLOW(1)~DNTO(1)), (DNFLAB(1)~DNDIST(I>)~
(DNFROM(1),DNBADJ(1))~ (DNGCAP(1)~DNBTOF(1))
C
C ~ ~ ~ ~ ~ ~ ~ ~ ~ ~
C CALLING CONDITIONS:
C I N T E R N A L C A L L F R O M D N S U B ONLY.
C INPUT (IN C O M M O N ) :
C SCALARS: DNARC
C ARRAYS: DNFAD~, DNBAD~
C OUTPUT:
D. Goldfarb , M.D. Grigoriadis, Dinic and network simplex methods for max flow 121

C A R R A Y S (IN C O M M O N ) : D N F A D J ~ D N B A D J
C ~ ~ ~ ~ ~ ~ ~ ~ ~ ~
C
C C L E A R M A R K I N G S IN A R R A Y S D N F A D J ( ) A N D D N B A D J ( ) . :
D O 100 I 4 = I ~ D N A R C
IF ( D N B A D J (I4). LT. 0) D N B A D J (14) = - D N B A D J (I4)
IF(DNFADJ(I4).LT.O)DNFADJ(I4)=-DNFADJ(I4)
100 CONTINUE
RETURN
END
SUBROUTINE DNCUT(NNCUT,NACUT)
C
C
C D I M E N S I O N T H E S E T O A T L E A S T M A X N U M B E R O F N O D E S + 2:
INTEGER DNLIST(4700),DNDIST(4700)
INTEGER DNFAPT(4700),DNPTRF(4700),DNBAPT(4700),
DNFLAB(4700),DNPTRB(4700)
C D I M E N S I O N T H E S E T O A T L E A S T M A X N U M B E R O F A R C S + i:
INTEGER DNFADJ(27000),DNFROM(27000),DNTO(27000),DNBADJ(27000)
INTEGER DNCAP(27000),DNGCAP(27000),DNFLOW(27000),DNBTOF(27000)
C
C O M M O N /DNOI/DNFAPT/DN02/DNFADJ/DN03/DNCAP/DN04/DNFROM
C O M M O N /DN05/DNFLOW/DN06/DNGCAP/DN08/DNPTRF/DNO9/DNLIST
C O M M O N /DNIO/DNBAPT/DNll/DNFLAB/DNI2/DNPTRB
C
INTEGER DNNODE,NODPI2, DNSRCE,DNSINK, I2, II2,K2,J2, IHEAD2,
ITAIL2,QHEAD2,QTAIL2,KP2, KSAT2,DNNOP2,LAYMAX,K2DST,K2DPI,MAXD2
INTEGER DNARC,DNFVA, DNAUG, DNLFVA,DNLAUG,DNSTGE,DNIBIG
C O M M O N /DN00/DNARC,DNFVA,DNAUG, DNLFVA,DNLAUG,DNSTGE ,
DNELTM,DNIBIG, DNNODE,DNSRCE,DNSINK,DNNOP2
EQUIVALENCE (DNFLOW(1),DNTO(1)), (DNFLAB(1),DNDIST(1)),
(DNFROM(1),DNBADJ(1)), (DNGCAP(1),DNBTOF(1))
C
C i ~ l i ~ i ~ ~ i ~ ~ ~ ~ ~ ~ ~ ~
C CALLING CONDITIONS:
C ONLY AFTER A SUCCESSFUL RETURN FROM A CALL TO DNSUB.
C INPUT ARRAYS:
C DNFAPT, DNFADJ, DNCAP,DNFLOW, DNDI~T
C OUTPUT ARRAYS:
C DNFAPT,DNFADJ
C P A S S E D IN C A L L I N G S E Q U E N C E :
C NNCUT: NUMBER OF NODES ON source SIDE OF FINAL CUT (INTEGER~4)
C NACUT: N U M B E R O F A R C S IN T H E F I N A L C U T ( I N T E G E R ~ 4 )
C ~ ~ i ~ i ~ i i ~ ! ~ ~ ~ i ~ ~ ~ ~ ~
C
C N E G A T E D N F A P T ( . ) F O R N O D E S O N S I N K S I D E O F CUT:
NNCUT=O
DO 1700 I2=I,DNNODE
IF(DNDIST(I2).NE.DNNOP2)GOTOI650
DNFAPT(12)=-IABS(DNFAPT(12))
GOTOI700
1650 NNCUT=NNCUT+I
1700 CONTINUE
C N E G A T E D N F A D J ( . ) F O R T H O S E S A T U R A T E D A R C S IN M I N CUT:
NACUT=O
DO 1900 I2=I,DNNODE
IF(DNFAPT(I2).LT.0)GOTOI900
C N O D E I2 IS O N s o u r c e S I D E :
IBEG4=IABS(DNFAPT(I2))
IEND4=IABS(DNFAPT(I2+I))-I
122 D. Goldfarb, M.D. Grigoriadis, Dinic and network simplex methods for max flow

DO 1800
I4=IBEG4, IEND4
JJDN=DNFADJ (I4)
IF(DNFAPT(JJDN) ~GT..O)GOT(]I800
C NODE DNFADJ(I4) O N SIIqK S I D E :
IF ( D N F L O W (I4) ~ NE. D N C A P (I4)) G O T O I 8 0 0
I 1 2 = D N F A D J ( 14 )
D N F A D J (I4) = - I A B S ( I I2)
NACUT=NACUT+ I
1800 CONT INUE
1900 CON]" I NUE
RETURN
END
SUBROUTINE DNOUT(IUNREP)
C
C
C DIMENSION THESE TO AT LEAST MAX NUMBER O F N O D E S + 2:
INTEGER DNLIST (4700) ,DNDIST (4700)
INTEGER DNFAPT (4700) ,DNPTRF (4700) ~DNBAPT (4700) ,
* DNI--LAB ( 4 7 0 0 ) , DNPTRB ( 4 7 0 0 )
C DIMENSION THESE TO AT LEAST MAX NUMBER OF ARCS + I :
INTEGER DNFADLI (27~'~00 ) , D N F R O M (2 7 0 0 0 ) ~ D N T O (2 7 0 0 0 ) , D N B A D J ( 2 7 0 0 0 )
INTEGER D N C A P ( 2 7 0 0 0 ) ,D N G C A P ( 2 7 0 0 0 ) , D N F L O W ( 2 7 0 0 0 ) ~ D N B T O F ( 2 7 0 0 0 )

COMMON /DNOI/DNFAPT/DN02/DNFADJ/DN03/DNCAP/DNO4/DNFROM
COMMON /DN05/DNFLOW/DN06/DNGCAP/DN08/DNPTRF/DN09/DNLIST
COMMON /DNIO/DNBAPT/DNII/DNFLAB/DNI2/DNPTRB

INTEGER DNNODE, NODP12, DNSRCE, DNSINK, 1 2 , I I 2 , K 2 , J 2 , IHEAD2,


* I T A I L 2 , QHEAD2, Q T A I L 2 , K P 2 , KSAT2~ DNNOP2, LAYMAX, K2DST, K 2 D P I , MAXD2
INTEGER DNARC, DNFVA, DNAUG, DNLFVA, DNLAUG, DNSTGE, D N I B I G
COMMON /DNOO/DNARC,DNFVA,DNAUG,DNLFVA, DNLAUG,DNSTGE,
* D N E L T M , D N I B I G , DNNODE,DNSRCE,DNSINK,DNNOP2
EQUIVALENCE ( D N F L O W ( 1 ) ~ D N T O ( 1 ) ) ~ ( D N F L A B ( 1 ) , D N D I S T ( 1 ) ) ,
* (DNFROM(1),DNBADJ(1))~ (DNGCAP(1),DNBTOF(1))
DATA B L 3 ~ B L 4 , B S A T , B C U T , J J T / 3 H ,4H ~3HSAT, 4 H - C U T , I H T /
DATA J J S / I H S /
C
************************************************************************
C
C PREPARES OUTPUT REPORT. MAY ONLY BE CALLED AFTER A CALl_ TO DNSUB.
C
************************************************************************
C
C CALLING CONDITIONS:
C USER CALLABLE; ONLY AFTER CALLING DNSUB.
C INPUT ARRAYS:
C DNFAPT, DNFADJ, DNCAP, DNFLOW
C SCRATCH ARRAYS:
C DNLIST
C OUTPUT ARRAYS: NONE
************************************************************************
C
C PRINT SUMMARY SECTION:
WRITE(IUNREP~I000)
* DNNODE,DNARC,DNSRCE,DNSINK,DNFVA,DNELTM,DNAUG,DNSTGE
C PRINT CUT-SET NODES:
K2=0
DO 200 I2=I,DNNODE
IF ( D N E A P T (I2) . LT. 0) G O T 0 2 0 0
K2=K2+I
DNLIST(K2)=I2 123
200 CONTINUE
WRITE(IUNREP~IOO3)K2
WRITE(IUNREP~I004) (DNLIST(I2)~I2=I~K2)
WRITE(IUNREP~I001)
C PRINT ARC LIST:
NTOTC4=O
NSAINC=O
NSA=O
NPOSFL=O
DO 400 I 2 = I ~ D N N O D E
IBEG4=IABS(DNFAPT(I2))
IEND4=IABS(DNFAPT(I2+I))-I
DO 300 I 4 = I B E G 4 ~ I E N D 4
MFLO4=DNFLOW(I4)
IF(MFLO4.EQ.0)GOT0300
NPOSFL=NPOSFL+I
TAGI=BL3
IF(MFLO4.NE.DNCAP(I4))GOT0270
TAGI=BSAT
NSA=NSA+I
27(I TAG2=BL4
C IF(DNFADJ(I4).LT.O)TAG2=BCUT
IF(DNFADJ(I4).GE.0)GOT0280
NTOTC4=NTGTC4+MFL04
NSAINC=NSAINC+I
28(I ISIDE=J~S
JSIDE=ISIDE
IF(DNFAPT(I2).LT.O)ISIDE=JJT
IFAPLU=DNFADO(I4)
IFAPLU=IABS(IFAPLU)
IF(DNFAPT(IFAPLU).LT.O)OSIDE=JJT
IF(ISIDE.NE.JSIDE)TAG2=BCUT
C
WRITE(IUNREP~1002) I2~ISIDE~IFAPLU~JSIDE~
MFLO4~DNCAP(I4)~TAGI~TAG2
300 CONTINUE
400 CONTINUE
C PRINT M A X F L O V A L U E AND C O M P U T E D M I N C U T
WRITE(IUNREP~I006)NPOSFL~NSA~NSAINC
IF(DNFVA.NE.NTOTC4)WRITE(IUNREP~I007)DNFVA~NTOTC4
WRITE(IUNREP~I008)
RETURN
C
C FORMATS:
1000 FORMAT(
~/13H PROBLEM HAS ~I6~7H NODES~I6~24H ARCS~ S O U R C E / S I N K P A I R ~216
~/16H MAX F L O W V A L U E = ~ I I 6 ~ 2 1 H ~ P C / A T E L A P S E D TIME ~ F 8 . 2 ~ 6 H SECS.
~/21H T O T A L A U G M E N T A T I O N S = ~ I 6 ~ 4 H IN ~I6~7H STAGES)
1001 F O R M A T ( / 2 5 H ARCS WITH P O S I T I V E F L O W
~I123H TAIL HEAD~
40H FLOW CAPACITY STATUS/)
1002 FORMAT(IIO~IX~AI~IIO~IX~AI~2II5~4X~A3~A4)
1003 F O R M A T ( / 1 0 H T H E R E A R E ~ I S ~ 2 9 H N O D E S IN S O U R C E S I D E OF C U T ~
10H THEY ARE /)
1004 FORMAT(1016)
1006 FORMAT(17H NOTES
~ / 1 0 X ~ I 3 H I) T H E R E ARE, I 6 ~ 2 6 H A R C S W I T H P O S I T I V E FLOW~ ~ I 5 ~ 3 H OF
~/10X~22H T H E M S A T U R A T E D (~I5~26H OF W H I C H ARE IN MIN CUT).
~ / 1 0 X ~ 5 2 H 2) S / T R E F E R TO S O U R C E / S I N K S I D E S OF CUT.)
1007 FORMAT(
1 0 X ~ 4 1 H 3) MAX F L O W V A L U E F R O M S O U R C E TO S I N K IS~II4~IH~
~/IOX~41H BUT~ C U T C A P A C I T Y W A S C O M P U T E D AS ~I14)
1008 F O R M A T ( / 1 5 H END OF REPORT.)
END

You might also like