Dinic's algorithm

From HandWiki

Dinic's algorithm or Dinitz's algorithm is a strongly polynomial algorithm for computing the maximum flow in a flow network, conceived in 1970 by Israeli (formerly Soviet) computer scientist Yefim Dinitz.[1] The algorithm runs in [math]\displaystyle{ O(|V|^2|E|) }[/math] time and is similar to the Edmonds–Karp algorithm, which runs in [math]\displaystyle{ O(|V||E|^2) }[/math] time, in that it uses shortest augmenting paths. The introduction of the concepts of the level graph and blocking flow enable Dinic's algorithm to achieve its performance.


Dinitz invented the algorithm in January 1969, as a master's student in Georgy Adelson-Velsky's group. A few decades later, he would recall:[2]

In Adel'son-Vel'sky's Algorithms class, the lecturer had a habit of giving the problem to be discussed at the next meeting as an exercise to students. The DA was invented in response to such an exercise. At that time, the author was not aware of the basic facts regarding [the Ford–Fulkerson algorithm]….

Ignorance sometimes has its merits. Very probably, DA would not have been invented then, if the idea of possible saturated edge desaturation had been known to the author.

In 1970, Dinitz published a description of the algorithm in Doklady Akademii Nauk SSSR. In 1974, Shimon Even and (his then Ph.D. student) Alon Itai at the Technion in Haifa were very curious and intrigued by Dinitz's algorithm as well as Alexander V. Karzanov's related idea of blocking flow. However it was hard for them to decipher these two papers, each being limited to four pages to meet the restrictions of journal Doklady Akademii Nauk SSSR. Even did not give up, and after three days of effort managed to understand both papers except for the layered network maintenance issue. Over the next couple of years, Even gave lectures on "Dinic's algorithm", mispronouncing the name of the author while popularizing it. Even and Itai also contributed to this algorithm by combining BFS and DFS, which is how the algorithm is now commonly presented.[2]

For about 10 years of time after the Ford–Fulkerson algorithm was invented, it was unknown if it could be made to terminate in polynomial time in the general case of irrational edge capacities. This caused a lack of any known polynomial-time algorithm to solve the max flow problem in generic cases. Dinitz's algorithm and the Edmonds–Karp algorithm (published in 1972) both independently showed that in the Ford–Fulkerson algorithm, if each augmenting path is the shortest one, then the length of the augmenting paths is non-decreasing and the algorithm always terminates.


Let [math]\displaystyle{ G = ((V,E),c,f,s,t) }[/math] be a network with [math]\displaystyle{ c(u,v) }[/math] and [math]\displaystyle{ f(u,v) }[/math] the capacity and the flow of the edge [math]\displaystyle{ (u,v) }[/math], respectively.

The residual capacity is a mapping [math]\displaystyle{ c_f\colon V\times V \to R^+ }[/math] defined as,
  1. if [math]\displaystyle{ (u,v)\in E }[/math],
    [math]\displaystyle{ c_f(u,v) = c(u,v) - f(u,v) }[/math]
  2. if [math]\displaystyle{ (v,u)\in E }[/math],
    [math]\displaystyle{ c_f(u,v) = f(v,u) }[/math]
  3. [math]\displaystyle{ c_f(u,v) = 0 }[/math] otherwise.
