mirror of
https://github.com/TheAlgorithms/Java.git
synced 2025-07-24 13:10:13 +08:00
Resolve build errors & cleanup structure (#2334)
This commit is contained in:
@ -2,32 +2,33 @@
|
||||
Time Complexity = O(E), where E is equal to the number of edges
|
||||
*/
|
||||
|
||||
package A_Star;
|
||||
package Graphs;
|
||||
|
||||
import java.lang.reflect.Array;
|
||||
import java.util.*;
|
||||
|
||||
public class A_Star {
|
||||
|
||||
private static class Graph {
|
||||
// Graph's structure can be changed only applying changes to this class.
|
||||
private ArrayList<Edge>[] graph;
|
||||
private ArrayList<ArrayList<Edge>> graph;
|
||||
|
||||
// Initialise ArrayLists in Constructor
|
||||
public Graph(int size) {
|
||||
this.graph = new ArrayList[size];
|
||||
this.graph = new ArrayList<>();
|
||||
for (int i = 0; i < size; i++) {
|
||||
this.graph[i] = new ArrayList<>();
|
||||
this.graph.set(i, new ArrayList<>());
|
||||
}
|
||||
}
|
||||
|
||||
private ArrayList<Edge> getNeighbours(int from) {
|
||||
return this.graph[from];
|
||||
return this.graph.get(from);
|
||||
}
|
||||
|
||||
// Graph is bidirectional, for just one direction remove second instruction of this method.
|
||||
private void addEdge(Edge edge) {
|
||||
this.graph[edge.getFrom()].add(new Edge(edge.getFrom(), edge.getTo(), edge.getWeight()));
|
||||
this.graph[edge.getTo()].add(new Edge(edge.getTo(), edge.getFrom(), edge.getWeight()));
|
||||
this.graph.get(edge.getFrom()).add(new Edge(edge.getFrom(), edge.getTo(), edge.getWeight()));
|
||||
this.graph.get(edge.getTo()).add(new Edge(edge.getTo(), edge.getFrom(), edge.getWeight()));
|
||||
}
|
||||
}
|
||||
|
||||
@ -83,7 +84,7 @@ public class A_Star {
|
||||
private void printSolution() {
|
||||
if (this.path != null)
|
||||
System.out.println(
|
||||
"Optimal path: " + this.path.toString() + ", distance: " + this.distance);
|
||||
"Optimal path: " + this.path + ", distance: " + this.distance);
|
||||
else System.out.println("There is no path available to connect the points");
|
||||
}
|
||||
}
|
||||
@ -129,13 +130,13 @@ public class A_Star {
|
||||
|
||||
Graph graph = new Graph(20);
|
||||
ArrayList<Integer> graphData =
|
||||
new ArrayList<>(
|
||||
Arrays.asList(
|
||||
0, 19, 75, null, 0, 15, 140, null, 0, 16, 118, null, 19, 12, 71, null, 12, 15, 151,
|
||||
null, 16, 9, 111, null, 9, 10, 70, null, 10, 3, 75, null, 3, 2, 120, null, 2, 14,
|
||||
146, null, 2, 13, 138, null, 2, 6, 115, null, 15, 14, 80, null, 15, 5, 99, null, 14,
|
||||
13, 97, null, 5, 1, 211, null, 13, 1, 101, null, 6, 1, 160, null, 1, 17, 85, null,
|
||||
17, 7, 98, null, 7, 4, 86, null, 17, 18, 142, null, 18, 8, 92, null, 8, 11, 87));
|
||||
new ArrayList<>(
|
||||
Arrays.asList(
|
||||
0, 19, 75, null, 0, 15, 140, null, 0, 16, 118, null, 19, 12, 71, null, 12, 15, 151,
|
||||
null, 16, 9, 111, null, 9, 10, 70, null, 10, 3, 75, null, 3, 2, 120, null, 2, 14,
|
||||
146, null, 2, 13, 138, null, 2, 6, 115, null, 15, 14, 80, null, 15, 5, 99, null, 14,
|
||||
13, 97, null, 5, 1, 211, null, 13, 1, 101, null, 6, 1, 160, null, 1, 17, 85, null,
|
||||
17, 7, 98, null, 7, 4, 86, null, 17, 18, 142, null, 18, 8, 92, null, 8, 11, 87));
|
||||
initializeGraph(graph, graphData);
|
||||
|
||||
PathAndDistance solution = aStar(3, 1, graph, heuristic);
|
||||
@ -147,10 +148,10 @@ public class A_Star {
|
||||
// estimated value
|
||||
// given by the heuristic function to reach the destination point from the current point.
|
||||
PriorityQueue<PathAndDistance> queue =
|
||||
new PriorityQueue<>(Comparator.comparingInt(a -> (a.getDistance() + a.getEstimated())));
|
||||
new PriorityQueue<>(Comparator.comparingInt(a -> (a.getDistance() + a.getEstimated())));
|
||||
|
||||
// dummy data to start the algorithm from the beginning point
|
||||
queue.add(new PathAndDistance(0, new ArrayList<>(Arrays.asList(from)), 0));
|
||||
queue.add(new PathAndDistance(0, new ArrayList<>(List.of(from)), 0));
|
||||
|
||||
boolean solutionFound = false;
|
||||
PathAndDistance currentData = new PathAndDistance(-1, null, -1);
|
||||
|
Reference in New Issue
Block a user