forked from thuva4/Algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEdmondsKarp.java
More file actions
199 lines (157 loc) · 6.14 KB
/
EdmondsKarp.java
File metadata and controls
199 lines (157 loc) · 6.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
import java.util.*;
public class EdmondsKarp {
public static void main(String[] args) {
int verticesCount = 6;
double[][] capacity = initCapacity(verticesCount);
int s = 0;
int t = verticesCount - 1;
Map<Integer, List<Edge>> graphForwardEdges = initForwardGraphEdges();
Map<Integer, List<Edge>> graphReverseEdges = initReverseGraphEdges();
double maxFlow = calculateMaxFlow(graphForwardEdges, graphReverseEdges, s, t, capacity);
System.out.println(maxFlow);
}
private static double calculateMaxFlow(Map<Integer, List<Edge>> graphForwardEdges,
Map<Integer, List<Edge>> graphReverseEdges,
int s, int t, double[][] capacity) {
int verticesCount = graphForwardEdges.size();
double[][] flow = new double[verticesCount][verticesCount];
double maxFlow = 0.0;
boolean isPathExist = true;
Deque<Integer> queue = new ArrayDeque<>();
while (isPathExist) {
Edge[] parent = new Edge[verticesCount];
boolean[] visited = new boolean[verticesCount];
queue.addLast(s);
// choose path from s to t
while (!queue.isEmpty()) {
int currentVertex = queue.pollFirst();
visited[currentVertex] = true;
List<Edge> outEdges = graphForwardEdges.get(currentVertex);
for (Edge edge : outEdges) {
int to = edge.getTo();
if (!visited[to]) {
if (capacity[currentVertex][to] - flow[currentVertex][to] > 0 &&
flow[currentVertex][to] >= 0) {
parent[to] = edge;
queue.addLast(to);
}
}
}
List<Edge> inEdges = graphReverseEdges.get(currentVertex);
for (Edge edge : inEdges) {
int from = edge.getFrom();
if (!visited[from]) {
if (flow[from][currentVertex] > 0) {
parent[from] = edge;
queue.addLast(from);
}
}
}
}
isPathExist = visited[t];
// find max possible flow of the chosen path
if (isPathExist) {
int child = t;
double bottleneck = Double.MAX_VALUE;
while (child != s) {
Edge edge = parent[child];
if (!edge.isReverse()) {
bottleneck = Math.min(bottleneck, capacity[edge.getFrom()][edge.getTo()] -
flow[edge.getFrom()][edge.getTo()]);
} else {
bottleneck = Math.min(bottleneck, flow[edge.getFrom()][edge.getTo()]);
}
child = edge.isReverse() ? edge.getTo() : edge.getFrom();
}
// update flow
maxFlow += bottleneck;
child = t;
while (child != s) {
Edge edge = parent[child];
int from = (!edge.isReverse()) ? edge.getFrom() : edge.getTo();
flow[from][child] += bottleneck;
flow[child][from] -= bottleneck;
child = (!edge.isReverse()) ? edge.getFrom() : edge.getTo();
}
}
}
return maxFlow;
}
private static double[][] initCapacity(int verticesCount) {
double[][] capacity = new double[verticesCount][verticesCount];
capacity[0][1] = 10;
capacity[1][0] = 10;
capacity[0][3] = 10;
capacity[3][0] = 10;
capacity[1][2] = 4;
capacity[2][1] = 4;
capacity[1][3] = 2;
capacity[3][1] = 2;
capacity[1][4] = 8;
capacity[4][1] = 8;
capacity[2][5] = 10;
capacity[5][2] = 10;
capacity[3][4] = 9;
capacity[4][3] = 9;
capacity[4][2] = 6;
capacity[2][4] = 6;
capacity[4][5] = 10;
capacity[5][4] = 10;
return capacity;
}
private static Map<Integer, List<Edge>> initForwardGraphEdges() {
Map<Integer, List<Edge>> graph = new HashMap<>();
graph.put(0, createForwardEdges(0, 1, 3));
graph.put(1, createForwardEdges(1, 2, 3, 4));
graph.put(2, createForwardEdges(2, 5));
graph.put(3, createForwardEdges(3, 4));
graph.put(4, createForwardEdges(4, 2, 5));
graph.put(5, Collections.emptyList());
return graph;
}
private static Map<Integer, List<Edge>> initReverseGraphEdges() {
Map<Integer, List<Edge>> graph = new HashMap<>();
graph.put(0, Collections.emptyList());
graph.put(1, Collections.emptyList());
graph.put(2, createReverseEdges(2, 1, 4));
graph.put(3, createReverseEdges(3, 1));
graph.put(4, createReverseEdges(4, 1, 3));
graph.put(5, Collections.emptyList());
return graph;
}
private static List<Edge> createForwardEdges(int from, Integer... toVertices) {
List<Edge> edges = new ArrayList<>();
for (Integer to : toVertices) {
Edge edge = new Edge(from, to, false);
edges.add(edge);
}
return edges;
}
private static List<Edge> createReverseEdges(int to, Integer... fromVertices) {
List<Edge> edges = new ArrayList<>();
for (Integer from : fromVertices) {
Edge edge = new Edge(from, to, true);
edges.add(edge);
}
return edges;
}
private static class Edge {
private int from;
private int to;
private boolean isReverse;
Edge(int from, int to, boolean isReverse) {
this.from = from;
this.to = to;
this.isReverse = isReverse;
}
int getFrom() {
return from;
}
int getTo() {
return to;
}
boolean isReverse() {
return isReverse;
}
}
}