-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFlowNetWork.java
More file actions
55 lines (50 loc) · 1.47 KB
/
FlowNetWork.java
File metadata and controls
55 lines (50 loc) · 1.47 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
import java.util.Random;
public class FlowNetWork{
private int V;
private int E=0;
private Bag<FlowEdge>[] adj;
public FlowNetWork(int v){
this.V=v;
adj=(Bag<FlowEdge>[]) new Bag[v];
for (int i=0;i<v;i++){
adj[i]=new Bag<FlowEdge>();
}
}
public int V(){return V;}
public int E(){return E;}
public void addEdge(FlowEdge edge){
int v=edge.from();
adj[v].add(edge);
E++;
}
public Iterable<FlowEdge> adj(int v){
return adj[v];
}
public String toString(){
String s=V+" vertices"+" "+E+" edges\n";
for (int v=0;v<V;v++){
s+=v+":";
for (FlowEdge edge:this.adj(v)){
int w=edge.to();
s+=w+", flow:"+String.format("%.2f",edge.flow())+", "
+"capacity:"+String.format("%.2f",edge.capacity())+", ";
}
s+="\n";
}
return s;
}
public static void main(String[] args){
Random r = new Random();
int V=10;
FlowNetWork g=new FlowNetWork(V);
for (int i=0;i<2*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);
}
//for (int w:g.adj(1)) System.out.println(w+"");
System.out.println(g);
}
}