Maximum flow algorithm for network flow (EDMONDSKARP) __ Network flow

Source: Internet
Author: User

The network flow has a lot of algorithms, these days to learn two kinds, record the EK algorithm.

First, there are some definitions in the network stream:

V represents a collection of all nodes in the entire diagram.
E represents a collection of all the edges in the entire diagram.
G = (v,e), representing the entire diagram.
s represents the source point of the network, T represents the meeting point of the network.
For each side (U,V), there is a capacity C (U,v) (c (u,v) >=0), if C (u,v) = 0, then (u,v) does not exist on the network. Conversely, if an edge (U,V) does not exist in the original network, then C (u,v) =0.
For each edge (U,V), there is a flow f (u,v).

A simple example. A network can be imagined as a conduit for water. The number to the right of parentheses indicates the capacity of the pipe C, the number on the left indicates the current flow of the pipeline F.


Three properties of network flow:

1, capacity limit: f[u,v]<=c[u,v]
2, Inverse symmetry: f[u,v] =-F[v,u]
3, Flow balance: For not the source point is not a meeting point of any node, inflow of traffic and equal to the flow of the node outflow and.
As long as the three properties are satisfied, it is a legitimate network stream.

The maximum flow problem is to find the maximum flow of the source S to the meeting point T in the case of satisfying the network flow properties.


There are many algorithms to find the maximum flow of a network flow here we first introduce the Augmented path algorithm (EK)

Before learning an algorithm, first look at the definitions in several diagrams involved in this algorithm:


* * Residual network

In order to facilitate the implementation of the algorithm, a residual network is generally defined according to the original network. where R (U,V) is the capacity of the residual network.
R (u,v) = C (u,v) –f (u,v)
In layman's terms: it is for a certain edge (also called arc), but also how much flow through.
Gf Residual network, EF represents the edge set of a residual network.


This is a residual network in the diagram above. Residual network (if the capacity of one edge of the network is 0, this edge is not considered to be in the residual network.)

R (S,V1) = 0, so it's not drawn. In addition, for example: R (v1,s) = C (v1,s) –f (v1,s) = 0– (-f (s,v1)) = f (s,v1) = 4.

An edge like (v1,s) is called a back arc, which means that from V1 to S can increase the flow of 4 units.

But from V1 to S is not the opposite of the arc in the original network. Obviously "from V1 to S can also increase the flow of 4 units" this message is meaningless. So, is it necessary to build these back arcs?

Obviously, the drawing in the 1th picture is not a maximum flow.

However, if we add the flow of the arcs through which the-> v2-> v1-> t is increased by 2, the maximum flow of the network is obtained.

Notice that the path passes through a back arc: (V2,V1).

If you do not set up a back arc, the algorithm will not find this path.

* * Essentially, the back arc provides the possibility for the algorithm to correct its own error, allowing the algorithm to cancel the previous error (allowing 2 of the flow of the stream from V1 to v2)

Note that the rear arc is only conceptual, and there is no difference between the back arc and the forward arc in the program.


* * Augmented Road

Augmented path definition: A path from S to T in a residual network, in which any arc (U,V) has r[u,v]>0.


As shown in the green is an augmented road.

See so many concepts believe that the augmented path algorithm has a general idea of it.


* * Augmented path algorithm

Augmented path algorithm: each time using BFS to find a shortest augmented path, and then along this path to modify the flow value (the actual modification is the Benquan of the residual network). When there is no augmented path, the algorithm stops, at which point the flow is the maximum flow.


* * Efficiency of the augmented path algorithm

Set n = | v|, M = | e|

Each augmentation is a BFS, and the efficiency is O (m), and in the worst case it needs to be n-2 augmented. (That is, except the source and the meeting point are not connected, all points are only with S and T connected)

Therefore, the total time complexity is O (m*n), so the efficiency in the sparse map is still relatively high.


Hdoj 1532 is a practicing that can be used as a template topic.

Template code:

#include <cstdio> #include <cstring> #include <iostream> #include <string> #include <
algorithm> #include <map> #include <vector> using namespace std;
const int N = 1100;

const int INF = 0X3F3F3F3F;  struct Node {int to;//endpoint int cap;//capacity int rev;

Reverse Side};
Vector<node> V[n];

BOOL Used[n];
    void Add_node (int from,int to,int cap)//heavy edge condition does not affect {V[from].push_back (Node) {to,cap,v[to].size ()});
V[to].push_back (Node) {from,0,v[from].size ()-1});
    int dfs (int s,int t,int f) {if (s==t) return F;
    Used[s]=true;  for (int i=0;i<v[s].size (); i++) {Node &tmp = v[s][i];
            Note if (Used[tmp.to]==false && tmp.cap>0) {int D=dfs (tmp.to,t,min (F,tmp.cap));
                if (d>0) {tmp.cap-=d;
                V[tmp.to][tmp.rev].cap+=d;
            return D;
}} return 0;
  int max_flow (int s,int t) {  int flow=0; for (;;)
        {memset (used,false,sizeof (used));
        int F=dfs (s,t,inf);
        if (f==0) return flow;
    Flow+=f;
    int main () {int n,m;
        while (~SCANF ("%d%d", &n,&m)) {memset (v,0,sizeof (v));
            for (int i=0;i<n;i++) {int x,y,z;
            scanf ("%d%d%d", &x,&y,&z);
        Add_node (X,Y,Z);
    printf ("%d\n", Max_flow (1,m));
 }
}


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.