2727 * Space Complexity: O(V + E)
2828 * </p>
2929 */
30- public class AStarSearch {
30+ public final class AStarSearch {
3131
32- private static class Node implements Comparable <Node > {
32+ private static final class Node implements Comparable <Node > {
3333 private final int id ;
34- private final double g ; // cost from start
35- private final double h ; // heuristic to goal
36- private final double f ; // total cost = g + h
34+ private final double costFromStart ;
35+ private final double heuristicCost ;
36+ private final double totalCost ;
3737 private final Node parent ;
3838
39- Node (int id , double g , double h , Node parent ) {
39+ Node (int id , double costFromStart , double heuristicCost , Node parent ) {
4040 this .id = id ;
41- this .g = g ;
42- this .h = h ;
43- this .f = g + h ;
41+ this .costFromStart = costFromStart ;
42+ this .heuristicCost = heuristicCost ;
43+ this .totalCost = costFromStart + heuristicCost ;
4444 this .parent = parent ;
4545 }
4646
4747 @ Override
4848 public int compareTo (Node other ) {
49- return Double .compare (this .f , other .f );
49+ return Double .compare (this .totalCost , other .totalCost );
5050 }
5151 }
5252
@@ -72,12 +72,12 @@ public void addEdge(int u, int v, int weight) {
7272 /**
7373 * Heuristic function for A* (simplified as absolute difference).
7474 *
75- * @param node current node
76- * @param goal goal node
75+ * @param currentNode current node
76+ * @param goalNode goal node
7777 * @return heuristic estimate
7878 */
79- private double heuristic (int node , int goal ) {
80- return Math .abs (goal - node );
79+ private double heuristic (int currentNode , int goalNode ) {
80+ return Math .abs (goalNode - currentNode );
8181 }
8282
8383 /**
@@ -102,16 +102,22 @@ public List<Integer> findPath(int start, int goal) {
102102 }
103103
104104 closedSet .add (current .id );
105- for (int [] edge : graph .getOrDefault (current .id , new ArrayList <>())) {
105+
106+ List <int []> edges = graph .getOrDefault (current .id , Collections .emptyList ());
107+ for (int [] edge : edges ) {
106108 int neighbor = edge [0 ];
107- double tentativeG = current .g + edge [1 ];
109+ double edgeWeight = edge [1 ];
110+ double tentativeG = current .costFromStart + edgeWeight ;
111+
108112 if (closedSet .contains (neighbor )) {
109113 continue ;
110114 }
111115
112- if (tentativeG < gScore .getOrDefault (neighbor , Double .MAX_VALUE )) {
116+ double currentGScore = gScore .getOrDefault (neighbor , Double .MAX_VALUE );
117+ if (tentativeG < currentGScore ) {
113118 gScore .put (neighbor , tentativeG );
114- openSet .add (new Node (neighbor , tentativeG , heuristic (neighbor , goal ), current ));
119+ double neighborHeuristic = heuristic (neighbor , goal );
120+ openSet .add (new Node (neighbor , tentativeG , neighborHeuristic , current ));
115121 }
116122 }
117123 }
@@ -126,15 +132,20 @@ public List<Integer> findPath(int start, int goal) {
126132 */
127133 private List <Integer > reconstructPath (Node node ) {
128134 List <Integer > path = new ArrayList <>();
129- while (node != null ) {
130- path .add (node .id );
131- node = node .parent ;
135+ Node current = node ;
136+ while (current != null ) {
137+ path .add (current .id );
138+ current = current .parent ;
132139 }
133140 Collections .reverse (path );
134141 return path ;
135142 }
136143
137- /** Reads input dynamically and runs A* algorithm. */
144+ /**
145+ * Main method to demonstrate A* algorithm with user input.
146+ *
147+ * @param args command line arguments
148+ */
138149 public static void main (String [] args ) {
139150 Scanner sc = new Scanner (System .in );
140151 AStarSearch aStar = new AStarSearch ();
0 commit comments