You are on page 1of 4

MATLAB SOLUTIONS FOR MAXFLOW/MINCUT AND FORD FULKERSTON ALGORITHM

Borivoje Milosevic
Technical College VTS, Nis, Serbia

Slobodan Obradovic
Faculty for Computer Science, Belgrade, Serbia

Abstract A common question about networks is what is the maximum flow rate between a given node and some other node in the network? When the system is mapped as a network, the arcs represent channels of flow with limited capacities. To find the maximum flow, assign flow to each arc in the network such that the total simultaneous flow between the two end-point nodes is as large as possible. The maximum flow problem can be solved by linear programming, but the Ford and Fulkerson method is simple and even faster than linear programming when implemented on a computer. Min cut problem delete "best" set of edges to disconnect source from destination in the network graph. In the network, we use the Ford-Fulkerson Algorithm to find a flow of maximum value and a cut of minimum value. Keywords: MaxFlow, MinCut, Ford Fulkerson, Sparse matrix, .

INTRODUCTION Max-flow and linear programming are two big hammers in algorithm design: each are expressive enough to represent many polytime solvable problems. A flow network N=G(V;E) is formally defined as a fully connected directed graph where each edge (u; v) in E has a positive capacity c(u; v) 0. Two special vertices in a flow network are designated the source s and the sink t respectively. To find the maximum flow, we have to assign flow to each arc in the network, such that the total simultaneous flow between the two end-point nodes is as large as possible. Finding the maximum flow involves looking at all of the possible routes of flow between the two end-points in question. Max flow problem assigns flow to edges so as to equalize inflow and outflow at every intermediate vertex. Solution of the min cut problem is to find an s-t cut of minimum capacity. capacity(S, T) = sum of weights of edges leaving S. The max-flow min-cut theorem states: The maximum value of an s-t flow is equal to the minimum capacity of an s-t cut.

11

1 1

INTERNATIONAL SCIENTIFIC CONFERENCE 18 19 November 2011, GABROVO

Zoran Mijovic
Serbian Railway, Nis, Serbia

A cut in a network (or just a digraph) is a set of arcs such that if they are removed, there is not a path from the source to the sink. EXPOSITION Ford and Fulkerson first published their method in the Canadian Journal of Mathematics in 1956 it is a real classic paper, very often referenced to this day. The main idea is careful bookkeeping of the flows assigned to different routes from the origin node to the destination node. The steps in the method are to: 1. Find any path from the origin node to the destination node that has a strictly positive flow capacity remaining. If there are no more such paths, exit. 2. Determine f, the maximum flow along this path, which will be equal to the smallest flow capacity on any arc in the path (the bottleneck arc). 3. Subtract f from the remaining flow capacity in the forward direction for each arc in the path. Add f to the remaining flow capacity in the backwards direction for each arc in the path. 4. Go to Step 1.

On termination, the sum of the flows along the paths found during Step 1 gives the maximum total flow between the origin and destination nodes. The Ford-Fulkerson Algorithm is really quite natural. We start with no flow at all, that is, with every xi,j set equal to 0. Then we find what is called an augmenting path from the source to the sink. This is, as it says, a path from the source to the sink, that has excess capacity. We then figure out how much more we could pipe down that path and add this to the flow we are building. We will apply the algorithm to the network from Figure 1.
4 2 5 8 1 9 3 3 2 9 5 1 6 4 3

And let take the path 1 2 4 56


arc 12 24 45 56 total capacity 5 4 1 9 current load 3 3 0 3 excess capacity tc - cl 2 1 1 6 new capacity min(tc cl)+cl 3+1=4 3+1=4 0+1=1 3+1=4

The updated MaxFlow is shown in Figure 2.


4 2 4 0 1 5 3 3 2 4 5 1 6 4 3

Fig. 2. New Network MaxFlow Graph

Fig. 1. Network Graph

If we start with the path 1 3 6 we ask how much more stuff we can pipe down this route. We can only pipe the minimum of excess capacity numbers, so we will only be able to send 2 units of stuff down this path.
arc 13 36 total capacity 9 2 current load 0 0 excess capacity tc - cl 9 2 new capacity min(tc cl)+cl 2+0=2 2+0=2

Were managing to get 9 units from the source to the sink. Then, trying to move stuff from the source to the sink depends very much on the paths from the source to the sink. It is depends on the ways in which those paths can be cut off. Then, maximum value of an s-t flow is equal to the minimum capacity of an s-t cut, Figure 3.
4 2 5 8 1 9 3 3 2 9 5 1 6 4 3

