mirror of
https://github.com/TheAlgorithms/Java.git
synced 2025-12-19 07:00:35 +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);
|
||||
|
||||
@@ -20,7 +20,7 @@ public class HashMapLinearProbing {
|
||||
public HashMapLinearProbing(int hsize) {
|
||||
this.buckets = new Integer[hsize];
|
||||
this.hsize = hsize;
|
||||
this.AVAILABLE = new Integer(Integer.MIN_VALUE);
|
||||
this.AVAILABLE = Integer.MIN_VALUE;
|
||||
this.size = 0;
|
||||
}
|
||||
|
||||
@@ -44,7 +44,7 @@ public class HashMapLinearProbing {
|
||||
* @param key the desired key to be inserted in the hash map
|
||||
*/
|
||||
public void insertHash(int key) {
|
||||
Integer wrappedInt = new Integer(key);
|
||||
Integer wrappedInt = key;
|
||||
int hash = hashing(key);
|
||||
|
||||
if (isFull()) {
|
||||
@@ -73,7 +73,7 @@ public class HashMapLinearProbing {
|
||||
* @param key the desired key to be deleted
|
||||
*/
|
||||
public void deleteHash(int key) {
|
||||
Integer wrappedInt = new Integer(key);
|
||||
Integer wrappedInt = key;
|
||||
int hash = hashing(key);
|
||||
|
||||
if (isEmpty()) {
|
||||
@@ -115,7 +115,7 @@ public class HashMapLinearProbing {
|
||||
* @return int the index where the key is located
|
||||
*/
|
||||
public int findHash(int key) {
|
||||
Integer wrappedInt = new Integer(key);
|
||||
Integer wrappedInt = key;
|
||||
int hash = hashing(key);
|
||||
|
||||
if (isEmpty()) {
|
||||
|
||||
@@ -2,19 +2,18 @@
|
||||
|
||||
stack is an ADT (abstract data type ) that act like list of objects but there is a diffrents.
|
||||
|
||||
stack act is *LIFO* (Last In First Out), it means that when we want to get an element from the stack we get the last element in the stack.
|
||||
stack act is _LIFO_ (Last In First Out), it means that when we want to get an element from the stack we get the last element in the stack.
|
||||
|
||||
stack is bast on two methods ( functions)
|
||||
stack is bast on two methods ( functions)
|
||||
|
||||
## push & pop
|
||||
## push & pop
|
||||
|
||||
**push**: add an alement to last index of stack.
|
||||
**push**: add an alement to last index of stack.
|
||||
|
||||
for example: we have `1, 3, 5` in stack, then we call push(9),
|
||||
|
||||
`9` will add to last index of stack -> `1, 3, 5 , 9`
|
||||
|
||||
|
||||
**pop**: remove the last element from stack.
|
||||
for example: we have `1, 3, 5 , 9` in stack, then we call pop(),
|
||||
|
||||
|
||||
@@ -82,7 +82,7 @@ public class AVLTree {
|
||||
System.out.println(this.root.height);
|
||||
}
|
||||
private void display (Node node) {
|
||||
String str="";
|
||||
Strings str="";
|
||||
if(node.left!=null)
|
||||
str+=node.left.data+"=>";
|
||||
else
|
||||
|
||||
Reference in New Issue
Block a user