Skip to content

Commit 2da1d4f

Browse files
authored
Update AStarSearch.java
1 parent 6a307fa commit 2da1d4f

File tree

1 file changed

+25
-53
lines changed

1 file changed

+25
-53
lines changed

src/main/java/com/thealgorithms/graph/AStarSearch.java

Lines changed: 25 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -7,35 +7,34 @@
77
import java.util.List;
88
import java.util.Map;
99
import java.util.PriorityQueue;
10-
import java.util.Scanner;
1110
import java.util.Set;
1211

1312
/**
1413
* Implementation of the A* Search Algorithm for shortest path finding.
1514
*
16-
* <p>
17-
* A* combines Dijkstra's algorithm with a heuristic to efficiently find the
18-
* shortest path in weighted graphs.
19-
* </p>
20-
*
21-
* <p>
22-
* Reference: https://en.wikipedia.org/wiki/A*_search_algorithm
23-
* </p>
24-
*
25-
* <p>
26-
* Time Complexity: O(E + V log V) with a binary heap priority queue.
27-
* Space Complexity: O(V + E)
28-
* </p>
15+
* @author Your Name
16+
* @version 1.0
2917
*/
30-
public final class AStarSearch {
18+
public final class AStar {
3119

20+
/**
21+
* Represents a node in the graph for A* algorithm.
22+
*/
3223
private static final class Node implements Comparable<Node> {
3324
private final int id;
3425
private final double costFromStart;
3526
private final double heuristicCost;
3627
private final double totalCost;
3728
private final Node parent;
3829

30+
/**
31+
* Constructs a new Node.
32+
*
33+
* @param id the node identifier
34+
* @param costFromStart the cost from start node to this node
35+
* @param heuristicCost the heuristic cost from this node to goal
36+
* @param parent the parent node
37+
*/
3938
Node(int id, double costFromStart, double heuristicCost, Node parent) {
4039
this.id = id;
4140
this.costFromStart = costFromStart;
@@ -52,8 +51,10 @@ public int compareTo(Node other) {
5251

5352
private final Map<Integer, List<int[]>> graph;
5453

55-
/** Constructs an empty graph. */
56-
public AStarSearch() {
54+
/**
55+
* Constructs an empty graph.
56+
*/
57+
public AStar() {
5758
graph = new HashMap<>();
5859
}
5960

@@ -88,11 +89,15 @@ private double heuristic(int currentNode, int goalNode) {
8889
* @return list of nodes representing the shortest path
8990
*/
9091
public List<Integer> findPath(int start, int goal) {
92+
if (start == goal) {
93+
return List.of(start);
94+
}
95+
9196
PriorityQueue<Node> openSet = new PriorityQueue<>();
9297
Map<Integer, Double> gScore = new HashMap<>();
9398
Set<Integer> closedSet = new HashSet<>();
9499

95-
openSet.add(new Node(start, 0, heuristic(start, goal), null));
100+
openSet.add(new Node(start, 0.0, heuristic(start, goal), null));
96101
gScore.put(start, 0.0);
97102

98103
while (!openSet.isEmpty()) {
@@ -102,13 +107,13 @@ public List<Integer> findPath(int start, int goal) {
102107
}
103108

104109
closedSet.add(current.id);
105-
110+
106111
List<int[]> edges = graph.getOrDefault(current.id, Collections.emptyList());
107112
for (int[] edge : edges) {
108113
int neighbor = edge[0];
109114
double edgeWeight = edge[1];
110115
double tentativeG = current.costFromStart + edgeWeight;
111-
116+
112117
if (closedSet.contains(neighbor)) {
113118
continue;
114119
}
@@ -140,37 +145,4 @@ private List<Integer> reconstructPath(Node node) {
140145
Collections.reverse(path);
141146
return path;
142147
}
143-
144-
/**
145-
* Main method to demonstrate A* algorithm with user input.
146-
*
147-
* @param args command line arguments
148-
*/
149-
public static void main(String[] args) {
150-
Scanner sc = new Scanner(System.in);
151-
AStarSearch aStar = new AStarSearch();
152-
153-
System.out.print("Enter number of edges: ");
154-
int edges = sc.nextInt();
155-
System.out.println("Enter edges in format: u v weight");
156-
for (int i = 0; i < edges; i++) {
157-
int u = sc.nextInt();
158-
int v = sc.nextInt();
159-
int w = sc.nextInt();
160-
aStar.addEdge(u, v, w);
161-
}
162-
163-
System.out.print("Enter start node: ");
164-
int start = sc.nextInt();
165-
System.out.print("Enter goal node: ");
166-
int goal = sc.nextInt();
167-
168-
List<Integer> path = aStar.findPath(start, goal);
169-
if (path.isEmpty()) {
170-
System.out.println("No path found from " + start + " → " + goal);
171-
} else {
172-
System.out.println("Shortest path from " + start + " → " + goal + ": " + path);
173-
}
174-
sc.close();
175-
}
176148
}

0 commit comments

Comments
 (0)