1
+ package com .thealgorithms .others ;
2
+ import java .util .*;
3
+
4
+ public class FordFulkerson {
5
+
6
+ // Method to perform Breadth-First Search to find an augmenting path
7
+ boolean bfs (int rGraph [][], int source , int sink , int parent []) {
8
+ // Create a visited array and mark all vertices as not visited
9
+ boolean visited [] = new boolean [rGraph .length ];
10
+ Arrays .fill (visited , false );
11
+
12
+ // Create a queue, enqueue source vertex and mark it as visited
13
+ LinkedList <Integer > queue = new LinkedList <Integer >();
14
+ queue .add (source );
15
+ visited [source ] = true ;
16
+ parent [source ] = -1 ;
17
+
18
+ // Standard BFS loop
19
+ while (!queue .isEmpty ()) {
20
+ int u = queue .poll ();
21
+
22
+ for (int v = 0 ; v < rGraph .length ; v ++) {
23
+ if (!visited [v ] && rGraph [u ][v ] > 0 ) { // if not yet visited and there is an edge
24
+ if (v == sink ) { // if we found the sink
25
+ parent [v ] = u ;
26
+ return true ;
27
+ }
28
+ queue .add (v );
29
+ parent [v ] = u ;
30
+ visited [v ] = true ;
31
+ }
32
+ }
33
+ }
34
+
35
+ return false ; // No augmenting path found
36
+ }
37
+
38
+ // Returns the maximum flow from source to sink in the given graph
39
+ int fordFulkerson (int graph [][], int source , int sink ) {
40
+ int u , v ;
41
+
42
+ // Create a residual graph and fill the residual graph with given capacities
43
+ int rGraph [][] = new int [graph .length ][graph .length ];
44
+
45
+ for (u = 0 ; u < graph .length ; u ++) {
46
+ for (v = 0 ; v < graph .length ; v ++) {
47
+ rGraph [u ][v ] = graph [u ][v ];
48
+ }
49
+ }
50
+
51
+ // This array is filled by BFS and to store the path
52
+ int parent [] = new int [graph .length ];
53
+
54
+ int maxFlow = 0 ; // Initially, no flow
55
+
56
+ // Augment the flow while there is a path from source to sink
57
+ while (bfs (rGraph , source , sink , parent )) {
58
+ // Find the maximum flow through the path found by BFS
59
+ int pathFlow = Integer .MAX_VALUE ;
60
+ for (v = sink ; v != source ; v = parent [v ]) {
61
+ u = parent [v ];
62
+ pathFlow = Math .min (pathFlow , rGraph [u ][v ]);
63
+ }
64
+
65
+ // Update residual capacities of the edges and reverse edges along the path
66
+ for (v = sink ; v != source ; v = parent [v ]) {
67
+ u = parent [v ];
68
+ rGraph [u ][v ] -= pathFlow ;
69
+ rGraph [v ][u ] += pathFlow ;
70
+ }
71
+
72
+ // Add the path flow to the overall flow
73
+ maxFlow += pathFlow ;
74
+ }
75
+
76
+ return maxFlow ;
77
+ }
78
+
79
+ public static void main (String [] args ) {
80
+ Scanner scanner = new Scanner (System .in );
81
+
82
+ System .out .println ("Enter the number of vertices:" );
83
+ int V = scanner .nextInt ();
84
+
85
+ int graph [][] = new int [V ][V ];
86
+
87
+ System .out .println ("Enter the number of edges:" );
88
+ int E = scanner .nextInt ();
89
+
90
+ System .out .println ("Enter the edges with capacities (u v capacity):" );
91
+ for (int i = 0 ; i < E ; i ++) {
92
+ int u = scanner .nextInt ();
93
+ int v = scanner .nextInt ();
94
+ int capacity = scanner .nextInt ();
95
+ graph [u ][v ] = capacity ;
96
+ }
97
+
98
+ System .out .println ("Enter the source vertex:" );
99
+ int source = scanner .nextInt ();
100
+
101
+ System .out .println ("Enter the sink vertex:" );
102
+ int sink = scanner .nextInt ();
103
+
104
+ FordFulkerson maxFlow = new FordFulkerson ();
105
+ System .out .println ("The maximum possible flow is: " + maxFlow .fordFulkerson (graph , source , sink ));
106
+
107
+ scanner .close ();
108
+ }
109
+ }
0 commit comments