forked from matthewsamuel95/ACM-ICPC-Algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
EdmondsKarp.java
94 lines (81 loc) · 2.61 KB
/
EdmondsKarp.java
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
/**
* @author dbatchunag
*/
import java.util.*;
public class EdmondsKarp {
private int maxFlowEdmondsKarp(final int [][]edges, final int[][] capacity, final int source, final int target) {
final int n = capacity.length;
int maxFlow = 0;
final int[][] resGraph = new int [n][n];
while (true) {
//backtrack for augmenting path
final int[] parent = new int[n];
Arrays.fill(parent, -1);
parent[source] = -2;
//parent is updated during the bfs
final int pathFlow = getAugmentingPath(edges, capacity, source, target, resGraph, parent);
if (pathFlow == 0) {
//No augmenting path is found
break;
}
maxFlow += pathFlow;
int v = target;
while (v != source) {
//traverse along the backtrack
final int u = parent[v];
resGraph[u][v] = resGraph[u][v] + pathFlow;
resGraph[v][u] = resGraph[v][u] - pathFlow;
v = u;
}
}
return maxFlow;
}
//Returns
private int getAugmentingPath(final int[][] edges,
final int[][] capacity,
final int source,
final int target,
final int[][] resGraph,
final int[] parent) {
final int n = capacity.length;
final int[] maxFlow = new int[n];
maxFlow[source] = Integer.MAX_VALUE;
final Deque<Integer> bfsQueue = new ArrayDeque<Integer>();
bfsQueue.add(source);
while (bfsQueue.size()> 0) {
final int u = bfsQueue.pop();
for (final int v : edges[u]) {
if (capacity[u][v] - resGraph[u][v] > 0 && parent[v] == -1) {
//we may use edge u->v (on source ~> .. ~> target)
parent[v] = u;
//Maximum possible flow size from source to v is computed below.
maxFlow[v] = Math.min(maxFlow[u], capacity[u][v] - resGraph[u][v]);
if (v != target) {
bfsQueue.add(v);
} else {
//Path found
return maxFlow[target];
}
}
}
}
return 0;
}
private void run() {
// Adjacency Matrix
int [][] edges = new int[][]{{1, 2}, {2, 3}, {3}, {}};
// Capacity Matrix
int [][] capacity = new int[][]{
{0, 1000000, 1000000, 0},
{0, 0, 1, 1000000},
{0, 0, 0, 1000000},
{0, 0, 0, 0}
};
final int source = 0;
final int target = 3;
System.out.println(String.format("The maximum possible flow is %d", maxFlowEdmondsKarp(edges, capacity, source, target)));
}
public static void main(String[] args) {
new EdmondsKarp().run();
}
}