"Network Flow learning note 02--edmonds-karp,ford-fulkerson,dinic Three algorithms to achieve maximum flow"

Source: Internet
Author: User
Tags acos cas

"HDU3549" topic Link: click here

Three methods are used, the comparison of the minimum Ek, only with 46ms.

"Edmonds-karp algorithm"
The basis of the maximum flow algorithm, each BFS to find the shortest possible to augment, find a residual path on it. Then the residual network is augmented, do not forget the positive augmentation, the equivalent of negative reduction, but also to save records in the diagram.
Finally, a cut set to get the maximum flow, the efficiency O (VE2), "Find any path" the simplest way is to use DFS, but the data to be slightly increased will become slower, using BFS, source and sink points are saved in S and T, net traffic is stored in the variable F.

Code:

/*edmonds-karp algorithm source and sink points remain in S and T, net flow remains in variable f */#include <math.h> #include <queue> #include <deque># Include <vector> #include <stack> #include <stdio.h> #include <ctype.h> #include <string.h > #include <stdlib.h> #include <iostream> #include <algorithm>using namespace std; #define MAX (A, B) A>b?a:b#define Min (A, B) a>b?b:a#define mem (A, B) memset (A,b,sizeof (a)) int dir[4][2]= {{1,0},{-1,0},{0,1},{0,-1} };const double eps = 1e-6;const double Pi = ACOs ( -1.0); const int MAXN = 20;const int Inf=0x3f3f3f3f;int cap[20][20],flow[2 0][20];//capacity.     Traffic int max_flow (int t) {int s=1,i,j,f=0,p[20],a[20];//p represents the previous node, and a represents the residuals.    MEM (flow,0);    Queue<int> Q; while (1) {while (!        Q.empty ()) Q.pop ();        memset (A,0,sizeof (a));        Q.push (s);        A[s]=inf; while (!            Q.empty ()) {i =q.front ();            if (i==t) break;            Q.pop ();  for (j=1; j<=t; J + +) {              if (I!=j&&!a[j]&&cap[i][j]>flow[i][j]) {p[j]=i;                    Q.push (j); a[j]= A[i]>cap[i][j]-flow[i][j]?                Cap[i][j]-flow[i][j]:a[i];        }}} if (!a[t]) return f;//cannot find the augmentation path, indicating that this is the maximum current.            for (i=t; i!=s; I=p[i]) {Flow[i][p[i]]-=a[t];        Flow[p[i]][i] +=a[t];    } F+=a[t];    }}int Main () {int a,b,c,n,m,ncase,cas=1;    scanf ("%d", &ncase);        while (ncase--) {scanf ("%d%d", &n,&m);        memset (cap,0,sizeof (CAP));            while (m--) {scanf ("%d%d%d", &a,&b,&c);            if (a==b) continue;        Cap[a][b]+=c;    } printf ("Case%d:%d\n", Cas++,max_flow (n)); } return 0;}
"Ford-fulkerson"

The algorithm is the basis of a large number of algorithms, there are many ways to implement.

The Ford-fulkerson algorithm is an iterative algorithm that first zeros the flow size of all vertex pairs in the graph, at which point the network flow size is also 0. In each iteration, the value of the stream is increased by looking for an "augmented path" (augument path). The augmented path can be thought of as a path from the source point S to the meeting point T, and along this path can add more streams. Iterations until the augmented path position can no longer be found, at which point there must be at least one edge full edge in all paths from the source point to the sink point (i.e. the size of the edge stream is equal to the capacity size of the edge).


