-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBFSMaHighWay.java
More file actions
287 lines (208 loc) · 7.36 KB
/
BFSMaHighWay.java
File metadata and controls
287 lines (208 loc) · 7.36 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
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
package asroche.hw4;
import algs.days.day20.DepthFirstPaths;
import algs.days.day21.BreadthFirstPaths;
import algs.hw4.map.*;
import edu.princeton.cs.algs4.Graph;
/**
* The goal of this question is to:
*
* 1. Find the western-most location in Massachusetts
* 2. Find the eastern-most location in Massachusetts
* 3. Determine the shortest distance between these two locations IN TERMS OF THE
* TOTAL NUMBER OF HIGHWAY EDGES USED. YOU ARE NOT YET READY TO TAKE MILEAGE INTO ACCOUNT
* 4. Next create a copy of the highway graph that removes all line segments from I-90, the
* Massachusetts Turnpike toll road.
* 5. From this copy, determine the shortest distance between these two locations IN TERMS OF THE
* TOTAL NUMBER OF HIGHWAY EDGES USED. YOU ARE NOT YET READY TO TAKE MILEAGE INTO ACCOUNT.
*/
public class Q2 {
/**
* This method must create a copy of the graph, which you can do by recreate a graph with
* the same number of vertices as the original one, BUT you only add an edge to the copy
* if the edge in the original graph IS NOT EXCLUSIVELY a line segment from the Mass Pike.
*
* For example, in the data set you will see two nodes:
*
* I-90@49|MA 42.169702 -72.580876
* I-90@51|MA 42.161558 -72.541995
*
* These lines correspond to vertex #639 (the first one @49) and vertex #641 (the second one @51).
* Because the label for both of these vertices includes "I-90@" this edge must not appear in
* the copied graph, since it is a highway segment exclusively on the Mass Turnpike.
*
* Note that the edge is eliminated only when BOTH are present. For example, the following
* line segment will remain:
*
* I-95(23)/MA128 ==> vertex #705
* I-90@123B&I-95@24&MA128@24(95) ==> vertex #1785
*/
static Information remove_I90_segments(Information info) {
Graph copy = null;
copy = new Graph(info.graph.V());
for(int i=0; i<info.graph.V(); i++)
{
//Boolean[] marked = new Boolean[info.graph.V()];
//marked[i] = true;
for(int j:info.graph.adj(i))
{
if (info.labels.get(j).contains("I-90@") && info.labels.get(i).contains("I-90@"))
{
continue;
}
if(i<j)
copy.addEdge(i, j);
}
}
Information newInfo = new Information(copy, info.positions, info.labels);
return newInfo;
}
/**
* This helper method returns the western-most data point in the Information, given its latitude and
* longitude.
*
* https://en.wikipedia.org/wiki/Latitude
* https://en.wikipedia.org/wiki/Longitude
*
*/
public static int westernMostVertex(Information info) {
double longitude = Integer.MAX_VALUE;
int westernVertex = 0;
for(int id:info.labels.keys())
{
GPS gps = info.positions.get(id);
if(gps.longitude < longitude)
{
longitude = gps.longitude;
westernVertex = id;
}
}
return westernVertex;
}
/**
* This helper method returns the western-most data point in the Information, given its latitude and
* longitude.
*
* https://en.wikipedia.org/wiki/Latitude
* https://en.wikipedia.org/wiki/Longitude
*
*/
public static int easternMostVertex(Information info) {
double longitude = Integer.MIN_VALUE;
int easternVertex = 0;
for(int id:info.labels.keys())
{
GPS gps = info.positions.get(id);
if(gps.longitude > longitude)
{
longitude = gps.longitude;
easternVertex = id;
}
}
return easternVertex;
}
/**
* This helper method returns the southern-most data point in the Information, given its latitude and
* longitude.
*
* https://en.wikipedia.org/wiki/Latitude
* https://en.wikipedia.org/wiki/Longitude
*
*/
public static int southernMostVertex(Information info) {
double latitude = Integer.MAX_VALUE;
int southernVertex = 0;
for(int id:info.labels.keys())
{
GPS gps = info.positions.get(id);
if(gps.latitude < latitude)
{
latitude = gps.latitude;
southernVertex = id;
}
}
return southernVertex;
}
/**
* This helper method returns the northern-most data point in the Information, given its latitude and
* longitude.
*
* https://en.wikipedia.org/wiki/Latitude
* https://en.wikipedia.org/wiki/Longitude
*
*/
public static int northernMostVertex(Information info) {
double latitude = Integer.MIN_VALUE;
int northernVertex = 0;
for(int id:info.labels.keys())
{
GPS gps = info.positions.get(id);
if(gps.latitude > latitude)
{
latitude = gps.latitude;
northernVertex = id;
}
}
return northernVertex;
}
public static void main(String[] args) {
Information info = HighwayMap.undirectedGraph();
int north = northernMostVertex(info);
int south = southernMostVertex(info);
int east = easternMostVertex(info);
int west = westernMostVertex(info);
System.out.println("eastern most vertex: " + east + " - > " + info.labels.get(east));
System.out.println("western most vertex: " + west + " - > " + info.labels.get(west));
System.out.println("northern most vertex: " + north + " - > " + info.labels.get(north));
System.out.println("southern most vertex: " + south + " - > " + info.labels.get(south));
//Now we have to find the shortest path from the western most vertex to eastern most vertex
BreadthFirstPaths bp = new BreadthFirstPaths(info.graph, west);
BreadthFirstPaths bp2 = new BreadthFirstPaths(info.graph, south);
DepthFirstPaths dp = new DepthFirstPaths(info.graph, west);
DepthFirstPaths dp2 = new DepthFirstPaths(info.graph, south);
int counter = 0;
System.out.println("Shortest path from west to east");
for(int i:bp.pathTo(east))
{
counter++;
System.out.println(i + " - > " + info.labels.get(i));
}
System.out.println(" ");
System.out.println("----------------------------------------------------------------------------");
System.out.println("Shortest path from south to north");
int counter1 = 0;
for(int i:bp2.pathTo(north))
{
counter1++;
System.out.println(i + " - > " + info.labels.get(i));
}
int counter2 = 0;
for(int i:dp.pathTo(east))
{
counter2++;
}
int counter3 = 0;
for(int i:dp2.pathTo(north))
{
counter3++;
}
Information info2 = remove_I90_segments(info);
BreadthFirstPaths bfp = new BreadthFirstPaths(info2.graph, west);
BreadthFirstPaths bfp2 = new BreadthFirstPaths(info2.graph, south);
int counter4 = 0;
for(int i:bfp.pathTo(east))
{
counter4++;
}
System.out.println("There are " + (counter4-1) + " Edges in a breadth first search from the western most vertex to the eastern most vertex after we remove the I-90 segments");
int counter5 = 0;
for(int i:bfp2.pathTo(north))
{
counter5++;
}
System.out.println("There are " + (counter5-1) + " Edges in a breadth first search from the southern most vertex to the northern most vertex after we remove the I-90 segments");
System.out.println("There are " + (counter-1) + " Edges in the shortest path from the western most vertex to the eastern most vertex");
System.out.println("There are " + (counter1-1) + " Edges in the shortest path from the southern most vertex to the northern most vertex");
System.out.println("There are " + (counter2-1) + " Edges in a depth first search from the western most vertex to the eastern most vertex");
System.out.println("There are " + (counter3-1) + " Edges in a depth first search from the southern most vertex to the northern most vertex");
}
}