-
Notifications
You must be signed in to change notification settings - Fork 0
/
FordFulkerson.java
199 lines (143 loc) · 4.29 KB
/
FordFulkerson.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
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.io.*;
import java.util.*;
public class FordFulkerson {
public static ArrayList<Integer> pathDFS(Integer source, Integer destination, WGraph graph){
ArrayList<Integer> Stack = new ArrayList<Integer>();
ArrayList<Integer> returnStack = new ArrayList<Integer>();
boolean[] visited = new boolean[graph.getNbNodes()];
ArrayList<ArrayList<Integer>> p = new ArrayList<ArrayList<Integer>>();
for(int i=0; i<graph.getNbNodes(); i++) {
p.add(new ArrayList<Integer>());
}
visited[source] = true;
returnStack.add(source);
while(!returnStack.isEmpty()) {
int current = returnStack.remove(Stack.size());
if (current == destination) {
break;
}
ArrayList<Integer> children = new ArrayList<Integer>();
for(Edge e: graph.getEdges()) {
if(e.nodes[0] == current) {
children.add(e.nodes[1]);
}
}
for(int c : children) {
p.get(c).add(current);
if(!visited[c]) {
visited[c] = true;
returnStack.add(c);
}
}
}
if(visited[destination]) {
int node = destination;
Stack.add(node);
while(node != source) {
Stack.add(0,p.get(node).get(0));
node = p.get(node).get(0);
}
return Stack;
}
return Stack;
}
public static void fordfulkerson(Integer source, Integer destination, WGraph graph, String filePath){
String answer="";
String myMcGillID = "0000000000"; //Please initialize this variable with your McGill ID
int maxFlow = 0;
float bottleneck1 = Float.MAX_VALUE;
int bottleneck = (int) bottleneck1;
WGraph residualGraph = new WGraph(graph);
WGraph flow = new WGraph(graph);
for(Edge e : flow.getEdges()) {
e.weight = 0;
}
ArrayList<Integer> path = pathDFS(source, destination, residualGraph);
if (source == destination) {
System.out.println(-1);
}
else {
while(!path.isEmpty()) {
for (int i=0; i<path.size()-1; i++) {
if (residualGraph.getEdge(path.get(i), path.get(i+1)).weight < bottleneck) {
bottleneck = residualGraph.getEdge(path.get(i), path.get(i+1)).weight;
}
}
for(int i = 0; i<path.size()-1; i++) {
if(flow.getEdge(path.get(i), path.get(i+1)) != null) {
flow.getEdge(path.get(i), path.get(i+1)).weight+=bottleneck;
}
else if(flow.getEdge(path.get(i+1), path.get(i)) !=null) {
flow.getEdge(path.get(i+1), path.get(i)).weight-=bottleneck;
}
}
residualGraph = new WGraph();
ArrayList<Edge> front = new ArrayList<Edge>();
ArrayList<Edge> back = new ArrayList<Edge>();
for(Edge e : graph.getEdges()) {
int capacity = e.weight;
int w = flow.getEdge(e.nodes[0], e.nodes[1]).weight;
if (w > 0) {
back.add(new Edge(e.nodes[1], e.nodes[0], capacity));
}
if(w < capacity) {
front.add(new Edge(e.nodes[0], e.nodes[1], capacity-w));
}
}
for(Edge e : back) {
if(e.weight != 0) {
residualGraph.addEdge(e);
}
}
for(Edge e : front) {
if(e.weight == 0) {
residualGraph.getEdge(e.nodes[0], e.nodes[1]).weight += e.weight;
}
else {
if (residualGraph.getEdge(e.nodes[0], e.nodes[1]) == null) {
residualGraph.addEdge(e);
}
}
}
path = pathDFS(source, destination, residualGraph);
}
for( Edge edge : flow.getEdges()) {
if(edge.nodes[0] == source) {
maxFlow = maxFlow + edge.weight;
}
}
}
graph = flow;
answer += maxFlow + "\n" + graph.toString();
writeAnswer(filePath+myMcGillID+".txt",answer);
System.out.println(answer);
}
public static void writeAnswer(String path, String line){
BufferedReader br = null;
File file = new File(path);
// if file doesnt exists, then create it
try {
if (!file.exists()) {
file.createNewFile();
}
FileWriter fw = new FileWriter(file.getAbsoluteFile(),true);
BufferedWriter bw = new BufferedWriter(fw);
bw.write(line+"\n");
bw.close();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (br != null)br.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
public static void main(String[] args){
String file = args[0];
File f = new File(file);
WGraph g = new WGraph(file);
fordfulkerson(g.getSource(),g.getDestination(),g,f.getAbsolutePath().replace(".txt",""));
}
}