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

1. `import java.util.LinkedList;`
2. `import java.util.Queue;`
3. `import java.util.Scanner;`
4. ` `
5. `public class FordFulkerson`
6. `{`
7. `    private int[] parent;`
8. `    private Queue<Integer> queue;`
9. `    private int numberOfVertices;`
10. `    private boolean[] visited;`
11. ` `
12. `    public FordFulkerson(int numberOfVertices)`
13. `    {`
14. `        this.numberOfVertices = numberOfVertices;`
15. `        this.queue = new LinkedList<Integer>();`
16. `        parent = new int[numberOfVertices + 1];`
17. `        visited = new boolean[numberOfVertices + 1];		`
18. `    }`
19. ` `
20. `    public boolean bfs(int source, int goal, int graph[][])`
21. `    {`
22. `        boolean pathFound = false;`
23. `        int destination, element;`
24. ` `
25. `        for(int vertex = 1; vertex <= numberOfVertices; vertex++)`
26. `        {`
27. `            parent[vertex] = -1;`
28. `            visited[vertex] = false;`
29. `        }`
30. ` `
31. `        queue.add(source);`
32. `        parent[source] = -1;`
33. `        visited[source] = true;`
34. ` `
35. `        while (!queue.isEmpty())`
36. `        { `
37. `            element = queue.remove();`
38. `            destination = 1;`
39. ` `
40. `            while (destination <= numberOfVertices)`
41. `            {`
42. `                if (graph[element][destination] > 0 &&  !visited[destination])`
43. `                {`
44. `                    parent[destination] = element;`
45. `                    queue.add(destination);`
46. `                    visited[destination] = true;`
47. `                }`
48. `                destination++;`
49. `            }`
50. `        }`
51. `        if(visited[goal])`
52. `        {`
53. `            pathFound = true;`
54. `        }`
55. `        return pathFound;`
56. `    }`
57. ` `
58. `    public int fordFulkerson(int graph[][], int source, int destination)`
59. `    {`
60. `        int u, v;`
61. `        int maxFlow = 0;`
62. `        int pathFlow;`
63. ` `
64. `        int[][] residualGraph = new int[numberOfVertices + 1][numberOfVertices + 1];`
65. `        for (int sourceVertex = 1; sourceVertex <= numberOfVertices; sourceVertex++)`
66. `        {`
67. `            for (int destinationVertex = 1; destinationVertex <= numberOfVertices; destinationVertex++)`
68. `            {`
69. `                residualGraph[sourceVertex][destinationVertex] = graph[sourceVertex][destinationVertex];`
70. `            }`
71. `        }`
72. ` `
73. `        while (bfs(source ,destination, residualGraph))`
74. `        {`
75. `            pathFlow = Integer.MAX_VALUE;`
76. `            for (v = destination; v != source; v = parent[v])`
77. `            {`
78. `                u = parent[v];`
79. `                pathFlow = Math.min(pathFlow, residualGraph[u][v]);`
80. `            }`
81. `            for (v = destination; v != source; v = parent[v])`
82. `            {`
83. `                u = parent[v];`
84. `                residualGraph[u][v] -= pathFlow;`
85. `                residualGraph[v][u] += pathFlow;`
86. `            }`
87. `            maxFlow += pathFlow;	`
88. `        }`
89. ` `
90. `        return maxFlow;`
91. `    }`
92. ` `
93. `    public static void main(String...arg)`
94. `    {`
95. `        int[][] graph;`
96. `        int numberOfNodes;`
97. `        int source;`
98. `        int sink;`
99. `        int maxFlow;`
100. ` `
101. `        Scanner scanner = new Scanner(System.in);`
102. `        System.out.println("Enter the number of nodes");`
103. `        numberOfNodes = scanner.nextInt();`
104. `        graph = new int[numberOfNodes + 1][numberOfNodes + 1];`
105. ` `
106. `        System.out.println("Enter the graph matrix");`
107. `        for (int sourceVertex = 1; sourceVertex <= numberOfNodes; sourceVertex++)`
108. `        {`
109. `           for (int destinationVertex = 1; destinationVertex <= numberOfNodes; destinationVertex++)`
110. `           {`
111. `               graph[sourceVertex][destinationVertex] = scanner.nextInt();`
112. `           }`
113. `        }`
114. ` `
115. `        System.out.println("Enter the source of the graph");`
116. `        source= scanner.nextInt();`
117. ` `
118. `        System.out.println("Enter the sink of the graph");`
119. `        sink = scanner.nextInt();`
120. ` `
121. `        FordFulkerson fordFulkerson = new FordFulkerson(numberOfNodes);`
122. `        maxFlow = fordFulkerson.fordFulkerson(graph, source, sink);`
123. `        System.out.println("The Max Flow is " + maxFlow);`
124. `        scanner.close();`
125. `    }`
126. `}`

```\$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```

Sanfoundry Global Education & Learning Series – 1000 Java Programs. 