Javatpoint Logo
Javatpoint Logo

Ford Fulkerson Algorithm in C++

In this article, we will discuss the Ford Fulkerson Algorithm in C++ and its implementation.

What is the Ford Fulkerson Algorithm?

The Ford-Fulkerson algorithm is often used to solve maximum flow problems in flow. The maximum flow problem is concerned with finding the maximum amount of flow that can be transferred from the given source vertex to a sink vertex in a directionally weighted graph while considering edge capacity constraints. The technique operates by iteratively identifying an enhanced path in the residual graph, which connects the beginning point to the sink. It is the graph created by subtracting a current flow of data from the capacity of each edge. The program then increases the flow along this path by the maximum amount possible, which is equal to the minimum capacity of the path's edges.

Problem:

Consider a graph that depicts a flow network and each edge having the capacity value. Also, given two vertices in the network, source 's' and sink 't', determine the largest feasible flow from s to t with a few constraints:

  • Flow on an edge is limited to the edge's capacity.
  • Except for s and t, every vertex has the same incoming and outgoing flow.

The Ford-Fulkerson Algorithm

The following is a fundamental concept of the Ford-Fulkerson algorithm:

  1. Begin with an initial flow of 0.
  2. It also contains an additional passage from the source to the sink:
    • Generate an expanded path using a path search technique such as: Breadth-first search or depth-first search.
    • Calculate the flow rate that can be transferred along an extended path. This is the minimum remaining capacity at the end of the path.
    • Increase the rate of flow along the augmenting path to the specified amount.
  3. Return the highest possible flow.

Implementation:

First, let us explain the concept of Residual Graph, which is required to understand the implementation.

The residual graph of a flow network indicates more possible flows. If the residual graph has a path from source to sink, flow can be added. Every edge in a residual graph has an indicator called residual capacity, whose value is equal to the edge's initial capacity minus its present flow. Residual capacity refers to the edge's current capacity.

Now, let's go into the details of implementation. The residual capacity is zero if there is no edge between two vertices in the residual graph. We may initialize the residual graph as the initial graph because there is no starting flow, and the amount of residual capacity is equal to that of the original capacity at the start. We can perform a BFS or a DFS on the residual graph to discover an augmenting path. We can use BFS to determine whether or not a route exists from source to sink. BFS also generates a parent[] array. Using the parent[] array, we walk the discovered path to determine a feasible flow through it by calculating the minimal residual capacity along the path. After that, we include the discovered route flow into the overall flow.

The crucial point is that we update the residual capacity in the residual graph. We subtract route flow from all edges throughout the path and add it to the reverse edges. We need to provide route flow along reverse edges since we may need to send flow in the reverse direction.

Example:

Let us take an example to illustrate the Ford Fulkerson Algorithm in C++.

FileName: FordFulkerson.cpp

Output:

The maximum possible flow is 24

Complexity Analysis:

Time Complexity:

Time complexity is O(|V| * E^2), where E is the number of edges and V is the number of vertices.

Space complexity:

The space complexity is O(V) because we constructed a queue.

The above program used for the Ford Fulkerson Algorithm is referred to as the Edmonds-Karp Algorithm. Edmonds-Karp proposes using BFS in the Ford Fulkerson solution because BFS always chooses the path with the fewest number of edges. When using BFS, the worst-case time complexity is lowered to O(VE^2). The above implementation employs adjacency matrix accountability, however where BFS takes O(V^2) time, the above implementation requires O(EV^3).

Applications of Ford Fulkerson Algorithm:

The most popular technique for figuring out the maximum flow in a flow network is the Ford-Fulkerson algorithm. It has several uses in diverse fields. The Ford-Fulkerson method is commonly used in the following applications and scenarios:

Network:

Ford-Fulkerson algorithm is mostly used to solve network flow problems. It specifies the maximum quantity of flow that may be sent from a source to a sink in a flow network.

Transportation:

Optimizing transportation and logistics networks involves maximizing the flow of products from suppliers to the consumer through roads, trains, or other transportation linkages.

Telecommunication:

The technique can improve data flow in telecommunication networks, leading to effective communication between multiple nodes.

Image segmentation:

In artificial intelligence, the Ford-Fulkerson method may split images into segments depending on characteristics like intensity or color.







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA