Given a graph which represents a flow network where every edge has a capacity. Also given two vertices source ‘s’ and sink ‘t’ in the graph, find the maximum possible flow from s to t with following constraints :
- Flow on an edge doesn’t exceed the given capacity of the edge.
- Incoming flow is equal to outgoing flow for every vertex except s and t.
For example, in following input graph,
the maximum s-t flow is 19 which is shown below.
- Max Flow Problem Introduction : We introduced Maximum Flow problem, discussed Greedy Algorithm and introduced residual graph.
- Ford-Fulkerson Algorithm and Edmond Karp Implementation : We discussed Ford-Fulkerson algorithm and its implementation. We also discussed residual graph in detail.
Time complexity of Edmond Karp Implementation is O(VE2). In this post, a new Dinic’s algorithm is discussed which is a faster algorithm and takes O(EV2).
Like Edmond Karp’s algorithm, Dinic’s algorithm uses following concepts :
- A flow is maximum if there is no s to t path in residual graph.
- BFS is used in a loop. There is a difference though in the way we use BFS in both algorithms.
In Edmond’s Karp algorithm, we use BFS to find an augmenting path and send flow across this path. In Dinic’s algorithm, we use BFS to check if more flow is possible and to construct level graph. In level graph, we assign levels to all nodes, level of a node is shortest distance (in terms of number of edges) of the node from source. Once level graph is constructed, we send multiple flows using this level graph. This is the reason it works better than Edmond Karp. In Edmond Karp, we send only flow that is send across the path found by BFS.
1) Initialize residual graph G as given graph. 1) Do BFS of G to construct a level graph (or assign levels to vertices) and also check if more flow is possible. a) If more flow is not possible, then return. b) Send multiple flows in G using level graph until blocking flow is reached. Here using level graph means, in every flow, levels of path nodes should be 0, 1, 2... (in order) from s to t.
A flow is Blocking Flow if no more flow can be sent using level graph, i.e., no more s-t path exists such that path vertices have current levels 0, 1, 2… in order. Blocking Flow can be seen same as maximum flow path in Greedy algorithm discussed discussed here.
Initial Residual Graph (Same as given Graph)
Total Flow = 0
First Iteration : We assign levels to all nodes using BFS. We also check if more flow is possible (or there is a s-t path in residual graph).
Now we find blocking flow using levels (means every flow path should have levels as 0, 1, 2, 3). We send three flows together. This is where it is optimized compared to Edmond Karp where we send one flow at a time.
4 units of flow on path s – 1 – 3 – t.
6 units of flow on path s – 1 – 4 – t.
4 units of flow on path s – 2 – 4 – t.
Total flow = Total flow + 4 + 6 + 4 = 14
After one iteration, residual graph changes to following.
Second Iteration : We assign new levels to all nodes using BFS of above modified residual graph. We also check if more flow is possible (or there is a s-t path in residual graph).
Now we find blocking flow using levels (means every flow path should have levels as 0, 1, 2, 3, 4). We can send only one flow this time.
5 units of flow on path s – 2 – 4 – 3 – t
Total flow = Total flow + 5 = 19
The new residual graph is
Third Iteration : We run BFS and create a level graph. We also check if more flow is possible and proceed only if possible. This time there is no s-t path in residual graph, so we terminate the algorithm.
Below is c++ implementation of Dinic’s algorithm:
Maximum flow 23
Time Complexity : O(EV2). Doing a BFS to construct level graph takes O(E) time. Sending multiple more flows until a blocking flow is reached takes O(VE) time. The outer loop runs at-most O(V) time. In each iteration, we construct new level graph and find blocking flow. It can be proved that the number of levels increase at least by one in every iteration (Refer the below reference video for the proof). So the outer loop runs at most O(V) times. Therefore overall time complexity is O(EV2).
This article is contributed by Nishant Singh. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
- Ford-Fulkerson Algorithm for Maximum Flow Problem
- Spanning Tree With Maximum Degree (Using Kruskal's Algorithm)
- Hopcroft–Karp Algorithm for Maximum Matching | Set 1 (Introduction)
- Hopcroft–Karp Algorithm for Maximum Matching | Set 2 (Implementation)
- Max Flow Problem Introduction
- Cuts and Network Flow
- Find minimum s-t cut in a flow network
- Minimize Cash Flow among a given set of friends who have borrowed money from each other
- Jump Pointer Algorithm
- Prim's algorithm using priority_queue in STL
- Relabel-to-front Algorithm
- Bellman–Ford Algorithm | DP-23
- Floyd Warshall Algorithm | DP-16
- Boruvka's algorithm | Greedy Algo-9
- Hierholzer's Algorithm for directed graph