VOOZH about

URL: https://www.geeksforgeeks.org/dsa/dinics-algorithm-maximum-flow/

⇱ Dinic's algorithm for Maximum Flow - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Dinic's algorithm for Maximum Flow

Last Updated : 23 Jul, 2025

Problem Statement : 
Given a graph that 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 the following constraints :  

  1. Flow on an edge doesn’t exceed the given capacity of the edge.
  2. An incoming flow is equal to an outgoing flow for every vertex except s and t.

For example: 

In the following input graph, 

πŸ‘ Dinic's algorithm for Maximum Flow 1


the maximum s-t flow is 19 which is shown below. 

πŸ‘ Image
 


Background : 

  1. Max Flow Problem Introduction: We introduced the Maximum Flow problem, discussed Greedy Algorithm, and introduced the residual graph.
  2. Ford-Fulkerson Algorithm and Edmond Karp Implementation: We discussed the Ford-Fulkerson algorithm and its implementation. We also discussed the residual graph in detail.

The 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 : 

  1. A flow is maximum if there is no s to t path in residual graph.
  2. 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.

Outline of Dinic's algorithm : 

  1. Initialize residual graph G as given graph.
  2. Do BFS of G to construct a level graph (or assign levels to vertices) and also check if more flow is possible.
    1. If more flow is not possible, then return
    2. 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 here.

Illustration : 
Initial Residual Graph (Same as given Graph) 
 

πŸ‘ Initial Residual 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). 
 

πŸ‘ First Iteration

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. 
 

πŸ‘ residual graph after first iteration


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). 

πŸ‘ Second Iteration


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 
 

πŸ‘ Rssidual graph after Second Iteration

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.

Implementation : 
Below is c++ implementation of Dinic's algorithm:  


Output
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). 

Space complexity:
The space complexity of Dinic’s algorithm is O(V+E), since it requires O(V) to store the level array and O(E) to store the graph's adjacency list.


 

Comment
Article Tags: