Skip to content

Commit af7f33f

Browse files
committed
Add A* Search algorithm for shortest path
1 parent bb6385e commit af7f33f

File tree

1 file changed

+122
-0
lines changed

1 file changed

+122
-0
lines changed
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
package Astar;
2+
3+
import java.util.*;
4+
5+
/**
6+
* A* Search Algorithm for shortest pathfinding.
7+
*
8+
* <p>Commonly used in games, navigation, and robotics. Combines Dijkstra’s Algorithm and heuristic estimation.</p>
9+
*
10+
*
11+
*/
12+
public class AStarSearch {
13+
14+
static class Node implements Comparable<Node> {
15+
int id;
16+
double g; // Cost from start
17+
double h; // Heuristic to goal
18+
double f; // Total cost = g + h
19+
Node parent;
20+
21+
Node(int id, double g, double h, Node parent) {
22+
this.id = id;
23+
this.g = g;
24+
this.h = h;
25+
this.f = g + h;
26+
this.parent = parent;
27+
}
28+
29+
@Override
30+
public int compareTo(Node o) {
31+
return Double.compare(this.f, o.f);
32+
}
33+
}
34+
35+
private final Map<Integer, List<int[]>> graph;
36+
37+
public AStarSearch() {
38+
graph = new HashMap<>();
39+
}
40+
41+
public void addEdge(int u, int v, int weight) {
42+
graph.computeIfAbsent(u, k -> new ArrayList<>()).add(new int[]{v, weight});
43+
graph.computeIfAbsent(v, k -> new ArrayList<>()).add(new int[]{u, weight}); // if undirected
44+
}
45+
46+
private double heuristic(int node, int goal) {
47+
return Math.abs(goal - node); // Simplified heuristic
48+
}
49+
50+
public List<Integer> findPath(int start, int goal) {
51+
PriorityQueue<Node> openSet = new PriorityQueue<>();
52+
Map<Integer, Double> gScore = new HashMap<>();
53+
Set<Integer> closedSet = new HashSet<>();
54+
55+
openSet.add(new Node(start, 0, heuristic(start, goal), null));
56+
gScore.put(start, 0.0);
57+
58+
while (!openSet.isEmpty()) {
59+
Node current = openSet.poll();
60+
61+
if (current.id == goal) {
62+
return reconstructPath(current);
63+
}
64+
65+
closedSet.add(current.id);
66+
67+
for (int[] edge : graph.getOrDefault(current.id, new ArrayList<>())) {
68+
int neighbor = edge[0];
69+
double tentativeG = current.g + edge[1];
70+
71+
if (closedSet.contains(neighbor)) continue;
72+
73+
if (tentativeG < gScore.getOrDefault(neighbor, Double.MAX_VALUE)) {
74+
gScore.put(neighbor, tentativeG);
75+
Node next = new Node(neighbor, tentativeG, heuristic(neighbor, goal), current);
76+
openSet.add(next);
77+
}
78+
}
79+
}
80+
return Collections.emptyList();
81+
}
82+
83+
private List<Integer> reconstructPath(Node node) {
84+
List<Integer> path = new ArrayList<>();
85+
while (node != null) {
86+
path.add(node.id);
87+
node = node.parent;
88+
}
89+
Collections.reverse(path);
90+
return path;
91+
}
92+
93+
public static void main(String[] args) {
94+
Scanner sc = new Scanner(System.in);
95+
AStarSearch aStar = new AStarSearch();
96+
97+
System.out.print("Enter number of edges: ");
98+
int edges = sc.nextInt();
99+
100+
System.out.println("Enter edges in format: u v weight");
101+
for (int i = 0; i < edges; i++) {
102+
int u = sc.nextInt();
103+
int v = sc.nextInt();
104+
int w = sc.nextInt();
105+
aStar.addEdge(u, v, w);
106+
}
107+
108+
System.out.print("Enter start node: ");
109+
int start = sc.nextInt();
110+
111+
System.out.print("Enter goal node: ");
112+
int goal = sc.nextInt();
113+
114+
List<Integer> path = aStar.findPath(start, goal);
115+
if (path.isEmpty()) {
116+
System.out.println("No path found from " + start + " → " + goal);
117+
} else {
118+
System.out.println("Shortest path from " + start + " → " + goal + ": " + path);
119+
}
120+
sc.close();
121+
}
122+
}

0 commit comments

Comments
 (0)