Network Flow Exploration: A Collection of Algorithms to Solve the Network Maximum Flow Problem

1. First introduction to network flow

Network flow has always been a difficult hurdle for beginners to overcome. Many people say that it is not as intuitive an algorithm as DFS/BFS. At the same time, there are also various uneven materials on the Internet, which makes people feel that they have little understanding. .
If you also feel this way, then don’t be discouraged and hold on, Because network flow is the pearl of combinatorial optimization theory and is your gateway to the profound mysteries of computer theory. first step.
Although the concept of path is very basic, it has many applications. For example, the transportation network can be viewed as a graph, then the path on the graph is a legal transportation route. We can use the shortest path The algorithm finds the best path. Similarly, we can regard social networks, that is, the relationships between people, as a network, and we can use paths to analyze the various nature relationships of social networks; we can even view the written program as a graph, and we can model it through paths The properties of the program, for example, can prove whether certain errors made in the program will occur. These are all kinds of interesting applications based on graph theory.

2. Definition and basis of the maximum flow problem

The maximum flow problem belongs to the category of graph theory. The graph here is a directed weighted graph, and the edges have directions and weights. There are many nodes in the graph, one of which is the starting point, denoted as s, and another The node is the end point, denoted as t.
The maximum flow problem can be understood like this: We want to send water from node s to end point t. The water must be transported through pipes. These pipes are the edges in the graph, and the edges have weights. The weights can Understood as the capacity of the pipe, otherwise it may cause excessive pressure and cause the pipe to burst. So, given the limitations of the pipe, what is the maximum flow?
The first step is to create a residual graph, whose weight is equal to the idle amount of the water pipe. There is no water flow initially, so it is idle. Amount equals capacity;
Insert image description here
The most intuitive and simple algorithm is:
The second step is to find the augmenting path in the residual graph,augumenting Path refers to the simple path from the starting point s to the end point t. Find the minimum weight on the path. Assuming that the minimum weight is x, then the path can only transmit x units of water flow at most, and more If so, the weakest pipes will burst. In addition, if we let x units of water flow through this path, then the free amount of all edges under this path must be subtracted by There is no way to send more water from the starting point to the end point, and the cycle should be terminated at this time!
Insert image description here
**Simple algorithms sometimes fail! **This algorithm can only find blocking flow. Blocking flow means that the network is blocked and cannot transmit more water flow. Blocking flow may not be the maximum flow. The specific example is as follows:
Insert image description here
This shows that the simple algorithm cannot guarantee that the result is the maximum flow. The algorithm introduced later can guarantee that the result must be the maximum flow. .

3.Ford–Fulkerson algorithm

As mentioned earlier, the simple algorithm may not necessarily find the maximum flow. The main reason is that the simple algorithm will not regret and cannot correct errors. Once a bad path is selected, the algorithm cannot find the maximum flow.
But the Ford–Fulkerson algorithm can definitely find the maximum flow, because it can cancel bad paths .
Compared with the simple algorithm, the only difference between the Ford–Fulkerson algorithm and the simple algorithm is that adds a reverse path , so that Come, even if the path you chose before is not good, you can still undo and return to the original path.
Insert image description here
The Ford-Fulkerson algorithm is a classic algorithm used to solve the maximum flow problem of the network. It is based on the idea of ​​finding augmenting paths, increasing traffic by continuously searching for augmenting paths until no augmenting path can be found.
The steps of this algorithm are as follows:

  1. Create a residual graph and initialize all edge flows to 0.
  2. When there is an augmenting path from the source node to the sink node:
    • Use depth-first search or breadth-first search to find an augmenting path. An augmenting path means that the remaining capacity of each edge on the path is greater than 0.
    • After an augmenting path is found, the flow is increased by updating the remaining capacity of the edges on the path.
    • Update the remaining capacity of the edges on the path, that is, reduce the remaining capacity of the forward edge, increase the remaining capacity of the reverse edge.
  3. When the augmenting path cannot be found, the algorithm terminates. At this time, the resulting flow is the maximum flow.
    Insert image description here
    It should be noted that the time complexity of the Ford-Fulkerson algorithm is very slow and will fall into an infinite loop in some cases. Therefore, in order to ensure the termination and convergence of the algorithm, other methods are usually needed. Techniques such as the shortest augmenting path strategy used in the Edmonds-Karp algorithm.
    In short, the Ford-Fulkerson algorithm is a classic algorithm that gradually increases traffic by constantly searching for augmenting paths, and is used to solve the maximum flow problem in the network.

4.Edmonds-Karp Algorithm

