Network Flow
1: Problem Description
As stated, given a network graph with a source node and a sink node, calculate the maximum flow in the network.
1.1: Input Format
The first line contains four positive integers: $$n,m,s,t$$, representing the number of nodes, the number of directed edges, the source node index, and the sink node index, respectively.
The following mm lines each contain three positive integers: $u_i,v_i,w_i$, representing the i-th directed edge starting from uiui, ending at $v_i$, with a weight $w_i$ (indicating the maximum flow capacity of the edge).
1.2: Output Format
A single line containing a single positive integer, which is the maximum flow in the network.
1.3: Example
Input:
1 |
|
Output
1 |
|
2: Solution 1: Edmonds–Karp Algorithm
- Find a path from the source ss to the sink tt where all edges have remaining capacity ($capacity−flow>0$). Such a path is called “connected.”
- Determine the bottleneck capacity along this path as $Augment=min(capacity−flow)$.
- Add the $Augment$ to the maxflow result
- Repeat the previous steps until the algorithm cannot find any connected path from s to t
2.1: Code (Part)
1 |
|
2.2: Time Complexity Analysis
- The time complexity of finding augment path for one time is $O(m)$
- The upper limit of times of Augument is $O(nm)$
- So the time complexity of this algorithm is $O(nm^2)$
3: Solution 2: Dinic Algorithm
The Dinic algorithm works by dividing the graph into layers and repeatedly finding augmenting paths to increase the flow. Here’s the main idea:
BFS Layering: First, perform a Breadth-First Search (BFS) to divide the graph into layers. The distance from a node uu to the source ss determines its layer. Remove edges where the flow is equal to or exceeds the capacity to create the “level graph” $G_L=(V,E_L)$.
Blocking Flow: In the level graph $G_L$, find a maximal flow that cannot be further augmented. This is called the blocking flow.
Repeat: Continue finding and adding blocking flows to the total flow until no augmenting path exists between the source s and the sink t.
3.1: Key Optimization
While performing DFS to find blocking flows:
If you encounter a node u where all outgoing edges are already saturated, skip u.
Maintain a pointer for each node uu to keep track of which edge to explore next. This prevents revisiting edges unnecessarily and improves efficiency.
3.2: Code (Part)
1 |
|
4: Soluiton 3: ISAP Algorithmn
Perform a BFS from t to s and mark the depth (why it’s from t to s will be explained later).
Perform a DFS from s to t, similar to Dinic’s algorithm. However, after finishing processing a node, if the flow passed from the previous node exceeds the used capacity of the current node (for the current depth), the current node is considered “wasted” for the remaining path. In this case, increment its depth by 1. If a gap (a depth level with no nodes) appears, terminate the algorithm.
If step 2 does not terminate the algorithm, repeat step 2.
4.1: Optimization:
Compare to Dinic Algorithm, ISAP doesnt require to run bfs several times.
4.2: Code (Part)
1 |
|