Javatpoint Logo
Javatpoint Logo

Edmonds Karp Algorithm in C++

In this article, we will discuss the Edmonds Karp Algorithm in C++ with implementation.

The Edmonds-Karp algorithm is a variant of the Ford-Fulkerson approach. It is used to determine the maximum flow in a flow network. It increases the effectiveness of the Ford-Fulkerson algorithm by employing BFS (Breadth-First Search) to locate augmenting pathways with the fewest amounts of edges. By doing this, the algorithm is guaranteed to run in O(VE2) time complexity, where V is the number of graph vertices and E is the number of graph edges.

Flow Network:

A flow network is made up of a directed graph where each edge is assigned a capacity (the most flow it can support) and a flow (the volume of flow that passes through it). The objective is to determine the greatest flow between a source vertex and a sink vertex while considering the edges' capacity restrictions.

Algorithm Steps:

There are the following steps of Edmonds-Karp algorithm:

1. Initialization:

Set the residual capacity matrix's initial value to the same value as the capacity matrix. Graph adjacency list initialization.

2. Increasing Paths:

Find a path from the source to the washbasin that is enhanced by using BFS. When a road from a source to a sink contains edges with an available capacity larger than 0, the path is said to be augmenting.

The BFS guarantees that we discover the augmenting pathways with the fewest edges, which is a crucial aspect of the Edmonds-Karp algorithm.

3. Flows Update:

Determine the highest flow that can be sent along the path after discovering an augmenting one. All the edges in the augmenting path have a minimum capacity of this.

The forward and backward edges along the augmenting path's residual capabilities are updated by subtracting the flow from the forward edge and adding it to the backward edge.

4. Repeat:

When no more enhancing pathways can be located, repeat steps 2 and 3 once more.

5. Termination:

When no additional enhancing pathways are detected, the process is finished. The total of all flows exiting the source is the greatest flow that can be obtained.

Implementation information in C++:

The Edmonds-Karp algorithm is implemented using the provided C++ code in the following ways:

  1. It employs a graph representation known as an adjacency list, where adj[u] holds information on the vertex u's
  2. The initial value of the capacity matrix, which stores the edge capacity, is 0.
  3. The BFS is carried out by the bfs function, which then returns the maximum amount of flow that may be delivered along the path to locate augmenting paths.
  4. The user enters the number of nodes, the number of edges, the capacities of the edges, the source, and the sink in the main function.
  5. The network's flow is updated as a result of the algorithm's employment of the bfs function to locate augmenting pathways.
  6. Calculated and written at the conclusion is the maximum flow.

Overview of the Edmonds-Karp algorithm's operation:

  1. Set each edge's flow initialized to 0.
  2. When an augmenting path (a route from the source to the washbasin with capacity available on all edges) is present, take the following actions:
    1. To identify an augmenting path, do a BFS from the source to the washbasin. Only take into account edges with available capacity during the BFS.
    2. If no augmenting path can be found, stop the algorithm because no more advancements are possible.
    3. Determine the minimal capacity (residual capacity) along the path of an augmenting path is discovered. It will be the maximum flow that the network can accommodate.
    4. By removing the backward edges (to represent the reverse flow) and adding to the forward edges (to update the flow along the augmenting channel), the minimum capacity is applied to the forward edges.
  3. Add up the flow that is leaving the source vertex to determine the overall flow.

Example:

Let's take a program to illustrate the Edmonds-Karp algorithm in C++:

Output:

enter the number of nodes and edges
6
10
enter the source and sink
0
5
enter the start and end vertex along with the capacity
0
1
16
enter the start and end vertex along with the capacity
0
2
13
enter the start and end vertex along with capacity
1
2
10
enter the start and end vertex along with capacity
2
1
4
enter the start and end vertex along with capacity
1
3
12
enter the start and end vertex along with capacity
3
2
9
enter the start and end vertex along with capacity
2
4
14
enter the start and end vertex along with capacity
4
3
7
enter the start and end vertex along with capacity
4
5
4
enter the start and end vertex along with capacity
3
5
20
Max Flow is:23

Explanation:

  1. The code begins with several #include instructions that add the header files required for input/output operations, queue handling, text manipulation, and vector usage.
  2. The code declares and initializes different arrays and vectors to store data about the flow network:
    • capacities[10][10]: The capacity of edges connecting nodes is stored in this array. The capacities[i][j] is a 2D array that stores the capacity from node i to node j.
    • flowPassed[10][10]: The flow that has traversed each edge connecting nodes is stored in this array.
    • vector<int>graph[10]: The adjacency list of the graph is shown by this array of vectors. The vector connected to each element records the nodes connected by edges, and each element is a node.
    • parentsList[10]: It is an array that records the route followed by the BFS traversal.
    • currentPathCapacity[10]: It is an array that keeps track of the capability of the path being examined at the moment.
  3. The bfs function is intended to carry out a Breadth-First Search from the startNode to the endNode to locate an augmenting path with available capacity. A source-to-sink path that has edges that can handle more flow is known as an augmenting path.
    The BFS algorithm employs a queue for layer-by-layer node exploration. The currentPathCapacity is updated during traversal to keep track of the path and the path's minimal capacity.
  4. Implementation of the core Edmonds-Karp algorithm is done via the edmondsKarp function. Up until there are no more augmenting paths left to be found, it repeatedly invokes the bfs function to locate augmenting paths and adjusts the flow along those paths. From the source to the washbasin, it sends the whole maximum flow.
    • As long as there is still flow to be added, the while loop inside of this function keeps looking for ways to augment the flow.
    • The flow variable is derived from the bfs function, which determines the maximum flow that can be added along the current path.
    • As a result of the altered flow in the graph, the loop then modifies the flowPassed array.
  5. The main function is where the execution starts:
    • It accepts input for the number of nodes, edges, sources, and sinks.
    • It populates the capacities array and the graph's adjacency list by iterating through each edge while collecting input for the start node, end node, and capacity.
    • Finally, it uses the Edmonds Karp function to determine the maximum flow and outputs the result.

The code applies the Edmonds-Karp method to address the maximum flow issue in a flow network. BFS is used to locate augmenting paths to compute the maximum flow from the source to the sink. After that, it updates the flow through the network until no more augmenting paths are found.







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