The Edmonds-Karp paper was published in 1972, 12 years later than the Ford–Fulkerson algorithm. The Edmonds-Karp algorithm is not a new algorithm, but a special case of the Ford–Fulkerson algorithm. The contributions of Edmonds and Karp lie in their Demonstrated better time complexity, time complexity does not depend on the size of the largest flow. Edmonds-Karp Algorithm is almost identical to Edmonds-Karp Algorithm.The only difference is that Edmonds-Karp Algorithm uses the shortest path algorithm when searching for paths.

The Edmonds-Karp algorithm is an improved version of the Ford-Fulkerson algorithm, which is used to solve the maximum flow problem in the network. It improves the efficiency and convergence speed of the algorithm by using breadth-first search to select the shortest augmenting path.
The following are the detailed steps of the Edmonds-Karp algorithm:

  1. Initialize the flow rate of all edges to 0.
  2. While there is an augmenting path from the source node to the sink node:
    • Use breadth-first search to find a shortest augmenting path. The shortest augmenting path refers to the path with the smallest number of edges, that is, the shortest distance.
    • After finding the shortest augmenting path, calculate the minimum remaining capacity on the path. The minimum remaining capacity refers to the minimum remaining capacity of all edges on the path.
    • Increase the flow by updating the flow of the edges on the path, i.e. increase the flow on the forward edge, or decrease the flow on the reverse edge.
    • Update the remaining capacity of the edges on the path, that is, reduce the remaining capacity of the forward edge and increase the remaining capacity of the reverse edge.
  3. The algorithm terminates when there is no augmenting path from the source node to the sink node. At this time, the resulting flow is the maximum flow.
    The Edmonds-Karp algorithm selects the shortest augmentation path by using breadth-first search. Compared with the depth-first search of the Ford-Fulkerson algorithm, it can find the augmentation path faster. This is because breadth-first search traverses the network layer by layer in a hierarchical manner to find the shortest distance path. This strategy of selecting the shortest augmentation path ensures the convergence and termination of the algorithm.
    In summary, the Edmonds-Karp algorithm is an improved Ford-Fulkerson algorithm based on breadth-first search, which is used to solve the maximum flow problem in the network. It improves the efficiency and convergence speed of the algorithm by selecting the shortest augmentation path.
    Insert image description here
    As can be seen from the figure, this time complexity is much better than the Ford-Fulkerson algorithm because it does not Depends on the size of the network stream. The better contribution of Edmonds and Karp is to prove better time complexity. The time complexity of the Edmonds-Karp algorithm will be briefly analyzed below.
    The main meaning is: assuming that the residual graph has m edges and n points, each cycle of the algorithm must look for unweighted ones in the residual graph The shortest path, so the time complexity of O(m) is required. The reason is that if there are m edges in the original graph, then there are at most m reverse edges in the residual graph, so the residual graph There are no more than 2m edges in total. If an unweighted graph has 2m edges, then it will take O(m) time to find the shortest path. In addition, Edmonds and Karp proved that the algorithm can cycle up to mn rounds, that is, each round of the algorithm takes O(m) time and can cycle up to mn rounds, so the worst-case time complexity is O ( m 2 n ) O(m^2n) O(m2n), this time complexity has nothing to do with the maximum flow, the maximum flow can be Hundreds and tens of millions will not affect the time complexity.
    Insert image description here

5.Dinic’s Algorithm

