The maximum flow algorithm outlined five

Each network maximum flow algorithm summary

The maximum total network algorithm flow algorithm is generally two to five kinds of the following table: n = number of vertices, m is a number of arcs, U representative of the maximum capacity of each of the arcs

Algorithm name the complexity Algorithms Summary
General augmenting path algorithm \ ((Scene) \) Labeling each take to find a way in augmenting the capacity of the network augmentation (or any of the residual every network looking for an augmented augmenting path) until augmenting path does not exist so far.
The shortest augmenting path algorithm \(O(nm^2)\) Each stage: hierarchical network, and continue to be augmented with the BFS algorithm does not exist until the date augmenting paths. If the sink is not hierarchical network, the algorithm ends.
Continuous shortest augmenting path algorithm (Dinic) \ (O (n ^ 2m) \) On the basis of the shortest augmenting path algorithm on the transformation: At each stage, the process of implementing several times with a dfs augmented. If the sink is not level network, then the algorithm ends.
General pre-flow Boosting \ (O (n ^ 2m) \) Maintain a pre-flow, continue to advance the implementation of active vertices (Push) operation or re-label (Relabel) to adjust the pre-flow operation, until inoperable.
Boosting the highest grade pre-flow \ (O (n ^ 2 \ sqrt m) \) Checking each node having the highest numbered active
  • General augmenting path algorithm (Ford-Fulkerson):

    1) process for each vertex reference numeral has two quantities: the inflow rate of the vertex alpha [u] indicates numerals obtained pre [u] from which the apex

    2) adjustment [n-1] be optimized according to this article by Canton Road a = alpha numerals obtained after each of the last V

\(f(u,v) = f(u,v) + a\ when<u,v>\in P+\)

\(f(u,v) = f(u,v)+a\ when <u,v>\in P-\)

$ f(u,v) = f(u,v)  when <u,v>\notin P$

Optimized code to multiple augmenting paths through bfs as follows:

#include<bits/stdc++.h>
using namespace std;
#define MAXN 1000
#define INF 1000000//根据题目条件改变
struct ArcType {
    int c,f;//容量,流量
};
ArcType Edge[MAXN][MAXN];
int n,m;
int flag[MAXN];//顶点状态:-1未标号 0已标号未检查 1已标号以检查
int pre[MAXN];//标号的第一个分量:指明从哪个点得到,以便找出可改进量和增广路径
int alpha[MAXN];//标号的第二个分量:可改进量a
queue<int > q;
int v;
void ford() {
    for(;;) {//标号直至不存在可改进路
        memset(flag,0xff,sizeof(flag));//均初始化为-1
        memset(pre,0xff,sizeof(pre));
        memset(alpha,0xff,sizeof(alpha));
        flag[0] = pre[0] = 0; alpha[0] = INF;//对源点标号
        while(!q.empty()) q.pop();//清空队列
        q.push(0);
        while(!q.empty() && flag[n-1] == -1) {
            v = q.front();q.pop();
            for(int i = 0;i < n;i++) {
                if(flag[i] == -1) {//正向且未饱和
                    if(Edge[v][i].c < INF && Edge[v][i].f < Edge[v][i].c) {
                        flag[i] = 0;pre[i] = v;
                        alpha[i] = min(alpha[v],Edge[v][i].c - Edge[v][i].f);
                        q.push(i);
                    }
                    else if(Edge[i][v].c < INF && Edge[i][v].f > 0) {//反向且非0
                        flag[i] = 0;pre[i] = -v;
                        alpha[i] = min(Edge[i][v].f,alpha[v]);
                        q.push(i);
                    }
                }
            }
            flag[v] = 1;
        }
        if(flag[n-1] == -1 || alpha[n-1] == 0) break;//当汇点无需调整,退出循环
        int k1 = n-1,k2 = abs(pre[k1]);
        int a = alpha[n-1];    
        while(1) {//沿着路径改进
            if(Edge[k2][k1].f < INF) Edge[k2][k1].f += a;
            else Edge[k1][k2].f -= a;
            if(k2 == 0) break;
            k1 = k2; k2 = abs(pre[k2]);
        }
    }
    int maxFlow = 0;
    for(int i = 0;i < n;i++) {
        for(int j = 0;j < n;j++) {
            if(i == 0 && Edge[i][j].f < INF) maxFlow+=Edge[i][j].f;//求出源点的流出量
            if(Edge[i][j].f < INF) printf("%d->%d:%d\n",i,j,Edge[i][j].f);
        }
    }
    printf("maxFlow:%d\n",maxFlow);
} 
int main() {
    int u,v,c,f;
    scanf("%d%d",&n,&m);
    for(int i = 0;i < n;i++) {
        for(int j = 0;j < n;j++) Edge[i][j].c = Edge[i][j].f = INF;
    }
    for(int i = 0;i < m;i++) {
        scanf("%d%d%d%d",&u,&v,&c,&f);
        Edge[u][v].c = c;Edge[u][v].f = f;
    }
    ford();
    return 0;
}

