[Study notes] network flow basics (1)

The concept of
network flow Network flow problem is a common problem in graph theory.
Many systems include flow, such as vehicle flow in the highway system, information flow in the control system, water flow in the water supply system, cash flow in the financial system, and so on.
Network flow is a model with a wide range of applications, and there are many related algorithms.
Nevertheless, the concepts, ideas and basic algorithms in network flow are not difficult to understand.
The maximum flow problem
Let's first look at an example of transportation scheme design.
Figure (a) is a transportation network connecting the product origin V[s] (called the source point) and the sales location V[t] (called the meeting point). Each arc <u, v> represents the transportation line from to , The product is transported from u to v through this arc, and the number beside the arc indicates the maximum passing capacity of this transportation line (hereinafter referred to as capacity), the unit is 100 tons.
The product is transported from V[s] to V[t] through the transportation network.
It is now required to develop a transportation plan to maximize the number of products transported from V[s] to V[t].
Insert picture description here

Insert picture description here

Figure (b) shows a feasible transportation plan (the arc represented by the thick line is the arc in the transportation plan):
(1) 2 hundred tons of materials along the directed path P1(V[s], V[1] , V[2], V[4], V[t]) to the place of sale;
(2) 200 tons of materials along the directed path P2(V[s], V[1], V[3], V[t]) is transported to the place of sale;
(3) 100 tons of materials are transported to the place of sale along the directed path P3(V[s], V[2], V[3], V[t]).
The total transportation volume is 500 tons.
In figure (b), the two numbers next to each arc, such as (4, 3), represent the capacity of the arc and the actual transportation volume.
A feasible transportation plan should satisfy:
(1) The actual transportation volume cannot be negative;
(2) The actual transportation volume of each arc cannot be greater than the capacity of the arc;
(3) Except for the source point V[s] and the sink point V In addition to [t], for the other vertices u, the sum of the traffic on all arcs flowing into u should be equal to the sum of the traffic on all arcs starting from u.
The question now is:
(1) Can the transportation volume from V[s] to V[t] be increased?
(2) What is the maximum transportation volume from V[s] to V[t]?
We call this problem the maximum flow problem.
In the capacity network G(V, E), the feasible flow is a network flow that meets the following conditions:
Arc flow restriction conditions: 0 ≤ f(u, v) ≤ c(u, v), <u, v> ∈ E
balance condition:Insert picture description here

