Network flow (i)----maximum flow

Source: Internet
Author: User

File Download Http://files.cnblogs.com/files/tenlee/gw_netflow.pdf

Given a graph g= (v,e), the edges in the diagram are treated as pipelines, and each edge has a weighted value that represents the maximum flow rate for that pipeline. Given the source point S and the meeting point T, now suppose that there is a water source in s, and there is a reservoir at T,

Ask the maximum water flow from S to T is how much network flow graph, the amount of the source point outflow, equal to the amount of sink inflow, in addition to Yuanhui any point outside the sum of its inflow is equal to the outflow of two and

Ford-fulkerson algorithm for solving maximum flow

The basic idea is simple, each time using DFS to find a feasible path from the source to the sink, and then to fill the road. The capacity of the smallest edge on this path is the traffic found by DFS. Then for each edge on the path, its capacity is subtracted from the traffic just found. In this way, each DFS may increase traffic until the DFS is unable to find a viable path, and the maximum flow is calculated

Come on, is this the right idea?

If we go along the s-a-b-t route, we can only get a 100 flow.

In fact, this figure has a flow of 200 flow

The problem is that the traffic on the side A→b B is not 0 0 prematurely, thus "blocking" the possibility that traffic will continue to increase.

An improved idea: should be able to modify the established flow network, so that "unreasonable" traffic is deleted. One implementation: Add a "reverse" edge to the edge on the traffic path found at the last Dfs, the capacity on the reverse edge equals the traffic on that edge that was found at the last Dfs, and then use "reverse" capacity and remaining capacity on other edges to find the path.

After the first DFS, add a new graph with the reverse edge: so that we can find a new path in the new network when we second DFS search this is a cancel stream operation can also be considered as two paths

The second DFS search found a stream with a traffic of 100, plus a flow of 100 for the first search, and the total traffic went up to 200.

Then add a reverse edge to the second post-DFS diagram,

becomes: Dfs again on this diagram, the path has not been found, so traffic can no longer increase, the maximum flow is 200

Residual Network (residualnetwork)

On a network flow graph, find a source to the sink path (that is, found a traffic), on the path of all sides, its capacity minus the traffic found, on all sides of the path, all add a reverse edge, the capacity is equal to the traffic found, so that the new figure is called the original "residual network" Why is it valid to add a reverse edge (cancel stream)? Suppose that the first time you look for a stream, you can find that traffic n comes from the source on the B->a, arrives at B, and then exits a to the meeting point.

Why is it valid to add a reverse edge (cancel stream)? Build the residual network when adding reverse edge a->b, capacity is N, augmented when the traffic n-k found, that is, the new n-k traffic. This n-k flow, from a into, b out, eventually flow to the sink n-k now to prove this 2n-k from the flow, in the original image is really can from the source to sink. Merge the edge of the inflow B into one, and merge the edges of the outflow a into the same way that the edges of the inflow A and outflow B are merged.

In the original image can be distributed as follows, you can have 2n-k from source to sink point:

No strict proof, only give a perceptual knowledge. A number of complex cases connected to opposite edges can also be analyzed similarly.

Ford-fulkerson algorithm

? The process of finding the maximum flow is to find a source-to-sink path continuously, then construct the residual network, then find a new path on the residual network, increase the total traffic, then form a new residual network, and then look for a new path .... Until a path from source to sink is not found on a residual network, the maximum flow is out. Each time you look for new traffic and construct a new residual network, it's called "augmented path" for traffic, or "augmented", which now assumes that the capacity of each edge is an integer. This algorithm can increase the flow by at least 1 each time because the entire network traffic does not exceed the capacity and C of all the edges in the graph, thus the algorithm will End now look at the complexity of finding an augmented path algorithm can be used DFS, complexity is the number of edges m+ vertices ndfs up to C times so the time complexity is c* (m+n) =c* n^2 the algorithm is simple but notice that in the figure C may be very large, such as the following picture

If you have a bad luck, this graph will let your program execute 200 times DFS although actually at least 2 times we can get maximum flow how to avoid the above situation happen? At each augmentation, select the augmented path from source to sink with a minimum number of sides, i.e. instead of looking up the augmented path through DFS, and looking for an augmented path through BFS. This is the Edmonds-karp shortest augmented path algorithm has shown that the complexity of this algorithm is capped at NM 2 (n is the number of points, M is the edge)

Examples:

HDU 1532 http://acm.hdu.edu.cn/showproblem.php?pid=1532

or POJ 1273 http://poj.org/problem?id=1273

Max Streaming Template code

1#include <cstdio>2#include <cstdlib>3#include <cstring>4#include <cctype>5#include <cmath>6#include <algorithm>7#include <vector>8#include <queue>9#include <stack>Ten#include <map> One using namespacestd; A  - Const intINF =0x3f; - Const intINF =0x3f3f3f3f; the Const intMAXN =205; - intG[MAXN][MAXN], PREV[MAXN]; - BOOLVISIT[MAXN]; -queue<int>Q; + intN, M; -  + intAugment (); A BOOLBFS (); at  - intMain () - { -     intx, y, Z; -      while(~SCANF ("%d%d", &m, &N)) -     { inmemset (G,0,sizeof(G)); -          while(m--) to         { +scanf" %d%d%d", &x, &y, &z); -G[x][y] + =Z; the         } *         intMaxflow =0; $         int;Panax Notoginseng          while(BFS ())//The maximum flow is found without an augmented path -Maxflow + =Augment (); theprintf"%d\n", Maxflow); +     } A } the //using BFS to search for augmented road warp + BOOLBFS () - { $      while(!q.empty ()) Q.pop (); $     intV, I; -memset (Prev,0,sizeof(Prev)); -memset (Visit,0,sizeof(visit)); theprev[1] =0; -visit[1] =1;WuyiQ.push (1); the      while(!q.empty ()) -     { Wuv =Q.front (); - Q.pop (); About          for(inti =1; I <= N; i++) $         { -             if(G[v][i] >0&&!Visit[i]) -             { -Prev[i] = v; Visit[i] =1; A                 if(i = =N) +                 { the                     return true; -                 } $                 Else the                 { the Q.push (i); the                 } the             } -         } in     }  the     return false; the } About // the intAugment () the { the     intNminflow =INF; +     intv =N; -     //This is an augmented path that was just found with BFS. the     //look for the least-capacity edge on the source-to-sink path, and its capacity is the total volume of this increaseBayi      while(Prev[v]) the     { theNminflow =min (Nminflow, g[prev[v]][v]); -v =Prev[v]; -     } the      thev =N; the     //fix the capacity of each edge. Add a reverse Edge the      while(Prev[v]) -     { theG[PREV[V]][V]-=Nminflow; theG[V][PREV[V]] + =Nminflow; thev =Prev[v];94     } the     returnNminflow; the}
View Code

Network flow (i)----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.