Java Program to Implement Ford–Fulkerson Algorithm

This Java program is to Implement Ford-Fulkerson algorithm. The Ford–Fulkerson method (named for L. R. Ford, Jr. and D. R. Fulkerson) is an algorithm which computes the maximum flow in a flow network.
The idea behind the algorithm is simple. As long as there is a path from the source (start node) to the sink (end node), with available capacity on all edges in the path, we send flow along one of these paths. Then we find another path, and so on. A path with available capacity is called an augmenting path.

Here is the source code of the Java program to implement ford-fulkerson algorithm. The Java program is successfully compiled and run on a Linux system. The program output is also shown below.

import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;
 
public class FordFulkerson
{
    private int[] parent;
    private Queue<Integer> queue;
    private int numberOfVertices;
    private boolean[] visited;
 
    public FordFulkerson(int numberOfVertices)
    {
        this.numberOfVertices = numberOfVertices;
        this.queue = new LinkedList<Integer>();
        parent = new int[numberOfVertices + 1];
        visited = new boolean[numberOfVertices + 1];		
    }
 
    public boolean bfs(int source, int goal, int graph[][])
    {
        boolean pathFound = false;
        int destination, element;
 
        for(int vertex = 1; vertex <= numberOfVertices; vertex++)
        {
            parent[vertex] = -1;
            visited[vertex] = false;
        }
 
        queue.add(source);
        parent = -1;
        visited = true;
 
        while (!queue.isEmpty())
        { 
            element = queue.remove();
            destination = 1;
 
            while (destination <= numberOfVertices)
            {
                if (graph[element][destination] > 0 &&  !visited[destination])
                {
                    parent[destination] = element;
                    queue.add(destination);
                    visited[destination] = true;
                }
                destination++;
            }
        }
        if(visited[goal])
        {
            pathFound = true;
        }
        return pathFound;
    }
 
    public int fordFulkerson(int graph[][], int source, int destination)
    {
        int u, v;
        int maxFlow = 0;
        int pathFlow;
 
        int[][] residualGraph = new int[numberOfVertices + 1][numberOfVertices + 1];
        for (int sourceVertex = 1; sourceVertex <= numberOfVertices; sourceVertex++)
        {
            for (int destinationVertex = 1; destinationVertex <= numberOfVertices; destinationVertex++)
            {
                residualGraph[sourceVertex][destinationVertex] = graph[sourceVertex][destinationVertex];
            }
        }
 
        while (bfs(source ,destination, residualGraph))
        {
            pathFlow = Integer.MAX_VALUE;
            for (v = destination; v != source; v = parent[v])
            {
                u = parent[v];
                pathFlow = Math.min(pathFlow, residualGraph[u][v]);
            }
            for (v = destination; v != source; v = parent[v])
            {
                u = parent[v];
                residualGraph[u][v] -= pathFlow;
                residualGraph[v][u] += pathFlow;
            }
            maxFlow += pathFlow;	
        }
 
        return maxFlow;
    }
 
    public static void main(String...arg)
    {
        int[][] graph;
        int numberOfNodes;
        int source;
        int sink;
        int maxFlow;
 
        Scanner scanner = new Scanner(System.in);
        System.out.println("Enter the number of nodes");
        numberOfNodes = scanner.nextInt();
        graph = new int[numberOfNodes + 1][numberOfNodes + 1];
 
        System.out.println("Enter the graph matrix");
        for (int sourceVertex = 1; sourceVertex <= numberOfNodes; sourceVertex++)
        {
           for (int destinationVertex = 1; destinationVertex <= numberOfNodes; destinationVertex++)
           {
               graph[sourceVertex][destinationVertex] = scanner.nextInt();
           }
        }
 
        System.out.println("Enter the source of the graph");
        source= scanner.nextInt();
 
        System.out.println("Enter the sink of the graph");
        sink = scanner.nextInt();
 
        FordFulkerson fordFulkerson = new FordFulkerson(numberOfNodes);
        maxFlow = fordFulkerson.fordFulkerson(graph, source, sink);
        System.out.println("The Max Flow is " + maxFlow);
        scanner.close();
    }
}
$javac FordFulkerson.java
$java FordFulkerson
 
Enter the number of nodes
6
Enter the graph matrix
0 16 13 0 0 0
0 0  10 12 0 0
0 4 0 0 14 0
0 0 9 0 0 20
0 0 0 7 0 4
0 0 0 0 0 0
Enter the source of the graph
1
Enter the sink of the graph
6
The Max Flow is 23

Related posts:

Java Program to Check if any Graph is Possible to be Constructed for a Given Degree Sequence
Java Program to Implement Counting Sort
Java Program to Implement Singly Linked List
Spring WebClient vs. RestTemplate
Giới thiệu luồng vào ra (I/O) trong Java
Java Program to Solve a Matching Problem for a Given Specific Case
Các kiểu dữ liệu trong java
Transaction Propagation and Isolation in Spring @Transactional
Feign – Tạo ứng dụng Java RESTful Client
Serverless Functions with Spring Cloud Function
Hướng dẫn sử dụng luồng vào ra nhị phân trong Java
Lớp Collections trong Java (Collections Utility Class)
Java 9 Stream API Improvements
Java Program to Implement Vector API
Map to String Conversion in Java
Spring Security Basic Authentication
Spring Cloud – Securing Services
Java Program to Implement Traveling Salesman Problem using Nearest neighbour Algorithm
The SpringJUnitConfig and SpringJUnitWebConfig Annotations in Spring 5
Java Program to Implement RoleUnresolvedList API
Using Spring @ResponseStatus to Set HTTP Status Code
Primitive Type Streams in Java 8
Java Program to Implement Dijkstra’s Algorithm using Set
Java Program to Perform Polygon Containment Test
How to Get All Dates Between Two Dates?
Generating Random Dates in Java
Using a List of Values in a JdbcTemplate IN Clause
Flattening Nested Collections in Java
Hướng dẫn Java Design Pattern – Service Locator
Java 14 Record Keyword
Java Program to Implement Stack API
Java Program to Create a Minimal Set of All Edges Whose Addition will Convert it to a Strongly Conne...