-ek algorithm of network maximum flow algorithm

Source: Internet
Author: User
Tags stdin

Objective

The EK algorithm is the most basic algorithm to find the maximum flow of the network, and it is a better understanding algorithm, which can solve most of the maximum flow problems.

However, this algorithm often has the risk of tle because of the limitation of time complexity.

Thought

Remember the solution we mentioned when we introduced the maximum flow?

For a network flow graph, each time it is found its minimum residue (can be augmented by the amount), it is augmented.

Yes, the EK algorithm uses this idea to solve the problem.

Realize

When the EK algorithm is implemented, it needs to traverse one side of the entire graph.

So how do we traverse it? BFS or Dfs?

Because of the DFS search order reason, so some cancer people will construct a data card you, specific how to card should be relatively simple, but in order to prevent everyone become such a person I don't say (#^.^#)

So we choose BFS

As you traverse the graph, record the maximum value that can be augmented and record which edges the maximum value passes.

We'll broaden the edge of this augmented road after we've traversed it.

Code

Here's the question.

in the code I added some notes to some of the key points, if I didn't write it down. Welcome to comment below : blush:

#include <iostream>#include<cstdio>#include<cstring>#include<cmath>#include<queue>using namespacestd;Const intmaxn=2*1e6+Ten;Const intinf=1e8+Ten; inlineCharNC () {Static CharBuf[maxn],*p1=buf,*p2=buf; returnp1==p2&& (p2= (p1=buf) +fread (buf,1, Maxn,stdin), P1==P2)? eof:*p1++;} InlineintRead () {CharC=NC ();intx=0, f=1;  while(c<'0'|| C>'9'){if(c=='-') f=-1; c=NC ();}  while(c>='0'&&c<='9') {x=x*Ten+c-'0'; c=NC ();} returnx*F;}structnode{intU,V,FLOW,NXT;} EDGE[MAXN];intHEAD[MAXN];intnum=0;//Note here num must start from 0InlinevoidAdd_edge (intXintYintz) {edge[num].u=x; EDGE[NUM].V=y; Edge[num].flow=Z; EDGE[NUM].NXT=Head[x]; HEAD[X]=num++;} InlinevoidAddedge (intXintYintz)    {Add_edge (x, y, z); Add_edge (Y,x,0);//look, don't forget to add the reverse side here.}intn,m,s,t;intPATH[MAXN];//the path passedintA[MAXN];//s to the minimum flow of the nodeInlineintEK () {intans=0;//Maximum Flow     while(true)//keep looking for the augmented path.{memset (A,0,sizeof(A)); Queue<int>q;//Lazy Handwriting Queue ... Q.push (S); A[s]=INF;  while(Q.size ()! =0)        {            intp=Q.front (); Q.pop ();  for(inti=head[p];i!=-1; i=edge[i].nxt) {                if(! a[edge[i].v]&&Edge[i].flow) {path[EDGE[I].V]=i;//record the path passed to facilitate later augmentationA[edge[i].v]=min (A[p],edge[i].flow);//record the minimum flowQ.push (EDGE[I].V); }            }            if(A[t]) Break;//a small optimization        }        if(! A[T]) Break;//no path can be augmented, exit directly         for(intI=T;I!=S;I=EDGE[PATH[I]].U)//go back and broaden.{Edge[path[i]].flow-=A[t]; Edge[path[i]^1].FLOW+=A[T];//Use the XOR operator to find the opposite side, 0^1=1 1^1=0} ans+=A[t]; }    returnans;}intMain () {#ifdef WIN32 freopen ("a.in","R", stdin); #else    #endif memset (Head,-1,sizeof(head)); N=read (), M=read (), S=read (), t=read ();  for(intI=1; i<=m;i++)    {        intX=read (), Y=read (), z=read ();     Addedge (x, y, z); } printf ("%d", EK ()); return 0;}

Performance analysis

It is not difficult to see that the performance of this algorithm is good,

But you can submit it here to see how fast the algorithm is (man).

It can be proved that the time complexity of this algorithm is $o (n*m^2) $

General Proof:

In the worst case, we only need to augment one edge at a time, and we have to augment $m-1$ times.

In BFS, the worst case is $n*m$ due to the presence of a reverse arc

The total time complexity is $O (n*m^2) $

Postscript

The EK algorithm is here to end.

But how can loj that problem?

This is going to use the other algorithms we're going to talk about.

-ek algorithm of network maximum flow algorithm

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.