-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEdmondKarp.java
More file actions
189 lines (173 loc) · 4.05 KB
/
EdmondKarp.java
File metadata and controls
189 lines (173 loc) · 4.05 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
package hackpack;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.Scanner;
public class EdmondKarp {
public static void main(String[] args){
Scanner scan = new Scanner(System.in);
int cases = scan.nextInt();
int counter = 1;
while(cases-->0){
int nodes = scan.nextInt();
int[][] adj = new int[nodes][nodes];
System.out.println("Graph " + counter + ":");
int source = scan.nextInt();
int sink = scan.nextInt();
ArrayList<Integer>[] neighbors = new ArrayList[nodes];
for(int i = 0; i < nodes; i++){
neighbors[i] = new ArrayList<Integer>();
for(int j = 0; j < nodes; j++){
adj[i][j] = scan.nextInt();
System.out.print(adj[i][j] + " ");
if(adj[i][j] == 0){
adj[i][j] = Integer.MAX_VALUE;
}else{
neighbors[i].add(j);
}
}
System.out.println();
}
System.out.println();
System.out.println("Running EdmundsKarp:");
int flow = edmondsKarp(neighbors,adj,source,sink);
System.out.println("Flow is " + flow);
System.out.println();
counter++;
}
}
public static int edmondsKarp(ArrayList<Integer>[] neighbors, int[][] capacity, int source, int sink) {
int n = capacity.length;
int[][] flow = new int[n][n];
while (true) {
int[] parent = new int[n];
Arrays.fill(parent, -1);
parent[source] = source;
int[] max = new int[n]; // Capacity of path to node
max[source] = Integer.MAX_VALUE;
// BFS queue
LinkedList<Integer> queue = new LinkedList<Integer>();
queue.offer(source);
// Run BFS
bfs: while (!queue.isEmpty()) {
int u = queue.poll();
for (int v : neighbors[u]) {
// Check if there is available capacity, and if v has not been seen before
if (capacity[u][v] - flow[u][v] > 0 && parent[v] == -1) {
parent[v] = u;
max[v] = Math.min(max[u], capacity[u][v] - flow[u][v]);
if (v != sink)
queue.offer(v);
else {
// Backtrack search, and write flow
while (parent[v] != v) {
u = parent[v];
flow[u][v] += max[sink];
flow[v][u] -= max[sink];
v = u;
}
break bfs;
}
}
}
}
if (parent[sink] == -1) { // We did not find a path to t
int sum = 0;
for (int x : flow[source])
sum += x;
return sum;
}
}
}
}
/*
10
8
0 5
0 0 0 6 5 0 0 0
7 0 2 9 0 0 0 0
6 6 0 0 4 8 0 0
0 7 0 0 0 9 0 0
0 0 0 0 0 0 0 0
0 0 0 8 0 0 0 0
0 0 7 0 0 0 0 8
0 0 0 0 0 0 0 0
4
0 3
0 2 4 0
0 0 3 1
0 0 0 5
0 0 0 0
8
0 7
0 3 7 6 6 0 0 0
0 0 8 0 0 1 6 0
0 0 0 0 0 2 0 0
0 0 0 0 0 0 0 5
0 0 3 0 0 0 0 8
0 0 0 0 0 0 0 3
0 0 0 0 5 8 0 0
0 0 0 0 0 0 0 0
6
0 5
0 10 0 0 0 0
0 0 10 0 0 0
0 0 0 0 7 0
0 5 0 0 0 0
0 0 0 5 0 15
0 0 0 0 0 0
8
1 7
0 0 0 0 0 0 0 0
1 0 0 0 0 0 0 0
0 0 0 0 1 3 7 0
0 0 0 0 0 0 0 5
0 0 5 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 4 0 4 9 0 0
0 0 0 0 0 5 0 0
10
0 9
0 0 0 0 0 343 0 1435 464 0
0 0 0 0 0 879 954 811 0 524
0 0 0 0 1364 1054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