Skip to content

Commit e813c12

Browse files
authored
Update AStarSearch.java
1 parent 6f2a510 commit e813c12

File tree

1 file changed

+43
-11
lines changed

1 file changed

+43
-11
lines changed

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

Lines changed: 43 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,30 @@
11
package com.thealgorithms.graph;
22

3-
import java.util.*;
4-
53
import java.util.ArrayList;
64
import java.util.Collections;
75
import java.util.HashMap;
86
import java.util.HashSet;
97
import java.util.List;
108
import java.util.Map;
119
import java.util.PriorityQueue;
10+
import java.util.Scanner;
1211
import 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 Dijkstras Algorithm and heuristic estimation.</p>
1918
*
2019
* <p>Reference: https://en.wikipedia.org/wiki/A*_search_algorithm</p>
2120
*/
2221
public 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

Comments
 (0)