Graph theory max stream ek algorithm

Source: Internet
Author: User

Today contact the largest stream, the web has a lot of PPT, patience to see, and then hit a few times the code will probably understand the meaning of

EK algorithm

The key is to understand the meaning of estoppel, because every time when you choose a way, but this approach is not necessarily optimal, so we have to create a reverse side,

To complete the policy of estoppel.

Then it is probably always looking for the augmented path, changing the maximum value, until the augmentation path can not be found

Now attach the code to the template and give the comment

There are two ways to do this is the purple book Rujia code, there is a link to the star, but also a kind of Dfs way, will continue to add

Two kinds of code are given separately

struct edge{
int from,to,cap,flow;

From: Start side, to: End Edge, Cap: capacity, Flow: traffic

Edge (int u,int v,int c,int f): From (U), to (v), Cap (c), Flow (f) {}//built-in function
};
struct edmondskarp{
int n,m;
Vector<edge> edges; //Edges is used to save all edges, forward to opposite edges
Vector<int> G[MAXN]; //Used to record the ordinal number of each side of the first point
int A[MAXN]; //Can be used to save the changed value on the path
int P[MAXN]; //used to log in-number


void init (int n) {
for (int i=0;i<n;i++) g[i].clear ();
Edges.clear ();
}


void Addedge (int from, int to, int cap) {

The addition of edges is very tricky.

Because we can go back, so we have to deposit the opposite side, the reverse side of the capacity of 0

Of course, sometimes, depending on the test instructions, the capacity of the opposite side is not 0, so it is stored according to the data given by the topic.

Edges.push_back (Edge (from, to, Cap, 0));
Edges.push_back (Edge (to, from, 0, 0));
m = Edges.size ();

//This also has the skill, actually this and the chain type forward to the star very much resembles (two changes the storage cnt++, but is also together, this also )

each time the number of edges connected by this point is push in
g[from].push_back (m-2);
g[to].push_back (m-1);
}
int maxflow (int s,int t) {
Span style= "White-space:pre" > int flow = 0;
for (;;) {
CLR (a);
queue<int> Q;
Q.push (s);
A[s]=inf;
while (! Q.empty ()) {
int x = Q.front ();
Q.pop ();
for (int i=0;i<g[x].size (); i++) {

Access all the edges in this point
edge& e = edges[g[x][i]];

E get information on this side
if (!a[e.to] && e.cap>e.flow) {

This part is more difficult to understand.

! A[E.TO] Minimum residue of 0 is only updated if no update has been updated for 0 instructions

The current capacity must be greater than the traffic
P[e.to]=g[x][i];

Stores the label of the current point for later updates
A[e.to]=min (A[x],e.cap-e.flow);

Q.push (e.to);
}
}
if (a[t]) break; //If the break is updated
}
if (!a[t]) break;

The augmented path cannot be found at this time
for (int u=t;u! = S;u = Edges[p[u]].from) {
Edges[p[u]].flow + = a[t];
Edges[p[u]^1].flow-= a[t];
}
Flow + = A[t];
}
return flow;
}
};

The following is a chain-forward star code

void Addedge (int u,int v,int W) {//This side is a simple chain-forward star processing
Pnt[cnt]=v;
Nxt[cnt]=head[u];
Cap[cnt]=w;
head[u]=cnt++;
}
void adddouble (int u,int v,int W) {//The same plus side, two strips
Addedge (U,V,W);
Addedge (v,u,0);
}
int maxflow (int s,int t) {
int flow = 0;
for (;;) {
CLR (a);
Queue<int> Q;
Q.push (s);
A[s]=inf;//Do not forget to initialize to INF
while (!q.empty ()) {
int u = Q.front ();
Q.pop ();
for (int i=head[u]; ~i; i= Nxt[i]) {
int v =pnt[i];
if (!a[v] && Cap[i] > 0) {
A[v]=min (Cap[i],a[u]);
Pre[v]=i;//At this time the pre is also record the current point of the label, remember to use the chain forward to the star when this is I!
Q.push (v);
}
}
if (a[t]) break;
}
if (!a[t]) break;
Flow + = A[t];
for (int i,u=t;u!=s;u = pnt[i^1]) {//This part of the update is also. To understand the chain forward to the star is OK!
i = Pre[u];
Cap[i]-= a[t];//At this time I omitted the flow, only use the traffic so the addition and subtraction is reversed, understanding
Cap[i^i] + = a[t];
}
}
return flow;
}







Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Graph theory max stream ek 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.