# Dinic's algorithm

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 ${\displaystyle O(V^{2}E)}$ time and is similar to the Edmonds–Karp algorithm, which runs in ${\displaystyle O(VE^{2})}$ 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.

## Definition

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

The residual capacity is a mapping ${\displaystyle c_{f}\colon V\times V\to R^{+}}$ defined as,
The residual graph is the graph ${\displaystyle G_{f}=((V,E_{f}),c_{f}|_{E_{f}},s,t)}$, where
${\displaystyle E_{f}=\{(u,v)\in V\times V:c_{f}(u,v)>0\}}$.
An augmenting path is an ${\displaystyle s-t}$ path in the residual graph ${\displaystyle G_{f}}$.
Define ${\displaystyle \operatorname {dist} (v)}$ to be the length of the shortest path from ${\displaystyle s}$ to ${\displaystyle v}$ in ${\displaystyle G_{f}}$. Then the level graph of ${\displaystyle G_{f}}$ is the graph ${\displaystyle G_{L}=(V,E_{L},c_{f}|_{E_{L}},s,t)}$, where
${\displaystyle E_{L}=\{(u,v)\in E_{f}:\operatorname {dist} (v)=\operatorname {dist} (u)+1\}}$.
A blocking flow is an ${\displaystyle s-t}$ flow ${\displaystyle f}$ such that the graph ${\displaystyle G'=(V,E_{L}',s,t)}$ with ${\displaystyle E_{L}'=\{(u,v):f(u,v) contains no ${\displaystyle s-t}$ path.

## Algorithm

Dinic's Algorithm

Input: A network ${\displaystyle G=((V,E),c,s,t)}$.
Output: An ${\displaystyle s-t}$ flow ${\displaystyle f}$ of maximum value.

## Analysis

It can be shown that the number of edges in each blocking flow increases by at least 1 each time and thus there are at most ${\displaystyle n-1}$ blocking flows in the algorithm, where ${\displaystyle n}$ is the number of vertices in the network. The level graph ${\displaystyle G_{L}}$ can be constructed by Breadth-first search in ${\displaystyle O(E)}$ time and a blocking flow in each level graph can be found in ${\displaystyle O(VE)}$ time. Hence, the running time of Dinic's algorithm is ${\displaystyle O(V^{2}E)}$.

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

### Special cases

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

In networks arising during the solution of bipartite matching problem, the number of phases is bounded by ${\displaystyle O({\sqrt {V}})}$, therefore leading to the ${\displaystyle O({\sqrt {V}}E)}$ 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.Template:Sfn

## Example

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

${\displaystyle G}$ ${\displaystyle G_{f}}$ ${\displaystyle G_{L}}$
1.

The blocking flow consists of

1. ${\displaystyle \{s,1,3,t\}}$ with 4 units of flow,
2. ${\displaystyle \{s,1,4,t\}}$ with 6 units of flow, and
3. ${\displaystyle \{s,2,4,t\}}$ with 4 units of flow.

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

2.

The blocking flow consists of

1. ${\displaystyle \{s,2,4,3,t\}}$ with 5 units of flow.

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

3.

Since ${\displaystyle t}$ cannot be reached in ${\displaystyle G_{f}}$. 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.

## History

Dinic's algorithm was published in 1970 by former Russian Computer Scientist Yefim (Chaim) A. Dinitz, who is today a member of the Computer Science department at Ben-Gurion University of the Negev (Israel), earlier than the Edmonds–Karp algorithm, which was published in 1972 but was discovered earlier. They independently showed that in the Ford–Fulkerson algorithm, if each augmenting path is the shortest one, the length of the augmenting paths is non-decreasing.

## Notes

1. {{#invoke:Citation/CS1|citation |CitationClass=journal }}

## References

• {{#invoke:citation/CS1|citation

|CitationClass=book }}

• {{#invoke:citation/CS1|citation

|CitationClass=book }}

• {{#invoke:citation/CS1|citation

|CitationClass=book }}