-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGraph.java
More file actions
530 lines (482 loc) · 13.2 KB
/
Graph.java
File metadata and controls
530 lines (482 loc) · 13.2 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
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
// Name : Hoshank Mali
// ID : 801254416
// Graph.java
import java.io.FileReader;
import java.io.IOException;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.StringTokenizer;
import java.util.TreeSet;
import java.util.List;
import java.util.ListIterator;
import java.util.Map;
import java.util.LinkedList;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Iterator;
import java.util.NoSuchElementException;
import java.util.PriorityQueue;
import java.util.Scanner;
class GraphException extends RuntimeException {
public GraphException(String name) {
super(name);
}
}
// Class to represent a Vertex
class Vertex {
public String name;
public List<Edge> adj; // list of adjacent vertices
public Vertex prev; // maintain previous vertex for finding shortest path
public boolean status; // maintain if a vertex is available or not
public Double dist; // distance to reach a vertex
public boolean visited; // maintain if vertex is already visited during reachable
public Vertex(String nm) {
name = nm;
adj = new LinkedList<Edge>();
reset();
status = true;
dist = Graph.INFINITY;
visited = false;
}
public void reset() {
dist = Graph.INFINITY;
prev = null;
visited = false;
}
//get set methods
public boolean getStatus() {
return this.status;
}
public boolean isVisited() {
return this.visited;
}
public void setStatus(boolean status) {
this.status = status;
}
public void setVisited(boolean visited) {
this.visited = visited;
}
public double getDist() {
return this.dist;
}
public void setDist(double dist) {
this.dist = dist;
}
}
//class to represent a Edge
class Edge {
public double cost; // cost of an edge
public Vertex dest; // destination of an edge
public boolean status; // maintain if an edge is available or not
public Edge(Vertex dest, double cost) {
this.dest = dest;
this.cost = cost;
this.status = true;
}
//get set methods
public String getDest() {
return dest.name;
}
public double getCost() {
return cost;
}
public boolean getStatus() {
return status;
}
public void setStatus(boolean st) {
this.status = st;
}
public void setCost(double cost) {
this.cost = cost;
}
}
//class Graph
public class Graph {
public static final Double INFINITY = Double.MAX_VALUE;
private Map<String, Vertex> vertexMap = new HashMap<String, Vertex>(); // Map of vertices
boolean flag = true;
// Add new edge to graph
public void addEdge(String sourceName, String destName, double cost) {
Vertex v = getVertex(sourceName); // create source vertex if does not exist
Vertex w = getVertex(destName); // create destination vertex if does not exist
Iterator<Edge> listIterator = v.adj.listIterator();
int flag = 0;
while (listIterator.hasNext()) {
Edge edge = listIterator.next();
if (w.name.equals(edge.dest.name)) { // check if edge already exists
edge.cost = cost; // update cost of already existing edge
flag = 1;
}
}
if (flag == 0) // Edge does not exist
v.adj.add(new Edge(w, cost));
}
// Dijktras Algorithm
public void Dijktras(String source, String dest) {
clearAll(); // reset all vertices
PriorityQueue<Vertex> pq = new PriorityQueue<>(new comp()); // min heap priority queue
List<String> visited = new ArrayList<String>();
Vertex start = vertexMap.get(source);
if (start == null) { // source vertex does not exist in the graph
System.out.println("Source vertex not found");
return;
} else if (!start.getStatus()) { // source vertex is down
System.out.println(start.name + " is down");
return;
} else {
// set distance of source vertex to 0 and previous as null
start.dist = Double.valueOf(0);
start.prev = null;
}
pq.add(start);
while (!pq.isEmpty()) {
Vertex v = pq.remove();
visited.add(v.name);
ListIterator<Edge> i = v.adj.listIterator();
while (i.hasNext()) {
Edge e = (Edge) i.next();
if (e.getStatus()) {
String sDesti = e.getDest();
Vertex vDestn = vertexMap.get(sDesti);
if (vDestn.getStatus()) {
if (vDestn.getDist() > (v.getDist() + e.getCost())) {
vDestn.setDist(v.getDist() + e.getCost()); // update distance of vertex
vDestn.prev = v; // update previous of vertex
System.out.println(vDestn.name + " " + vDestn.prev.name);
}
if (!visited.contains(vDestn.name) && !pq.contains(vDestn)) {
pq.add(vDestn);
}
}
}
}
}
printPath(dest);
if (flag) {// print cost only if printPath executes correctly
double fc = vertexMap.get(dest).getDist();
BigDecimal bd = new BigDecimal(fc).setScale(2, RoundingMode.HALF_UP);
System.out.print(" " + bd);
System.out.println();
}
}
public void reachable() {
TreeSet<String> treeSet = new TreeSet<String>();
System.out.println();
for (String key : vertexMap.keySet()) { // add available vertices to treeset
if (vertexMap.get(key).getStatus())
treeSet.add(key);
}
Iterator<String> i = treeSet.iterator();
while (i.hasNext()) {
String vertex = i.next();
System.out.println(vertex);
breadthFirstSearch(vertex);
}
}
// Breadth First Search algorithm to print all reachable vertices from a given
// vertex
// BFS gives time complexity O(V+E)
// Running for V vertices. total time complexity is O(V(V+E))
public void breadthFirstSearch(String vertex) {
clearAll(); // reset all vertices
TreeSet<String> treeSet = new TreeSet<String>();
PriorityQueue<String> pq = new PriorityQueue<String>();
Vertex v = vertexMap.get(vertex);
v.visited = true; // set visited as true for this vertex
pq.add(v.name);
while (pq.size() > 0) {
String ve = pq.remove();
ListIterator<Edge> i = vertexMap.get(ve).adj.listIterator();
while (i.hasNext()) {
Edge e = (Edge) i.next();
if (e.getStatus() && (!e.dest.isVisited()) && (e.dest.getStatus())) {
treeSet.add(e.getDest());
pq.add(e.getDest());
e.dest.setVisited(true);
}
}
}
for (String s : treeSet) {
System.out.println(" " + s);
}
}
// print all vertices and edges
public void print() {
TreeSet<String> treeSet = new TreeSet<String>();
System.out.println();
for (String key : vertexMap.keySet()) {
treeSet.add(key);
}
Iterator<String> i = treeSet.iterator();
while (i.hasNext()) {
String s = i.next();
System.out.print(s);
if (!vertexMap.get(s).getStatus())
System.out.print(" DOWN"); // print DOWN if vertex is down
System.out.println();
TreeSet<String> adj = new TreeSet<String>();
HashMap<String, Double> cost = new HashMap<String, Double>(); // maintain vertex name and cost of edge
HashMap<String, Boolean> status = new HashMap<String, Boolean>(); // maintain vertex name and status of edge
for (Edge e : vertexMap.get(s).adj) {
adj.add(e.dest.name);
cost.put(e.dest.name, e.cost);
status.put(e.dest.name, e.status);
}
for (String si : adj) {
System.out.print(" " + si + " " + cost.get(si)); // print all the adjacent vertex in the current vertex
// list
if (status.get(si) == false)
System.out.print(" DOWN");
System.out.println();
}
}
}
private Vertex getVertex(String vertexName) {
Vertex v = vertexMap.get(vertexName);
if (v == null) {
v = new Vertex(vertexName); // add new vertex if vertex is null
vertexMap.put(vertexName, v);
}
return v;
}
// Vertex Down
public void vertexDown(String v) {
Vertex ve = vertexMap.get(v);
if (ve == null) {
System.out.println("vertex not found");
return;
}
if (ve.status) {
ve.setStatus(false);
} else {
System.out.println(ve.name + " is already Down");
}
}
// Vertex Up
public void vertexUp(String v) {
Vertex ve = vertexMap.get(v);
if (ve == null) {
System.out.println("vertex not found");
return;
}
if (!ve.status) {
ve.setStatus(true);
} else {
System.out.println(ve.name + " is already Up");
}
}
// Edge Down
public void edgeDown(String src, String dest) {
Vertex s = vertexMap.get(src);
Vertex d = vertexMap.get(dest);
boolean done = false;
if (s == null || d == null) {
System.out.println("Source/Destination vertex not found");
return;
}
ListIterator<Edge> i = s.adj.listIterator();
while (i.hasNext()) {
Edge e = (Edge) i.next();
if (e.getDest().equals(dest)) {
if (e.getStatus()) {
e.setStatus(false);
done = true;
} else {
System.out.println("Edge is already Down");
done = true;
}
}
}
if (!done) {
System.out.println("No such Edge exists");
}
}
// Edge Up
public void edgeUp(String src, String dest) {
Vertex s = vertexMap.get(src);
Vertex d = vertexMap.get(dest);
boolean done = false;
if (s == null || d == null) {
System.out.println("Source/Destination vertex not found");
return;
}
ListIterator<Edge> i = s.adj.listIterator();
while (i.hasNext()) {
Edge e = (Edge) i.next();
if (e.getDest().equals(dest)) {
if (!e.getStatus()) {
e.setStatus(true);
done = true;
} else {
System.out.println("Edge is already Up");
done = true;
}
}
}
if (!done) {
System.out.println("No such Edge exists");
}
}
// Delete Edge
public void deleteEdge(String src, String dest) {
Vertex s = vertexMap.get(src);
Vertex d = vertexMap.get(dest);
boolean done = false;
if (s == null || d == null) {
System.out.println("Source/Destination vertex not found");
return;
}
ListIterator<Edge> i = s.adj.listIterator();
while (i.hasNext()) {
Edge e = (Edge) i.next();
if (e.getDest().equals(dest)) {
s.adj.remove(e);
done = true;
}
}
if (!done) {
System.out.println("No such Edge exists");
}
}
// used to print path after Dijkstras Algorithm
public void printPath(String destName) {
Vertex w = vertexMap.get(destName);
if (w == null) {
flag = false;
System.out.println("Destination vertex not found");
} else if (w.dist == INFINITY) {
System.out.println(destName + " is unreachable");
flag = false;
} else {
printPath(w);
}
}
private void printPath(Vertex dest) {
if (dest.prev != null) {
printPath(dest.prev);
System.out.print(" to ");
}
System.out.print(dest.name);
}
/**
* Initializes the vertex output info prior to running any shortest path
* algorithm.
*/
private void clearAll() {
for (Vertex v : vertexMap.values())
v.reset();
}
/**
* Process a request; return false if end of file.
*/
public static boolean processRequest(Scanner in, Graph g) {
try {
String command, src, dest;
Double cost;
String query = in.nextLine();
StringTokenizer tokens = new StringTokenizer(query);
switch (tokens.countTokens()) {
case 1:
command = tokens.nextToken();
if (command.equals("print")) {
g.print();
} else if (command.equals("reachable")) {
g.reachable();
} else if (command.equals("quit")) {
System.exit(0);
} else {
System.out.println("Invalid Query");
}
break;
case 2:
command = tokens.nextToken();
src = tokens.nextToken();
if (command.equals("vertexdown")) {
g.vertexDown(src);
} else if (command.equals("vertexup")) {
g.vertexUp(src);
} else {
System.out.println("Invalid Query");
}
break;
case 3:
command = tokens.nextToken();
src = tokens.nextToken();
dest = tokens.nextToken();
if (command.equals("deleteedge")) {
g.deleteEdge(src, dest);
} else if (command.equals("edgedown")) {
g.edgeDown(src, dest);
} else if (command.equals("edgeup")) {
g.edgeUp(src, dest);
} else if (command.equals("path")) {
g.Dijktras(src, dest);
} else {
System.out.println("Invalid Query");
}
break;
case 4:
command = tokens.nextToken();
src = tokens.nextToken();
dest = tokens.nextToken();
cost = Double.parseDouble(tokens.nextToken());
if (command.equals("addedge")) {
g.addEdge(src, dest, cost);
} else {
System.out.println("Invalid Query");
}
break;
default:
System.out.println("Invalid Query");
break;
}
} catch (NoSuchElementException e) {
System.out.print(e);
return false;
} catch (GraphException e) {
System.err.println(e);
}
return true;
}
public static void main(String[] args) {
Graph g = new Graph();
try {
FileReader fin = new FileReader(args[0]);
Scanner graphFile = new Scanner(fin);
// Read the edges and insert
String line;
while (graphFile.hasNextLine()) {
line = graphFile.nextLine();
StringTokenizer st = new StringTokenizer(line);
try {
if (st.countTokens() != 3) {
System.err.println("Skipping ill-formatted line " + line);
continue;
}
String source = st.nextToken();
String dest = st.nextToken();
double cost = Double.parseDouble(st.nextToken());
g.addEdge(source, dest, cost);
g.addEdge(dest, source, cost);
} catch (NumberFormatException e) {
System.err.println("Skipping ill-formatted line " + line);
}
}
} catch (IOException e) {
System.err.println(e);
}
System.out.println("File read...");
System.out.println(g.vertexMap.size() + " vertices");
Scanner in = new Scanner(System.in);
while (processRequest(in, g))
;
}
class comp implements Comparator<Vertex> {
@Override
public int compare(Vertex o1, Vertex o2) {
// TODO Auto-generated method stub
return o1.dist.compareTo(o2.dist);
}
}
}