-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSymbolEWDigraph.java
More file actions
56 lines (48 loc) · 1.67 KB
/
Copy pathSymbolEWDigraph.java
File metadata and controls
56 lines (48 loc) · 1.67 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
public class SymbolEWDigraph {
private ST<String, Integer> st; // string -> index
private String[] keys; // index -> string
private EdgeWeightedDigraph G;
public SymbolEWDigraph(String filename, String delimiter) {
st = new ST<String, Integer>();
// First pass builds the index by reading strings to associate
// distinct strings with an index
In in = new In(filename);
while (in.hasNextLine()) {
String[] a = in.readLine().split(delimiter);
for (int i = 0; i < a.length; i++) {
if (!st.contains(a[i]))
st.put(a[i], st.size());
}
}
// inverted index to get string keys in an array
keys = new String[st.size()];
for (String name : st.keys()) {
keys[st.get(name)] = name;
}
}
public void createDigraph(String filename, String delimiter, GeoInfo locations) {
G = new EdgeWeightedDigraph(st.size());
In in = new In(filename);
while (in.hasNextLine()) {
String[] a = in.readLine().split(delimiter);
int v = st.get(a[0]);
for (int i = 1; i < a.length; i++) {
int w = st.get(a[i]);
double weight = (locations.get(a[0])).distance(locations.get(a[i]));
G.addEdge(new DirectedEdge(v, w, weight));
}
}
}
public boolean contains(String s) {
return st.contains(s);
}
public int index(String s) {
return st.get(s);
}
public String name(int v) {
return keys[v];
}
public EdgeWeightedDigraph G() {
return G;
}
}