88 *
99 * Ford fulkerson method edmonds karp algorithm for finding max flow
1010 *
11+ * Capacity - Capacity of an edge to carry units from source to destination vertex
12+ * Flow - Actual flow of units from source to destination vertex of an edge
13+ * Residual capacity - Remaining capacity on this edge i.e capacity - flow
14+ * AugmentedPath - Path from source to sink which has residual capacity greater than 0
15+ *
1116 * Time complexity is O(VE^2)
1217 *
1318 * References:
1621 */
1722public class FordFulkerson {
1823
19- public int maxFlow (int capacity [][], int s , int t ){
20- int flow [][] = new int [capacity .length ][capacity [0 ].length ];
24+ public int maxFlow (int capacity [][], int source , int sink ){
25+
26+ //declare and initialize residual capacity as total avaiable capacity initially.
27+ int residualCapacity [][] = new int [capacity .length ][capacity [0 ].length ];
28+ for (int i = 0 ; i < capacity .length ; i ++) {
29+ for (int j = 0 ; j < capacity [0 ].length ; j ++) {
30+ residualCapacity [i ][j ] = capacity [i ][j ];
31+ }
32+ }
33+
34+ //this is parent map for storing BFS parent
2135 Map <Integer ,Integer > parent = new HashMap <>();
36+
37+ //stores all the augmented paths
38+ List <List <Integer >> augmentedPaths = new ArrayList <>();
39+
40+ //max flow we can get in this network
2241 int maxFlow = 0 ;
2342 while (true ){
24- int minCapacity = BFS (capacity , flow , parent , s , t );
25- if (minCapacity == 0 ) {
43+ //see if we can find augmented path using BFS
44+ boolean foundAugmentedPath = BFS (residualCapacity , parent , source , sink );
45+
46+ //if no augmented path is found then break out of loop
47+ if (!foundAugmentedPath ) {
2648 break ;
2749 }
50+
51+ List <Integer > augmentedPath = new ArrayList <>();
52+ int v = sink ;
53+ int minCapacity = Integer .MAX_VALUE ;
54+ //find minimum residual capacity in augmented path
55+ //also add vertices to augmented path list
56+ while (v != source ){
57+ augmentedPath .add (v );
58+ int u = parent .get (v );
59+ if (minCapacity > residualCapacity [u ][v ]) {
60+ minCapacity = residualCapacity [u ][v ];
61+ }
62+ v = u ;
63+ }
64+ augmentedPath .add (source );
65+ Collections .reverse (augmentedPath );
66+ augmentedPaths .add (augmentedPath );
67+
68+ //add min capacity to max flow
2869 maxFlow += minCapacity ;
2970
30- int v = t ;
31- while (v != s ){
71+ //decrease residual capacity by min capacity from u to v in augmented path
72+ // and increase residual capacity by min capacity from v to u
73+ v = sink ;
74+ while (v != source ){
3275 int u = parent .get (v );
33- flow [u ][v ] + = minCapacity ;
34- flow [v ][u ] - = minCapacity ;
76+ residualCapacity [u ][v ] - = minCapacity ;
77+ residualCapacity [v ][u ] + = minCapacity ;
3578 v = u ;
3679 }
3780 }
81+ printAugmentedPaths (augmentedPaths );
3882 return maxFlow ;
3983 }
40-
41- private int BFS (int [][] capacity , int [][] flow , Map <Integer ,Integer > parent ,
42- int s , int t ){
4384
85+ /**
86+ * Prints all the augmented path which contribute to max flow
87+ */
88+ private void printAugmentedPaths (List <List <Integer >> augmentedPaths ) {
89+ augmentedPaths .forEach (path -> {
90+ path .forEach (i -> System .out .print (i + " " ));
91+ System .out .println ();
92+ });
93+ }
94+
95+ private boolean BFS (int [][] residualCapacity , Map <Integer ,Integer > parent ,
96+ int source , int sink ){
4497 Set <Integer > visited = new HashSet <>();
4598 Queue <Integer > queue = new LinkedList <>();
46- Map < Integer , Integer > minCapacity = new HashMap <>( );
47- queue .add (s );
48- visited . add ( s ) ;
49- minCapacity . put ( s , Integer . MAX_VALUE );
99+ queue . add ( source );
100+ visited .add (source );
101+ boolean foundAugmentedPath = false ;
102+ //see if we can find augmented path from source to sink
50103 while (!queue .isEmpty ()){
51104 int u = queue .poll ();
52- for (int v = 0 ; v < flow .length ; v ++){
53- int residualCapacity = capacity [u ][v ] - flow [u ][v ];
54- if (!visited .contains (v ) && residualCapacity > 0 ){
105+ for (int v = 0 ; v < residualCapacity .length ; v ++){
106+ //explore the vertex only if it is not visited and its residual capacity is
107+ //greater than 0
108+ if (!visited .contains (v ) && residualCapacity [u ][v ] > 0 ){
109+ //add in parent map saying v got explored by u
55110 parent .put (v , u );
56- if (residualCapacity < minCapacity .get (u )) {
57- minCapacity .put (v , residualCapacity );
58- } else {
59- minCapacity .put (v , minCapacity .get (u ));
60- }
61- if (v == t ){
62- return minCapacity .get (t );
63- }
111+ //add v to visited
64112 visited .add (v );
113+ //add v to queue for BFS
65114 queue .add (v );
115+ //if sink is found then augmented path is found
116+ if ( v == sink ) {
117+ foundAugmentedPath = true ;
118+ break ;
119+ }
66120 }
67121 }
68122 }
69- return 0 ;
123+ //returns if augmented path is found from source to sink or not
124+ return foundAugmentedPath ;
70125 }
71126
72127 public static void main (String args []){
@@ -79,12 +134,6 @@ public static void main(String args[]){
79134 {0 , 0 , 0 , 0 , 0 , 0 , 9 },
80135 {0 , 0 , 0 , 0 , 0 , 0 , 0 }};
81136
82- int [][] capacity1 = {{0 , 16 , 13 , 0 , 0 , 0 },
83- {0 , 0 , 10 , 12 , 0 , 0 },
84- {0 , 4 , 0 , 0 , 14 , 0 },
85- {0 , 0 , 9 , 0 , 0 , 20 },
86- {0 , 0 , 0 , 7 , 0 , 4 },
87- {0 , 0 , 0 , 0 , 0 , 0 }};
88- System .out .print ("Maximum capacity " + ff .maxFlow (capacity1 , 0 , 5 ));
137+ System .out .println ("Maximum capacity " + ff .maxFlow (capacity , 0 , 6 ));
89138 }
90139}
0 commit comments