And lets take the path 1 3 5 6 to start with. Current load becomes new capacity and is equal: min(tc cl)+cl:
arc 13 35 56 total capacity 9 3 9 current load 2 0 0 excess capacity tc - cl 7 3 9 new capacity min(tc cl)+cl 3+2=5 3+0=3 3+0=3

Fig. 3. New Network MinCut Graph with capacity 3+1+2+3=9

This time lets take the path 1 2 4 6.


arc 12 24 46 total capacity 5 4 3 current load 0 0 0 excess capacity tc - cl 5 4 3 new capacity min(tc cl)+cl 3+0=3 3+0=3 3+0=3

And now, let's try to solve this problem in the MATLAB way: First we use sparse function. The sparse function generates matrices in the MATLAB sparse storage organization. S = sparse(A) converts a full matrix to sparse form by squeezing out any zero elements. If S is already sparse, sparse(S) returns S. S = sparse( i,j,s,m,n ) uses nzmax = length(s) and uses vectors i, j, and s to generate an m-by-n sparse matrix such that S(i(k),j(k)) = s(k), with space allocated for nzmax nonzeros. Vectors i, j,

11

and s are all the same length. Any elements of s that are zero are ignored, along with the corresponding values of i and j. Any elements of s that have duplicate values of i and j are added together. The incidence tabela of the graph in Figure 1 is
Source Destination Capacity 1 2 5 1 3 9 2 4 4 3 4 8 3 5 3 3 6 2 4 5 1 4 6 3 5 6 9

For next values of parameters: M = The Maximum Flow F = Flow on each link K = Minimum cut ( result displayed in matrix ) cm = Refers to the collection of data we inputted early, a N x N matrix 1 = The Source node 6 = The Destination node We applied the algorithm Maximum Flow / Minimum Cut using the command: [M,F,K] = graphmaxflow(cm,1,6); System respond: M= 9 F = (1,2) 4 (1,3) 5 (2,4) 4 (3,5) 3 (4,5) 1 (3,6) 2 (4,6) 3 (5,6) 4 K= 1 1 1 1 0 0 For MaxFlow solution: view(biograph(F,[],'ShowWeights','on')) Viewing the Maximum Flow Network Diagram, Figure 5.

Based on tables sparse function looks like: cm = sparse([1 1 2 3 3 3 4 4 5],[2 3 4 4 5 6 5 6 6],[5 9 4 8 3 2 1 3 9],6,6) and system respond: cm = (1,2) 5 (1,3) 9 (2,4) 4 (3,4) 8 (3,5) 3 (4,5) 1 (3,6) 2 (4,6) 3 (5,6) 9 To see a graph of the core network represented by these parameters MATLAB uses the command: view(biograph(cm,[],'ShowWeights','on' and system respond: Biograph object with 6 nodes and 9 edges. Then draw line graph of primary network, nodes and capacities, Figure 4.

Fig. 4. MATLAB Network Graph

Fig. 5. Maximum Flow Network Diagram 12456

11

For MinCut solution, MATLAB command is: set(h.Nodes(K(1,:)),'Color',[1 0 0]) And view of the Minimum Cut is Figure 6. NOTE: Chart graph of basic network must be open!

CONCLUSION Function [ MaxFlow, FlowMatrix, Cut ] = graphmaxflow(G, SNode, TNode) calculates the maximum flow of directed graph G from node SNode to node TNode. Input G is an N-byN sparse matrix that represents a directed graph. Nonzero entries in matrix G represent the capacities of the edges. Output MaxFlow is the maximum flow, and FlowMatrix is a sparse matrix with all the flow values for every edge. FlowMatrix(X,Y) is the flow from node X to node Y. Output Cut is a logical row vector indicating the nodes connected to SNode after calculating the minimum cut between SNode and TNode. If several solutions to the minimum cut problem exist, then Cut is a matrix. We demonstrated that the Max Flow-Min Cut algorithm has a very good Matlab solution. REFERENCE
[1] Vince Vatter, Graphs, Flows, and the FordFulkerson Algorithm, August 12, 2004 [2]Alexander Schrijver, A Course in Combinatorial Optimization, Department of Mathematics, University of Amsterdam, Plantage Muidergracht 24, 1018 TV Amsterdam, The Netherlands. [3] Max Flow Min Cut Algorithm, jacques@ucsd.edu [4] John W. Chinneck, Practical Optimization: a Gentle Introduction, 2000,
http://www.sce.carleton.ca/faculty/chinneck/po.html

Fig. 6. Maximum Cut Network Diagram with capacity 2 + 1 + 3 + 3 = 9

11

You might also like