Where ∑v' f(u, v') is the sum of flows out of vertex u, ∑v'' f(v'', u) is the sum of flows into vertex u, and |f| is the total amount of feasible flows Flow is the net outflow from the source and the net inflow from the sink.
For any capacity network, a feasible flow always exists, such as f = 0, that is, the flow on each arc is 0, and this network flow is called zero flow.
The maximum flow refers to the feasible flow that meets the arc flow restriction and equilibrium conditions and has the maximum flow in the capacity network G(V, E).
EK algorithm
chain and augmented road.
In the capacity network G(V, E), there is a feasible flow f = {f(u, v)}. According to the
amount of flow on each arc and the relationship between flow and capacity, There are four types of arcs:
(1) Saturated arc: f(u, v) = c(u, v);
(2) Unsaturated arc: f(u, v) <c(u, v);
( 3) Zero arc: f(u, v) = 0;
(4) Non-zero arc: f(u, v)> 0.
It is not difficult to see that the two pairs of concepts, saturated arc and non-saturated arc, zero-current arc and non-zero-current arc are interlaced. Saturated arcs are generally non-zero-current arcs, and zero-current arcs are generally non-saturated arcs.
Chain
in the capacity of the network, said sequence of vertex u, u [1], u [2] ... v is a chain, the requirements
o has an arc between two vertices, such as <u, u1> or <u1, u> Is an arc in the capacity network. Let P be a chain from V[s] to V[t] in G. It is agreed that the direction from V[s] to V[t] is the positive direction of the chain.
Note that the concept of a chain is not the same as the concept of a directed path. In a chain, all arcs are not required to be in the same direction as the positive direction of the chain.
Along a chain from V[s] to V[t], the arcs can be divided into two categories:
(1) Forward arc (arc whose direction is consistent with the positive direction of the chain), its set is denoted as P+;
(2) Backward arc (arc whose direction is opposite to the positive direction of the chain), its set is denoted as P-.
Note that the forward arc and the backward arc are relative, that is, relative to the positive direction of the specified chain.
For example, in Figure (a), the specified chain is: P = {V[s], V[1], V[2], V[4], V[t]}, this chain is in Figure (a) Marked in blue line.
Then P+ and P- are respectively:
P+ = {<V[s], V[1]>, <V[2], V[4]>, <V[4], V[t]>}, P- = {<V[2], V[1]>}
Insert picture description here
Note that the same arc may be a forward arc in one chain and a backward arc in another chain.
For example, as shown in Figure (b), the arc <V[2], V[1]> in the chain P1 = {V[s], V[1], V[2], V[4], V[t ]} is the backward arc, and the chain P2 = {V[s], V[2], V[1], V[3], V[t]} is the forward arc.
Augmented Road
Suppose f is a feasible flow in a capacity network G, and P is a chain from V[s] to V[t], if P satisfies the following conditions:
(1) All forward arcs in P<u, v>, 0 ≤ f(u, v) ≤ c(u, v), that is, every arc in P+ is an unsaturated arc;
(2) On all backward arcs, 0 <f(u, v) ) ≤ c(u, v), that is, each arc in P- is a non-zero arc.
Then P is called an augmented road about feasible flow f, which is called augmented road for short.
Then, why is the chain P with the above characteristics called an augmented road?
The reason is that the existing feasible flow f can be improved into a larger flow f1 by modifying the flow f(u, v) of all arcs on P.
The operation of improving the feasible flow along the augmented path is called augmentation.
Residual and residual network
residuals: Given a capacity network G(u, v) and feasible flow f, the residual on the arc <u, v> is recorded as c(u, v) = c(u, v)- f(u, v).
The residual volume of each arc represents the flow that can be increased on that arc.
Because the decrease in flow from vertex u to vertex v is equivalent to the increase in flow from vertex v to vertex u, there is a residual in the opposite direction on each arc <u, v>.
Residual network: There is a capacity network G(V, E) and the network flow f on it. The residual network of G with respect to f is denoted as G'(V', E'), where the vertex set V'of G'and The vertex set V of G is the same, that is, V'= V. For any arc in G<u, v>, if f(u, v) <c(u, v) then there is an arc in G'<u , v>∈E', its capacity is c'(u, v) = c(u, v)-f(u, v), if f(u, v)> 0, there is an arc in G' <u, v>∈E', its capacity is c'(v, u) = f(u, v).
It can be seen from the definition of residual network that each arc in the original capacity network is transformed into one or two arcs in the residual network.
For example, in the capacity network G shown in Figure (a), the residual network G'is Figure (b).
Each arc in the residual network indicates that it can be expanded along its direction in the original capacity network. The capacity of the arc <u, v> indicates that the original capacity network can increase along the direction from u to v by c'(u, v) The flow rate.
Therefore, in the residual network, any simple path from the source point to the sink point corresponds to an augmented road, and the minimum capacity of each arc on the path is the maximum flow that can be augmented at one time.
For example, in Figure (b), a path from the source point to the sink point is (V[s], V[2], V[4], V[t]]). This path has 3 arcs and the capacity They are 1, 4, and 5, so the increase along this path can increase the flow rate by one unit.
Insert picture description here

The relationship between the residual network and the original network: Let f be the feasible flow of the capacity network G(V, E), f'is the feasible flow of the residual network G', then f + f'is still a feasible flow of the capacity network G . (F + f'represents the addition of the flow on the corresponding arc).
Obviously, as long as there is an augmentation path in the residual network, the traffic can be increased.
It can be proved that its inverse proposition is also true: if there is no augmentation path in the residual network, the current flow is the maximum flow.
This is the famous augmented road theorem.
A good way to find an augmented path is to use BFS, which is sufficient to deal with the problem of network flow where data is not tricky.
This is the shortest augmented path algorithm (Edmonds-Karp algorithm, EK algorithm for short), that is, each time the augmentation is performed along the shortest augmented path (that is, the augmented path with the least number of sides).
The time complexity of the algorithm is: O(VE ^ 2).
Implement EK algorithm

