Ford
Ford
Maximum flow problems involve finding a feasible flow through a single-source, single-sink flow network that
is maximum.
Each edge is labeled with a capacity, the maximum amount of stuff that it can carry. The goal is to figure out
how much stuff can be pushed from the vertex s(source) to the vertex t(sink).
.
Maximum flow possible is : 23
Following are different approaches to solve the problem :
1. Naive Greedy Algorithm Approach (May not produce optimal or correct result)
Greedy approach to the maximum flow problem is to start with the all-zero flow and greedily produce flows
with ever-higher value. The natural way to proceed from one to the next is to send more flow on some path from
s to t
Note that the path search just needs to determine whether or not there is an s-t path in the subgraph of edges e
with f(e) < C(e). This is easily done in linear time using BFS or DFS.
There is a path from source (s) to sink(t) [ s -> 1 -> 2 -> t] with maximum flow 3 unit ( path show in blue color )
After removing all useless edge from graph its look like
For above graph there is no path from source to sink so maximum flow : 3 unit But maximum flow is 5 unit. to
over come form this issue we use residual Graph.
2. Residual Graphs
The idea is to extend the naive greedy algorithm by allowing undo operations. For example, from the point
where this algorithm gets stuck in above image, wed like to route two more units of flow along the edge (s, 2),
then backward along the edge (1, 2), undoing 2 of the 3 units we routed the previous iteration, and finally along
the edge (1,t)
Time Complexity: Time complexity of the above algorithm is O(max_flow * E). We run a loop while there is
an augmenting path. In worst case, we may add 1 unit flow in every iteration. Therefore the time complexity
becomes O(max_flow * E).
Let us now talk about implementation details. Residual capacity is 0 if there is no edge between two vertices of
residual graph. We can initialize the residual graph as original graph as there is no initial flow and initially
residual capacity is equal to original capacity. To find an augmenting path, we can either do a BFS or DFS of the
residual graph. We have used BFS in below implementation. Using BFS, we can find out if there is a path from
source to sink. BFS also builds parent[] array. Using the parent[] array, we traverse through the found path and
find possible flow through this path by finding minimum residual capacity along the path. We later add the
found path flow to overall flow.
The important thing is, we need to update residual capacities in the residual graph. We subtract path flow from
all edges along the path and we add path flow along the reverse edges We need to add path flow along reverse
edges because may later need to send flow in reverse direction