-
Notifications
You must be signed in to change notification settings - Fork 0
/
FlowNetwork.java
111 lines (97 loc) · 3.2 KB
/
FlowNetwork.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
package com.company;
public class FlowNetwork {
private static final String NEWLINE = System.getProperty("line.separator");
private final int V;
private int E;
private Bag<FlowEdge>[] adj;
private String cityNames[];
/**
* Initializes an empty flow network with <tt>V</tt> vertices and 0 edges.
* param V the number of vertices
* @throws java.lang.IllegalArgumentException if <tt>V</tt> < 0
*/
public FlowNetwork(int V) {
if (V < 0) throw new IllegalArgumentException("Number of vertices in a Graph must be nonnegative");
this.V = V;
this.E = 0;
adj = (Bag<FlowEdge>[]) new Bag[V];
for (int v = 0; v < V; v++)
adj[v] = new Bag<FlowEdge>();
cityNames = new String[V];
}
public void setCityNames(int i, String name){
cityNames[i] = name;
}
/**
* Returns the number of vertices in the edge-weighted graph.
* @return the number of vertices in the edge-weighted graph
*/
public int V() {
return V;
}
/**
* Returns the number of edges in the edge-weighted graph.
* @return the number of edges in the edge-weighted graph
*/
public int E() {
return E;
}
// throw an IndexOutOfBoundsException unless 0 <= v < V
private void validateVertex(int v) {
if (v < 0 || v >= V)
throw new IndexOutOfBoundsException("vertex " + v + " is not between 0 and " + (V-1));
}
/**
* Adds the edge <tt>e</tt> to the network.
* @param e the edge
* @throws java.lang.IndexOutOfBoundsException unless endpoints of edge are between 0 and V-1
*/
public void addEdge(FlowEdge e) {
int v = e.from();
int w = e.to();
validateVertex(v);
validateVertex(w);
adj[v].add(e);
adj[w].add(e);
E++;
}
/**
* Returns the edges incident on vertex <tt>v</tt> (includes both edges pointing to
* and from <tt>v</tt>).
* @param v the vertex
* @return the edges incident on vertex <tt>v</tt> as an Iterable
* @throws java.lang.IndexOutOfBoundsException unless 0 <= v < V
*/
public Iterable<FlowEdge> adj(int v) {
validateVertex(v);
return adj[v];
}
// return list of all edges - excludes self loops
public Iterable<FlowEdge> edges() {
Bag<FlowEdge> list = new Bag<FlowEdge>();
for (int v = 0; v < V; v++)
for (FlowEdge e : adj(v)) {
if (e.to() != v)
list.add(e);
}
return list;
}
/**
* Returns a string representation of the flow network.
* This method takes time proportional to <em>E</em> + <em>V</em>.
* @return the number of vertices <em>V</em>, followed by the number of edges <em>E</em>,
* followed by the <em>V</em> adjacency lists
*/
public String toString() {
StringBuilder s = new StringBuilder();
s.append(V + " " + E + NEWLINE);
for (int v = 0; v < V; v++) {
s.append(v + ": ");
for (FlowEdge e : adj[v]) {
if (e.to() != v) s.append(e + " ");
}
s.append(NEWLINE);
}
return s.toString();
}
}