
Learn through the super-clean Baeldung Pro experience:
>> Membership and Baeldung Pro.
No ads, dark-mode and 6 months free of IntelliJ Idea Ultimate to start with.
Last updated: May 31, 2023
Dinic’s Algorithm is a powerful tool in computer science for solving network flow problems efficiently. In this tutorial, we’ll take a look at what this algorithm means and what it does.
Then, we’ll go through the steps of the Dinic algorithm. In addition to an example of resolving the maximum flow problem using Dinic’s algorithm, we’ll discuss the time complexity and the use cases of this algorithm.
Dinic’s algorithm is a popular algorithm for determining the maximum flow in a flow network. Which is regarded as one of the most effective algorithms for resolving the maximum flow problem. It was created in 1970 by computer scientist Yefim (Chaim) Dinic.
Dinic’s algorithm, which is based on level graphs and blocking flows, has an complexity, where
is the number of network vertices (also known as nodes) and
is the number of network edges.
Here are additional concepts used in the Dinic algorithm:
Let’s imagine that this weekend we have plans to visit an amusement park. However, all we know is that it’s 10 minutes south of our house. Since we have never been to this amusement park, we are unsure of its exact location. How do we plan to get there? South, southeast, or southwest are the only directions that make sense to take. This algorithm will make sure that we only move forward in the direction of our goal.
Therefore, the question is how to use this to address the maximum flow issue. The algorithm can be outlined as follows:
Let’s now discover how the fundamental components of Dinic’s algorithm interact to produce such an effective algorithm.
First, we initialize the flow by giving the network a zero initial flow. Then, we create the level graph:
Let’s now locate blocking flows:
By enhancing the flow along the augmenting paths identified in step , we update the network’s flow. This is accomplished by selecting the edge along the augmenting path with the lowest capacity, and adding it to the flow.
Let’s now repeat steps until no additional augmenting paths are visible in the residual graph. The algorithm concludes at this point, and the obtained flow represents the maximum possible flow through the network.
Here’s an initial residual graph:
In the initial graph, the .
Let’s use BFS to assign levels to all nodes. Then, we investigate whether an additional flow is possible (or there is a path in the residual graph):
The edges marked with a block sign cannot be used to send flow because they do not connect nodes of a lower level to nodes of a higher level. The blocking flow is found using levels, where every flow path should have levels as 0, 1, 2, 3. Three flows are sent at once, unlike Edmond Karp where only one flow is sent at a time:
The total flow is calculated as . Then, the residual graph changes to the following after the first iteration:
Using the BFS for the above modified residual graph, we assign new levels to all nodes. We also investigate whether an additional flow is possible (or there is a path in the residual graph):
The edges marked with a block sign cannot be used to send flow because they do not connect nodes of a lower level to nodes of a higher level (from level to level
). So, the blocking flow is identified using levels, where every flow path should have levels as 0, 1, 2, 3, 4. In this case, only one flow can be sent at a time:
The residual graph changes to the following after the second iteration:
In the third iteration, we run BFS and draw a level graph. We also determine whether more flow is possible and proceed only if it is. There is no path in the residual graph this time, which means the algorithm is terminated.
Let’s now look at the implementation of Dinic’s algorithm. Here’s a pseudocode for implementing the algorithm:
algorithm DinicAlgorithm(G, s, t):
// INPUT
// G = the original flow network
// s = the source vertex
// t = the sink vertex
// OUTPUT
// maxFlow = the maximum flow in the network
Gf <- initialize a residual graph with capacities and flows from G
level <- initialize an array to store levels of vertices
ptr <- initialize an array to keep track of the next edge to be explored for each vertex
// Initialize the maximum flow to 0
maxFlow <- 0
while true:
// Reset level array for each iteration
level <- fill array level with -1
// Set the level of the source vertex to 0
level[s] <- 0
Find the levels of all vertices using breadth-first search
if level[t] = -1:
// If the level of the sink vertex remains -1, no more augmenting paths exist
break
// Reset the pointer array for each iteration
ptr <- array of size V, initialized with 0
while true:
// Find the augmenting path and its bottleneck capacity
bottleneck <- DFS(s, infinity)
if bottleneck = 0:
// If no more augmenting paths can be found, exit the inner loop
break
// Update the maximum flow by adding the bottleneck capacity
maxFlow <- maxFlow + bottleneck
return maxFlow
Dinic’s algorithm efficiently computes the maximum flow in a flow network. It uses the original flow network and its residual graph
, with
as the source vertex and
as the sink vertex.
The algorithm employs and
techniques, utilizing the
and
arrays. In
,
represents the neighbors of vertex
.
The algorithm iterates until there are no more augmenting paths in It constructs the level graph using
and finds augmenting paths using
to update the network flow (
is a large constant used as the starting point for bottleneck capacity).
The algorithm’s complexity is :
Dinic’s algorithm is a powerful algorithm for efficiently solving network flow problems. Here are some examples of appropriate applications for Dinic’s algorithm:
In this tutorial, we provided an overview of Dinic’s algorithm, its steps, implementation, complexity, and use cases. We also highlighted its efficiency in solving network flow problems across diverse domains.
Dinic’s algorithm emerges as a valuable tool for optimizing resources, information, and energy flow in various applications, making it a preferred choice in network flow optimization.