Finding M in The Network: A Matlab Program and Application: Aximum Flow
Finding M in The Network: A Matlab Program and Application: Aximum Flow
Article
WenJun Zhang
School of Life Sciences, Sun Yat-sen University, Guangzhou 510275, China; International Academy of Ecology and
Environmental Sciences, Hong Kong
E-mail: zhwj@mail.sysu.edu.cn, wjzhang@iaees.org
Received 9 August 2016; Accepted 6 October 2017; Published online 1 June 2018
Abstract
Maximum flow problems are expected occurring in some biological networks. As early as in 1950s, Ford and
Fulkcerson proposed an algorithm to find maximum flow in a network. In this study I presented the full codes
of Ford-Fulkcerson algorithm and given an application example.
Computational Ecology and Software
ISSN 2220721X
URL: http://www.iaees.org/publications/journals/ces/onlineversion.asp
RSS: http://www.iaees.org/publications/journals/ ces/rss.xml
Email: ces@iaees.org
EditorinChief: WenJun Zhang
Publisher: International Academy of Ecology and Environmental Sciences
1 Introduction
Suppose there are n nodes in a network. The node v0 is the source that has not any terminal link, and the node
vn is the sink node that has not any initial link. The weight cij of directed link eij is the flow capacity of the link.
fij is the flow of the link, 0fijcij, and the in-flow sum and out-flow sum of node vj are the same (Ford and
Fulkcerson, 1956, 1957; Zhang, 2012, 2018)
n-1 n
∑fij=∑fjk, j=1,2,..., n-1
i=0 k=1
which means that the out-flow sum of source node is equal to the in-flow sum of sink node. The maximum
flow problem is
n
max ∑f0j
j=1
Maximum flow problems are expected occurring in ecological networks and other types of biological
IAEES www.iaees.org
58 Computational Ecolgoy and Software, 2018, 8(2): 57-61
networks. Ford and Fulkcerson (1957) proposed an algorithm to find maximum flow in a network. In this
study I present the full Matlab codes of Ford-Fulkcerson algorithm and give an application example.
2 Algorithm
Ford-Fulkerson (1957) algorithm for maximum flow is as follows (Chan et al, 1982; Zhang, 2012, 2018):
(1) Labeling.
(a) Label the source node vs with (+, +∞), ds = +∞.
(b) Choose a labeled node x. For all unlabeled adjacent nodes y of x, handle them by the following rules
If yxE and fyx>0, let dy=min {fyx , dx}, and label y with (x, dy).
If xyE and fxy<Cxy, let dy =min {Cxy-fxy, dx}, and label y with (x+, dy).
(c) Repeat the step (b) until the sink node vt has been labeled or no more nodes can be labeled. If vt is
labeled, then there exists an augmenting chain, and return the procedure (2) to adjust the process; if vt is not
labeled, labeling process is not able to be conducted, and f is thus the maximum flow.
(2) Adjusting.
(a) Determine adjusting magnitude d=dvt, and let u=vt .
(b) If the node u is labeled by (v+, du), then replace fvu with fvu +d, and if the node u is labeled by (v, du),
then replace fvu with fvu+d.
(c) If v=vs, then remove all labels and return the procedure (1) for labeling again; otherwise let u=v,
return (b).
If the computation terminates, let the set of labeled nodes be S, and cutset (S, Sc) is the minimum cut, and
the maximum flow is Mf =C(S, Sc).
The following are Matlab codes, maxiFlow.m, for Ford-Fulkerson algorithm. The Matlab algorithm needs
the user to load an excel file that stores the Two Array Listing data of the form (d1i, d2i, ci), where d1i, d2i, and
ci are start node and end node of the link i, and the flow capacity of the link i, respectively, i=1,2,…,e.
IAEES www.iaees.org
Computational Ecolgoy and Software, 2018, 8(2): 57-61 59
end; end
for i=1:v
no(i)=0;
d(i)=0;
end
pr=1;
while(v>0)
no(1)=v+1;
d(1)=1e+30;
while(v>0)
pr=1;
for i=1:v
if (no(i)~=0)
for j=1:v
if ((no(j)==0) & (f(i,j)<c(i,j)))
no(j)=i;
d(j)=c(i,j)-f(i,j);
pr=0;
if (d(j)>d(i)) d(j)=d(i); end
end
if ((no(j)==0) & (f(j,i)>0))
no(j)=-i;
d(j)=f(j,i);
pr=0;
if (d(j)>d(i)) d(j)=d(i); end
end
end
end; end
if ((no(v)~=0) | (pr~=0)) break; end
end
if (pr~=0) break; end
dv=d(v);
s=v;
while (v>0)
if (no(s)>0) f(no(s),s)=f(no(s),s)+dv; end
if (no(s)<0) f(no(s),s)=f(no(s),s)-dv; end
if (no(s)==1)
for i=1:v
no(i)=0;
d(i)=0;
end
break;
end
s=no(s);
end; end
IAEES www.iaees.org
60 Computational Ecolgoy and Software, 2018, 8(2): 57-61
mf=0;
for j=1:v
mf=mf+f(1,j);
end
fprintf(['Maximum flow matrix:' '\n']);
f
fprintf(['Maximum flow=:' num2str(mf) '\n']);
fprintf(['Labels for minimum cut:' '\n']);
no
3 Application Example
As an example, suppose there are 8 nodes and 11 links in a network. The data are as follows
Using the algorithm above, the maximum flow matrix is achieved as the following
0 2 1 2 0 0 0 0
0 0 0 0 0 2 0 0
0 0 0 0 0 0 0 1
0 0 0 0 0 0 2 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 2 0
0 0 0 0 0 0 0 4
0 0 0 0 0 0 0 0
The maximum flow is 5. And the labels for minimum cut are, 9, 1, 0, 1, 0, 2, 6, 0.
IAEES www.iaees.org
Computational Ecolgoy and Software, 2018, 8(2): 57-61 61
References
Chan SB, et al. 1982. Graph Theory and Its Applications. Science Press, Beijing, China
Ford LR Jr, Fulkerson DR. l956. Maxima1 flow through a network. Canadian Journal of Mathematics, 8:
399-404
Ford LR Jr, Fulkerson DR. l957. A simple algorithm for finding maximal network flow and application to the
Hitchcock problem. Canadian Journal of Mathematics, 9: 210-218
Zhang WJ. 2012. Computational Ecology: Graphs, Networks and Agent-based Modeling. World Scientific,
Singapore
Zhang WJ. 2018. Fundamentals of Network Biology. World Scientific, Singapore
IAEES www.iaees.org