|
1 | 1 | package com.interview.graph; |
2 | 2 |
|
3 | | -import java.util.HashMap; |
4 | | -import java.util.LinkedList; |
5 | | -import java.util.Map; |
6 | | -import java.util.Queue; |
| 3 | +import java.util.*; |
7 | 4 |
|
8 | 5 | /** |
9 | | - http://www.geeksforgeeks.org/ford-fulkerson-algorithm-for-maximum-flow-problem/ |
| 6 | + * Date 04/14/2014 |
| 7 | + * @author Tushar Roy |
| 8 | + * |
| 9 | + * Ford fulkerson method edmonds karp algorithm for finding max flow |
| 10 | + * |
| 11 | + * Time complexity is O(VE^2) |
| 12 | + * |
| 13 | + * References: |
| 14 | + * http://www.geeksforgeeks.org/ford-fulkerson-algorithm-for-maximum-flow-problem/ |
| 15 | + * https://en.wikipedia.org/wiki/Edmonds%E2%80%93Karp_algorithm |
10 | 16 | */ |
11 | | -public class FordFulkerson<T> { |
| 17 | +public class FordFulkerson { |
12 | 18 |
|
13 | | - public int maxFlow(Graph<T> graph, Vertex<T> s, Vertex<T> t){ |
14 | | - |
15 | | - int[][] matrix = new int[graph.getAllVertex().size()][graph.getAllVertex().size()]; |
16 | | - for(Edge<T> edge : graph.getAllEdges()){ |
17 | | - matrix[(int)edge.getVertex1().getId()][(int)edge.getVertex2().getId()] = edge.getWeight(); |
18 | | - } |
19 | | - |
20 | | - |
21 | | - Map<Integer,Integer> parent = new HashMap<Integer,Integer>(); |
22 | | - |
23 | | - int max_flow = 0; |
24 | | - while(BFS(matrix,parent,(int)s.getId(),(int)t.getId())){ |
25 | | - |
26 | | - int min_flow = 10000; |
27 | | - int v = (int)t.getId(); |
28 | | - while(parent.containsKey(v)){ |
29 | | - int u = parent.get(v); |
30 | | - if(min_flow > matrix[u][v]){ |
31 | | - min_flow = matrix[u][v]; |
32 | | - } |
33 | | - v = u; |
| 19 | + public int maxFlow(int capacity[][], int s, int t){ |
| 20 | + int flow[][] = new int[capacity.length][capacity[0].length]; |
| 21 | + Map<Integer,Integer> parent = new HashMap<>(); |
| 22 | + int maxFlow = 0; |
| 23 | + while(true){ |
| 24 | + int minCapacity = BFS(capacity, flow, parent, s, t); |
| 25 | + if(minCapacity == 0) { |
| 26 | + break; |
34 | 27 | } |
35 | | - |
36 | | - max_flow += min_flow; |
37 | | - |
38 | | - v = (int)t.getId(); |
39 | | - while(parent.containsKey(v)){ |
| 28 | + maxFlow += minCapacity; |
| 29 | + |
| 30 | + int v = t; |
| 31 | + while(v != s){ |
40 | 32 | int u = parent.get(v); |
41 | | - matrix[u][v] -= min_flow; |
42 | | - matrix[v][u] += min_flow; |
| 33 | + flow[u][v] += minCapacity; |
| 34 | + flow[v][u] -= minCapacity; |
43 | 35 | v = u; |
44 | 36 | } |
45 | 37 | } |
46 | | - return max_flow; |
| 38 | + return maxFlow; |
47 | 39 | } |
48 | 40 |
|
49 | | - private boolean BFS(int[][] matrix, Map<Integer,Integer> parent, |
| 41 | + private int BFS(int[][] capacity, int[][] flow, Map<Integer,Integer> parent, |
50 | 42 | int s, int t){ |
51 | | - |
52 | | - Map<Integer,Boolean> visited = new HashMap<Integer,Boolean>(); |
53 | | - Queue<Integer> queue = new LinkedList<Integer>(); |
| 43 | + |
| 44 | + Set<Integer> visited = new HashSet<>(); |
| 45 | + Queue<Integer> queue = new LinkedList<>(); |
54 | 46 | queue.add(s); |
55 | | - visited.put(s, true); |
56 | | - while(queue.size() != 0){ |
| 47 | + visited.add(s); |
| 48 | + int minCapacity = Integer.MAX_VALUE; |
| 49 | + while(!queue.isEmpty()){ |
57 | 50 | int u = queue.poll(); |
58 | | - for(int v =0 ; v < matrix.length; v++){ |
59 | | - if(!visited.containsKey(v) && matrix[u][v] > 0){ |
| 51 | + for(int v = 0; v < flow.length; v++){ |
| 52 | + if(!visited.contains(v) && capacity[u][v] - flow[u][v] > 0){ |
60 | 53 | parent.put(v, u); |
| 54 | + if (minCapacity > capacity[u][v] - flow[u][v]) { |
| 55 | + minCapacity = capacity[u][v] - flow[u][v]; |
| 56 | + } |
61 | 57 | if(v == t){ |
62 | | - return true; |
| 58 | + return minCapacity; |
63 | 59 | } |
64 | | - visited.put(v, true); |
| 60 | + visited.add(v); |
65 | 61 | queue.add(v); |
66 | 62 | } |
67 | 63 | } |
68 | 64 | } |
69 | | - return false; |
| 65 | + return 0; |
70 | 66 | } |
71 | 67 |
|
72 | 68 | public static void main(String args[]){ |
73 | | - |
74 | | - Graph<Integer> graph = new Graph<Integer>(true); |
75 | | - |
76 | | - Vertex<Integer> s = graph.addSingleVertex(0); |
77 | | - Vertex<Integer> t = graph.addSingleVertex(7); |
78 | | - graph.addEdge(0, 1,10); |
79 | | - graph.addEdge(1, 2,9); |
80 | | - graph.addEdge(2, 7,10); |
81 | | - graph.addEdge(0, 3,5); |
82 | | - graph.addEdge(3, 4,8); |
83 | | - graph.addEdge(4, 7,10); |
84 | | - graph.addEdge(0, 5,15); |
85 | | - graph.addEdge(5, 6,16); |
86 | | - graph.addEdge(6, 7,10); |
87 | | - graph.addEdge(1, 3,4); |
88 | | - graph.addEdge(3, 5,4); |
89 | | - graph.addEdge(1, 4,15); |
90 | | - graph.addEdge(2, 4,15); |
91 | | - graph.addEdge(6, 3,6); |
92 | | - graph.addEdge(4, 6,15); |
93 | | - |
94 | | - |
95 | | - FordFulkerson<Integer> ff = new FordFulkerson<Integer>(); |
96 | | - int result = ff.maxFlow(graph, s, t); |
97 | | - System.out.print(result); |
| 69 | + FordFulkerson ff = new FordFulkerson(); |
| 70 | + int[][] capacity = {{0, 3, 0, 3, 0, 0, 0}, |
| 71 | + {0, 0, 4, 0, 0, 0, 0}, |
| 72 | + {3, 0, 0, 1, 2, 0, 0}, |
| 73 | + {0, 0, 0, 0, 2, 6, 0}, |
| 74 | + {0, 1, 0, 0, 0, 0, 1}, |
| 75 | + {0, 0, 0, 0, 0, 0, 9}, |
| 76 | + {0, 0, 0, 0, 0, 0, 0}}; |
| 77 | + |
| 78 | + int [][] capacity1 = {{0, 16, 13, 0, 0, 0}, |
| 79 | + {0, 0, 10, 12, 0, 0}, |
| 80 | + {0, 4, 0, 0, 14, 0}, |
| 81 | + {0, 0, 9, 0, 0, 20}, |
| 82 | + {0, 0, 0, 7, 0, 4}, |
| 83 | + {0, 0, 0, 0, 0, 0}}; |
| 84 | + System.out.print("Maximum capacity " + ff.maxFlow(capacity1, 0, 5)); |
98 | 85 | } |
99 | 86 | } |
0 commit comments