-
Notifications
You must be signed in to change notification settings - Fork 0
/
homework.java
539 lines (474 loc) · 15.9 KB
/
homework.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
/**
* Created by Tirthmehta on 9/14/16.
*/
import java.io.*;
import java.util.*;
class Node
{
String location;
Node(String locs)
{
location=locs;
}
ArrayList<Edge> arr =new ArrayList<Edge>();
int distancetravelled;
int timestamp;
public void setTimestamp(int timestamp)
{
this.timestamp=timestamp;
}
}
class Edge
{
String dest;
String source;
int weight;
int sundayvalue1,sundayvalue2;
Edge(String sourc,String destination,int value)
{
source=sourc;
dest=destination;
weight=value;
}
public void setSundayvalue(int sun1,int sun2)
{
sundayvalue1=sun1;
sundayvalue2=sun2;
}
}
class CreateGraph
{
int length;
Node rootnode;
HashMap<String,Node> hmap=new HashMap<String,Node>();
public void setRootNode(Node n)
{
rootnode=n;
}
public void setLength(int len)
{
length=len;
}
public Node getRootNode()
{
return rootnode;
}
public void addnode(String loc,String start)
{
Node n=new Node(loc);
hmap.put(loc,n);
if(n.location.equals(start)){
setRootNode(n);}
}
public void addedge(String sourc,Edge e)
{
hmap.get(sourc).arr.add(e);
}
public void bfs(String start,String end) throws IOException
{
Map<String, String> hmapbfs = new HashMap<String, String>();
Map<String,Boolean> visited=new HashMap<String, Boolean>();
hmapbfs.put(rootnode.location,null);
Queue q=new LinkedList();
q.add(this.rootnode);
visited.put(rootnode.location,true);
String temp=rootnode.location;
while(!q.isEmpty()) {
Node n = (Node) q.remove();
if(n.location.equals(end))
{
break;
}
temp = n.location;
Node children = null;
for (int i = 0; i < n.arr.size(); i++) {
Edge e1 = n.arr.get(i);
Node iterate = hmap.get(e1.dest);
if (visited.get(iterate.location) == null) {
hmapbfs.put(iterate.location, n.location);
visited.put(iterate.location, true);
q.add(iterate);
}
}
}
printpathbfs(hmapbfs,end);
}
public void dfs(String start,String end) throws IOException
{
Map<String, String> hmapdfs = new HashMap<String, String>();
Map<String,Boolean> visited=new HashMap<String, Boolean>();
hmapdfs.put(rootnode.location,null);
Stack s=new Stack();
Stack aux=new Stack();
s.push(this.rootnode);
visited.put(rootnode.location,true);
String temp=rootnode.location;
while(!s.isEmpty()) {
Node n = (Node)s.peek();
if(n==rootnode)
{
n=(Node)s.pop();
}
while(!s.isEmpty())
{
aux.push(s.pop());
}
if((!aux.isEmpty())&&(aux.peek()!=rootnode))
{
n=(Node)aux.pop();
}
if(n.location.equals(end))
{
break;
}
for (int i = 0; i < n.arr.size(); i++) {
Edge e1 = n.arr.get(i);
Node iterate = hmap.get(e1.dest);
if (visited.get(iterate.location) == null) {
hmapdfs.put(iterate.location, n.location);
visited.put(iterate.location, true);
s.push(iterate);
}
}
while(!aux.isEmpty())
{
s.push(aux.pop());
}
}
printpathdfs(hmapdfs,end);
}
public void ucs(String start,String end) throws IOException
{
int timer=0;
Map<String, String> hmapucs = new HashMap<String, String>();
Map<String, Integer> hmapucs2 = new HashMap<String, Integer>();
Map<String,Boolean> visited=new HashMap<String, Boolean>();
hmapucs.put(rootnode.location,null);
hmapucs2.put(rootnode.location,0);
rootnode.distancetravelled=0;
Comparator<Node> comparator = new StringLengthComparator();
PriorityQueue<Node> queue =
new PriorityQueue<Node>(length, comparator);
rootnode.setTimestamp(timer);
timer++;
queue.add(rootnode);
visited.put(rootnode.location,true);
String temp=rootnode.location;
while(!queue.isEmpty()) {
Node n = (Node) queue.poll();
temp = n.location;
if(n.location.equals(end))
{
break;
}
Node children = null;
for (int i = 0; i < n.arr.size(); i++) {
Edge e1 = n.arr.get(i);
Node iterate = hmap.get(e1.dest);
if (visited.get(iterate.location) == null) {
iterate.distancetravelled=e1.weight+n.distancetravelled;
hmapucs.put(iterate.location, n.location);
iterate.setTimestamp(timer);
timer++;
visited.put(iterate.location, true);
queue.add(iterate);
hmapucs2.put(iterate.location,iterate.distancetravelled);
}
else if(visited.get(iterate.location)==true)
{
if(iterate.distancetravelled>n.distancetravelled+e1.weight)
{
iterate.distancetravelled=e1.weight+n.distancetravelled;
hmapucs.remove(iterate.location);
hmapucs.remove(iterate.location);
hmapucs.put(iterate.location, n.location);
iterate.setTimestamp(timer);
timer++;
visited.put(iterate.location, true);
queue.add(iterate);
hmapucs2.put(iterate.location,iterate.distancetravelled);
}
}
}
}
printpathucs(hmapucs,hmapucs2,end);
}
public void astar(String start,String end) throws IOException
{
int timer=0;
Map<String, String> hmapastar = new HashMap<String, String>();
Map<String, Integer> hmapastar2 = new HashMap<String, Integer>();
Map<String, Integer> hmapastar3 = new HashMap<String, Integer>();
Map<String,Boolean> visited=new HashMap<String, Boolean>();
hmapastar.put(rootnode.location,null);
hmapastar2.put(rootnode.location,0);
hmapastar3.put(rootnode.location,0);
rootnode.distancetravelled=0;
Comparator<Node> comparator = new StringLengthComparator();
PriorityQueue<Node> queue =
new PriorityQueue<Node>(length, comparator);
rootnode.setTimestamp(timer);
timer++;
queue.add(rootnode);
visited.put(rootnode.location,true);
String temp=rootnode.location;
while(!queue.isEmpty()) {
Node n = (Node) queue.poll();
temp = n.location;
Node children = null;
if(n.location.equals(end))
{
break;
}
for (int i = 0; i < n.arr.size(); i++) {
Edge e1 = n.arr.get(i);
Node iterate = hmap.get(e1.dest);
if (visited.get(iterate.location) == null) {
iterate.distancetravelled=e1.weight+n.distancetravelled+e1.sundayvalue2-e1.sundayvalue1;
hmapastar.put(iterate.location, n.location);
iterate.setTimestamp(timer);
timer++;
visited.put(iterate.location, true);
queue.add(iterate);
hmapastar2.put(iterate.location,iterate.distancetravelled);
hmapastar3.put(iterate.location,iterate.distancetravelled-e1.sundayvalue2);
}
else if(visited.get(iterate.location)==true)
{
if(iterate.distancetravelled>n.distancetravelled+e1.weight+e1.sundayvalue2-e1.sundayvalue1)
{
iterate.distancetravelled=n.distancetravelled+e1.weight+e1.sundayvalue2-e1.sundayvalue1;
hmapastar.remove(iterate.location);
hmapastar2.remove(iterate.location);
hmapastar3.remove(iterate.location);
hmapastar.put(iterate.location, n.location);
iterate.setTimestamp(timer);
timer++;
visited.put(iterate.location, true);
queue.add(iterate);
hmapastar2.put(iterate.location,iterate.distancetravelled);
hmapastar3.put(iterate.location,iterate.distancetravelled-e1.sundayvalue2);
}
}
}
}
printpathastar(hmapastar,hmapastar3,end);
}
private void printpathbfs(Map<String,String> hmap,String end) throws IOException
{
int value=0;
ArrayList printing=new ArrayList();
printing.add(end);
String a= hmap.get(end);
printing.add(a);
while(a!=null)
{
a=hmap.get(a);
if(a!=null)
printing.add(a);
}
writerbfs(printing,value);
}
public void writerbfs(ArrayList printing,int value) throws IOException
{
FileWriter fw = new FileWriter("output.txt");
PrintWriter pw = new PrintWriter(fw);
for(int i=printing.size()-1;i>=0;i--) {
pw.println(printing.get(i)+" "+value);
value++;
}
fw.close();
}
private void printpathdfs(Map<String,String> hmap,String end) throws IOException
{
int value=0;
ArrayList printing=new ArrayList();
printing.add(end);
String a= hmap.get(end);
printing.add(a);
while(a!=null)
{
a=hmap.get(a);
if(a!=null)
printing.add(a);
}
writerdfs(printing,value);
}
public void writerdfs(ArrayList printing,int value) throws IOException
{
FileWriter fw = new FileWriter("output.txt");
PrintWriter pw = new PrintWriter(fw);
for(int i=printing.size()-1;i>=0;i--) {
pw.println(printing.get(i)+" "+value);
value++;
}
fw.close();
}
private void printpathucs(Map<String,String> hmap,Map<String,Integer> hmap2,String end) throws IOException
{
int value=0;
ArrayList printing=new ArrayList();
int values[]=new int[hmap.size()];
printing.add(end);
values[value]=hmap2.get(end);
value++;
String a= hmap.get(end);
values[value]=hmap2.get(a);
value++;
printing.add(a);
while(a!=null)
{
a=hmap.get(a);
if(a!=null) {
printing.add(a);
values[value]=hmap2.get(a);
value++;
}
}
writerucs(printing,values);
}
public void writerucs(ArrayList printing,int values[]) throws IOException
{
FileWriter fw = new FileWriter("output.txt");
PrintWriter pw = new PrintWriter(fw);
int temp=0;
for(int i=printing.size()-1;i>=0;i--) {
pw.println(printing.get(i)+" "+values[i]);
}
fw.close();
}
private void printpathastar(Map<String,String> hmap,Map<String,Integer> hmap2,String end) throws IOException
{
int value=0;
ArrayList printing=new ArrayList();
int values[]=new int[hmap.size()];
printing.add(end);
values[value]=hmap2.get(end);
value++;
String a= hmap.get(end);
values[value]=hmap2.get(a);
value++;
printing.add(a);
while(a!=null)
{
a=hmap.get(a);
if(a!=null) {
printing.add(a);
values[value]=hmap2.get(a);
value++;
}
}
writerastar(printing,values);
}
public void writerastar(ArrayList printing,int values[]) throws IOException
{
FileWriter fw = new FileWriter("output.txt");
PrintWriter pw = new PrintWriter(fw);
int temp=0;
for(int i=printing.size()-1;i>=0;i--) {
pw.println(printing.get(i)+" "+values[i]);
}
fw.close();
}
public void writerstartgoal(String starter) throws IOException
{
FileWriter fw = new FileWriter("output.txt");
PrintWriter pw = new PrintWriter(fw);
pw.println(starter+" 0");
fw.close();
}
}
public class homework {
public static void main(String[] args) throws IOException {
String list[]=reader();
String algo=list[0];
String startstate=list[1];
String goalstate=list[2];
int livetrafficlines=Integer.parseInt(list[3]);
String livetrafficlist[]=new String[livetrafficlines];
int m=0;
for(int i=4;i<livetrafficlines+4;i++){
livetrafficlist[m]=list[i];
m++;}
int sundaytrafficlines=Integer.parseInt(list[livetrafficlines+4]);
String sundaytrafficlist[]=new String[sundaytrafficlines];
int n=0;
for(int i=livetrafficlines+5;i<livetrafficlines+sundaytrafficlines+5;i++)
{
sundaytrafficlist[n]=list[i];
n++;
}
String arr1[]=new String[3];
String arr2[]=new String[2];
CreateGraph cg=new CreateGraph();
Map<String,Integer> hmapsunday=new HashMap<String,Integer>();
for(int i=0;i<sundaytrafficlines;i++)
{
arr2=sundaytrafficlist[i].split(" ");
hmapsunday.put(arr2[0],Integer.parseInt(arr2[1]));
}
hmapsunday.remove(startstate);
hmapsunday.put(startstate,0);
for(int i=0;i<livetrafficlines;i++)
{
arr1=livetrafficlist[i].split(" ");
if(!(cg.hmap.containsKey(arr1[0]))){
cg.addnode(arr1[0],startstate);}
if(!(cg.hmap.containsKey(arr1[1])))
{cg.addnode(arr1[1],startstate);}
Edge e=new Edge(arr1[0],arr1[1],Integer.parseInt(arr1[2]));
e.setSundayvalue(hmapsunday.get(arr1[0]),hmapsunday.get(arr1[1]));
cg.addedge(arr1[0],e);
arr1=null;
}
cg.setLength(cg.hmap.size());
int afact=0;
if(startstate.equals(goalstate))
{
cg.writerstartgoal(startstate);
afact=1;
}
if(afact!=1) {
if (algo.equals("BFS")) {
cg.bfs(startstate, goalstate);
} else if (algo.equals("DFS")) {
cg.dfs(startstate, goalstate);
} else if (algo.equals("UCS")) {
cg.ucs(startstate, goalstate);
} else if (algo.equals("A*")) {
cg.astar(startstate, goalstate);
}
}
}
public static String[] reader() throws IOException
{
FileReader fr=new FileReader("input.txt");
BufferedReader br=new BufferedReader(fr);
String c;
int lines=0;
while((c=br.readLine())!=null)
lines++;
String arr[]=new String[lines];
int i=0;
br.close();
FileReader fr2=new FileReader("input.txt");
BufferedReader br2=new BufferedReader(fr2);
while((c=br2.readLine())!=null) {
arr[i]=c;
i++;
}
br.close();
return arr;
}
}
class StringLengthComparator implements Comparator<Node>
{
@Override
public int compare(Node o1, Node o2) {
if(o1.distancetravelled!=o2.distancetravelled){
return o1.distancetravelled-o2.distancetravelled;}
else{
return o1.timestamp-o2.timestamp;
}
}
}