The network flow has many algorithms, these days learned two kinds of, record the EK algorithm.
First, there are some definitions in the network flow:
V represents the collection of all the nodes in the entire graph.
E represents a collection of all the edges in the entire graph.
G = (v,e), which represents the entire graph.
s represents the source point of the network, and T represents the sink point for the network.
For each edge (U,V), there is a capacity of C (U,V) (c (u,v) >=0), and if C (u,v) = 0, then (u,v) does not exist in the network. Conversely, if there is no edge (u,v) in the original network, then C (u,v) =0.
For each edge (U,V), there is a flow f (u,v).
A simple example. The network can be imagined as a pipeline of water. The number to the right of the parentheses indicates the capacity of the pipe C, and the number on the left indicates the current flow F of the pipe.
Three properties of the network stream:
1. Capacity limit: F[u,v]<=c[u,v]
2, anti-symmetry: f[u,v] =-F[v,u]
3. Flow balance: For any node that is not a source point or a sink point, the traffic flowing into the node is equal to the traffic flowing out of the node.
As long as these three properties are met, it is a legitimate network flow.
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 nature of the network flow.
To find the maximum flow of a network stream there are many algorithms here first introduce the Augmented path algorithm (EK)
Before learning the algorithm, first look at the definition of several diagrams involved in this algorithm:
* * Network of residual quantity
In order to make the algorithm more convenient, a residual network is usually 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 words: it is for a certain edge (also called arc), but also how much flow through.
Gf Residue Network, Ef represents the edge set of a residual network.
This is a residual network of the above figure. Residual network (if the capacity of an edge in the network is 0, the edge is not considered to be in the residual network.)
R (S,V1) = 0, so it is not drawn. Another example: R (v1,s) = C (v1,s) –f (v1,s) = 0– (-f (s,v1)) = f (s,v1) = 4.
Where a side like (V1,s) is called a back arc, it means that the flow from V1 to S can also be increased by 4 units.
But from V1 to S is not the same direction as the arc in the original network? Obviously, "from V1 to S can also increase 4 unit flow" This information is meaningless. So, is it necessary to build these back arcs?
Obviously, the drawing in the 1th figure is not a maximum flow.
However, if we add 2 to the flow of the arc that passes through the path of V1---v2---------------
Notice that this path passes through a back arc: (V2,V1).
If you do not set up a back arc, the algorithm cannot find the path.
* * In essence, a back arc provides the possibility for the algorithm to correct its own errors, which allows the algorithm to cancel the behavior of the previous error (letting the 2-unit stream flow from V1 to v2)
Note that the back arc is conceptually, 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 the residual network, with any arc (U,V), has r[u,v]>0.
The green one is an augmented road.
See so many concepts believe that we have 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 traffic value (actually modified is the Benquan of the residual network). When there is no augmented path, the algorithm stops, and the flow at this time is the maximum flow.
* * Efficiency of the augmented path algorithm
Set n = | v|, M = | e|
Each augmentation is a BFS, the efficiency is O (m), and in the worst case (n-2 augmentation). (that is, except for the source and sink points are not connected, all points are only and S and T connect)
Therefore, the total time complexity is O (m*n), so in the sparse graph is still relatively high efficiency.
Hdoj 1532 is one that can be used as a template topic practiced hand.
Template code:
[CPP]View Plaincopyprint?
- #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; //end point
- 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));
- }
- }
(go) Maximum flow algorithm (EDMONDSKARP) of the network stream