Complexity brief analysis :

Obviously, if the network capacity and the capacity of each arc initial flow are positive integers, each of the Ford-Fulkerson algorithm augmented time, the flow rate will increase at least one unit, so Ford-Fulkerson algorithm certainly so in a limited step network flow maximized. Similar reasons explained if the capacity of the arc is a rational number, such that the network can be maximized within a limited flow step. However, if the capacity of the arc may be an irrational number, the algorithm does not necessarily terminate in a finite number of steps.

Since the cutting $ {Vs}, V- {Vs} $ in front to the number of the n arcs at most, the maximum flow rate of the flow | F. | Upper bound is nU (U represents the maximum capacity of the network, each arc). In addition, because each augmenting take up again for all the arcs checks, so the time complexity of the Ford-Fulkerson algorithm is $ O (mnU) $.

Example: Poj1149

  • The shortest augmenting path algorithm:

    The shortest augmenting paths of thought:

    (1) initial capacity networks and network streams.

    (2) the network configuration and the residual levels of the network, if the sink is not in the network hierarchy, then the algorithm ends.

    (3) continue with BFS augmented at the level of the network until the network hierarchy until no augmenting path; each augmentation is complete, at the level of the network to be removed due to improved flow and lead to saturation of the arc.

    (4) go to step (2)

  • Continuous shortest augmenting path algorithm (Dinic algorithm):

    Dinic algorithm ideas:

    Thought Dinic algorithm is augmented in stages in a hierarchical network. It is the shortest path algorithm difference is: the shortest augmenting each stage of the implementation of End after the first augmented BFS, BFS to restart from the source Vs start looking for another augmenting path; and in Dinic algorithm, just once dfs process can achieve multiple augmentation, this is the trick of Dinic algorithm. Dinic algorithm specific steps are as follows:

    (1) initial capacity networks and network flow

    (2) the network configuration and the residual levels of the network, if the sink is not in the network hierarchy, then the algorithm ends.

    (3) a dfs process is augmented by the network level, dfs process is finished, the stage is augmented also executed.

    (4) go to step (2)

    In Dinic algorithm, only the first (3) different steps and the shortest augmenting path algorithm. A very large increase efficiency.

  • General pre-flow Boosting

    1. augmenting path algorithm drawbacks:

    After augmenting path algorithm is to find an augmenting path, immediately along the road network augmented flow augmentation. Each augmented may need to be up to n-1 arcs operation, therefore, each time complexity is augmented \ (O (n-) \) , in some cases, the cost of operation is high.

    2. distance label

    For a residual network G ', how to determine its exact distance label it? Vt of sink can begin, breadth-first search of the arc, the complexity of this process is the \ (O (m) \)

Preflow: Let \ (f = \ {f ( u, v) \} \) is a network flow capacity of the network, if each edge arcs G are satisfied: \ (0 \ Leq F (U, V) \ leq c (u, v) \ )

Further, in addition to the source of each sink vertex u surplus E (u) are satisfied: \ (E (u) \ GEQ 0 \) called the network flow is a preflow in G.

Pre-flow is advanced through the continuous improvement of active nodes to achieve the maximum flow

Guess you like

Origin www.cnblogs.com/pot-a-to/p/10949209.html