Java实现Ford-Fulkerson算法求解最大流问题

 @author keshansheng
 * @ Version 1.0
 * @since JDK1.8
 */
public class Char {
    public static final double EPS =1e-10 ;
    /**
     * marked[u] =true iff s->u is a path in the residual graph./
     */
    private boolean[] marked;
    /**
     * edgesTO[u] = last edge on shortest  resiudual s->u path. */
    private FlowEdge[] edgesTO;
    /**
     * value = max flow in the network from s to t. */
    private double value;
    /**
     * Compute a mam flow and min cut in the network g from vertex s to t;<br></>
     * The  key is increasing flow  along augfmenting paths.
     *
     * @param g the flow network
     * @param s the source vertex
     * @param t the sink vertex
     *
     **/
        public Char(FlowNetwork g, int s, int t) {
            validate(s,g.getVertexNum());
            validate(t,g.getVertexNum());
            if(s == t){
            throw new IllegalArgumentException( "Source equals sink");
        }
            if(! isFeasible(g,s,t)) {
                throw new IllegalArgumentException("Initial flow is infeasible");
            }
            //While there exists an augmenting path,use it (the Value).
            value = exceess(g,s);
            while(hasAugmentingPath(g,s,t)) {
                // 1 compute bottleneck capacity
                //Find the min capacity of the augmenting path.
                double min = Double.POSITIVE_INFINITY;
                for(int v = t; v != s; v = edgesTO[v].other(v)) {
                    bottleneck = Math.min(bottleneck.edgeTo[v].residualCapacityTo(v));
                }
                //2 update the flow on the augment path 
                for(int v=t; V!= s; v = edgeTo[v].other[v])) {
                    bottleneck = Mach.min(bottleneck, edgeTo[v].residualCapacityTo(v));
                    
                }
                //2 update the flow on the augment path 
                for (int v = t; v !=s;v=edgeTo[v].other(v)){
                    edgeTo[v].addResidualFlowTo(v,bottleneck);
                    {
                        //3 update current "max-flow"
                        value += bottleneck;
                    }
                    //if there is on augmenting path,then the max-flow/min-cut is found.
                }
                /***
                 * Is uin the s side of the mins-t cut?/Is u reachable from s in the residual network?
                 * @param u the vertex
                 * @return true if  uertex is on the side of mincut,false otherwise 
                 */
                public boolean inCut(int v){
                    validata(v, marked.length);
                    residualCapacityTo(v);
                      return marked[v];
                }
                }
                //2 update the flow on the augment path
                for(int v =t; v!=s; v = edgeTo[v].other(v)) {
                    bottleneck = Math.min(bottleneck,edgesTO)
                    
                }




                            min = Math.min(min,edgesTO[v].residualCapacity());
                    min = Math.min(min,edgesTO[v].residualCapacity());

                }
                //Augment the flow.
                for(int v = t; v != s; v = edgesTO[v].from()) {}

            }
            while(hasAugmentingPath(g,s,t)) { }··
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值