Repository navigation
Expand file tree
/
Copy pathMain.java
More file actions
370 lines (334 loc) · 11.9 KB
/
Copy pathMain.java
File metadata and controls
370 lines (334 loc) · 11.9 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
import java.util.*;
public class Main
{
public static void main(String[] args)
{
GridGraph graph = new GridGraph();
graph = createRandomGridGraph(100);
GridNode start = graph.getByCoordinates(0,0);
GridNode end = graph.getByCoordinates(99,99);
ArrayList<GridNode> path = astar(start, end);
for (GridNode x : path)
System.out.println(x.value);
}
// UNDIRECTED METHODS
static unDirectedGraph createRandomUnweightedGraphIter(int n)
{
unDirectedGraph graph = new unDirectedGraph();
for (int i = 0; i < n; i++)
{
graph.addNode(i);
}
// Between each two nodes, give a 50/50 chance of putting an edge in between
HashSet<GraphNode> nodes = graph.getAllNodes();
for(int i = 0; i < nodes.size(); i++) {
for (int j = i; j < nodes.size(); j++) {
Random rand = new Random();
int add = rand.nextInt(2);
if (add==1) {
GraphNode obj1 = null;
GraphNode obj2 = null;
for (GraphNode node : nodes) {
if (node.equals(i))
obj1=node;
}
for (GraphNode node : nodes) {
if (node.equals(j))
obj2=node;
}
graph.addUndirectedEdge(obj1, obj2);
}
}
}
return graph;
}
static unDirectedGraph createLinkedList(int n)
{
unDirectedGraph graph = new unDirectedGraph();
for (int i = 0; i < n; i++)
{
graph.addNode(i);
}
// Attach edge between one node and the next only
HashSet<GraphNode> nodes = graph.getAllNodes();
for (int i = 0; i < n-1; i++)
{
GraphNode obj1 = null;
GraphNode obj2 = null;
obj1 = graph.get(i);
obj2 = graph.get(i+1);
graph.addUndirectedEdge(obj1, obj2);
}
return graph;
}
static ArrayList<GraphNode> BFTRecLinkedList()
{
Graph graph = createLinkedList(100);
return GraphSearch.BFTRec(graph);
}
static ArrayList<GraphNode> BFTIterLinkedList()
{
Graph graph = createLinkedList(10000);
return GraphSearch.BFTIter(graph);
}
// DIRECTED METHOD
static DirectedGraph createRandomDAGIter(final int n)
{
DirectedGraph graph = new DirectedGraph();
for (int i = 0; i < n; i++)
{
graph.addNode(i);
}
// Between each two nodes, give a 50% chance of putting an edge in between
//HashSet<GraphNode> nodes = graph.getAllNodes();
for(int i = 0; i < n; i++) {
for (int j = i+1; j < n; j++) {
Random rand = new Random();
int add = rand.nextInt(10);
if (add>5 && graph.checkCycles(graph, graph.get(j))) {
graph.addDirectedEdge(graph.get(i), graph.get(j));
}
}
}
return graph;
}
// WEIGHTED METHODS
static WeightedGraph createRandomCompleteWeightedGraph(final int n) {
WeightedGraph graph = new WeightedGraph();
for (int i = 0; i < n; i++) {
graph.addNode(i);
}
HashSet<GraphNode> nodes = graph.getAllNodes();
for (int i = 0; i < nodes.size(); i++) {
for (int j = 0; j < nodes.size(); j++) {
if (i == j)
continue;
Random rand = new Random();
// Creates a random weight from 1-10
int weight = rand.nextInt(10) + 1;
GraphNode obj1 = graph.get(i);
GraphNode obj2 = graph.get(j);
graph.addWeightedEdge(obj1, obj2, weight);
}
}
return graph;
}
static WeightedGraph createWeightedLinkedList(int n)
{
WeightedGraph graph = new WeightedGraph();
ArrayList<GraphNode> ordered = new ArrayList<>();
for (int i = 0; i < n; i++)
{
graph.addNode(i);
ordered.add(graph.get(i));
}
// Attach edge between one node and the next only
HashSet<GraphNode> nodes = graph.getAllNodes();
for (int i = 0; i < n-1; i++)
{
graph.addWeightedEdge(ordered.get(i), ordered.get(i+1), 1);
}
return graph;
}
static HashMap<GraphNode, Integer> dijkstras (final GraphNode start)
{
HashMap<GraphNode, Integer> finList = new HashMap<>();
ArrayList<GraphNode> values = new ArrayList<>();
ArrayList<GraphNode> parents = new ArrayList<>();
ArrayList<Integer> distance = new ArrayList<>();
// Add the starting node
values.add(start);
parents.add(null);
distance.add(0);
GraphNode curr = start;
ArrayList<GraphNode> visited = new ArrayList<>();
boolean allVisited = false;
while (!allVisited)
{
visited.add(curr);
for (Edge edge : curr.neighbors)
{
GraphNode dest = edge.destination;
// If the node is not in values, add it
if (!values.contains(dest)) {
values.add(dest);
int dist = distance.get(values.indexOf(curr));
distance.add(edge.weight + dist);
parents.add(curr);
}
else // If node already in values, compare current distance to last dist
{
int index = values.indexOf(dest);
int currDist = distance.get(index);
int newDist = distance.get(values.indexOf(curr)) + edge.weight;
if (currDist < newDist)
continue;
else
{
distance.set(values.indexOf(dest), newDist);
}
}
}
// Find the next min that is not visited
while(!allVisited) {
int minIndex = findMin(distance, visited, values);
if (minIndex == -1)
allVisited = true;
else {
curr = values.get(minIndex);
if (!visited.contains(curr))
break;
}
}
for (GraphNode node : values)
{
if (!visited.contains(node)) {
allVisited = false;
break;
}
allVisited = true;
}
}
// Add everything to the HashMap
for (int i = 0; i < distance.size(); i++)
{
finList.put(values.get(i), distance.get(i));
}
return finList;
}
static int findMin(final ArrayList<Integer> distance, final ArrayList<GraphNode> visited, final ArrayList<GraphNode> values)
{
int min = Integer.MAX_VALUE;
int minIndex = -1;
for (int i = 0; i < distance.size(); i++) {
if (distance.get(i) == 0)
continue;
if (distance.get(i) < min && !visited.contains(values.get(i))) {
min = distance.get(i);
minIndex = i;
}
}
return minIndex;
}
// GridNode Methods
static GridGraph createRandomGridGraph(int n)
{
GridGraph graph = new GridGraph();
int val = 0;
for (int i = 0; i < n*n; i++)
{
graph.addGridNode(i%n, i/n, val);
val++;
}
for (GridNode node : graph.getAllNodes())
{
int x = node.x;
int y = node.y;
// Check down and right neighbors in grid
Random rand = new Random();
// Made it 75% chance of having an edge because 50% did not give many good graphs
int add = rand.nextInt(4);
if (add < 3 && x+1 < n)
{
GridNode temp = graph.getByCoordinates(x+1, y);
graph.addUndirectedEdge(node, temp, 1);
}
add = rand.nextInt(4);
if (add < 3 && y+1 < n)
{
GridNode temp = graph.getByCoordinates(x, y+1);
graph.addUndirectedEdge(node, temp, 1);
}
}
return graph;
}
static ArrayList<GridNode> astar(final GridNode sourceNode, final GridNode destNode)
{
ArrayList<GridNode> path = new ArrayList<>();
ArrayList<GridNode> values = new ArrayList<>();
ArrayList<GridNode> parents = new ArrayList<>();
ArrayList<Integer> distance = new ArrayList<>();
ArrayList<GridNode> visited = new ArrayList<>();
// Add the starting node
values.add(sourceNode);
parents.add(null);
distance.add(0);
GridNode curr = sourceNode;
boolean allVisited = false;
while (curr != destNode && !allVisited)
{
curr.visited = true;
visited.add(curr);
for (GridEdge edge : curr.neighbors)
{
GridNode temp = edge.destination;
if (temp.visited)
continue;
// If the node is not in values, add it
if (!values.contains(temp)) {
values.add(temp);
int dist = distance.get(values.indexOf(curr)) + 1;
distance.add(dist + manhattanDist(temp, destNode));
parents.add(curr);
}
else // If node already in values, compare current distance to last dist
{
int index = values.indexOf(temp);
int currDist = distance.get(index) + manhattanDist(temp, destNode);
int newDist = distance.get(values.indexOf(curr));
if (currDist < newDist)
continue;
else
{
distance.set(values.indexOf(temp), newDist);
}
}
}
// Find the next min that is not visited
do{
int minIndex = findMinAStar(distance, values);
if (minIndex == -1) {
break;
}
curr = values.get(minIndex);
} while(curr.visited);
for (GridNode node : values)
{
if (!node.visited) {
allVisited = false;
break;
}
allVisited = true;
}
}
// Add everything to the HashMap
while (curr != sourceNode)
{
path.add(0, curr);
int index = values.indexOf(curr);
curr = parents.get(index);
}
path.add(0, sourceNode);
if (path.get(path.size()-1) != destNode)
return null;
return path;
}
static int findMinAStar(final ArrayList<Integer> distance, final ArrayList<GridNode> values)
{
int min = Integer.MAX_VALUE;
int minIndex = -1;
for (int i = 0; i < distance.size(); i++) {
if (distance.get(i) == 0)
continue;
if (distance.get(i) < min && !values.get(i).visited) {
min = distance.get(i);
minIndex = i;
}
}
return minIndex;
}
static int manhattanDist(GridNode start, GridNode dest)
{
return Math.abs(dest.x-start.x) + Math.abs(dest.y-start.y);
}
}