-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAss2226200553917.java
598 lines (436 loc) · 18.1 KB
/
Ass2226200553917.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
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.*;
public class Ass2226200553917 {
static int N = 1000;
static HashMap<Integer, double[]> pubsLonLat = new HashMap <Integer, double[]>();
static TreeMap<Integer,String> pubNames = new TreeMap<Integer,String>();
static ArrayList<Integer> HighestNumberOfSP = new ArrayList<Integer>();
static double[][] edgesDistance = new double[N][N];
static Set<Integer> pubsKeys = pubNames.keySet();
static double[][] edges = new double[N][N];
/* Record the Starting Time */
static long startRunTime = System.nanoTime();
static long endRunTime;
static Graph g;
static void answerDisplaySection()
{
System.out.println("");
System.out.println("Question 1: " + g.shortestPaths(gettingThePubNumberFor("24 Kitchen Drive"), gettingThePubNumberFor("Baa Bar")).size());
System.out.println("Question 2: " + HighestNumberOfSP.get(0) + " " + HighestNumberOfSP.get(1));
System.out.println("Question 3: " + HighestNumberOfSP.get(2));
System.out.println("Question 4: " + firstElement(g.shortestPaths(HighestNumberOfSP.get(0), HighestNumberOfSP.get(1))).size());
System.out.println("Question 5: " + findTheFurthestPub(gettingThePubNumberFor("Cavern Pub")));
System.out.println("Question 6: " + findingTheLengthInSum(gettingThePubNumberFor("Clock"), gettingThePubNumberFor("Elm House")));
System.out.println("Question 7: " + findingTheLengthInKM(gettingThePubNumberFor("Foghertys"), gettingThePubNumberFor("Jolly Miller")));
System.out.println("");
/**** Testing purposes ******/
}
/**** For question 1 ******/
static int gettingThePubNumberFor(String pubName)
{
int key = 0;
for(Map.Entry<Integer, String> entry: pubNames.entrySet())
{
if(entry.getValue().equals(pubName))
{
key = entry.getKey();
}
}
return key;
}
/**** For Question 2 and 3 ******/
//Which pair of pubs have the highest number of shortest paths between them?
//Calculating the maximum number of shortest path between two locations
static void findingTheHighestNumberOfSP()
{
int pubStartingPositionWithHighestSP = 0;
int pubEndPositionWithHighestSP = 0;
int maximumNumberOfShortestPaths = 0;
HashSet <ArrayList <Integer>> shortestPaths;
for(int startingPub: pubsKeys)
{
for(int endingPub: pubsKeys)
{
shortestPaths = g.shortestPaths(startingPub, endingPub); // comparing the starting pub and end pub
int currentMaxSize;
if(!shortestPaths.isEmpty())
{
currentMaxSize = shortestPaths.size();
if(maximumNumberOfShortestPaths < currentMaxSize)
{
maximumNumberOfShortestPaths = currentMaxSize;
pubStartingPositionWithHighestSP = startingPub;
pubEndPositionWithHighestSP = endingPub;
}
}
}
}
HighestNumberOfSP.add(pubStartingPositionWithHighestSP);
HighestNumberOfSP.add(pubEndPositionWithHighestSP);
HighestNumberOfSP.add(maximumNumberOfShortestPaths); //to store for question 3
} // end of findingHighestNumberOfSP()
/**** For Question 4******/
@SuppressWarnings("unchecked")
static ArrayList<Integer> firstElement (HashSet <ArrayList <Integer>> s)
{
return (ArrayList<Integer>) s.toArray()[0];
}
/**** For Question 5 ******/
// Finding the furthest pub away from the Cavern Pub
static ArrayList<Integer> findTheFurthestPub(int startingPub)
{
int maximum = 0;
int currentSize = 0;
ArrayList<Integer> furthestPubList = new ArrayList<Integer> ();
ArrayList<Integer> tempHoldingList = new ArrayList<Integer> ();
for(int lastPub: pubsKeys)
{
if(startingPub != lastPub)
{
HashSet<ArrayList<Integer>> temporaryHash = g.shortestPaths(startingPub, lastPub);
if(!temporaryHash.isEmpty())
{
for(ArrayList<Integer> temporary: temporaryHash)
{
currentSize = temporary.size();
if(maximum <= currentSize)
{
maximum = currentSize;
if(!tempHoldingList.contains(lastPub)) tempHoldingList.add(lastPub);
}
}
}
}
}
for(int lastPub: tempHoldingList)
{
if(startingPub != lastPub)
{
HashSet<ArrayList<Integer>> temporaryHash = g.shortestPaths(startingPub, lastPub);
if(!temporaryHash.isEmpty())
{
for(ArrayList<Integer> temporary: temporaryHash)
{
currentSize = temporary.size();
if(maximum == currentSize)
{
if(!furthestPubList.contains(lastPub)) furthestPubList.add(lastPub);
}
}
}
}
}
return furthestPubList;
}
/**** For Question 6 ******/
static double findingTheLengthInSum(int startingPub, int endPub)
{
double length = 0.0;
double temporaryHolder = 0.0;
ArrayList<Integer> pathForQ6 = g.dijkstra(startingPub, endPub);
if(!pathForQ6.isEmpty())
{
if(pathForQ6.size() == 2)
{
temporaryHolder = edges[pathForQ6.get(0)][pathForQ6.get(1)];
edges[pathForQ6.get(0)][pathForQ6.get(1)] = 0.0;
ArrayList<Integer> finalizedPath = g.dijkstra(startingPub, endPub);
edges[pathForQ6.get(0)][pathForQ6.get(1)] = temporaryHolder;
length = g.Q.get(finalizedPath.get(finalizedPath.size() - 1));
}
else
{
length = g.Q.get(pathForQ6.get(pathForQ6.size() - 1));
}
}
return length;
}//end of findingTheLengthInSum
/**** For Question 7 ******/
static double realDistance(double lat1, double lon1, double lat2, double lon2)
{
int R = 6371;
// km (change this constant to get miles)
double dLat = (lat2-lat1) * Math.PI / 180;
double dLon = (lon2-lon1) * Math.PI / 180;
double a = Math.sin(dLat/2) * Math.sin(dLat/2) +
Math.cos(lat1 * Math.PI / 180 )
* Math.cos(lat2 * Math.PI / 180 )
* Math.sin(dLon/2)
* Math.sin(dLon/2);
double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
double d = R * c;
return d;
}
/**** For Question 7 ******/
static double findingTheLengthInKM(int startingPub, int endPub)
{
double distance = 0.0;
double temporary = 0.0;
double[] currentPub = null;
double[] secondPub = null;
edges = edgesDistance;
g = new Graph(edges);
ArrayList<Integer> pathforQ7 = g.dijkstra(startingPub, endPub);
if(!pathforQ7.isEmpty())
{
if(pathforQ7.size() == 2)
{
temporary = edges[pathforQ7.get(0)][pathforQ7.get(1)];
edges[pathforQ7.get(0)][pathforQ7.get(1)] = 0.0;
ArrayList<Integer> pathFinal = g.dijkstra(startingPub, endPub);
edges[pathforQ7.get(0)][pathforQ7.get(1)] = temporary;
for(int i = 0; i < pathforQ7.size(); i++)
{
if(secondPub == null)
{
secondPub = pubsLonLat.get(pathFinal.get(i));
}
else
{
currentPub = pubsLonLat.get(pathFinal.get(i));
distance += realDistance(currentPub[1], currentPub[0], secondPub[1], secondPub[0]);
secondPub = currentPub;
}
}
}
else
{
for(int i = 0; i < pathforQ7.size(); i++)
{
if(secondPub == null)
{
secondPub = pubsLonLat.get(pathforQ7.get(i));
}
else
{
currentPub = pubsLonLat.get(pathforQ7.get(i));
distance += realDistance(currentPub[1], currentPub[0], secondPub[1], secondPub[0]);
secondPub = currentPub;
}
}
}
}
return distance;
}// end of findTheLengthInKM
/**** From Assignment 2 ******/
static ArrayList<String> convert (ArrayList<Integer> m)
{
ArrayList<String> z= new ArrayList<String>();
for (Integer i:m)
z.add(pubNames.get(i));
return z;
}
/**** From Assignment 2 ******/
static HashSet<ArrayList<String>> convert (HashSet<ArrayList<Integer>> paths)
{
HashSet <ArrayList <String>> k= new HashSet
<ArrayList<String>>();
for (ArrayList <Integer> p:paths)
k.add(convert(p));
return k;
}
/****************************************************************
Graph class
----------------------------------------------------------------
***************************************************************/
public static class Graph
{
double [] [] adj;
static HashMap<Integer, Double> Q = new HashMap<Integer, Double>();
double sum = 0.0;
Graph (double [] [] a)
//constructor for Graph class
{
adj = new double [a.length][a.length];
for (int i=0;i<a.length;i++)
{
for (int j=0;j<a.length;j++)
{
adj[i][j]=a[i][j];
}
}
} //end of graph constructor
//start of neighbours
public HashSet<Integer> neighbours(int v)
{
HashSet <Integer> h = new HashSet <Integer> ();
for (int i=0;i<adj.length;i++)
if (adj[v][i]!=0)
h.add(i);
return h;
} //end of neighbours
// start of vertices
public HashSet <Integer> vertices()
{
HashSet <Integer> h = new HashSet <Integer>();
for (int i=0;i<adj.length;i++)
h.add(i);
return h;
} //end of vertices
//start of add to end
@SuppressWarnings("unchecked")
ArrayList <Integer> addToEnd (int i, ArrayList <Integer> path)
// returns a new path with i at the end of path
{
ArrayList <Integer> k;
k=(ArrayList<Integer>)path.clone();
k.add(i);
return k;
} // end of add to end
// start of shortestPaths1
@SuppressWarnings("unchecked")
public HashSet <ArrayList <Integer>> shortestPaths1(HashSet<ArrayList <Integer>> sofar, HashSet <Integer> visited, int end)
{
HashSet <ArrayList <Integer>> more = new HashSet <ArrayList<Integer>>();
HashSet <ArrayList <Integer>> result = new HashSet <ArrayList<Integer>>();
HashSet <Integer> newVisited = (HashSet <Integer>) visited.clone();
boolean done = false;
boolean carryon = false;
for (ArrayList <Integer> p: sofar)
{
for (Integer z: neighbours(p.get(p.size()-1)))
{
if (!visited.contains(z))
{
carryon=true; newVisited.add(z);
if (z==end) {
done=true;
result.add(addToEnd(z,p));
}
else
more.add(addToEnd(z,p));
}
}
}
if (done) return result; else
if (carryon)
return shortestPaths1(more, newVisited, end);
else
return new HashSet <ArrayList <Integer>>();
} // end of shortestPaths1
public HashSet <ArrayList <Integer>> shortestPaths( int first, int end)
{
HashSet <ArrayList <Integer>> sofar = new HashSet <ArrayList<Integer>>();
HashSet <Integer> visited = new HashSet<Integer>();
ArrayList <Integer> starting = new ArrayList<Integer>();
starting.add(first);
sofar.add(starting);
if (first==end) return sofar;
visited.add(first);
return shortestPaths1(sofar,visited,end);
} //end of shortestPaths
// start of findSmallest
int findSmallest(HashMap<Integer,Double> t)
{
Object [] things= t.keySet().toArray();
double val = t.get(things[0]);
int least=(int) things[0];
Set<Integer> k = t.keySet();
for (Integer i : k)
{
if (t.get(i) < val)
{
least=i;
val=t.get(i);
}
}
return least;
} //end of findSmallest
// start of dijkstra
@SuppressWarnings("unchecked")
public ArrayList <Integer> dijkstra (int start, int end)
{
int N= adj.length;
final double INFINITY = Double.POSITIVE_INFINITY;
ArrayList <Integer> [] paths = new ArrayList [N]; // Path
for (int i=0;i<N;i++)
{
Q.put(i, INFINITY);
paths[i]=new ArrayList <Integer>();
paths[i].add(Integer.valueOf(start));
}
HashSet <Integer> S= new HashSet<>();
S.add(start);
Q.put(start, 0.0);
while (!Q.isEmpty())
{
int v = findSmallest(Q);
if ((v==end) && (Q.get(v) != INFINITY))
return paths[end];
double w = Q.get(v);
S.add(v);
for(int u: neighbours(v))
if (!S.contains(u))
{
double w1= w + adj[v][u];
if (w1 < Q.get(u))
{
Q.put(u,w1);
paths[u]= addToEnd(u, paths[v]);
}
}
Q.remove(v);
}
return new ArrayList <Integer> ();
} //end of dijkstra
} //end of graph
//start of main
public static void main(String[] args) throws FileNotFoundException {
/* Record the Starting Time */
//startRunTime = System.nanoTime();
for(int i=0;i<N;i++)
for(int j=0;j<N;j++)
edges[i][j]=0.0;
/****************************************
Initializing and declaring of File Names
-----------------------------------------
*****************************************/
String firstFile = args[0]; //pubs
String secondFile = args[1]; //pubs_lon_lat
String thirdFile = args[2]; //randomGraph
String z;
// this program assumes the user runs the code with the following file arguments in this order: pubs pubs_lon_lat randomGraph
/* Scanning the Pub Names */
Scanner s = new Scanner(new FileReader(firstFile));
z = s.nextLine();
while(s.hasNext())
{
z = s.nextLine();
String[] results = z.split(",");
pubNames.put(Integer.parseInt(results[0]), results[1]);//code, ATA
}
/* Scanning the pub longitude and latitude*/
s = new Scanner(new FileReader(secondFile));
z =s.nextLine();
while(s.hasNext())
{
z = s.nextLine();
String[] results = z.split(",");
double[] lon_lat = new double [2];
lon_lat[0] = Double.parseDouble(results[1]);//longtitude
lon_lat[1] = Double.parseDouble(results[2]);//latitude
pubsLonLat.put(Integer.parseInt(results[0]), lon_lat);
}
/* Scanning the randomGraph file*/
s = new Scanner(new FileReader(thirdFile));
z =s.nextLine();
while(s.hasNext())
{
z = s.nextLine();
String[] results = z.split(",");
edges[Integer.parseInt(results[0])][Integer.parseInt(results[1])] = Double.parseDouble(results[2]); //code, code, weight
edgesDistance[Integer.parseInt(results[0])][Integer.parseInt(results[1])] = realDistance(
pubsLonLat.get(Integer.parseInt(results[0]))[1], pubsLonLat.get(Integer.parseInt(results[0]))[0],
pubsLonLat.get(Integer.parseInt(results[1]))[1], pubsLonLat.get(Integer.parseInt(results[1]))[0]);
}
g = new Graph(edges);
System.out.println("Program calculating and generating answers for Qn 1-7 please wait");
System.out.println(" ");
findingTheHighestNumberOfSP();
//this will display the answers
answerDisplaySection();
endRunTime = System.nanoTime();
System.out.println("Execution Time: " + (endRunTime - startRunTime)/1000000 + " milliseconds");
} //end of main()
} //end of program