Graph matching problem and maximum flow problem (III.) maximum flow problem Ford-fulkerson method Java implementation

Source: Internet
Author: User

The last article mainly introduces the theoretical basis of Ford-fulkerson method, and gives a Java implementation in this paper.

To familiarize yourself with the following process with pseudo code first

Ford-fulkerson (G,t,s)

1 for each edge (U,V) belongs to E (G)

2 Do f[u,v]=0

3 f[v,u]=0

4 While there exists a path p from s to T in the residual network Gf

5 do CF (P) =min{cf (U,V):(u,v) was in P}

6 for each edge (U,V) in P

7 do F[U,V]=F[U,V]+CF (p)

8 F[v,u]=-f[u,v]

If we use breadth-first search in 4 lines to compute the augmented path p, that is to find the shortest augmented path of s to T, we can improve the bounds of Ford-fulerson, which is the edmonds-karp algorithm of Ford-fulkerson method.

It is proved that the running time of the algorithm is O (ve*e), and it is easy to know, the upper bound of all times of increasing convection is O (VE), each iteration time O (E)

Package maxflow;  
Import java.util.ArrayList;  
Import java.util.LinkedList;  
Import java.util.List;  
      
Import Java.util.Queue; Import util.  
Edgeutil; Import util.  
Nodeutil; Import entry.  
Edge; Import entry.  
Node; /** * Ford Fulkerson method for maximum flow, this is an iterative method, starting with the initial flow of 0, each iteration, the class by looking for an augmented path to increase the flow value. Repeat this process until you cannot find any augmented paths * This algorithm uses the EDMONDS-KARP algorithm (an implementation of the Ford Fulkerson method) and uses a method of finding the shortest path of s to T in search of the augmented path. Complexity O (VE2) * @author XHW * * */public class Fordfulkerson {private static double Residualnetwork[][]=nul  
    L  
          
    private static double flownetwork[][]=null;   
                          /** * @param args */public static void main (string[] args) {double graph[][]={{0,16,13,0,0,0}, {0,0,10,12,0,0}, {0,4,0,0,14,0}, {0,0  
              
        , 9,0,0,20}, {0,0,0,7,0,4}, {0,0,0,0,0,0}}; System.out.println (Edmondskarpmaxflow(graph,0,5)); /** * An algorithm for implementing the Fordfulkerson Method--edmondskarp algorithm * @param graph * @param s * @param t * @ return */public static double Edmondskarpmaxflow (double graph[][],int s,int t) {int Length=gra  
        Ph.length;  
        list<node> nodelist=nodeutil.generatenodelist (graph);  
        Double F[][]=new Double[length][length]; for (int i=0;i<length;i++) {for (int j=0;j<length;j++) {F[i]  
            [J]=0;  
              
        } double R[][]=residualnetwork (graph,f);  
        Node Result=augmentpath (r,s,t);  
        Double sum=0;  
            while (Result!=null) {double cfp=0;  
            Cfp=minimumaugment (R,result);  
            Indicates that there is no augmented path to the IF (cfp==0) {break;  
       while (Result.getparent ()!=null)     {Node parent=result.getparent ();  
                F[PARENT.NODEID][RESULT.NODEID]+=CFP;  
                      
                F[result.nodeid][parent.nodeid]=-f[parent.nodeid][result.nodeid];  
            Result=parent;  
            } SUM+=CFP;  
            R=residualnetwork (GRAPH,F);  
                  
        Result=augmentpath (r,s,t);  
        } residualnetwork=r;  
              
        Flownetwork=calculateflownetwork (GRAPH,R); 
                     
                /*for (int i=0;i<length;i++) {for (int j=0;j<length;j++) { 
            System.out.print ((flownetwork[i][j]>0?flownetwork[i][j]:0.0) + ""); 
        } System.out.println ();  
    }*/return sum; /** * Calculates the final stream network, which is the maximum stream network * @param graph * @param r * @return/private static Double[][] Calculateflownetwork (double[][] graph, double[][] r) {int length=graph.length;  
        Double F[][]=new Double[graph.length][graph.length]; for (int i=0;i<length;i++) {for (int j=0;j<length;j++) {F[i]  
            [J]=graph[i][j]-r[i][j];  
    } return F; /** * Determine augmented path expandable Stream value * @param graph * @param result * @return/public s  
        Tatic Double minimumaugment (double graph[][],node result) {double cfp=double.max_value;  
                  
            while (Result.getparent ()!=null) {Node parent=result.getparent ();  
            Double Weight=graph[parent.nodeid][result.nodeid];  
            if (weight<cfp&&weight>0) {cfp=weight;  
                else if (weight<=0) {cfp=0;  
   Break         } result=parent;  
    return CFP; /** * COMPUTE Residual network * @param c * @param f * @return * * private static double[]  
        [] residualnetwork (double c[][],double f[][]) {int length=c.length;  
        Double R[][]=new Double[length][length]; for (int i=0;i<length;i++) {for (int j=0;j<length;j++) {R[i]  
            [J]=c[i][j]-f[i][j];  
    } return R; /** * Breadth-first traversal, looking for the path of Grace, is also the shortest augmented path * @param graph * @param s * @param t * @r  
        Eturn */public static node Augmentpath (double graph[][],int s,int t) {Node result=null;  
        list<node> nodelist=nodeutil.generatenodelist (graph);  
        Nodelist.get (s). Distance=0;  
              
        Nodelist.get (s). state=1; Queue<node> queue=new LINKEDLIST&LT  
        Node> ();  
              
        Queue.add (Nodelist.get (s));  
            while (!queue.isempty ()) {Node u=queue.poll ();  
                    For (Node n:u.getadjacentnodes ()) {if (n.state==0) {  
                    n.state=1;  
                    n.distance=u.distance+1;  
                    N.setparent (U);  
                Queue.add (n);  
            }} u.state=2;  
                if (u.nodeid==t) {result=u;  
            Break  
              
    } return result;  
                  
    public static double[][] Getresidualnetwork () {return residualnetwork;  
    public static double[][] Getflownetwork () {return flownetwork; }  
      
      
      
}

Back to the column page: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/sjjg/

Author: csdn Blog Smartxxyx

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.