forked from TheAlgorithms/Java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAStarSearch.java
More file actions
148 lines (130 loc) · 4.41 KB
/
AStarSearch.java
File metadata and controls
148 lines (130 loc) · 4.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
package com.thealgorithms.graphs;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.PriorityQueue;
import java.util.Set;
/**
* Implementation of the A* Search Algorithm for shortest path finding.
*
* @author Your Name
* @version 1.0
*/
public final class AStarSearch {
/**
* Represents a node in the graph for A* algorithm.
*/
private static final class Node implements Comparable<Node> {
private final int id;
private final double costFromStart;
private final double heuristicCost;
private final double totalCost;
private final Node parent;
/**
* Constructs a new Node.
*
* @param id the node identifier
* @param costFromStart the cost from start node to this node
* @param heuristicCost the heuristic cost from this node to goal
* @param parent the parent node
*/
Node(int id, double costFromStart, double heuristicCost, Node parent) {
this.id = id;
this.costFromStart = costFromStart;
this.heuristicCost = heuristicCost;
this.totalCost = costFromStart + heuristicCost;
this.parent = parent;
}
@Override
public int compareTo(Node other) {
return Double.compare(this.totalCost, other.totalCost);
}
}
private final Map<Integer, List<int[]>> graph;
/**
* Constructs an empty graph.
*/
public AStarSearch() {
graph = new HashMap<>();
}
/**
* Adds an undirected edge between nodes u and v with the given weight.
*
* @param u first node
* @param v second node
* @param weight edge weight
*/
public void addEdge(int u, int v, int weight) {
graph.computeIfAbsent(u, k -> new ArrayList<>()).add(new int[] {v, weight});
graph.computeIfAbsent(v, k -> new ArrayList<>()).add(new int[] {u, weight});
}
/**
* Heuristic function for A* (simplified as absolute difference).
*
* @param currentNode current node
* @param goalNode goal node
* @return heuristic estimate
*/
private double heuristic(int currentNode, int goalNode) {
return Math.abs(goalNode - currentNode);
}
/**
* Finds the shortest path from start to goal using A* algorithm.
*
* @param start start node
* @param goal goal node
* @return list of nodes representing the shortest path
*/
public List<Integer> findPath(int start, int goal) {
if (start == goal) {
return List.of(start);
}
PriorityQueue<Node> openSet = new PriorityQueue<>();
Map<Integer, Double> gScore = new HashMap<>();
Set<Integer> closedSet = new HashSet<>();
openSet.add(new Node(start, 0.0, heuristic(start, goal), null));
gScore.put(start, 0.0);
while (!openSet.isEmpty()) {
Node current = openSet.poll();
if (current.id == goal) {
return reconstructPath(current);
}
closedSet.add(current.id);
List<int[]> edges = graph.getOrDefault(current.id, Collections.emptyList());
for (int[] edge : edges) {
int neighbor = edge[0];
double edgeWeight = edge[1];
double tentativeG = current.costFromStart + edgeWeight;
if (closedSet.contains(neighbor)) {
continue;
}
double currentGScore = gScore.getOrDefault(neighbor, Double.MAX_VALUE);
if (tentativeG < currentGScore) {
gScore.put(neighbor, tentativeG);
double neighborHeuristic = heuristic(neighbor, goal);
openSet.add(new Node(neighbor, tentativeG, neighborHeuristic, current));
}
}
}
return Collections.emptyList();
}
/**
* Reconstructs the path by following parent nodes.
*
* @param node end node
* @return path from start to end
*/
private List<Integer> reconstructPath(Node node) {
List<Integer> path = new ArrayList<>();
Node current = node;
while (current != null) {
path.add(current.id);
current = current.parent;
}
Collections.reverse(path);
return path;
}
}