Problem Statement :

Given a graph which 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 following constraints :

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









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



For example, in following input graph,the maximum s-t flow is 19 which is shown below.

Background :

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

Time complexity of Edmond Karp Implementation is O(VE 2 ). In this post, a new Dinic’s algorithm is discussed which is a faster algorithm and takes O(EV 2 ).

Like Edmond Karp’s algorithm, Dinic’s algorithm uses following concepts :

A flow is maximum if there is no s to t path in residual graph. 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. 1) Do BFS of G to construct a level graph (or assign levels to vertices) and also check if more flow is possible. a) If more flow is not possible, then return. b) 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.

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 discussed A flow isif 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 discussed here

Illustration :









Total Flow = 0 Initial Residual Graph (Same as given 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).



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

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









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







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 After one iteration, residual graph changes to following.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).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 – tTotal flow = Total flow + 5 = 19





The new residual graph is

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 :







