Network Flow foundation and Python implementation

First of all, I would like to thank the following references and their authors:
Graph theory and network flow_ Book search (duxiu.com)
Optimization method and its MATLAB implementation (duxiu.com)
Implementing network flow with Python networkx_ Ugly gardon CSDN blog
Network flow and matching (yuyanwz.cn)
networkx solves the minimum cost maximum flow and visualizes the data_ engineoid blog - CSDN blog
The text begins below.
In life, people often use directed graph to create models for various network models, such as transportation network, communication network, transmission network, oil and gas pipeline network, Internet, social network and so on. The common feature of these networks is that they are all directed graphs, with starting points, receiving points and transfer points. Each directed graph has the limitation of transmission capacity. For example, imagine a highway transportation network whose apex is the city (or the intersection of roads). The edge is the highway. The capacity on the edge refers to the transmission capacity, which can be the maximum transportation volume of goods. The sending point is the place where goods are provided, and the receiving point is the place where goods are needed. It can receive goods transmitted from other vertices. Network flow theory is also a very important branch of graph theory, and provides the proof of many famous conclusions in graph theory.
Figure 1 shows a network flow. The number next to each directed edge represents its capacity. There are several basic definitions in network flow:
• node: at this point, the inflow flow is the same as the outflow flow
• source: at this point, the outflow flow is greater than the inflow flow
• sink: at this point, inflow is greater than outflow
Capacity and value of a flow: there are two quantities on each directed edge, capacity and flow. The capacity from i to j is usually represented by c[i,j], and the flow is usually f[i,j].

And the Edmonds Karp algorithm

If source is compared to a production plant, the maximum flow problem is to find out how much goods can be delivered from the plant and not exceed the capacity limit of the road.
One of the most basic methods to calculate the maximum flow is The Edmonds Karp algorithm . Let's take chestnuts for example.
First, let's start with zero flow (all flows are 0 flows). If there is such a path, this path starts from the source point and is connected to the sink section by section, and each section of this path meets the flow < capacity. Then, we must be able to find the minimum delta among the (capacity flow) values of each section of the road. We add this delta to the flow of each section of the road to ensure that the flow is still a feasible flow, which is obvious. In this way, we get a larger flow. Its flow is the previous flow + delta. Obviously, when delta=0, it is the maximum flow that the algorithm can find.

Taking the network flow model in Figure 2(a) as an example, we found the flow 1-2-3-4 for the first time. When this flow is added, the delta becomes 0 (Figure 2(b)). However, 1 is obviously not the maximum flow, because if you choose 1-2-4 and 1-3-4, you can get a flow with flow of 2.
The problem with the above method is that there is no "estoppel" mechanism, and Edmonds Karp algorithm solves this problem by introducing "reverse edge".
Go back to figure 2(a) and select 1-2-3-4 to turn into Figure 2 ©. Due to the existence of the reverse edge, we can find the path 1-3-2-4, and then get the correct maximum flow 2 as shown in Fig. 2(d).

Now how to solve this problem?

In 1955, when studying the maximum railway flux, Harris first proposed the problem of finding the maximum transportation volume between two points on a given network. In 1956, Ford et al. Gave a labeling method to solve this kind of problem, so as to establish the network flow theory. Now we solve the network flow problem as complex as in the previous chapter. Through NetworkX, a third-party library of Python, we can quickly establish a network and solve it.

Figure 3 shows the running results of NetworkX. You can also try to run the following code yourself:

import networkx as nx
import matplotlib.pyplot as plt

# Create an empty directed graph
G = nx.DiGraph()

#Drawing
G.add_edges_from([('s','v1',{'capacity': 1}),
                  ('s','v2',{'capacity': 1}),
                  ('v1','v2',{'capacity': 1}),
                  ('v1','t',{'capacity': 1}),
                  ('v2','t',{'capacity': 1})])

pos=nx.spring_layout(G)#The default assigned position of the force oriented layout algorithm
#Initialization, initial position of selected node / fixed node
pos['t'][0]=1;pos['t'][1]=0
pos['s'][0]=-1;pos['s'][1]=0
pos['v1'][0]=0;pos['v1'][1]=1
pos['v2'][0]=0;pos['v2'][1]=-1

#Display graph
#Capacity displayed on the processing edge
edge_label = nx.get_edge_attributes(G,'capacity')

#Add the capacity labels of nodes and edges to the graph
nx.draw_networkx_nodes(G,pos)
nx.draw_networkx_labels(G,pos)
nx.draw_networkx_edges(G,pos)
nx.draw_networkx_edge_labels(G, pos,edge_label,font_size=15)#Display original image

#Solving maximum flow
maxFlow = nx.maximum_flow(G, 's', 't')
# print(maxFlow)#You can output stream information to see
maxFlow_const=maxFlow[1]

#Take out the flow information of each side and store it in the side display value
for i in maxFlow_const.keys():
    for j in maxFlow_const[i].keys():
        edge_label[(i,j)] = str(edge_label[(i,j)]) + ',F=' + str(maxFlow_const[i][j])

#Add the label of the new edge to the diagram
nx.draw_networkx_edge_labels(G, pos,edge_label,font_size=12)

#Display flow and original diagram
plt.axis('on')
plt.xticks([])
plt.yticks([])
plt.show()

Tags: Python Graph Theory

Posted on Tue, 16 Nov 2021 04:51:57 -0500 by jokerofsouls