# Dinic's algorithm

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

## History

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.

## Definition

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,- if [math]\displaystyle{ (u,v)\in E }[/math],
- [math]\displaystyle{ c_f(u,v) = c(u,v) - f(u,v) }[/math]

- if [math]\displaystyle{ (v,u)\in E }[/math],
- [math]\displaystyle{ c_f(u,v) = f(v,u) }[/math]

- [math]\displaystyle{ c_f(u,v) = 0 }[/math] otherwise.

- if [math]\displaystyle{ (u,v)\in E }[/math],

- 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]}

## Algorithm

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

- Set [math]\displaystyle{ f(e) = 0 }[/math] for each [math]\displaystyle{ e\in E }[/math].
- 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].
- Find a blocking flow [math]\displaystyle{ f' }[/math] in [math]\displaystyle{ G_L }[/math].
- Add augment flow [math]\displaystyle{ f }[/math] by [math]\displaystyle{ f' }[/math] and go back to step 2.

## Analysis

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]}

## Example

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.

## See also

## Notes

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

- ↑ 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.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.0}^{3.1}Tarjan 1983, p. 102. - ↑ 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.

## References

- 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. - Kadar, Ilan; Albagli, Sivan (18 April 2019). "Dinitz's algorithm for finding a maximum flow in a network". Ben-Gurion University. https://slideplayer.com/slide/16480683/.
- Korte, B. H.; Vygen, Jens (2008). "8.4 Blocking Flows and Fujishige's Algorithm".
*Combinatorial Optimization: Theory and Algorithms (Algorithms and Combinatorics, 21)*. Springer Berlin Heidelberg. pp. 174–176. ISBN 978-3-540-71844-4. - Tarjan, R. E. (1983).
*Data structures and network algorithms*.

Original source: https://en.wikipedia.org/wiki/Dinic's algorithm.
Read more |