-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFordFulkerson.java
More file actions
71 lines (63 loc) · 2.31 KB
/
FordFulkerson.java
File metadata and controls
71 lines (63 loc) · 2.31 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
import java.util.Random;
public class FordFulkerson{
private boolean marked[];
private FlowEdge edgeTo[];
private double value;
public FordFulkerson(FlowNetWork G,int s,int t){
while (hasAugmentingPath(G, s, t)){
double bottle=Double.POSITIVE_INFINITY;
for (int v=t;v!=s;v=edgeTo[v].other(v)){
bottle=Math.min(bottle, edgeTo[v].residualCapacityTo(v));
}
for (int v=t;v!=s;v=edgeTo[v].other(v)){
edgeTo[v].addResidualFlowTo(v, bottle);
//System.out.println(edgeTo[v]);
}
value+=bottle;
}
}
private boolean hasAugmentingPath(FlowNetWork G,int s,int t){
marked=new boolean[G.V()];
edgeTo=new FlowEdge[G.V()];
Queue<Integer> queue=new Queue<Integer>();
queue.enqueue(s);
marked[s]=true;
while (!queue.isEmpty()){
int current=queue.dequeue();
for (FlowEdge edge:G.adj(current)){
int point=edge.other(current);
while (!marked[point] && edge.residualCapacityTo(point)>0){
marked[point]=true;
edgeTo[point]=edge;
queue.enqueue(point);
}
}
}
return marked[t];
}
public double value(){return value;}
public boolean inCut(int v){return marked[v];}
public static void main(String[] args){
//Random r = new Random();
//int V=10;
FlowNetWork g=new FlowNetWork(6);
// for (int i=0;i<4*V;i++){
// int w=r.nextInt(V-1);
// int v=r.nextInt(V-1);
// double capacity=r.nextDouble();
// FlowEdge edge=new FlowEdge(w,v,capacity);
// g.addEdge(edge);
// }
int[] v={0,0,1,1,2,2,3,4};
int[] w={1,2,3,4,3,4,5,5};
double[] capacity={2.0,3.0,3.0,1.0,1.0,1.0,2.0,3.0};
for (int i=0;i<v.length;i++){
FlowEdge edge=new FlowEdge(v[i],w[i],capacity[i]);
g.addEdge(edge);
}
//for (int w:g.adj(1)) System.out.println(w+"");
System.out.println(g);
FordFulkerson ff=new FordFulkerson(g, 0, 5);
System.out.println(ff.value());
}
}