66 * A* Search Algorithm for shortest pathfinding.
77 *
88 * <p>Commonly used in games, navigation, and robotics.
9- * Combines Dijkstra’ s Algorithm and heuristic estimation.</p>
9+ * Combines Dijkstra' s Algorithm and heuristic estimation.</p>
1010 *
1111 * <p>Reference: https://en.wikipedia.org/wiki/A*_search_algorithm</p>
1212 */
1313public class AStarSearch {
1414
1515 static class Node implements Comparable <Node > {
1616 int id ;
17- double g ; // Cost from start
18- double h ; // Heuristic to goal
19- double f ; // Total cost = g + h
17+ double g ; // cost from start
18+ double h ; // heuristic to goal
19+ double f ; // total cost = g + h
2020 Node parent ;
2121
2222 Node (int id , double g , double h , Node parent ) {
@@ -28,8 +28,8 @@ static class Node implements Comparable<Node> {
2828 }
2929
3030 @ Override
31- public int compareTo (Node o ) {
32- return Double .compare (this .f , o .f );
31+ public int compareTo (Node other ) {
32+ return Double .compare (this .f , other .f );
3333 }
3434 }
3535
@@ -39,13 +39,17 @@ public AStarSearch() {
3939 graph = new HashMap <>();
4040 }
4141
42- /** Adds an undirected edge between nodes u and v with the given weight. */
42+ /**
43+ * Adds an undirected edge between nodes u and v with the given weight.
44+ */
4345 public void addEdge (int u , int v , int weight ) {
44- graph .computeIfAbsent (u , k -> new ArrayList <>()).add (new int []{v , weight });
45- graph .computeIfAbsent (v , k -> new ArrayList <>()).add (new int []{u , weight });
46+ graph .computeIfAbsent (u , k -> new ArrayList <>()).add (new int [] {v , weight });
47+ graph .computeIfAbsent (v , k -> new ArrayList <>()).add (new int [] {u , weight });
4648 }
4749
48- /** Heuristic function (simplified for numeric nodes). */
50+ /**
51+ * Heuristic function (simplified for numeric nodes).
52+ */
4953 private double heuristic (int node , int goal ) {
5054 return Math .abs (goal - node );
5155 }
@@ -54,7 +58,7 @@ private double heuristic(int node, int goal) {
5458 * Finds the shortest path from start to goal using A* algorithm.
5559 *
5660 * @param start starting node
57- * @param goal goal node
61+ * @param goal goal node
5862 * @return list of nodes representing the shortest path
5963 */
6064 public List <Integer > findPath (int start , int goal ) {
@@ -74,7 +78,7 @@ public List<Integer> findPath(int start, int goal) {
7478
7579 closedSet .add (current .id );
7680
77- for (int [] edge : graph .getOrDefault (current .id , new ArrayList <> ())) {
81+ for (int [] edge : graph .getOrDefault (current .id , Collections . emptyList ())) {
7882 int neighbor = edge [0 ];
7983 double tentativeG = current .g + edge [1 ];
8084
@@ -89,11 +93,12 @@ public List<Integer> findPath(int start, int goal) {
8993 }
9094 }
9195 }
92-
9396 return Collections .emptyList ();
9497 }
9598
96- /** Reconstructs path by following parent nodes. */
99+ /**
100+ * Reconstructs path by following parent nodes.
101+ */
97102 private List <Integer > reconstructPath (Node node ) {
98103 List <Integer > path = new ArrayList <>();
99104 while (node != null ) {
0 commit comments