#include <cstdio>
#include <queue>
#include <cstring>
#include <iostream>
using namespace std;
const int MAX_N = 200;
const int MAX_M = 400;
const int INF = 0x3f3f3f3f;
struct edge {
    
    
    int u, v, c, next;
} e[MAX_M];
int p[MAX_N], eid, S, T;
void init() {
    
    
    memset(p, -1, sizeof(p));
    eid = 0;
}
void insert(int u, int v, int c) {
    
    
    e[eid].u = u;
    e[eid].v = v;
    e[eid].c = c;
    e[eid].next = p[u];
    p[u] = eid++;
}
void addedge(int u, int v, int c) {
    
    
    insert(u, v, c);
    insert(v, u, 0);
}

Dinic algorithm
Similar to the EK algorithm, the Dinic algorithm is also constantly looking for ways of augmentation. There are two differences

  1. Construct a hierarchical graph and expand according to the hierarchical relationship. Specifically, BFS is performed on the source point, and each node is numbered according to the level. When we expand, the traffic will only go from the low-level node to the high-level node, and there will be no backflow. That is to increase according to the shortest path.
  2. DFS performs multi-channel augmentation. The EK algorithm only expands one path at a time, and Dinic can find multiple wide paths at the same time through DFS.
    Through the above two optimizations, the time efficiency of the Dinic algorithm is greatly improved.
    Basic steps of Dinic algorithm
  3. Initialize the network and network flow.
  4. Construct the remaining network and the hierarchical network. If the sink is not in the hierarchical network, the algorithm ends.
  5. A DFS process is used for augmentation in the hierarchical graph G ^ L. When the DFS is executed, the augmentation of this stage is
    completed.
  6. Go to step 2.
    Algorithm example
    Insert picture description here
    In the above network, the source point is 0 and the sink point is 5. The reverse arc corresponding to each arc in the network is reflected in the figure. First establish a hierarchical graph:
    Insert picture description here
    the number at the bottom right of each vertex is its corresponding level. Next, perform DFS and find an augmented road with a flow rate of 4:
    Insert picture description here
    back to vertex 3, find that it has no other allowable arcs, and continue back to 1. In the process of retreating, the capacity of the arc flowing through is constantly modified. Next, it is found that vertex 1 has an allowable arc to connect to 4 and continue DFS. Note that the upper flow limit has been updated to 10 − 4 = 6 at this time.
    Insert picture description here
    Find another augmented road with a flow rate of min(6, 8, 10) = 6.
    The upper limit of the flow at vertex 1 is already 0, and it retreats from vertex 1 to the source point. Modify the remaining capacity of the relevant arc.
    Insert picture description here
    Continue to search for the adjacent vertex 2 of the source point and find an augmented road. Since the upper limit of the source point's capacity is always INF, the flow rate of the Zengguang Road is min(10, 9, 4) = 4.
    Insert picture description here
    Go back to the source point and adjust the capacity of the corresponding arc. It is found that the source point has no other adjacent vertices, and the DFS process ends.
    Insert picture description here
    BFS again, build a hierarchical map. We found that the level of vertex 1 has become 3, which is different from the last time.
    Insert picture description here
    Find an augmented road with a flow rate of min(6, 5, 6, 6) = 5. Insert picture description here
    It is found that there is no other augmentation road, back to the source point, and the capacity of the relevant arc is updated.
    Insert picture description here

Perform BFS, find that the meeting point is no longer in the hierarchical network, and the algorithm ends. The maximum flow is 4 + 6 + 4 + 5 = 19.
Insert picture description here
Implement Dinic algorithm

#include <iostream>
#include <string.h>
#include <queue>
using namespace std;
const int INF = 0x3f3f3f3f;
const int MAX_N = 100;
const int MAX_M = 10000;
struct edge {
    
    
    int v, c, fail;
} e[MAX_M];
int p[MAX_N], eid;
void init() {
    
    
    memset(p, -1, sizeof(p));
    eid = 0;
}
void insert(int u, int v, int c) {
    
    
    e[eid].v = v;
    e[eid].fail = p[u];
    e[eid].c = c;
    p[u] = eid++;
}
void addedge(int u, int v, int c) {
    
    
    insert(u, v, c);
    insert(v, u, 0);
}

Guess you like

Origin blog.csdn.net/yueyuedog/article/details/111222861