Dinic's Algorithm is an algorithm for finding the maximum flow in the network. Its time complexity is lower than the Edmonds-Karp Algorithm. Let's briefly compare the time complexity of the two algorithms. The details are as follows:
Insert image description here
It can be seen from the figure that if the number of edges is m and the number of points is n, the time complexity of Edmonds-Karp Algorithm is O( m 2 n m^ 2n m2n), and Dinic's Algorithm's time increase is O( m n 2 mn^ 2mn2), but generally the number of edges m will be much greater than the number of points n, so the time complexity of Dinic's Algorithm will lower.
Dinic’s Algorithm uses Blocking Flow. Blocking flow means that with these flows, more flows cannot be added, which means that these flows block the pipeline.
The maximum flow must be a blocking flow, but the blocking flow may not be the maximum flow. The simple algorithm introduced earlier can definitely find the blocking flow, but it may not be able to find the maximum flow.
In addition, Dinic’s Algorithm will also use Level Graph. Let’s briefly introduce Level Graph.
Given the original graph on the right, the Level Graph is constructed step by step below. Starting from the starting point S, one step can reach v 1 , v 2 v_{1} ,v_{2} in1,in2These two nodes, put v 1 , v 2 v_{1},v_{2} in1,in2Add the Level Graph on the left, and add v 1 , v 2 v_{1},v_{2} in1,in2 is recorded as the first layer, which means that it can be reached by taking one step from the starting point, and the edge from the starting point S to the first layer is retained. Look at the original picture on the right again, from the first layer v 1 , v 2 v_{1},v_{2} in1,in2Start, take one step to reach v 3 , v 4 v_{3},v_{4} in3,in4Two nodes, put v 3 , v 4 v_{3},v_{4} in3,in4Add the Level Graph on the left and add v 3 , v 4 v_{3},v_{4} in3,in4 is recorded as the second level, which means that it can be reached by taking two steps from the starting point, retaining three edges from the first level to the second level. Look at the original picture on the right again. From the nodes on the second layer v 3 , v 4 v_{3},v_{4} in3,in4Departure. You can reach the end point t by taking one step, add the end point t to the Level Graph on the left, and record the end point t as the third level, which means that you can reach it by taking three steps from the starting point, and retain the edge from the second level to the third level, < /span> Dinic’s algorithm is an efficient algorithm for solving the maximum flow problem. It is based on the concepts of augmented paths and hierarchical graphs. The following is a summary of the steps of Dinic’s algorithm:After mastering the preliminary knowledge, let’s get to the point! , the two purple edges on the right side of the figure below are from the first layer to the first layer, and the second layer to the second layer, so they cannot be added to the Level Graph. The edge of Level Graph refers from the previous layer to the next layer.
Insert image description here

  1. Build the initial residual network (residual graph): Initialize the traffic of all edges to 0, and build the initial residual network (residual graph) based on the edges in the network.
  2. Build Level Graph: Use Breadth First Search (BFS) to build a Level Graph. A hierarchical graph is a graph in which each node is layered according to its shortest distance to the source node in the residual network.
  3. Find augmenting paths: In a hierarchical graph, find augmenting paths from source nodes to sink nodes. An augmenting path means that the remaining capacity of all edges on the path is greater than 0.
  4. Update blocking flow and reverse edges: If an augmenting path is found, the minimum remaining capacity on the corresponding residual graph path is used as the blocking flow, the flow is increased, and the remaining capacity of the edges on the path is updated. At the same time, for each edge on the path, the remaining capacity of the reverse edge is increased.
  5. Repeat iteration: Repeat steps 2 to 4 until no more augmenting paths can be found. In each iteration, the level graph will be updated to better guide the search for the next augmentation path.
  6. Termination condition: When the augmenting path can no longer be found, the algorithm terminates. At this time, the resulting flow is the maximum flow.
    Dinic’s algorithm guides the search for augmented paths by constructing a level graph, thereby reducing ineffective searches and improving the efficiency of the algorithm. When finding an augmented path, in addition to updating the remaining capacity of the blocking flow and edges on the path, it is also necessary to increase the remaining capacity of the reverse edge to ensure the correctness of the reverse flow.
    ![Insert image description here](https://img-blog.csdnimg.cn/e82e433f7a91418d891a1c6d62c1f6fe.png

6.Minimum Cut Problem

Minimum Cut Problem is translated as the minimum cut problem. Minimum cut and maximum flow are equivalent.
The minimum cut problem can be understood in this way. Cut means to cut some pipes so that water cannot flow from the starting point s to the end point t. The goal of Min-Cut is to minimize the total capacity of the cut water pipes. In other words, I try to cut thin pipes instead of thick pipes, and I can cut off the flow of water with minimal effort.
For the same picture, there are different cutting methods. The cut amount on the left is 3, which is a minimum cut, while the cut on the right is 6, which is not a minimum cut! In addition, it should be noted that the smallest cut is not unique!
Insert image description here

7.Max-Flow Min-Cut Theorem

Max-Flow Min-Cut Theorem (Max-Flow Min-Cut Theorem) illustrates the equivalence of the maximum flow problem and the minimum cut problem!
For a network flow problem, The flow of the maximum flow is equal to the capacity of the minimum cut, that is, you find Solving the maximum flow problem is equivalent to solving the minimum cut problem. This theorem was proposed by Ford and Fulkerson in 1962.
Insert image description here
Next, we will introduce a problem of finding the minimum cut!
This method equivalently converts the minimum cut problem into a maximum flow problem. As long as we find the maximum flow, we can find the minimum cut.
To put it simply:
1. Use Edmonds-Karp Algorithm to find the final residual graph;
2. Then in the final Remove all reverse edges from the residual graph;
3. After removing all reverse edges from the residual graph, find all the vertices that can be reached from s from the graph and let them be a set part of the set, and the rest is used as the remaining part of the set;
4. Finally, find the minimum cut based on the divided two-part set.
Insert image description here

Guess you like

Origin blog.csdn.net/qq_51447436/article/details/133995708