/*ford-fulkerson Algorithm adjacency Table implementation */#include <ctype.h> #include <stdio.h> #include <vector> #include < stdlib.h> #include <string.h> #include <iostream> #include <algorithm>using namespace Std;const int MAXN = 1101;const int inf=0x3f3f3f3f;struct edge{int to,cap,rev;};        Vector<edge > G[MAXN];              The adjacency table of the graph represents bool USED[MAXN];    access token void Add_edge (int from,int to,int cap) {G[from].push_back (edge) {to,cap,g[to].size ()}); G[to].push_back (Edge) {from,0,g[from].size ()-1});}    int dfs (int v,int t,int f) {if (v==t) return F;    Used[v]=true;        for (int i=0; i<g[v].size (); i++) {Edge &e=G[v][i];            if (!used[e.to]&&e.cap>0) {int D=dfs (e.to,t,min (F,e.cap));                if (d>0) {e.cap-=d;                G[e.to][e.rev].cap+=d;            return D; }}} return 0;}   int max_flow (int s,int t) {int flow=0; for (;;)        {memset (used,0,sizeof (used));        int F=dfs (s,t,inf);        if (f==0) return flow;    Flow+=f;    }}int Main () {int n,m,too,capp,revv;    int tt,j=1;    scanf ("%d", &AMP;TT);        while (tt--) {scanf ("%d%d", &m,&n);        memset (g,0,sizeof (G));            for (int i=0; i<n; i++) {scanf ("%d%d%d", &AMP;TOO,&AMP;CAPP,&AMP;REVV);        Add_edge (TOO,CAPP,REVV);    } printf ("Case%d:%d\n", J++,max_flow (1,m)); } return 0;}
"Dinic algorithm"

One of the optimization algorithms for the maximum flow of the network flow, layering the original image at each step and then using DFS to find the augmented path. The complexity of Time is O (n^2*m).

Algorithmic flow
1. Calculate the hierarchy graph according to the residue network.
2. Use DFS in the hierarchy to augment until there is no augmented path.
3, repeat the above steps until it is not augmented.

The/*dinic algorithm always looks for the shortest path and is augmented along this route */#include <math.h> #include <queue> #include <deque> #include <vector > #include <stack> #include <stdio.h> #include <ctype.h> #include <string.h> #include < stdlib.h> #include <iostream> #include <algorithm>using namespace std; #define MAX (b) a>b?a:b# Define Min (A, B) a>b?b:a#define mem (A, B) memset (A,b,sizeof (a)) int dir[4][2]= {{1,0},{-1,0},{0,1},{0,-1}};const Double EPS = 1e-6;const double Pi = ACOs ( -1.0); const int MAXN = 18;const int inf=0x3f3f3f3f;struct edge{int from, to, cap;};          Vector<edge> eg;vector<int> g[maxn];int N, S, T, Ans;int LEVEL[MAXN];         Vertex to source point distance designator int CUR[MAXN];    The current arc, on its previous edge, has no use of void Add_edge (int from, int to, int cap) {Eg.push_back (edge) {from, to, Cap});    Eg.push_back (Edge) {To, from, 0});    G[from].push_back (Eg.size ()-2); G[to].push_back (Eg.size ()-1);}    BOOL BFs () {mem (level,-1);   Queue<int> que; Que.push (s);    Level[s] = 0;        while (!que.empty ()) {int v = que.front ();        Que.pop ();            for (int i = 0; i < g[v].size (); i++) {edge& e = eg[g[v][i]];                if (level[e.to] = =-1 && e.cap > 0) {level[e.to] = level[v]+1;            Que.push (e.to); }}} return (Level[t]!=-1);}    int dfs (int v, int a) {if (v = = T | | a = = 0) return A;    int flow = 0, F;        for (int& i = cur[v]; i < g[v].size (); i++) {edge& e = eg[g[v][i]];            if (level[v]+1 = = Level[e.to] && (f = DFS (e.to, Min (A, E.cap))) > 0) {e.cap-= f;            Eg[g[v][i]^1].cap + = f;            Flow + + F;            A-= f;        if (a = = 0) break; }} return flow;}    void Dinic () {ans = 0;        while (BFS ()) {mem (cur,0);    Ans + = DFS (s, INF);    }}int Main () {int T, M, from, to, Cap,cas=1;    scanf ("%d", &t);   while (t--) {scanf ("%d%d", &n, &m);            while (m--) {scanf ("%d%d%d", &from, &to, &cap);        Add_edge (from, to, CAP);        } s = 1, t = N;        Dinic ();        printf ("Case%d:%d\n", cas++, ans);        Eg.clear ();    for (int i = 0; I <= N; ++i) g[i].clear (); } return 0;}


"Network Flow learning note 02--edmonds-karp,ford-fulkerson,dinic Three algorithms to achieve maximum flow"

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.