Edmonds_karp algorithm Introduction (GO)

Source: Internet
Author: User

Reprinted from: http://blog.csdn.net/hsqlsd/article/details/7862903

There are n points, there is a forward edge of M bar, a point is very special, only out, called the source point, usually specified as 1th points. The other point is also very special, only can not enter, called the meeting point, usually specified as N point. Each having a forward edge has two quantities, capacity and flow, the capacity from I to J is usually expressed in c[i,j], the flow is usually f[i,j]. These edges can usually be imagined as a road, traffic is the flow of the road, capacity is the road can withstand the largest traffic. It is clear that the flow <= capacity. For each point that is not a source and sink point, it can be imagined as a stopover for goods without storage functions, all "into" their traffic and equal to all the traffic from him "out".

The problem with comparing the source point to the factory is to ask for the maximum number of goods that can be emitted from the factory, which is not to exceed the capacity limit of the road, that is, the maximum flow.

Like this picture. The number next to each edge indicates its capacity.

First, if the traffic on all sides does not exceed capacity (not greater than capacity), then this group of traffic, or, this flow, is called a feasible stream. One of the simplest examples is that 0 streams, that is, all traffic is 0 streams.

We start with this 0 flow, if there is such a road, the road from the source point has been a section of the connection to the meeting point, and, each section of the road to meet the flow < capacity, attention, is strict < Well, we can certainly find the minimum delta in each segment of the path (capacity-flow). We add this delta to each segment of the road, and it's clear that this flow is still a viable flow.

So we get a bigger flow, and his traffic is +delta the previous traffic, and this road is called the augmented path.

We continue to look for the augmented path from the starting point, each time it is augmented, until the source and sink point is not connected, that is, the augmentation path can not be found. when the augmented path is not found, the current flow is the maximum flow , the conclusion is very important.

When looking for an augmented path, we can simply start with the BFS from the source, and constantly modify the delta on this path until the source point is found or the augmented path cannot be located.

First of all, in the implementation of the program, we usually just use a C array to record capacity, and do not log traffic, when the traffic +1, we can through the capacity-one implementation, to facilitate the implementation of the program.

But in fact it is not so simple, the above-mentioned augmentation path is not complete, such as the following network flow model.

The first time we found the 1-2-3-4, the delta was obviously 1. So we modified it and got the following stream. (the number in the figure is the capacity)

At this time and (3,4) side of the traffic is equal to the capacity, we can no longer find the other augmented road, the current flow is 1.

But the answer is obviously not the maximum flow, because we can go 1-2-4 and 1-3-4 at the same time, so we can get a flow of 2.

So where's the problem with the algorithm we just had? The problem is that we do not give the program a "regret" opportunity, there should be a 2-3-4 (2-4) and change the mechanism. So how do we solve this problem? Search backwards? Then our efficiency goes up to an exponential level.

And this algorithm magically uses a concept called the opposite side to solve the problem. That is, each edge (I,J) has a reverse edge (j,i), and the opposite edge also has its capacity.

We look directly at how it is resolved:

After the first finding of the augmentation path, the capacity of each segment of the road is reduced by Delta, and the counter-direction capacity of each segment is increased by Delta. At the same time as Dec (C[x,y],delta), Inc (C[y,x],delta)

Let's take a look at the example above, and after finding the 1-2-3-4 this augmented road, change the capacity to the following

Then find the augmented road, you will find 1-3-2-4 this can be augmented, that is, the delta value of 1 can be augmented road. After this path is augmented, the maximum flow is 2.

So why would it be right to do so?

In fact, when our second augmented road 3-2 this opposite side, it is equivalent to 2-3 this positive edge is already used flow to "back" back, do not go 2-3 this road, and change away from 2 points from the other road is 2-4. (Someone asked if there is no 2-4 what to do, then if there is no 2-4 this road, eventually this augmented road will not exist, because he could not go to the meeting point) at the same time, the flow from the 3-4 is 1-3-4 this road to "take over." And finally 2-3 this road is going to flow 1, reverse flow 1, equals no traffic.

This is the essence of the algorithm, using the opposite edge, so that the program has a chance to regret and correct.

Attached EDMONDS_KARP algorithm template

#defineMAXN 220#defineINF 0x7f7f7f7fintCAP[MAXN][MAXN],FLOW[MAXN][MAXN]; intPRE[MAXN],RES[MAXN];//res[i] Residual quantityintEdmonds_karp (intStartintend) {      intmaxflow=0; memset (Flow,0,sizeof(flow)); memset (PRE,0,sizeof(pre)); Queue<int>Q;  while(true) {memset (res,0,sizeof(res)); Res[start]=INF;          Q.push (start);  while(!q.empty ())//BFS looking for augmented road        {              intu=Q.front ();              Q.pop ();  for(intv=1; v<=end;v++)                  if(!res[v]&&cap[u][v]>Flow[u][v]) {Res[v]=min (Res[u],cap[u][v]-flow[u][v]);//minimum residuals on the start-v path//Record V's father, and join the queuepre[v]=u;                  Q.push (v); }          }          if(res[end]==0)returnMaxflow;//cannot continue to update maximum traffic, the current stream is already the maximum stream         for(intU=end;u!=start;u=pre[u])//go back from the meeting point.{Flow[pre[u]][u]+=res[end];//Update forward FlowFlow[u][pre[u]]-=res[end];//Update Reverse Flow} maxflow+=res[end];//Update total traffic outflow from S    }  }  intMain () {//memset (Cap,0,sizeof(CAP)); //       for(/**/)      {          intu,v,s; scanf (" %d%d%d",&u,&v,&s); CAP[U][V]+=s;//to take into account the problem of heavy edges    }      //      return 0;}

Edmonds_karp algorithm Introduction (GO)

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.