-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathGraph.java
372 lines (328 loc) · 11.7 KB
/
Graph.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
/** Graph.java
* A class for building graphs, with methods for editing the graph and
* searching it.*
* @author Dillon John
* CSIT357-02 Artificial Intelligence
* Dr. Aparna Varde
* Assignment 2: Implementation of Search Methods
*/
import java.util.ArrayList;
public class Graph {
int[][] matrix; // The adjacencyk matrix for the graph
boolean[] visited; // An array, storing the 'visited' status of each node
String title;
boolean found = false;
int pathCost = 0;
ArrayList<Integer> searchPath = new ArrayList<Integer>();
/** Graph: default constructor
* Creates a graph object with a specified number of nodes.
* Initializes the graph's adjacency matrix with no connecting edges.
* Creates an array for tracking the visited status of the graph's nodes,
* or vertices, and Initializes that array to all false, meaning no node has
* yet been visited.
*/
Graph(int nodes, String name) {
// Create a 2D array for the adjacency matrix
matrix = new int[nodes][nodes];
// Set all edges in the matrix to 0: No edges connecting the nodes yet
for (int i = 0 ; i < nodes; i++ ) {
for(int j = 0; i < nodes; i++){
matrix[i][j] = 0;
}
}
// Create an array for tracking the 'visited' status of a node
visited = new boolean[nodes];
// Set all nodes to false: not yet visited
for (int node = 0; node < visited.length; node++) {
visited[node] = false;
}
// Give the grapha title
title = name;
}
/**
* printMatrix
* A method for printing an adjacency matrix.
* @param Graph: a Graph object
* @return void
*/
private void printMatrix() {
System.out.println("\n" + title + "\n");
// Print node numbers across the top
System.out.print("N ");
for(int n = 0; n < matrix.length; n++){
System.out.print(n+1 + " ");
}
System.out.println("\n");
int node = 1;
for (int i = 0; i < matrix.length; i++) {
System.out.print(node + ": ");
node++;
for (int j = 0; j < matrix.length; j++) {
System.out.print(" " + matrix[i][j] + " ");
}
System.out.println();
}
System.out.println();
}
/**
* edge
* A method for editing the adjacency matrix to account for connected nodes
* The default graph is set to all 0s, meaning no nodes are connected by edges
* Use 1 for representing the existence of an edge
* Other integer values can be used to represent a weighted edge length.
* @param node1: an integer, the 'start location' : "from here..."
* @param node2: an integer, a node that is connected to node 1 by an edge "... to here"
* @param weight: 1 if edge exists, >=1 if weighted edge. 0 means no edge exists
* @return void
*/
private void edge(int node1, int node2, int weight) {
matrix[node1 -1][node2 -1] = weight;
}
/**
* reset
* Sets all nodes to 'false' visited status
* Sets found to 'false'
* Sets pathCost to 0.
* Clears the search path.
* Allows for quickly searching the same graph with different parameters.
*/
private void reset(){
found = false;
pathCost = 0;
searchPath.clear();
for (int i = 0; i < visited.length; i++ ) {
visited[i] = false;
}
}
/**
* DFS (Depth-First Search)
* Given and accept states, searches the graph from the start state until
* it finds the accept state. Tracks the path cost as it traverses the graph.
* @param startAt: int: the start state, or the node from which the search begins
* @param lookFor: int: the accept state, or the node being searched for
* @return void
*/
private boolean DFS(int startAt, int lookFor){
// Since our nodes are labeled 1-n, subtract 1 for indexing
int currentNode = startAt -1;
int nextNode = 0;
visited[currentNode] = true;
if(!found){
if(startAt == lookFor) {
// The current node is the accept state.
found = true;
// Add the current node to the search path.
// Use startAt, since that has the appropriate node number, rather
// than the index of that node in the array or matrix
searchPath.add(startAt);
// Accept state has been reached
System.out.println("Successfully found node " + lookFor + "!");
System.out.println("Total path cost: " + pathCost);
System.out.print("Search path: ");
System.out.print(searchPath);
System.out.println("\n");
return found;
}
else{
// Still searching for the accept state
// Check for an edge connecting the current node to the accept state
if(matrix[currentNode][lookFor-1] > 0){
// There is an edge connecting the current node to the accept state
nextNode = lookFor;
// Add the current node to the search path.
// Use startAt, since that has the appropriate node number rather than
// the index of that node in the array or matrix.
searchPath.add(startAt);
// Update the path cost with the weight of the edge between the
// current node and the accept state.
pathCost += matrix[currentNode][lookFor-1];
// Recursively call DFS
this.DFS(nextNode, lookFor);
}
// If there is no edge connecting the current node to the accept
// state, look for an edge connecting to a node that hasn't been
// visited yet.
else{
for (int i = 0; i < matrix[currentNode].length; i++) {
if(matrix[currentNode][i] > 0 && !visited[i]){
// There is an edge connecting the current node to another
// node that hasn't been visited yet
// Add the current node to the search path
searchPath.add(startAt);
// Set the next node to that unvisited node
nextNode = i + 1;
// Update the path cost
pathCost += matrix[currentNode][i];
// Recursively call DFS
this.DFS(nextNode, lookFor);
}
}
}
}
}
return found;
}
/**
* tryEmAll
* A method for testing every valid combination of start and accept states
* for a given graph.
* @return: void;
*/
private void tryEmAll(){
for (int node = 0; node < matrix[1].length; node++) {
for(int vertex = 0; vertex < matrix[1].length; vertex++){
System.out.println("Starting from " + (node+1) + ", searching for " + (vertex+1) + ".");
this.reset();
if(!this.DFS(node + 1, vertex + 1)){
System.out.println("Could not find " + (vertex+1) + " from " + (node+1) + ".");
System.out.println("Total path cost: " + pathCost);
System.out.println("Search path: " + searchPath + "\n");
}
}
}
}
/**
* depthFirst
* A method for performing a single depth-first search, with appropriate
* success and failure messages.
* @param :startFrom: the starting node
* @param :lookFor: the accept state
* @return: void
*/
private void depthFirst(int startFrom, int lookFor){
this.reset();
System.out.println("Starting from " + startFrom + ", searching for " + lookFor + ".");
if(!this.DFS(startFrom, lookFor)){
System.out.println("Could not find " + startFrom + " from " + lookFor + ".");
System.out.println("Total path cost: " + pathCost);
System.out.println("Search path: " + searchPath + "\n");
}
}
public static void main(String[] args){
// Describe and build graph 1
System.out.println("\n\nThis graph has unweighted, non-directional edges.");
Graph g1 = new Graph(7, "Graph 1");
g1.edge(1, 2, 1); // Node 1 connects to nodes 2, 3 and 5
g1.edge(1, 3, 1);
g1.edge(1, 5, 1);
g1.edge(2, 1, 1); // Node 2 connects to 1, 3, 4, 7
g1.edge(2, 3, 1);
g1.edge(2, 4, 1);
g1.edge(2, 7, 1);
g1.edge(3, 1, 1); // Node 3 connects to 1, 2, 4, 5, 6, 7
g1.edge(3, 2, 1);
g1.edge(3, 4, 1);
g1.edge(3, 5, 1);
g1.edge(3, 6, 1);
g1.edge(3, 7, 1);
g1.edge(4, 2, 1); // Node 4 connects to 2, 3, 7
g1.edge(4, 3, 1);
g1.edge(4, 7, 1);
g1.edge(5, 1, 1); // Node 5 connects to 1, 3, 6
g1.edge(5, 3, 1);
g1.edge(5, 6, 1);
g1.edge(6, 3, 1); // Node 6 connects to 3, 5, 7
g1.edge(6, 5, 1);
g1.edge(6, 7, 1);
g1.edge(7, 2, 1); // Node 7 connects to 2, 3, 4, 6
g1.edge(7, 3, 1);
g1.edge(7, 4, 1);
g1.edge(7, 6, 1);
g1.printMatrix();
g1.depthFirst(1, 7);
g1.depthFirst(3, 6);
// Describe and build graph 2
System.out.println("\n\n\nThis graph has weighted, non-directional edges.");
Graph g2 = new Graph(7, "Graph 2");
g2.edge(1, 2, 3); // Node 1 connects to nodes 2, 3 and 5
g2.edge(1, 3, 1);
g2.edge(1, 5, 4);
g2.edge(2, 1, 3); // Node 2 connects to 1, 3, 4, 7
g2.edge(2, 3, 2);
g2.edge(2, 4, 1);
g2.edge(2, 7, 4);
g2.edge(3, 1, 1); // Node 3 connects to 1, 2, 4, 5, 6, 7
g2.edge(3, 2, 2);
g2.edge(3, 4, 1);
g2.edge(3, 5, 3);
g2.edge(3, 6, 4);
g2.edge(3, 7, 5);
g2.edge(4, 2, 1); // Node 4 connects to 2, 3, 7
g2.edge(4, 3, 1);
g2.edge(4, 7, 4);
g2.edge(5, 1, 4); // Node 5 connects to 1, 3, 6
g2.edge(5, 3, 3);
g2.edge(5, 6, 2);
g2.edge(6, 3, 4); // Node 6 connects to 3, 5, 7
g2.edge(6, 5, 2);
g2.edge(6, 7, 1);
g2.edge(7, 2, 6); // Node 7 connects to 2, 3, 4, 6
g2.edge(7, 3, 5);
g2.edge(7, 4, 4);
g2.edge(7, 6, 1);
g2.printMatrix();
g2.depthFirst(1, 7);
g2.depthFirst(3, 6);
System.out.println("\n\nThis graph has unweighted, directional edges.");
Graph g3 = new Graph(7, "Graph 3");
g3.edge(1, 2, 1); // Node 1 Connects to 2, 3, 5
g3.edge(1, 3, 1);
g3.edge(1, 5, 1);
g3.edge(2, 3, 1); // Node 2 connects to 3, 4, 7
g3.edge(2, 4, 1);
g3.edge(2, 7, 1);
g3.edge(3, 5, 1); // Node 3 connects to 5, 6
g3.edge(3, 6, 1);
g3.edge(4, 3, 1); // Node 4 connects to 3, 7
g3.edge(4, 7, 1);
g3.edge(5, 1, 1); // Node 5 connects to 1, 6
g3.edge(5, 6, 1);
g3.edge(6, 3, 1); // Node 6 connects to 3, 5, 7
g3.edge(6, 5, 1);
g3.edge(6, 7, 1);
g3.edge(7, 2, 1); // Node 7 connects to 2, 3
g3.edge(7, 3, 1);
g3.printMatrix();
g3.depthFirst(1, 7);
g3.depthFirst(3, 6);
System.out.println("\n\nThis graph has weighted, directional edges.");
Graph g4 = new Graph(7, "Graph 4");
g4.edge(1, 2, 3); // Node 1 Connects to 2, 3, 5
g4.edge(1, 3, 1);
g4.edge(1, 5, 4);
g4.edge(2, 3, 2); // Node 2 connects to 3, 4, 7
g4.edge(2, 4, 1);
g4.edge(2, 7, 6);
g4.edge(3, 5, 3); // Node 3 connects to 5, 6
g4.edge(3, 6, 4);
g4.edge(4, 3, 1); // Node 4 connects to 3, 7
g4.edge(4, 7, 4);
g4.edge(5, 1, 4); // Node 5 connects to 1, 6
g4.edge(5, 6, 2);
g4.edge(6, 3, 1); // Node 6 connects to 3, 5, 7
g4.edge(6, 5, 1);
g4.edge(6, 7, 1);
g4.edge(7, 2, 1); // Node 7 connects to 2, 3
g4.edge(7, 3, 1);
g4.printMatrix();
g4.depthFirst(1, 7);
g4.depthFirst(3, 6);
System.out.println("\n\nThis graph has 3 nodes, with no edges.");
Graph g5 = new Graph(3, "Graph 5");
g5.printMatrix();
// Search through graph five, using every valid combination of
// start and accept states.
g5.depthFirst(1, 3);
g5.depthFirst(3,3);
System.out.println("\n\nThis graph has 3 nodes, with unweighted, directional edges.");
Graph g6 = g5;
g6.title = "Graph 6";
g6.edge(1, 2, 1); // Node 1 connects to 2,3
g6.edge(1, 3, 1);
g6.edge(2, 3, 1); // Node 2 connects to 3
g6.edge(3, 1, 1); // Node 3 connects to 1
g6.printMatrix();
g6.depthFirst(1, 3);
g6.depthFirst(3, 2);
}
}