The residual graph is an unweighted graph [math]\displaystyle{ G_f = ((V, E_f), c_f|_{E_f}, s, t) }[/math], where
[math]\displaystyle{ E_f = \{(u,v)\in V \times V \colon\; c_f(u,v) \gt 0\} }[/math].
An augmenting path is an [math]\displaystyle{ s }[/math][math]\displaystyle{ t }[/math] path in the residual graph [math]\displaystyle{ G_f }[/math].
Define [math]\displaystyle{ \operatorname{dist}(v) }[/math] to be the length of the shortest path from [math]\displaystyle{ s }[/math] to [math]\displaystyle{ v }[/math] in [math]\displaystyle{ G_f }[/math]. Then the level graph of [math]\displaystyle{ G_f }[/math] is the graph [math]\displaystyle{ G_L = ((V, E_L), c_f|_{E_L}, s,t) }[/math], where
[math]\displaystyle{ E_L = \{(u,v)\in E_f \colon\; \operatorname{dist}(v) = \operatorname{dist}(u) + 1\} }[/math].
A blocking flow is an [math]\displaystyle{ s }[/math][math]\displaystyle{ t }[/math] flow [math]\displaystyle{ f' }[/math] such that the graph [math]\displaystyle{ G' = ((V,E_L'), s, t) }[/math] with [math]\displaystyle{ E_L' = \{(u,v) \colon\; f'(u,v) \lt c_f|_{E_L}(u,v)\} }[/math] contains no [math]\displaystyle{ s }[/math][math]\displaystyle{ t }[/math] path. [Note 1][3]


Dinic's Algorithm

Input: A network [math]\displaystyle{ G = ((V, E), c, s, t) }[/math].
Output: An [math]\displaystyle{ s }[/math][math]\displaystyle{ t }[/math] flow [math]\displaystyle{ f }[/math] of maximum value.
  1. Set [math]\displaystyle{ f(e) = 0 }[/math] for each [math]\displaystyle{ e\in E }[/math].
  2. Construct [math]\displaystyle{ G_L }[/math] from [math]\displaystyle{ G_f }[/math] of [math]\displaystyle{ G }[/math]. If [math]\displaystyle{ \operatorname{dist}(t) = \infty }[/math], stop and output [math]\displaystyle{ f }[/math].
  3. Find a blocking flow [math]\displaystyle{ f' }[/math] in [math]\displaystyle{ G_L }[/math].
  4. Add augment flow [math]\displaystyle{ f }[/math] by [math]\displaystyle{ f' }[/math] and go back to step 2.


It can be shown that the number of layers in each blocking flow increases by at least 1 each time and thus there are at most [math]\displaystyle{ |V|-1 }[/math] blocking flows in the algorithm. For each of them:

  • the level graph [math]\displaystyle{ G_L }[/math] can be constructed by breadth-first search in [math]\displaystyle{ O(E) }[/math] time
  • a blocking flow in the level graph [math]\displaystyle{ G_L }[/math] can be found in [math]\displaystyle{ O(VE) }[/math] time[Note 2]

with total running time [math]\displaystyle{ O(E + VE) = O(VE) }[/math] for each layer. As a consequence, the running time of Dinic's algorithm is [math]\displaystyle{ O(V^2 E) }[/math].[2]

Using a data structure called dynamic trees, the running time of finding a blocking flow in each phase can be reduced to [math]\displaystyle{ O(E \log V) }[/math] and therefore the running time of Dinic's algorithm can be improved to [math]\displaystyle{ O(VE \log V) }[/math].

Special cases

In networks with unit capacities, a much stronger time bound holds. Each blocking flow can be found in [math]\displaystyle{ O(E) }[/math] time, and it can be shown that the number of phases does not exceed [math]\displaystyle{ O(\sqrt{E}) }[/math] and [math]\displaystyle{ O(V^{2/3}) }[/math].[Note 3] Thus the algorithm runs in [math]\displaystyle{ O(\min\{V^{2/3}, E^{1/2}\}E) }[/math] time.[4]

In networks that arise from the bipartite matching problem, the number of phases is bounded by [math]\displaystyle{ O(\sqrt{V}) }[/math], therefore leading to the [math]\displaystyle{ O(\sqrt{V} E) }[/math] time bound. The resulting algorithm is also known as Hopcroft–Karp algorithm. More generally, this bound holds for any unit network — a network in which each vertex, except for source and sink, either has a single entering edge of capacity one, or a single outgoing edge of capacity one, and all other capacities are arbitrary integers.[3]


The following is a simulation of Dinic's algorithm. In the level graph [math]\displaystyle{ G_L }[/math], the vertices with labels in red are the values [math]\displaystyle{ \operatorname{dist}(v) }[/math]. The paths in blue form a blocking flow.

[math]\displaystyle{ G }[/math] [math]\displaystyle{ G_f }[/math] [math]\displaystyle{ G_L }[/math]
1. Dinic algorithm G1.svg Dinic algorithm Gf1.svg Dinic algorithm GL1.svg

The blocking flow consists of

  1. [math]\displaystyle{ \{s, 1, 3, t\} }[/math] with 4 units of flow,
  2. [math]\displaystyle{ \{s, 1, 4, t\} }[/math] with 6 units of flow, and
  3. [math]\displaystyle{ \{s, 2, 4, t\} }[/math] with 4 units of flow.

Therefore, the blocking flow is of 14 units and the value of flow [math]\displaystyle{ |f| }[/math] is 14. Note that each augmenting path in the blocking flow has 3 edges.

2. Dinic algorithm G2.svg Dinic algorithm Gf2.svg Dinic algorithm GL2.svg

The blocking flow consists of

  1. [math]\displaystyle{ \{s, 2, 4, 3, t\} }[/math] with 5 units of flow.

Therefore, the blocking flow is of 5 units and the value of flow [math]\displaystyle{ |f| }[/math] is 14 + 5 = 19. Note that each augmenting path has 4 edges.

3. Dinic algorithm G3.svg Dinic algorithm Gf3.svg Dinic algorithm GL3.svg

Since [math]\displaystyle{ t }[/math] cannot be reached in [math]\displaystyle{ G_f }[/math], the algorithm terminates and returns a flow with maximum value of 19. Note that in each blocking flow, the number of edges in the augmenting path increases by at least 1.

See also


  1. This means that the subgraph resulting from removing all saturated edges (edges [math]\displaystyle{ (u,v) }[/math] with [math]\displaystyle{ f'(u,v)=c_f|_{E_L}(u,v) }[/math]) does not contain any path from [math]\displaystyle{ s }[/math] to [math]\displaystyle{ t }[/math]. In other terms, the blocking flow is such that every possible path from [math]\displaystyle{ s }[/math] to [math]\displaystyle{ t }[/math] contains a saturated edge.
  2. Finding the blocking flow can be implemented in [math]\displaystyle{ O(E) }[/math] per path via a sequence of Advance and Retreat operations. See http://courses.csail.mit.edu/6.854/06/scribe/scribe11.pdf for more details.
  3. The [math]\displaystyle{ O(V^{2/3}) }[/math] bound assumes that no two edges connect the same pair of vertices in the same direction, while the [math]\displaystyle{ O(\sqrt E) }[/math] bound makes no such assumption.
  1. E. A. Dinic (1970). "Algorithm for solution of a problem of maximum flow in a network with power estimation". Doklady Akademii Nauk SSSR 11: 1277–1280. http://www.cs.bgu.ac.il/~dinitz/D70.pdf. 
  2. 2.0 2.1 2.2 Dinitz, Yefim (2006). "Dinitz' Algorithm: The Original Version and Even's Version". Theoretical Computer Science: Essays in Memory of Shimon Even. Springer. pp. 218–240. ISBN 978-3-540-32880-3. https://link.springer.com/chapter/10.1007/11685654_10. 
  3. 3.0 3.1 Tarjan 1983, p. 102.
  4. Even, Shimon; Tarjan, R. Endre (1975). "Network Flow and Testing Graph Connectivity". SIAM Journal on Computing 4 (4): 507–518. doi:10.1137/0204043. ISSN 0097-5397.