-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathParisMetro.java
executable file
·377 lines (272 loc) · 9.94 KB
/
ParisMetro.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
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
import java.io.BufferedReader;
import java.io.FileReader;
import java.util.StringTokenizer;
import net.datastructures.AdjacencyMapGraph;
//import net.datastructures.Dijkstra;
import net.datastructures.Edge;
import net.datastructures.Graph;
import net.datastructures.GraphAlgorithms;
import net.datastructures.Map;
import net.datastructures.Vertex;
import java.io.InputStreamReader;
import java.io.IOException;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.Set;
import java.util.HashSet;
import java.util.List;
public class ParisMetro {
private int numOfVertices;
private int numOfEdges;
private String[] stationsInOrder;
private Graph<Integer, Integer> sGraph;
private Hashtable<Integer, Vertex<Integer>> vertices;
private static final Integer WALKING_CONSTANT = new Integer(90);
/** Identify all the stations belonging to the
same line as station N1. */
public ParisMetro(Integer N1) throws Exception, IOException {
// System.out.println("Constructor 1\n");
FileReader file = new FileReader("metro.txt");
sGraph = new AdjacencyMapGraph<Integer, Integer>(true);
readMetro(file);
printAll(N1, -1, -1);
}
/** Find the shortest path between stations N1 & N2.
Print all stations of the path in order.
Print the total travel time. */
public ParisMetro(Integer N1, Integer N2) throws Exception, IOException {
// System.out.println("Constructor 2\n");
FileReader file = new FileReader("metro.txt");
sGraph = new AdjacencyMapGraph<Integer, Integer>(true);
readMetro(file);
printAll(N1, N2, -1);
}
/** Find the shortest path between stations N1 & N2
knowing that the line on which station N3 is situated is
not functioning. (Station N3 is an endpoint of its line.)
Print all stations of the path in order.
Print the total travel time. */
public ParisMetro(Integer N1, Integer N2, Integer N3) throws Exception, IOException {
// System.out.println("Constructor 3\n");
FileReader file = new FileReader("metro.txt");
sGraph = new AdjacencyMapGraph<Integer, Integer>(true);
readMetro(file);
printAll(N1, N2, N3);
}
public void readMetro(FileReader filename) {
try {
vertices = new Hashtable<Integer, Vertex<Integer>>();
BufferedReader metroFile = new BufferedReader(filename);
int lineNumber = 0;
boolean space = false;
boolean sdw = false;
String line = metroFile.readLine();
String currentStation = "";
while (line != null) {
if (lineNumber == 0) {
StringTokenizer tmpTok = new StringTokenizer(line);
String bugCharString = tmpTok.nextToken();
int ch=0;
if (bugCharString.length() == 4) { // ...this takes care of an invisible "\uFEFF" character if present.
System.out.println(bugCharString.length());
while(line.charAt(ch)!= '3'){
ch++;
}
//ch = 1;
}
else { // bugCharString.length() == 3 ...this means there is no "\uFEFF" character in front of "376".
System.out.println(bugCharString.length());
//ch = 0;
}
String strNumOfVertices = "";
String strNumOfEdges = "";
while (ch < line.length()) {
if (line.charAt(ch) == ' ') {
space = true;
ch++;
}
if (space) {
strNumOfEdges = strNumOfEdges + line.charAt(ch);
}
else {
strNumOfVertices = strNumOfVertices + line.charAt(ch);
}
ch++;
}
space = false;
numOfVertices = Integer.parseInt(strNumOfVertices);
numOfEdges = Integer.parseInt(strNumOfEdges);
System.out.println(numOfVertices);
System.out.println(numOfEdges);
stationsInOrder = new String[numOfVertices];
}
else {
StringTokenizer sTokenizer = new StringTokenizer(line);
String token = sTokenizer.nextToken();
if (token.equals("$")) {
sdw = true;
line = metroFile.readLine();
sTokenizer = new StringTokenizer(line);
token = sTokenizer.nextToken();
}
if (!sdw) {
for (int ch = 0; ch < line.length(); ch++) {
if ((line.charAt(ch) == ' ') && !space) {
space = true;
ch++;
}
if (space) {
currentStation = currentStation + line.charAt(ch);
}
}
space = false;
stationsInOrder[lineNumber-1] = currentStation;
currentStation = "";
}
else { // (sdw) // [ SOURCE DESTINATION WEIGHT ]
Integer source = new Integer(token);
token = sTokenizer.nextToken();
Integer dest = new Integer(token);
token = sTokenizer.nextToken();
Integer weight = new Integer(token);
if (weight == -1) {
weight = WALKING_CONSTANT;
}
Vertex<Integer> sv = vertices.get(source);
if (sv == null) {
sv = sGraph.insertVertex(source);
vertices.put(source, sv);
}
Vertex<Integer> dv = vertices.get(dest);
if (dv == null) {
dv = sGraph.insertVertex(dest);
vertices.put(dest, dv);
}
if (sGraph.getEdge(sv, dv) == null) {
Edge<Integer> e = sGraph.insertEdge(sv, dv, weight);
}
}
}
line = metroFile.readLine();
lineNumber++;
}
}
catch (FileNotFoundException e) {
e.printStackTrace();
}
catch (IOException e) {
e.printStackTrace();
}
}
/**
* Helper routine to get a Vertex (Position) from a string naming the vertex
* Modified by Thais Bardini on November 19th, 2017 ([email protected])
*/
public Vertex<Integer> getVertex(Integer vert) throws Exception {
for (Vertex<Integer> v : sGraph.vertices()) {
if (v.getElement().equals(vert)) {
return v;
}
}
throw new Exception("Vertex not in graph: " + vert);
}
/**
* Print the shortest distances
* Modified by Thais Bardini on November 19th, 2017 ([email protected])
* @throws Exception
*/
public void printAll(Integer start, Integer fin, Integer broke) throws Exception {
if (fin == -1 && broke == -1) {
System.out.println("Printing all stations on the same line as station " + start + ":");
Vertex<Integer> vSource = getVertex(start);
GraphAlgorithms dj = new GraphAlgorithms();
dj.DFSComplete(sGraph, vSource, stationsInOrder);
System.out.println();
}
else if (broke == -1) {
System.out.println("Printing shortest path from station " + start + " to station " + fin + ":");
Vertex<Integer> vSource = getVertex(start);
Vertex<Integer> vGoal = getVertex(fin);
GraphAlgorithms dj = new GraphAlgorithms();
Map<Vertex<Integer>, Integer> result = dj.shortestPathLengths(sGraph, vSource, vGoal);
List<Integer> interV = dj.verticesInShortestPath(sGraph, vSource, vGoal);
for (Integer i : interV) {
System.out.print(i + " ");
}
System.out.println("\n\nTotal travel time: " + result.get(vGoal) + " seconds.");
System.out.println("Total travel time is around: " + Math.round((float)result.get(vGoal)/60) + " minutes.\n");
}
else {
System.out.println("Printing shortest path from station " + start + " to station " + fin + " when the line containing station " + broke + " is closed:");
// Vertex<Integer> vBroke = getVertex(broke);
// for (Edge<Integer> e : sGraph.outgoingEdges(vBroke)) {
// if (e.getElement() != 90) {
// Vertex<Integer> vb = sGraph.opposite(vBroke, e);
// if (vb != null) {
// if (sGraph.inDegree(vb) <= 2 && sGraph.inDegree(vBroke) <= 2) {
// sGraph.removeVertex(vBroke);
// vBroke = vb;
// }
// else {
// vBroke = vb;
// }
// }
// }
// }
Vertex<Integer> vSource = getVertex(start);
Vertex<Integer> vGoal = getVertex(fin);
GraphAlgorithms dj = new GraphAlgorithms();
Map<Vertex<Integer>, Integer> result = dj.shortestPathLengths(sGraph, vSource, vGoal);
List<Integer> interV = dj.verticesInShortestPath(sGraph, vSource, vGoal);
for (Integer i : interV) {
System.out.print(i + " ");
}
System.out.println("\n\nTotal travel time: " + result.get(vGoal) + " seconds.");
System.out.println("Total travel time is around: " + Math.round((float)result.get(vGoal)/60) + " minutes.\n");
}
}
public static void main(String[] args) {
if (args.length == 1) {
System.out.print("\nOne INPUT: ");
System.out.println("N1 = " + args[0] + "\n");
Integer N1 = new Integer(args[0]);
try {
ParisMetro pM = new ParisMetro(N1);
}
catch (Exception except) {
System.err.println(except);
except.printStackTrace();
}
}
else if (args.length == 2) {
System.out.print("\nTwo INPUTS: ");
System.out.println("N1 = " + args[0] + " N2 = " + args[1] + "\n");
Integer N1 = new Integer(args[0]);
Integer N2 = new Integer(args[1]);
try {
ParisMetro pM = new ParisMetro(N1, N2);
}
catch (Exception except) {
System.err.println(except);
except.printStackTrace();
}
}
else if (args.length == 3) {
System.out.print("\nThree INPUTS: ");
System.out.println("N1 = " + args[0] + " N2 = " + args[1] + " N3 = " + args[2] + "\n");
Integer N1 = new Integer(args[0]);
Integer N2 = new Integer(args[1]);
Integer N3 = new Integer(args[2]);
try {
ParisMetro pM = new ParisMetro(N1, N2, N3);
}
catch (Exception except) {
System.err.println(except);
except.printStackTrace();
}
}
}
}