-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpruneData.java
More file actions
148 lines (105 loc) · 3.89 KB
/
Copy pathpruneData.java
File metadata and controls
148 lines (105 loc) · 3.89 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
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
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class pruneData {
public int[] stopIDArray;
public int[] stopIndicesArray; // Is it worth adding this? -> It does speed it up but at the expense of so much memory unused?
// Will use line number-1 as hash function for ease and memory efficiency in other algorithms?
public pruneData() throws FileNotFoundException {
stopIndicesArray = new int[12479];
stopIDArray = stopIDs();
}
// Store hash function inverse (mapping index back to value)
private int[] stopIDs() throws FileNotFoundException {
int maxStopID = 0;
int stops[] = new int[8757];
// Find Bus Stop from text file
File file = new File("input files/stops.txt");
Scanner scan = new Scanner(file);
String line;
int counter = 0;
int stopID;
line = scan.nextLine();
// scanning further lines
while (scan.hasNextLine()) {
line = scan.nextLine();
stopID = Integer.valueOf(line.substring(0, line.indexOf(",")));
stops[counter] = stopID;
stopIndicesArray[stopID] = counter;
if (stops[counter] > maxStopID) {
maxStopID = stops[counter];
}
counter++;
}
scan.close();
return stops;
}
// Adds Edges (Stop times)
private boolean addEdgesFromStopTimes(Graph myGraph) throws FileNotFoundException {
File file = new File("input files/stop_times.txt");
Scanner scan = new Scanner(file);
String[] line;
scan.nextLine();
int prevtripID = 0, prevStopID = 0;
int thisTripID, thisStopID;
// scanning further lines
while (scan.hasNextLine()) {
line = scan.nextLine().split(",");
thisTripID = Integer.valueOf(line[0]);
thisStopID = Integer.valueOf(line[3]);
if (prevtripID == thisTripID) {
// Add edge
myGraph.addEdge(stopIndicesArray[prevStopID], stopIndicesArray[thisStopID], thisTripID, 1.0);
}
prevtripID = thisTripID;
prevStopID = thisStopID;
}
scan.close();
return true;
}
private boolean addEdgesFromTransfers(Graph myGraph) throws FileNotFoundException {
File file = new File("input files/transfers.txt");
Scanner scan = new Scanner(file);
String[] line;
scan.nextLine();
String transferType;
int source;
int destination;
// scanning further lines
while (scan.hasNextLine()) {
line = scan.nextLine().split(",");
transferType = line[2].trim();
source = stopIndicesArray[Integer.valueOf(line[0])];
destination = stopIndicesArray[Integer.valueOf(line[1])];
// Add edge
if (transferType.compareTo("0") == 0) {
myGraph.addEdge(source, destination, 0, 2.0);
} else {
myGraph.addEdge(source, destination, 0, Double.valueOf(line[3])/100.0);
}
}
scan.close();
return true;
}
public Boolean fillInGraph(Graph graph) {
try {
this.addEdgesFromStopTimes(graph);
this.addEdgesFromTransfers(graph);
return false;
} catch (Exception e) {
return false;
}
}
// I can integrate this side by side into the above code for proficiency as the transfers go in the same order as stops
/*
public int findIndex(int stop) {
int index;
for (index = 0; index < stopIDArray.length; index++) {
if (stopIDArray[index] == stop) {
break;
}
}
return index;
}
*/
}