11package com .thealgorithms .graph ;
22
3- import java .util .*;
4-
53import java .util .ArrayList ;
64import java .util .Collections ;
75import java .util .HashMap ;
86import java .util .HashSet ;
97import java .util .List ;
108import java .util .Map ;
119import java .util .PriorityQueue ;
10+ import java .util .Scanner ;
1211import java .util .Set ;
1312
1413/**
1514 * A* Search Algorithm for shortest pathfinding.
1615 *
1716 * <p>Commonly used in games, navigation, and robotics.
18- * Combines Dijkstra' s Algorithm and heuristic estimation.</p>
17+ * Combines Dijkstra’ s Algorithm and heuristic estimation.</p>
1918 *
2019 * <p>Reference: https://en.wikipedia.org/wiki/A*_search_algorithm</p>
2120 */
2221public class AStarSearch {
2322
2423 static class Node implements Comparable <Node > {
2524 int id ;
26- double g ; // cost from start
27- double h ; // heuristic to goal
28- double f ; // total cost = g + h
25+ double g ; // Cost from start
26+ double h ; // Heuristic to goal
27+ double f ; // Total cost = g + h
2928 Node parent ;
3029
3130 Node (int id , double g , double h , Node parent ) {
@@ -37,8 +36,8 @@ static class Node implements Comparable<Node> {
3736 }
3837
3938 @ Override
40- public int compareTo (Node other ) {
41- return Double .compare (this .f , other .f );
39+ public int compareTo (Node o ) {
40+ return Double .compare (this .f , o .f );
4241 }
4342 }
4443
@@ -52,8 +51,8 @@ public AStarSearch() {
5251 * Adds an undirected edge between nodes u and v with the given weight.
5352 */
5453 public void addEdge (int u , int v , int weight ) {
55- graph .computeIfAbsent (u , k -> new ArrayList <>()).add (new int [] {v , weight });
56- graph .computeIfAbsent (v , k -> new ArrayList <>()).add (new int [] {u , weight });
54+ graph .computeIfAbsent (u , k -> new ArrayList <>()).add (new int []{v , weight });
55+ graph .computeIfAbsent (v , k -> new ArrayList <>()).add (new int []{u , weight });
5756 }
5857
5958 /**
@@ -67,7 +66,7 @@ private double heuristic(int node, int goal) {
6766 * Finds the shortest path from start to goal using A* algorithm.
6867 *
6968 * @param start starting node
70- * @param goal goal node
69+ * @param goal goal node
7170 * @return list of nodes representing the shortest path
7271 */
7372 public List <Integer > findPath (int start , int goal ) {
@@ -117,4 +116,37 @@ private List<Integer> reconstructPath(Node node) {
117116 Collections .reverse (path );
118117 return path ;
119118 }
119+
120+ /**
121+ * Dynamic usage: reads graph and start/goal from input.
122+ */
123+ public static void main (String [] args ) {
124+ Scanner sc = new Scanner (System .in );
125+ AStarSearch aStar = new AStarSearch ();
126+
127+ System .out .print ("Enter number of edges: " );
128+ int edges = sc .nextInt ();
129+
130+ System .out .println ("Enter edges in format: u v weight" );
131+ for (int i = 0 ; i < edges ; i ++) {
132+ int u = sc .nextInt ();
133+ int v = sc .nextInt ();
134+ int w = sc .nextInt ();
135+ aStar .addEdge (u , v , w );
136+ }
137+
138+ System .out .print ("Enter start node: " );
139+ int start = sc .nextInt ();
140+
141+ System .out .print ("Enter goal node: " );
142+ int goal = sc .nextInt ();
143+
144+ List <Integer > path = aStar .findPath (start , goal );
145+ if (path .isEmpty ()) {
146+ System .out .println ("No path found from " + start + " → " + goal );
147+ } else {
148+ System .out .println ("Shortest path from " + start + " → " + goal + ": " + path );
149+ }
150+ sc .close ();
151+ }
120152}
0 commit comments