-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGraph.java
More file actions
320 lines (308 loc) · 9.03 KB
/
Copy pathGraph.java
File metadata and controls
320 lines (308 loc) · 9.03 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
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Comparator;
import java.util.PriorityQueue;
import java.util.Queue;
import java.util.Stack;
public class Graph {
private static Node[] adjacencyList;
private Boolean[] isNodeVisited;
private Integer[] parent;
private static Integer[] pathCost;
private static String searchType;
private String startState;
private String goalState;
private BufferedReader in = null;
private BufferedWriter out = null;
private int no_of_states;
private String[] log;
private static class PQueue<N extends Node> implements Comparator<N> {
public int compare(N o1, N o2) {
if ("1".equals(searchType)) {
return o1.getName().compareTo(o2.getName());
}
int nodeIndex1 = getNodeIndex(o1.getName());
int nodeIndex2 = getNodeIndex(o2.getName());
if (pathCost[nodeIndex1] == pathCost[nodeIndex2]) {
return o1.getName().compareTo(o2.getName());
}
return pathCost[nodeIndex1] - pathCost[nodeIndex2];
}
}
private static int getNodeIndex(String name) {
for (int i = 0; i < adjacencyList.length; i++) {
if (adjacencyList[i].getName().equals(name)) {
return i;
}
}
return -1;
}
private void initialize() {
log = new String[adjacencyList.length*2];
parent = new Integer[adjacencyList.length];
for (int j = 0; j < parent.length; j++) {
parent[j] = -1;
}
pathCost = new Integer[adjacencyList.length];
for (int j = 0; j < pathCost.length; j++) {
pathCost[j] = 0;
}
isNodeVisited = new Boolean[adjacencyList.length];
for (int j = 0; j < isNodeVisited.length; j++) {
isNodeVisited[j] = false;
}
}
private int runUCS() {
initialize();
Queue<Node> queue = new PriorityQueue<Node>(adjacencyList.length,
new PQueue<Node>());
int startIndex = getNodeIndex(startState);
if (startIndex == -1) {
System.err.println("Invalid input start state: " + startState);
System.exit(1);
}
queue.offer(adjacencyList[startIndex]);
isNodeVisited[startIndex] = true;
int j = 0;
while (!queue.isEmpty()) {
Node n = queue.poll();
log[j++] = n.getName();
if (n.getName().equals(goalState)) {
return getNodeIndex(n.getName());
} else {
for (Neighbour nbr = n.getAdjacent(); nbr != null; nbr = nbr
.getNext()) {
int nodeIndex = getNodeIndex(n.getName());
if (!isNodeVisited[nbr.getNodeIndex()]) {
isNodeVisited[nbr.getNodeIndex()] = true;
parent[nbr.getNodeIndex()] = nodeIndex;
pathCost[nbr.getNodeIndex()] = pathCost[nodeIndex]
+ nbr.getWeight();
queue.offer(adjacencyList[nbr.getNodeIndex()]);
} else if (isNodeVisited[nbr.getNodeIndex()]) {
if (pathCost[nodeIndex] + nbr.getWeight() < pathCost[nbr
.getNodeIndex()]) {
if (queue
.contains(adjacencyList[nbr.getNodeIndex()]))
queue.remove(adjacencyList[nbr.getNodeIndex()]);
parent[nbr.getNodeIndex()] = nodeIndex;
pathCost[nbr.getNodeIndex()] = pathCost[nodeIndex]
+ nbr.getWeight();
queue.offer(adjacencyList[nbr.getNodeIndex()]);
}
}
}
}
}
return -1;
}
private int runBFS() {
initialize();
Queue<Node> queue = new PriorityQueue<Node>(adjacencyList.length,
new PQueue<Node>());
int startIndex = getNodeIndex(startState);
if (startIndex == -1) {
System.err.println("Invalid input start state: " + startState);
System.exit(1);
}
queue.offer(adjacencyList[startIndex]);
isNodeVisited[startIndex] = true;
int j = 0;
while (!queue.isEmpty()) {
Node n = queue.poll();
log[j++] = n.getName();
if (n.getName().equals(goalState)) {
return getNodeIndex(n.getName());
} else {
for (Neighbour nbr = n.getAdjacent(); nbr != null; nbr = nbr
.getNext()) {
int nodeIndex = getNodeIndex(n.getName());
if (!isNodeVisited[nbr.getNodeIndex()]) {
isNodeVisited[nbr.getNodeIndex()] = true;
parent[nbr.getNodeIndex()] = nodeIndex;
pathCost[nbr.getNodeIndex()] = pathCost[nodeIndex]
+ nbr.getWeight();
queue.offer(adjacencyList[nbr.getNodeIndex()]);
}
}
}
}
return -1;
}
private int runDFS() {
initialize();
Integer unitPathCost[] = new Integer[adjacencyList.length];
for (int j = 0; j < unitPathCost.length; j++) {
unitPathCost[j] = 0;
}
Stack<Node> stack = new Stack<Node>();
int startIndex = getNodeIndex(startState);
if (startIndex == -1) {
System.err.println("Invalid input start state: " + startState);
System.exit(1);
}
stack.push(adjacencyList[startIndex]);
isNodeVisited[startIndex] = true;
int j = 0;
while (!stack.empty()) {
Node n = stack.pop();
log[j++] = n.getName();
if (n.getName().equals(goalState)) {
return getNodeIndex(n.getName());
} else {
for (Neighbour nbr = n.getAdjacent(); nbr != null; nbr = nbr
.getNext()) {
int nodeIndex = getNodeIndex(n.getName());
if (!isNodeVisited[nbr.getNodeIndex()]) {
isNodeVisited[nbr.getNodeIndex()] = true;
parent[nbr.getNodeIndex()] = nodeIndex;
pathCost[nbr.getNodeIndex()] = pathCost[nodeIndex]
+ nbr.getWeight();
unitPathCost[nbr.getNodeIndex()] = unitPathCost[nodeIndex] + 1;
stack.push(adjacencyList[nbr.getNodeIndex()]);
} else if (isNodeVisited[nbr.getNodeIndex()]) {
if (unitPathCost[nodeIndex] + 1 < unitPathCost[nbr
.getNodeIndex()]) {
parent[nbr.getNodeIndex()] = nodeIndex;
pathCost[nbr.getNodeIndex()] = pathCost[nodeIndex]
+ nbr.getWeight();
unitPathCost[nbr.getNodeIndex()] = unitPathCost[nodeIndex] + 1;
stack.push(adjacencyList[nbr.getNodeIndex()]);
}
}
}
}
}
return -1;
}
private void printOutput(int i) throws IOException {
out = new BufferedWriter(new FileWriter("output.txt"));
// if (i <= -1) {
// out.write("NoPathAvailable");
// return;
// }
int m = 0;
while (true) {
out.write(log[m++]);
if (m < log.length && log[m] != null) {
out.write("-");
} else
break;
}
out.newLine();
if (i > -1) {
String path[] = new String[adjacencyList.length];
int k = 0;
path[k++] = adjacencyList[i].getName();
for (int j = parent[i]; j > -1; j = parent[j]) {
path[k++] = adjacencyList[j].getName();
}
for (int j = k - 1; j > 0; j--) {
out.write(path[j] + "-");
}
out.write(path[0]);
out.newLine();
out.write(pathCost[i].toString());
} else {
out.write("NoPathAvailable");
}
}
public Graph() {
String line;
try {
in = new BufferedReader(new FileReader("input.txt"));
searchType = in.readLine();
if (!"1".equals(searchType) && !"2".equals(searchType)
&& !"3".equals(searchType)) {
System.err
.println("Invalid search type : "
+ searchType
+ ". Valid inputs for search type are either 1, 2 or 3");
System.exit(1);
}
startState = in.readLine();
goalState = in.readLine();
no_of_states = Integer.parseInt(in.readLine());
adjacencyList = new Node[no_of_states];
for (int i = 0; i < no_of_states; i++) {
line = in.readLine();
adjacencyList[i] = new Node(line, null);
}
for (int i = 0; i < no_of_states; i++) {
line = in.readLine();
String[] neighbours = line.split(" ");
if (neighbours.length != adjacencyList.length) {
System.err.println("Incorrect adjacency matrix input : "
+ line);
System.exit(1);
}
for (int j = 0; j < neighbours.length; j++) {
int weight = Integer.parseInt(neighbours[j]);
if (weight > 0) {
Neighbour adjacent = adjacencyList[i].getAdjacent();
String newName = adjacencyList[j].getName();
Neighbour newElem = new Neighbour(
getNodeIndex(newName), newName, weight);
if (adjacent == null
|| newName.compareTo(adjacent.getName()) >= 0) {
newElem.setNext(adjacent);
adjacencyList[i].setAdjacent(newElem);
} else {
while (adjacent.getNext() != null) {
if (newName.compareTo(adjacent.getName()) >= 0) {
newElem.setNext(adjacent.getNext());
adjacent.setNext(newElem);
break;
} else
adjacent = adjacent.getNext();
}
if (adjacent.getNext() == null) {
newElem.setNext(null);
adjacent.setNext(newElem);
}
}
}
}
}
if ("1".equals(searchType)) {
int i = runBFS();
printOutput(i);
} else if ("2".equals(searchType)) {
int i = runDFS();
printOutput(i);
} else {
int i = runUCS();
printOutput(i);
}
} catch (IOException ioe) {
System.err
.println("Error occurred while performing file I/O operation: "
+ ioe.getLocalizedMessage());
} finally {
if (in != null) {
try {
in.close();
} catch (IOException io) {
System.err
.println("Error occurred while closing input file: "
+ io.getLocalizedMessage());
}
}
if (out != null) {
try {
out.close();
} catch (IOException io) {
System.err
.println("Error occurred while closing output file: "
+ io.getLocalizedMessage());
}
}
}
}
public static void main(String args[]) {
new Graph();
}
}